use of com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition 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.sqlite.model.SQLiteDaoDefinition in project kripton by xcesco.
the class BindDataSourceSubProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element dataSource : dataSets) {
SQLiteDatabaseSchema currentSchema = createDataSource(dataSource);
// definition
for (String daoName : currentSchema.getDaoNameSet()) {
// check dao into bean definition
createSQLEntityFromDao(currentSchema, dataSource, daoName);
}
// Get all generated dao definitions
for (String generatedDaoItem : currentSchema.getDaoNameSet()) {
createSQLDaoDefinition(currentSchema, globalBeanElements, globalDaoElements, generatedDaoItem);
}
analyzeForeignKey(currentSchema);
String msg;
if (currentSchema.getCollection().size() == 0) {
msg = String.format("No DAO definition with @%s annotation was found for class %s with @%s annotation", BindDao.class.getSimpleName(), currentSchema.getElement().getSimpleName().toString(), BindDataSource.class.getSimpleName());
// info(msg);
error(null, msg);
return true;
}
// for each dao definition, we define its uid
int uid = 0;
for (SQLiteDaoDefinition daoDefinition : currentSchema.getCollection()) {
String daoFieldName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, daoDefinition.getName()) + "_UID";
daoDefinition.daoUidName = daoFieldName;
daoDefinition.daoUidValue = uid;
uid++;
}
schemas.add(currentSchema);
}
return true;
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition in project kripton by xcesco.
the class SelectBeanHelper method generateSpecializedPart.
@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
SQLiteDaoDefinition daoDefinition = method.getParent();
SQLiteEntity entity = daoDefinition.getEntity();
// List<SQLProperty> fields = fieldList.value1;
// TypeName collectionClass;
TypeName entityClass = typeName(entity.getElement());
methodBuilder.addCode("\n");
methodBuilder.addCode("$T resultBean=null;\n", entityClass);
methodBuilder.addCode("\n");
methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
// generate index from columns
methodBuilder.addCode("\n");
{
int i = 0;
for (JQLProjection a : fieldList) {
SQLProperty item = a.property;
methodBuilder.addStatement("int index$L=_cursor.getColumnIndex($S)", (i++), item.columnName);
if (item.hasTypeAdapter()) {
methodBuilder.addStatement("$T $LAdapter=$T.getAdapter($T.class)", item.typeAdapter.getAdapterTypeName(), item.getName(), SQLTypeAdapterUtils.class, item.typeAdapter.getAdapterTypeName());
}
}
}
methodBuilder.addCode("\n");
methodBuilder.addCode("resultBean=new $T();\n\n", entityClass);
// generate mapping
int i = 0;
for (JQLProjection a : fieldList) {
SQLProperty item = a.property;
if (item.isNullable()) {
methodBuilder.addCode("if (!_cursor.isNull(index$L)) { ", i);
}
SQLTransformer.cursor2Java(methodBuilder, entityClass, item, "resultBean", "_cursor", "index" + i + "");
methodBuilder.addCode(";");
if (item.isNullable()) {
methodBuilder.addCode(" }");
}
methodBuilder.addCode("\n");
i++;
}
methodBuilder.addCode("\n");
methodBuilder.endControlFlow();
methodBuilder.addCode("return resultBean;\n");
// close try { open cursor
methodBuilder.endControlFlow();
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition in project kripton by xcesco.
the class SelectPaginatedResultHelper method generateSpecializedPart.
@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
SQLiteDaoDefinition daoDefinition = method.getParent();
SQLiteEntity entity = daoDefinition.getEntity();
// List<SQLProperty> fields = fieldList.value1;
TypeName entityClass = typeName(entity.getElement());
methodBuilder.addCode("\n");
methodBuilder.addStatement("$T<$T> resultList=new $T<$T>(_cursor.getCount())", List.class, entityClass, ArrayList.class, entityClass);
methodBuilder.addStatement("$T resultBean=null", entityClass);
methodBuilder.addCode("\n");
methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
// generate index from columns
methodBuilder.addCode("\n");
{
int i = 0;
for (JQLProjection a : fieldList) {
SQLProperty item = a.property;
methodBuilder.addStatement("int index$L=_cursor.getColumnIndex($S)", (i++), item.columnName);
if (item.hasTypeAdapter()) {
methodBuilder.addStatement("$T $LAdapter=$T.getAdapter($T.class)", item.typeAdapter.getAdapterTypeName(), item.getName(), SQLTypeAdapterUtils.class, item.typeAdapter.getAdapterTypeName());
}
}
}
methodBuilder.addCode("\n");
methodBuilder.beginControlFlow("do\n");
methodBuilder.addCode("resultBean=new $T();\n\n", entityClass);
// generate mapping
int i = 0;
for (JQLProjection a : fieldList) {
SQLProperty item = a.property;
if (item.isNullable()) {
methodBuilder.addCode("if (!_cursor.isNull(index$L)) { ", i);
}
SQLTransformer.cursor2Java(methodBuilder, typeName(entity.getElement()), item, "resultBean", "_cursor", "index" + i + "");
methodBuilder.addCode(";");
if (item.isNullable()) {
methodBuilder.addCode(" }");
}
methodBuilder.addCode("\n");
i++;
}
methodBuilder.addCode("\n");
methodBuilder.addCode("resultList.add(resultBean);\n");
methodBuilder.endControlFlow("while (_cursor.moveToNext())");
methodBuilder.endControlFlow();
methodBuilder.addCode("\n");
methodBuilder.addCode("return resultList;\n");
methodBuilder.endControlFlow();
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition in project kripton by xcesco.
the class SelectPaginatedResultHelper method generate.
@Override
public void generate(TypeSpec.Builder classBuilder, boolean mapFields, SQLiteModelMethod method) {
SQLiteDaoDefinition daoDefinition = method.getParent();
String pagedResultName = buildSpecializedPagedResultClass(classBuilder, method);
Set<JQLProjection> fieldList = JQLChecker.getInstance().extractProjections(method, method.jql.value, daoDefinition.getEntity());
{
MethodSpec.Builder methodBuilder = generateMethodBuilder(method);
// create PaginatedResult
String separator = "";
methodBuilder.addCode("$L paginatedResult=new $L(", pagedResultName, pagedResultName);
for (Pair<String, TypeName> item : method.getParameters()) {
// field
methodBuilder.addCode(separator + "$L", item.value0);
separator = ", ";
}
methodBuilder.addCode(");\n");
generateCommonPart(method, classBuilder, methodBuilder, fieldList, selectType.isMapFields(), GenerationType.NO_CONTENT, null);
methodBuilder.addStatement("return paginatedResult");
classBuilder.addMethod(methodBuilder.build());
}
// generate paged result method
{
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName()).addModifiers(Modifier.PRIVATE);
generateMethodSignature(method, methodBuilder, TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), TypeUtility.typeName(daoDefinition.getEntityClassName())), ParameterSpec.builder(TypeUtility.typeName(pagedResultName), "paginatedResult").build());
generateCommonPart(method, classBuilder, methodBuilder, fieldList, selectType.isMapFields(), GenerationType.NO_METHOD_SIGN, null, JavadocPart.build(JavadocPartType.ADD_PARAMETER, "paginatedResult", "handler of paginated result"), JavadocPart.build(JavadocPartType.RETURN, "", "result list"));
generateSpecializedPart(method, classBuilder, methodBuilder, fieldList, selectType.isMapFields());
classBuilder.addMethod(methodBuilder.build());
}
}
Aggregations