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);
}
Aggregations