Search in sources :

Example 1 with Column

use of org.litepal.annotation.Column in project LitePal by LitePalFramework.

the class LitePalBase method recursiveSupportedGenericFields.

private void recursiveSupportedGenericFields(Class<?> clazz, List<Field> supportedGenericFields) {
    if (clazz == DataSupport.class || clazz == Object.class) {
        return;
    }
    Field[] fields = clazz.getDeclaredFields();
    if (fields != null && fields.length > 0) {
        for (Field field : fields) {
            Column annotation = field.getAnnotation(Column.class);
            if (annotation != null && annotation.ignore()) {
                continue;
            }
            int modifiers = field.getModifiers();
            if (!Modifier.isStatic(modifiers) && isCollection(field.getType())) {
                String genericTypeName = getGenericTypeName(field);
                if (BaseUtility.isGenericTypeSupported(genericTypeName)) {
                    supportedGenericFields.add(field);
                }
            }
        }
    }
    recursiveSupportedGenericFields(clazz.getSuperclass(), supportedGenericFields);
}
Also used : Field(java.lang.reflect.Field) Column(org.litepal.annotation.Column) DataSupport(org.litepal.crud.DataSupport)

Example 2 with Column

use of org.litepal.annotation.Column in project LitePal by LitePalFramework.

the class LitePalBase method recursiveSupportedFields.

private void recursiveSupportedFields(Class<?> clazz, List<Field> supportedFields) {
    if (clazz == DataSupport.class || clazz == Object.class) {
        return;
    }
    Field[] fields = clazz.getDeclaredFields();
    if (fields != null && fields.length > 0) {
        for (Field field : fields) {
            Column annotation = field.getAnnotation(Column.class);
            if (annotation != null && annotation.ignore()) {
                continue;
            }
            int modifiers = field.getModifiers();
            if (!Modifier.isStatic(modifiers)) {
                Class<?> fieldTypeClass = field.getType();
                String fieldType = fieldTypeClass.getName();
                if (BaseUtility.isFieldTypeSupported(fieldType)) {
                    supportedFields.add(field);
                }
            }
        }
    }
    recursiveSupportedFields(clazz.getSuperclass(), supportedFields);
}
Also used : Field(java.lang.reflect.Field) Column(org.litepal.annotation.Column) DataSupport(org.litepal.crud.DataSupport)

Example 3 with Column

use of org.litepal.annotation.Column in project LitePal by LitePalFramework.

the class LitePalBase method analyzeClassFields.

/**
 * Introspection of the passed in class. Analyze the fields of current class
 * and find out the associations of it.
 *
 * @param className
 *            The class name to introspection.
 * @param action
 *            Between {@link org.litepal.LitePalBase#GET_ASSOCIATIONS_ACTION} and
 *            {@link org.litepal.LitePalBase#GET_ASSOCIATION_INFO_ACTION}
 */
private void analyzeClassFields(String className, int action) {
    try {
        Class<?> dynamicClass = Class.forName(className);
        Field[] fields = dynamicClass.getDeclaredFields();
        for (Field field : fields) {
            if (isNonPrimitive(field)) {
                Column annotation = field.getAnnotation(Column.class);
                if (annotation != null && annotation.ignore()) {
                    continue;
                }
                oneToAnyConditions(className, field, action);
                manyToAnyConditions(className, field, action);
            }
        }
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
        throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className);
    }
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) Field(java.lang.reflect.Field) Column(org.litepal.annotation.Column)

Example 4 with Column

use of org.litepal.annotation.Column in project LitePal by LitePalFramework.

the class LitePalBase method convertFieldToColumnModel.

/**
 * Convert a field instance into A ColumnModel instance. ColumnModel can provide information
 * when creating table.
 * @param field
 *          A supported field to map into column.
 * @return ColumnModel instance contains column information.
 */
private ColumnModel convertFieldToColumnModel(Field field) {
    String fieldType = field.getType().getName();
    String columnType = getColumnType(fieldType);
    boolean nullable = true;
    boolean unique = false;
    String defaultValue = "";
    Column annotation = field.getAnnotation(Column.class);
    if (annotation != null) {
        nullable = annotation.nullable();
        unique = annotation.unique();
        defaultValue = annotation.defaultValue();
    }
    ColumnModel columnModel = new ColumnModel();
    columnModel.setColumnName(DBUtility.convertToValidColumnName(field.getName()));
    columnModel.setColumnType(columnType);
    columnModel.setNullable(nullable);
    columnModel.setUnique(unique);
    columnModel.setDefaultValue(defaultValue);
    return columnModel;
}
Also used : Column(org.litepal.annotation.Column) ColumnModel(org.litepal.tablemanager.model.ColumnModel)

Example 5 with Column

use of org.litepal.annotation.Column in project LitePal by LitePalFramework.

the class LitePalBase method manyToAnyConditions.

/**
 * Deals with one to any association conditions. e.g. Song and Album. An
 * album have many songs, and a song belongs to one album. So if there's an
 * Album model defined in Song with private modifier, and in Album there's a
 * List or Set with generic type of Song and declared as private modifier,
 * they are one2many association. If there's no List or Set defined in
 * Album, they will become one2one associations. If there's also a Song
 * model defined in Album with private modifier, maybe the album just have
 * one song, they are one2one association too.
 *
 * When it's many2one association, it's easy to just simply add a foreign id
 * column to the many side model's table. But when it comes to many2many
 * association, it can not be done without intermediate join table in
 * database. LitePal assumes that this join table's name is the
 * concatenation of the two target table names in alphabetical order.
 *
 * @param className
 *            Source class name.
 * @param field
 *            A field of source class.
 * @param action
 *            Between {@link org.litepal.LitePalBase#GET_ASSOCIATIONS_ACTION} and
 *            {@link org.litepal.LitePalBase#GET_ASSOCIATION_INFO_ACTION}
 *
 * @throws ClassNotFoundException
 */
private void manyToAnyConditions(String className, Field field, int action) throws ClassNotFoundException {
    if (isCollection(field.getType())) {
        String genericTypeName = getGenericTypeName(field);
        // this genericTypeName class.
        if (LitePalAttr.getInstance().getClassNames().contains(genericTypeName)) {
            Class<?> reverseDynamicClass = Class.forName(genericTypeName);
            Field[] reverseFields = reverseDynamicClass.getDeclaredFields();
            // Look up if there's a reverse association
            // definition in the reverse class.
            boolean reverseAssociations = false;
            for (int i = 0; i < reverseFields.length; i++) {
                Field reverseField = reverseFields[i];
                // Only map private fields
                if (!Modifier.isStatic(reverseField.getModifiers())) {
                    Class<?> reverseFieldTypeClass = reverseField.getType();
                    // associations.
                    if (className.equals(reverseFieldTypeClass.getName())) {
                        if (action == GET_ASSOCIATIONS_ACTION) {
                            addIntoAssociationModelCollection(className, genericTypeName, genericTypeName, Const.Model.MANY_TO_ONE);
                        } else if (action == GET_ASSOCIATION_INFO_ACTION) {
                            addIntoAssociationInfoCollection(className, genericTypeName, genericTypeName, field, reverseField, Const.Model.MANY_TO_ONE);
                        }
                        reverseAssociations = true;
                    } else // association.
                    if (isCollection(reverseFieldTypeClass)) {
                        String reverseGenericTypeName = getGenericTypeName(reverseField);
                        if (className.equals(reverseGenericTypeName)) {
                            if (action == GET_ASSOCIATIONS_ACTION) {
                                addIntoAssociationModelCollection(className, genericTypeName, null, Const.Model.MANY_TO_MANY);
                            } else if (action == GET_ASSOCIATION_INFO_ACTION) {
                                addIntoAssociationInfoCollection(className, genericTypeName, null, field, reverseField, Const.Model.MANY_TO_MANY);
                            }
                            reverseAssociations = true;
                        }
                    }
                }
            }
            // are many2one unidirectional associations.
            if (!reverseAssociations) {
                if (action == GET_ASSOCIATIONS_ACTION) {
                    addIntoAssociationModelCollection(className, genericTypeName, genericTypeName, Const.Model.MANY_TO_ONE);
                } else if (action == GET_ASSOCIATION_INFO_ACTION) {
                    addIntoAssociationInfoCollection(className, genericTypeName, genericTypeName, field, null, Const.Model.MANY_TO_ONE);
                }
            }
        } else if (BaseUtility.isGenericTypeSupported(genericTypeName) && action == GET_ASSOCIATIONS_ACTION) {
            Column annotation = field.getAnnotation(Column.class);
            if (annotation != null && annotation.ignore()) {
                return;
            }
            GenericModel genericModel = new GenericModel();
            genericModel.setTableName(DBUtility.getGenericTableName(className, field.getName()));
            genericModel.setValueColumnName(DBUtility.convertToValidColumnName(field.getName()));
            genericModel.setValueColumnType(getColumnType(genericTypeName));
            genericModel.setValueIdColumnName(DBUtility.getGenericValueIdColumnName(className));
            mGenericModels.add(genericModel);
        }
    }
}
Also used : GenericModel(org.litepal.tablemanager.model.GenericModel) Field(java.lang.reflect.Field) Column(org.litepal.annotation.Column)

Aggregations

Column (org.litepal.annotation.Column)5 Field (java.lang.reflect.Field)4 DataSupport (org.litepal.crud.DataSupport)2 DatabaseGenerateException (org.litepal.exceptions.DatabaseGenerateException)1 ColumnModel (org.litepal.tablemanager.model.ColumnModel)1 GenericModel (org.litepal.tablemanager.model.GenericModel)1