Search in sources :

Example 6 with GeneratedTypeElement

use of com.abubusoft.kripton.processor.element.GeneratedTypeElement in project kripton by xcesco.

the class BindM2MBuilder method generateEntity.

/**
 * @param entity
 * @return
 * @throws IOException
 */
private void generateEntity(M2MEntity entity) throws IOException {
    if (!entity.needToCreate)
        return;
    String tableName = entity.tableName;
    String entityClassName = entity.name;
    AnnotationProcessorUtilis.infoOnGeneratedClasses(BindDaoMany2Many.class, entity.getPackageName(), entityClassName);
    // @formatter:off
    classBuilder = TypeSpec.classBuilder(entityClassName).addModifiers(Modifier.PUBLIC).addAnnotation(AnnotationSpec.builder(BindTable.class).addMember("name", "$S", tableName).build());
    // @formatter:on
    // javadoc for class
    classBuilder.addJavadoc("<p>");
    classBuilder.addJavadoc("\nGenerated entity implementation for <code>$L</code>\n", entity.name);
    classBuilder.addJavadoc("</p>\n");
    JavadocUtility.generateJavadocGeneratedBy(classBuilder);
    // classBuilder.addJavadoc(" @see $T\n",
    // TypeUtility.className(entity.getElement().getQualifiedName().toString()));
    {
        // @formatter:off
        FieldSpec fieldSpec = FieldSpec.builder(Long.TYPE, entity.idName, Modifier.PUBLIC).addJavadoc("Primary key\n").addAnnotation(AnnotationSpec.builder(BindColumn.class).addMember("columnType", "$T.$L", ColumnType.class, ColumnType.PRIMARY_KEY).build()).build();
        // @formatter:on
        classBuilder.addField(fieldSpec);
    }
    Converter<String, String> converterFK = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);
    Converter<String, String> converterFieldName = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL);
    Converter<String, String> converterField2ColumnName = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);
    String fkPrefix = converterFK.convert(entity.idName);
    String fk1Name = converterField2ColumnName.convert(entity.entity1Name.simpleName() + fkPrefix);
    String fk2Name = converterField2ColumnName.convert(entity.entity2Name.simpleName() + fkPrefix);
    String field1Name = converterFieldName.convert(entity.entity1Name.simpleName() + fkPrefix);
    String field2Name = converterFieldName.convert(entity.entity2Name.simpleName() + fkPrefix);
    {
        // @formatter:off
        FieldSpec fieldSpec = FieldSpec.builder(Long.TYPE, field1Name, Modifier.PUBLIC).addJavadoc("Foreign key to $T model class\n", entity.entity1Name).addAnnotation(AnnotationSpec.builder(BindColumn.class).addMember("foreignKey", "$T.class", entity.entity1Name).addMember("onDelete", "$T.$L", ForeignKeyAction.class, ForeignKeyAction.CASCADE).build()).build();
        // @formatter:on
        classBuilder.addField(fieldSpec);
    }
    {
        // @formatter:off
        FieldSpec fieldSpec = FieldSpec.builder(Long.TYPE, field2Name, Modifier.PUBLIC).addJavadoc("Foreign key to $T model class\n", entity.entity2Name).addAnnotation(AnnotationSpec.builder(BindColumn.class).addMember("foreignKey", "$T.class", entity.entity2Name).addMember("onDelete", "$T.$L", ForeignKeyAction.class, ForeignKeyAction.CASCADE).build()).build();
        // @formatter:on
        classBuilder.addField(fieldSpec);
    }
    TypeSpec typeSpec = classBuilder.build();
    JavaWriterHelper.writeJava2File(filer, entity.getPackageName(), typeSpec);
    List<SQLProperty> properties = new ArrayList<SQLProperty>();
    {
        SQLProperty property = new SQLProperty(entity.idName, entity.getClassName());
        property.columnType = ColumnType.PRIMARY_KEY;
        property.columnName = entity.idName;
        property.setNullable(false);
        property.setPrimaryKey(true);
        property.foreignClassName = null;
        properties.add(property);
    }
    {
        SQLProperty property = new SQLProperty(field1Name, entity.getClassName());
        property.columnType = ColumnType.INDEXED;
        property.columnName = fk1Name;
        property.setNullable(false);
        property.setPrimaryKey(false);
        property.onDeleteAction = ForeignKeyAction.CASCADE;
        property.foreignClassName = entity.entity1Name.toString();
        properties.add(property);
    }
    {
        SQLProperty property = new SQLProperty(field2Name, entity.getClassName());
        property.columnType = ColumnType.INDEXED;
        property.columnName = fk2Name;
        property.setNullable(false);
        property.setPrimaryKey(false);
        property.onDeleteAction = ForeignKeyAction.CASCADE;
        property.foreignClassName = entity.entity2Name.toString();
        properties.add(property);
    }
    GeneratedTypeElement entityElement = new GeneratedTypeElement(entity.getPackageName(), classBuilder.build(), tableName, fk1Name + ", " + fk2Name);
    entityElement.properties = properties;
    entityResult.add(entityElement);
}
Also used : GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) BindColumn(com.abubusoft.kripton.android.annotation.BindColumn) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) ArrayList(java.util.ArrayList) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

GeneratedTypeElement (com.abubusoft.kripton.processor.element.GeneratedTypeElement)6 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 BindDao (com.abubusoft.kripton.android.annotation.BindDao)2 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)2 BindColumn (com.abubusoft.kripton.android.annotation.BindColumn)1 BindContentProvider (com.abubusoft.kripton.android.annotation.BindContentProvider)1 BindContentProviderPath (com.abubusoft.kripton.android.annotation.BindContentProviderPath)1 BindDaoMany2Many (com.abubusoft.kripton.android.annotation.BindDaoMany2Many)1 BindGeneratedDao (com.abubusoft.kripton.android.annotation.BindGeneratedDao)1 AbstractDataSource (com.abubusoft.kripton.android.sqlite.AbstractDataSource)1 SQLiteEvent (com.abubusoft.kripton.android.sqlite.SQLiteEvent)1 SQLiteTable (com.abubusoft.kripton.android.sqlite.SQLiteTable)1 M2MEntity (com.abubusoft.kripton.processor.bind.model.many2many.M2MEntity)1 DaoDefinitionWithoutAnnotatedMethodException (com.abubusoft.kripton.processor.exceptions.DaoDefinitionWithoutAnnotatedMethodException)1 InvalidBeanTypeException (com.abubusoft.kripton.processor.exceptions.InvalidBeanTypeException)1 InvalidKindForAnnotationException (com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException)1 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)1 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)1