/* * This file is part of l2jserver2 . * * l2jserver2 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * l2jserver2 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with l2jserver2. If not, see . */ package com.l2jserver.util; import java.lang.reflect.Field; import com.l2jserver.service.database.ddl.annotation.ColumnAutoIncrement; import com.l2jserver.service.database.ddl.annotation.ColumnDefault; import com.l2jserver.service.database.ddl.annotation.ColumnNullable; import com.l2jserver.service.database.ddl.annotation.ColumnSize; import com.l2jserver.util.transformer.Transformer; import com.l2jserver.util.transformer.TransformerFactory; import com.mysema.query.sql.RelationalPath; import com.mysema.query.types.Path; /** * Class serving several utilities for {@link Path} and {@link RelationalPath} * * @author Rogiel */ public class QPathUtils { /** * Returns the {@link Path} represented by name within the * given {@link RelationalPath} * * @param relationalPath * the {@link RelationalPath} * @param name * the {@link Path} name * @return the {@link Path} instance, if existent */ public static Path getPath(RelationalPath relationalPath, String name) { for (final Path path : relationalPath.getColumns()) { if (path.getMetadata().getExpression().toString().equals(name)) return path; } return null; } /** * @param path * the {@link Path} to be searched in its root entity * @return the {@link Field} holding path */ public static Field getReflectionField(Path path) { return ClassUtils.getFieldWithValue(path.getMetadata().getParent(), path); } /** * @param relationalPath * the {@link RelationalPath} * @param pathName * the {@link Path} name * @return the {@link Field} holding pathName * @see #getPath(RelationalPath, String) */ public static Field getRefelctionField(RelationalPath relationalPath, String pathName) { return ClassUtils.getFieldWithValue(relationalPath, getPath(relationalPath, pathName)); } /** * @param path * the {@link Path} * @return the column maximum size */ public static int getColumnSize(Path path) { final Field field = getReflectionField(path); if (field == null) return 0; final ColumnSize size = field.getAnnotation(ColumnSize.class); if (size != null) return size.value(); return -1; } /** * @param path * the {@link Path} * @return whether the column is auto incrementable or not */ public static boolean isAutoIncrementable(Path path) { final Field field = getReflectionField(path); if (field == null) return false; final ColumnAutoIncrement autoInc = field .getAnnotation(ColumnAutoIncrement.class); if (autoInc != null) return true; return false; } /** * @param path * the {@link Path} * @return whether the column can be null */ public static boolean isNullable(Path path) { final Field field = getReflectionField(path); if (field == null) return false; final ColumnNullable nullable = field .getAnnotation(ColumnNullable.class); if (nullable != null) { return true; } else { return false; } } /** * @param path * the {@link Path} * @return true if the column has an default value */ public static boolean hasDefaultValue(Path path) { final Field field = getReflectionField(path); if (field == null) return false; final ColumnDefault def = field.getAnnotation(ColumnDefault.class); if (def == null) { return false; } else { return true; } } /** * @param path * the {@link Path} * @return the column default value */ public static String getDefaultUntransformedValue(Path path) { final Field field = getReflectionField(path); if (field == null) return null; final ColumnDefault def = field.getAnnotation(ColumnDefault.class); if (def != null) return def.value(); return null; } /** * @param * the column return type * @param path * the {@link Path} * @return the column default value (already transformed) */ @SuppressWarnings("unchecked") public static T getDefaultValue(Path path) { @SuppressWarnings("rawtypes") final Transformer transformer = TransformerFactory.getTransfromer(path .getType()); return (T) transformer.untransform(path.getType(), getDefaultUntransformedValue(path)); } }