Search in sources :

Example 1 with SQLiteDaoDefinition

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));
    }
}
Also used : BindDaoMany2Many(com.abubusoft.kripton.android.annotation.BindDaoMany2Many) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) InvalidBeanTypeException(com.abubusoft.kripton.processor.exceptions.InvalidBeanTypeException) BindContentProvider(com.abubusoft.kripton.android.annotation.BindContentProvider) InvalidKindForAnnotationException(com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException) DaoDefinitionWithoutAnnotatedMethodException(com.abubusoft.kripton.processor.exceptions.DaoDefinitionWithoutAnnotatedMethodException) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) BindDao(com.abubusoft.kripton.android.annotation.BindDao) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) BindGeneratedDao(com.abubusoft.kripton.android.annotation.BindGeneratedDao) M2MEntity(com.abubusoft.kripton.processor.bind.model.many2many.M2MEntity) BindContentProviderPath(com.abubusoft.kripton.android.annotation.BindContentProviderPath)

Example 2 with SQLiteDaoDefinition

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;
}
Also used : SQLiteDatabaseSchema(com.abubusoft.kripton.processor.sqlite.model.SQLiteDatabaseSchema) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Example 3 with SQLiteDaoDefinition

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();
}
Also used : TypeName(com.squareup.javapoet.TypeName) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLTypeAdapterUtils(com.abubusoft.kripton.common.SQLTypeAdapterUtils) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Example 4 with SQLiteDaoDefinition

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();
}
Also used : TypeName(com.squareup.javapoet.TypeName) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLTypeAdapterUtils(com.abubusoft.kripton.common.SQLTypeAdapterUtils) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Example 5 with SQLiteDaoDefinition

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());
    }
}
Also used : JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) ArrayList(java.util.ArrayList) List(java.util.List) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) Pair(com.abubusoft.kripton.common.Pair)

Aggregations

SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)43 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)25 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)22 TypeName (com.squareup.javapoet.TypeName)21 Pair (com.abubusoft.kripton.common.Pair)9 JQLProjection (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection)9 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)9 ArrayList (java.util.ArrayList)9 One (com.abubusoft.kripton.common.One)8 MethodSpec (com.squareup.javapoet.MethodSpec)8 InvalidMethodSignException (com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)6 ClassName (com.squareup.javapoet.ClassName)6 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)5 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)5 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)5 TypeSpec (com.squareup.javapoet.TypeSpec)5 SQLiteStatement (android.database.sqlite.SQLiteStatement)4 BindSqlUpdate (com.abubusoft.kripton.android.annotation.BindSqlUpdate)4 GeneratedTypeElement (com.abubusoft.kripton.processor.element.GeneratedTypeElement)4 JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)4