use of com.abubusoft.kripton.processor.element.GeneratedTypeElement in project kripton by xcesco.
the class BindDataSourceSubProcessor method createSQLDaoDefinition.
/**
* Create DAO definition
*
* @param globalBeanElements
* @param globalDaoElements
* @param generatedDaoParts
* @param daoItem
*/
protected void createSQLDaoDefinition(SQLiteDatabaseSchema schema, final Map<String, TypeElement> globalBeanElements, final Map<String, TypeElement> globalDaoElements, String daoItem) {
Element daoElement = globalDaoElements.get(daoItem);
if (daoElement.getKind() != ElementKind.INTERFACE) {
String msg = String.format("Class %s: only interfaces can be annotated with @%s annotation", daoElement.getSimpleName().toString(), BindDao.class.getSimpleName());
throw (new InvalidKindForAnnotationException(msg));
}
M2MEntity entity = M2MEntity.extractEntityManagedByDAO((TypeElement) daoElement);
// add to current schema generated entities too
for (GeneratedTypeElement genItem : this.generatedEntities) {
if (genItem.getQualifiedName().equals(entity.getQualifiedName())) {
schema.generatedEntities.add(genItem);
}
}
boolean generated = daoElement.getAnnotation(BindGeneratedDao.class) != null;
final SQLiteDaoDefinition currentDaoDefinition = new SQLiteDaoDefinition(schema, daoItem, (TypeElement) daoElement, entity.getClassName().toString(), generated);
// content provider management
BindContentProviderPath daoContentProviderPath = daoElement.getAnnotation(BindContentProviderPath.class);
if (daoContentProviderPath != null) {
currentDaoDefinition.contentProviderEnabled = true;
currentDaoDefinition.contentProviderPath = daoContentProviderPath.path();
currentDaoDefinition.contentProviderTypeName = daoContentProviderPath.typeName();
if (StringUtils.isEmpty(currentDaoDefinition.contentProviderTypeName)) {
Converter<String, String> convert = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);
AssertKripton.assertTrue(currentDaoDefinition.getParent().contentProvider != null, "DAO '%s' has an inconsistent content provider definition, perhaps you forget to use @%s in data source interface?", currentDaoDefinition.getElement().getQualifiedName(), BindContentProvider.class.getSimpleName());
currentDaoDefinition.contentProviderTypeName = currentDaoDefinition.getParent().contentProvider.authority + "." + convert.convert(currentDaoDefinition.getSimpleEntityClassName());
}
}
// set.
if (!globalBeanElements.containsKey(currentDaoDefinition.getEntityClassName()) && !isGeneratedEntity(currentDaoDefinition.getEntityClassName())) {
throw (new InvalidBeanTypeException(currentDaoDefinition));
}
schema.add(currentDaoDefinition);
fillMethods(currentDaoDefinition, daoElement);
/*
* if (generatedDaoPart != null) {
* currentDaoDefinition.addImplementedInterface(TypeUtility.typeName(
* generatedDaoPart)); fillMethods(currentDaoDefinition,
* generatedDaoPart); }
*/
// get @annotation associated to many 2 many relationship
BindDaoMany2Many daoMany2Many = daoElement.getAnnotation(BindDaoMany2Many.class);
// dao definition must have >0 method associated to query
if (currentDaoDefinition.getCollection().size() == 0 && daoMany2Many == null) {
throw (new DaoDefinitionWithoutAnnotatedMethodException(currentDaoDefinition));
}
}
use of com.abubusoft.kripton.processor.element.GeneratedTypeElement in project kripton by xcesco.
the class BindM2MBuilder method generateDaoPart.
private void generateDaoPart(M2MEntity entity) throws IOException {
String daoClassName = entity.daoName.simpleName();
// PackageElement pkg = elementUtils.getPackageElement(entity.getPackageName());
// String daoPackageName = pkg.getQualifiedName().toString();
String daoPackageName = entity.daoName.packageName();
String entityPackageName = entity.getPackageName();
String generatedDaoClassName = "Generated" + daoClassName;
AnnotationProcessorUtilis.infoOnGeneratedClasses(BindDaoMany2Many.class, daoPackageName, generatedDaoClassName);
// @formatter:off
classBuilder = TypeSpec.interfaceBuilder(generatedDaoClassName).addModifiers(Modifier.PUBLIC).addAnnotation(AnnotationSpec.builder(BindDao.class).addMember("value", "$T.class", TypeUtility.className(entityPackageName, entity.name)).build()).addAnnotation(AnnotationSpec.builder(BindGeneratedDao.class).addMember("dao", "$T.class", entity.daoName).build()).addAnnotation(AnnotationSpec.builder(BindDaoMany2Many.class).addMember("entity1", "$T.class", entity.entity1Name).addMember("entity2", "$T.class", entity.entity2Name).build()).addSuperinterface(entity.daoName);
// @formatter:on
JavadocUtility.generateJavadocGeneratedBy(classBuilder);
if (entity.generateMethods) {
generateSelects(entity, entityPackageName);
generateDeletes(entity, entityPackageName);
generateInsert(entity, entityPackageName);
}
TypeSpec typeSpec = classBuilder.build();
JavaWriterHelper.writeJava2File(filer, daoPackageName, typeSpec);
GeneratedTypeElement daoPartElement = new GeneratedTypeElement(daoPackageName, classBuilder.build(), null, null);
daoResult.add(daoPartElement);
}
use of com.abubusoft.kripton.processor.element.GeneratedTypeElement in project kripton by xcesco.
the class BindDataSourceBuilder method generateOnCreate.
/**
* @param schema
* @param orderedEntities
*/
private boolean generateOnCreate(SQLiteDatabaseSchema schema, List<SQLiteEntity> orderedEntities) {
boolean useForeignKey = false;
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("onCreate").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
methodBuilder.addParameter(SQLiteDatabase.class, "database");
methodBuilder.addJavadoc("onCreate\n");
methodBuilder.addCode("// generate tables\n");
if (schema.isLogEnabled()) {
// generate log section - BEGIN
methodBuilder.addComment("log section BEGIN");
methodBuilder.beginControlFlow("if (this.logEnabled)");
methodBuilder.beginControlFlow("if (options.inMemory)");
methodBuilder.addStatement("$T.info(\"Create database in memory\")", Logger.class);
methodBuilder.nextControlFlow("else");
methodBuilder.addStatement("$T.info(\"Create database '%s' version %s\",this.name, this.version)", Logger.class);
methodBuilder.endControlFlow();
// generate log section - END
methodBuilder.endControlFlow();
methodBuilder.addComment("log section END");
}
for (SQLiteEntity item : orderedEntities) {
if (schema.isLogEnabled()) {
// generate log section - BEGIN
methodBuilder.addComment("log section BEGIN");
methodBuilder.beginControlFlow("if (this.logEnabled)");
methodBuilder.addStatement("$T.info(\"DDL: %s\",$T.CREATE_TABLE_SQL)", Logger.class, BindTableGenerator.tableClassName(null, item));
// generate log section - END
methodBuilder.endControlFlow();
methodBuilder.addComment("log section END");
}
methodBuilder.addStatement("database.execSQL($T.CREATE_TABLE_SQL)", BindTableGenerator.tableClassName(null, item));
if (item.referedEntities.size() > 0) {
useForeignKey = true;
}
}
// if we have generated entities, we use foreign key for sure
if (schema.generatedEntities.size() > 0)
useForeignKey = true;
for (GeneratedTypeElement item : schema.generatedEntities) {
if (schema.isLogEnabled()) {
// generate log section - BEGIN
methodBuilder.addComment("log section BEGIN");
methodBuilder.beginControlFlow("if (this.logEnabled)");
methodBuilder.addStatement("$T.info(\"DDL: %s\",$T.CREATE_TABLE_SQL)", Logger.class, TypeUtility.className(BindTableGenerator.getTableClassName(item.getQualifiedName())));
// generate log section - END
methodBuilder.endControlFlow();
methodBuilder.addComment("log section END");
}
methodBuilder.addStatement("database.execSQL($T.CREATE_TABLE_SQL)", TypeUtility.className(BindTableGenerator.getTableClassName(item.getQualifiedName())));
}
methodBuilder.beginControlFlow("if (options.databaseLifecycleHandler != null)");
methodBuilder.addStatement("options.databaseLifecycleHandler.onCreate(database)");
methodBuilder.endControlFlow();
methodBuilder.addStatement("justCreated=true");
classBuilder.addMethod(methodBuilder.build());
return useForeignKey;
}
use of com.abubusoft.kripton.processor.element.GeneratedTypeElement in project kripton by xcesco.
the class BindDataSourceBuilder method generateOnUpgrade.
/**
* @param schema
* @param orderedEntities
*/
private void generateOnUpgrade(SQLiteDatabaseSchema schema, List<SQLiteEntity> orderedEntities) {
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("onUpgrade").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
methodBuilder.addParameter(SQLiteDatabase.class, "database");
methodBuilder.addParameter(Integer.TYPE, "previousVersion");
methodBuilder.addParameter(Integer.TYPE, "currentVersion");
methodBuilder.addJavadoc("onUpgrade\n");
Collections.reverse(orderedEntities);
if (schema.isLogEnabled()) {
// generate log section - BEGIN
methodBuilder.addComment("log section BEGIN");
methodBuilder.beginControlFlow("if (this.logEnabled)");
methodBuilder.addStatement("$T.info(\"Update database '%s' from version %s to version %s\",this.name, previousVersion, currentVersion)", Logger.class);
// generate log section - END
methodBuilder.endControlFlow();
methodBuilder.addComment("log section END");
}
methodBuilder.addComment("if we have a list of update task, try to execute them");
methodBuilder.beginControlFlow("if (options.updateTasks != null)");
methodBuilder.addStatement("$T<$T> tasks = buildTaskList(previousVersion, currentVersion)", List.class, SQLiteUpdateTask.class);
methodBuilder.beginControlFlow("for ($T task : tasks)", SQLiteUpdateTask.class);
// generate log section - BEGIN
methodBuilder.addComment("log section BEGIN");
methodBuilder.beginControlFlow("if (this.logEnabled)");
methodBuilder.addStatement("$T.info(\"Begin update database from version %s to %s\", previousVersion, previousVersion+1)", Logger.class);
// generate log section - END
methodBuilder.endControlFlow();
methodBuilder.addComment("log section END");
methodBuilder.addStatement("task.execute(database, previousVersion, previousVersion+1)");
// generate log section - BEGIN
methodBuilder.addComment("log section BEGIN");
methodBuilder.beginControlFlow("if (this.logEnabled)");
methodBuilder.addStatement("$T.info(\"End update database from version %s to %s\", previousVersion, previousVersion+1)", Logger.class);
// generate log section - END
methodBuilder.endControlFlow();
methodBuilder.addComment("log section END");
methodBuilder.addStatement("previousVersion++");
methodBuilder.endControlFlow();
methodBuilder.nextControlFlow("else");
methodBuilder.addComment("drop all tables");
methodBuilder.addStatement("$T.dropTablesAndIndices(database)", SQLiteUpdateTaskHelper.class);
// reorder entities
Collections.reverse(orderedEntities);
methodBuilder.addCode("\n");
methodBuilder.addCode("// generate tables\n");
for (SQLiteEntity item : orderedEntities) {
if (schema.isLogEnabled()) {
// generate log section - BEGIN
methodBuilder.addComment("log section BEGIN");
methodBuilder.beginControlFlow("if (this.logEnabled)");
methodBuilder.addCode("$T.info(\"DDL: %s\",$T.CREATE_TABLE_SQL);\n", Logger.class, BindTableGenerator.tableClassName(null, item));
// generate log section - END
methodBuilder.endControlFlow();
methodBuilder.addComment("log section END");
}
methodBuilder.addCode("database.execSQL($T.CREATE_TABLE_SQL);\n", BindTableGenerator.tableClassName(null, item));
}
// use generated entities too
for (GeneratedTypeElement item : schema.generatedEntities) {
if (schema.isLogEnabled()) {
// generate log section - BEGIN
methodBuilder.addComment("log section BEGIN");
methodBuilder.beginControlFlow("if (this.logEnabled)");
methodBuilder.addStatement("$T.info(\"DDL: %s\",$T.CREATE_TABLE_SQL)", Logger.class, TypeUtility.className(BindTableGenerator.getTableClassName(item.getQualifiedName())));
// generate log section - END
methodBuilder.endControlFlow();
methodBuilder.addComment("log section END");
}
methodBuilder.addStatement("database.execSQL($T.CREATE_TABLE_SQL)", TypeUtility.className(BindTableGenerator.getTableClassName(item.getQualifiedName())));
}
methodBuilder.endControlFlow();
methodBuilder.beginControlFlow("if (options.databaseLifecycleHandler != null)");
methodBuilder.addStatement("options.databaseLifecycleHandler.onUpdate(database, previousVersion, currentVersion, true)");
methodBuilder.endControlFlow();
classBuilder.addMethod(methodBuilder.build());
}
use of com.abubusoft.kripton.processor.element.GeneratedTypeElement in project kripton by xcesco.
the class BindDataSourceBuilder method buildDataSource.
public void buildDataSource(Elements elementUtils, Filer filer, SQLiteDatabaseSchema schema, String daoFactoryName) throws Exception {
ClassName daoFactoryClazz = className(daoFactoryName);
Converter<String, String> convert = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL);
ClassName dataSourceClassName = generateDataSourceName(schema);
AnnotationProcessorUtilis.infoOnGeneratedClasses(BindDataSource.class, dataSourceClassName);
classBuilder = TypeSpec.classBuilder(dataSourceClassName.simpleName()).addModifiers(Modifier.PUBLIC).superclass(AbstractDataSource.class).addSuperinterface(daoFactoryClazz).addSuperinterface(TypeUtility.typeName(schema.getElement().asType()));
classBuilder.addJavadoc("<p>\n");
classBuilder.addJavadoc("Represents implementation of datasource $L.\n", schema.getName());
classBuilder.addJavadoc("This class expose database interface through Dao attribute.\n", schema.getName());
classBuilder.addJavadoc("</p>\n\n");
JavadocUtility.generateJavadocGeneratedBy(classBuilder);
classBuilder.addJavadoc("@see $T\n", className(schema.getName()));
classBuilder.addJavadoc("@see $T\n", daoFactoryClazz);
for (SQLiteDaoDefinition dao : schema.getCollection()) {
TypeName daoImplName = BindDaoBuilder.daoTypeName(dao);
classBuilder.addJavadoc("@see $T\n", dao.getElement());
classBuilder.addJavadoc("@see $T\n", daoImplName);
String entity = BindDataSourceSubProcessor.generateEntityName(dao, dao.getEntity());
classBuilder.addJavadoc("@see $T\n", TypeUtility.typeName(entity));
}
// define static fields
// instance
classBuilder.addField(FieldSpec.builder(dataSourceClassName, "instance", Modifier.STATIC, Modifier.VOLATILE).addJavadoc("<p>datasource singleton</p>\n").build());
classBuilder.addField(FieldSpec.builder(Object.class, "mutex", Modifier.STATIC, Modifier.FINAL, Modifier.PRIVATE).addJavadoc("<p>Mutex to manage multithread access to instance</p>\n").initializer("new Object()").build());
for (SQLiteDaoDefinition dao : schema.getCollection()) {
// TypeName daoInterfaceName =
// BindDaoBuilder.daoInterfaceTypeName(dao);
TypeName daoImplName = BindDaoBuilder.daoTypeName(dao);
classBuilder.addField(FieldSpec.builder(daoImplName, convert.convert(dao.getName()), Modifier.PROTECTED).addJavadoc("<p>dao instance</p>\n").initializer("new $T(context)", daoImplName).build());
// dao with connections
{
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("get" + dao.getName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(BindDaoBuilder.daoTypeName(dao));
methodBuilder.addCode("return $L;\n", convert.convert(dao.getName()));
classBuilder.addMethod(methodBuilder.build());
}
}
if (schema.generateRx) {
generateRx(dataSourceClassName, daoFactoryName);
for (SQLiteDaoDefinition dao : schema.getCollection()) {
// subject
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, dao.getEntitySimplyClassName() + "Subject")).addModifiers(Modifier.PUBLIC);
methodBuilder.addStatement("return $L.subject()", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, convert.convert(dao.getName()))).returns(ParameterizedTypeName.get(PublishSubject.class, SQLiteEvent.class));
classBuilder.addMethod(methodBuilder.build());
}
}
// interface
generateMethodExecuteTransaction(daoFactoryName);
generateMethodExecuteBatch(daoFactoryName);
// generate instance
generateInstanceOrBuild(schema, dataSourceClassName.simpleName(), true);
// generate open
generateOpen(dataSourceClassName.simpleName());
// generate openReadOnly
generateOpenReadOnly(dataSourceClassName.simpleName());
// generate constructor
generateConstructor(schema);
// before use entities, order them with dependencies respect
List<SQLiteEntity> orderedEntities = generateOrderedEntitiesList(schema);
// onCreate
boolean useForeignKey = generateOnCreate(schema, orderedEntities);
// onUpgrade
generateOnUpgrade(schema, orderedEntities);
// onConfigure
generateOnConfigure(useForeignKey);
// generate
generateDaoUids(classBuilder, schema);
//
// generate prepared statement cleaner
{
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("clearCompiledStatements").addModifiers(Modifier.PUBLIC).returns(Void.TYPE);
for (SQLiteDaoDefinition dao : schema.getCollection()) {
methodBuilder.addStatement("$T.clearCompiledStatements()", TypeUtility.className(dao.getElement().getQualifiedName().toString() + "Impl"));
}
classBuilder.addMethod(methodBuilder.build());
}
// generate single thread datasource
generateDataSourceSingleThread(schema, dataSourceClassName.simpleName());
// generate build
generateInstanceOrBuild(schema, dataSourceClassName.simpleName(), false);
{
Builder f = FieldSpec.builder(ArrayTypeName.of(SQLiteTable.class), "TABLES", Modifier.FINAL, Modifier.STATIC).addJavadoc("List of tables compose datasource\n");
com.squareup.javapoet.CodeBlock.Builder c = CodeBlock.builder();
String s = "";
c.add("{");
for (SQLiteEntity entity : schema.getEntities()) {
String tableName = BindTableGenerator.getTableClassName(entity.getName());
c.add(s + "new $T()", TypeUtility.className(tableName));
s = ", ";
}
for (GeneratedTypeElement entity : schema.generatedEntities) {
String tableName = BindTableGenerator.getTableClassName(entity.getQualifiedName());
c.add(s + "new $T()", TypeUtility.className(tableName));
s = ", ";
}
c.add("}");
f.initializer(c.build());
classBuilder.addField(f.build());
classBuilder.addMethod(MethodSpec.methodBuilder("tables").addJavadoc("List of tables compose datasource:\n").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addStatement("return TABLES").returns(ArrayTypeName.of(SQLiteTable.class)).build());
}
TypeSpec typeSpec = classBuilder.build();
JavaWriterHelper.writeJava2File(filer, dataSourceClassName.packageName(), typeSpec);
}
Aggregations