Search in sources :

Example 6 with ModelAnnotation

use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.

the class SQLiteModelMethod method getJQLDeclared.

private String getJQLDeclared() {
    ModelAnnotation inserAnnotation = this.getAnnotation(BindSqlInsert.class);
    ModelAnnotation updateAnnotation = this.getAnnotation(BindSqlUpdate.class);
    ModelAnnotation selectAnnotation = this.getAnnotation(BindSqlSelect.class);
    ModelAnnotation deleteAnnotation = this.getAnnotation(BindSqlDelete.class);
    String jql = null;
    int counter = 0;
    if (selectAnnotation != null) {
        jql = selectAnnotation.getAttribute(AnnotationAttributeType.JQL);
        if (StringUtils.hasText(jql)) {
            counter++;
            AssertKripton.assertTrue(selectAnnotation.getAttributeCount() > 1, "Annotation %s in method %s.%s have more than one annotation with JQL attribute", selectAnnotation.getSimpleName(), this.getParent().getName(), this.getName());
        }
    }
    if (inserAnnotation != null) {
        jql = inserAnnotation.getAttribute(AnnotationAttributeType.JQL);
        if (StringUtils.hasText(jql)) {
            counter++;
            AssertKripton.assertTrue(inserAnnotation.getAttributeCount() > 1, "Annotation %s in method %s.%s have more than one annotation with JQL attribute", inserAnnotation.getSimpleName(), this.getParent().getName(), this.getName());
        }
    }
    if (updateAnnotation != null) {
        jql = updateAnnotation.getAttribute(AnnotationAttributeType.JQL);
        if (StringUtils.hasText(jql)) {
            counter++;
            AssertKripton.assertTrue(updateAnnotation.getAttributeCount() > 1, "Annotation %s in method %s.%s have more than one annotation with JQL attribute", updateAnnotation.getSimpleName(), this.getParent().getName(), this.getName());
        }
    }
    if (deleteAnnotation != null) {
        jql = deleteAnnotation.getAttribute(AnnotationAttributeType.JQL);
        if (StringUtils.hasText(jql)) {
            counter++;
            AssertKripton.assertTrue(deleteAnnotation.getAttributeCount() > 1, "Annotation %s in method %s.%s have more than one annotation with JQL attribute", deleteAnnotation.getSimpleName(), this.getParent().getName(), this.getName());
        }
    }
    AssertKripton.assertTrue(counter <= 1, "Method %s.%s have more than one annotation with JQL attribute", this.getParent().getName(), this.getName());
    // remove unscape charater (example \'%\' -> '%')
    jql = StringEscapeUtils.unescapeEcmaScript(jql);
    return jql;
}
Also used : ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation)

Example 7 with ModelAnnotation

use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.

the class AnnotationUtility method getAnnotationAttribute.

static <T> T getAnnotationAttribute(ModelWithAnnotation model, Class<? extends Annotation> annotation, AnnotationAttributeType attribute, T defaultValue, OnAnnotationAttributeListener<T> listener) {
    String attributeResult;
    ModelAnnotation item = model.getAnnotation(annotation);
    if (item != null) {
        attributeResult = item.getAttribute(attribute);
        return listener.onFound(attributeResult);
    }
    return defaultValue;
}
Also used : ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation)

Example 8 with ModelAnnotation

use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.

the class SqlInsertBuilder method detectInsertType.

public static InsertType detectInsertType(SQLiteModelMethod method) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    InsertType insertResultType = null;
    // check type of arguments
    int count = 0;
    for (Pair<String, TypeName> param : method.getParameters()) {
        if (TypeUtility.isEquals(param.value1, typeName(entity.getElement()))) {
            count++;
        }
    }
    AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() == 0, method, " INSERT operations require at least one parameter");
    if (count == 0) {
        // method to insert raw data: no bean is used
        insertResultType = InsertType.INSERT_RAW;
        ModelAnnotation annotation = method.getAnnotation(BindSqlInsert.class);
        // check value attribute
        AssertKripton.failWithInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.FIELDS).size() > 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.FIELDS.getValue());
        // check excludeFields attribute
        AssertKripton.failWithInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.EXCLUDED_FIELDS).size() > 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.EXCLUDED_FIELDS.getValue());
        // check if there is only one parameter
        AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() != 1 && TypeUtility.isEquals(method.getParameters().get(0).value1, daoDefinition.getEntityClassName()), method);
        // check no
        AssertKripton.failWithInvalidMethodSignException(annotation.getAttributeAsBoolean(AnnotationAttributeType.INCLUDE_PRIMARY_KEY), method, "attribute '%s' can not be used here", AnnotationAttributeType.INCLUDE_PRIMARY_KEY.getValue());
    } else if (count == 1) {
        insertResultType = InsertType.INSERT_BEAN;
        AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() > 1, method, " aspected only one parameter of %s type", daoDefinition.getEntityClassName());
    } else {
        throw (new InvalidMethodSignException(method, "only one parameter of type " + typeName(entity.getElement()) + " can be used"));
    }
    return insertResultType;
}
Also used : TypeName(com.squareup.javapoet.TypeName) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Example 9 with ModelAnnotation

use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.

the class SqlSelectBuilder method generateSelect.

/**
 * @param elementUtils
 * @param builder
 * @param method
 * @throws ClassNotFoundException
 */
public static void generateSelect(Builder builder, SQLiteModelMethod method) throws ClassNotFoundException {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    SelectBuilderUtility.SelectType selectResultType = null;
    // if true, field must be associate to ben attributes
    TypeName returnTypeName = method.getReturnClass();
    ParameterizedTypeName readBeanListener = ParameterizedTypeName.get(ClassName.get(OnReadBeanListener.class), ClassName.get(entity.getElement()));
    ClassName readCursorListener = ClassName.get(OnReadCursorListener.class);
    ModelAnnotation annotation = method.getAnnotation(BindSqlSelect.class);
    int pageSize = annotation.getAttributeAsInt(AnnotationAttributeType.PAGE_SIZE);
    AssertKripton.failWithInvalidMethodSignException(pageSize < 0, method, "in @%s(pageSize) must be set with positive number", BindSqlSelect.class.getSimpleName());
    AssertKripton.failWithInvalidMethodSignException(pageSize > 0 && method.hasDynamicPageSizeConditions(), method, "can not define @%s(pageSize) and mark a method parameter with @%s ", BindSqlSelect.class.getSimpleName(), BindSqlPageSize.class.getSimpleName());
    if (TypeUtility.isTypeIncludedIn(returnTypeName, Void.class, Void.TYPE)) {
        // return VOID (in the parameters must be a listener)
        if (SqlBuilderHelper.hasParameterOfType(method, readCursorListener)) {
            selectResultType = SelectBuilderUtility.SelectType.LISTENER_CURSOR;
        } else if (SqlBuilderHelper.hasParameterOfType(method, readBeanListener)) {
            selectResultType = SelectBuilderUtility.SelectType.LISTENER_BEAN;
        }
    } else if (TypeUtility.isTypeIncludedIn(returnTypeName, Cursor.class)) {
        // return Cursor (no listener)
        selectResultType = SelectBuilderUtility.SelectType.CURSOR;
    } else if (returnTypeName instanceof ParameterizedTypeName) {
        ParameterizedTypeName returnParameterizedTypeName = (ParameterizedTypeName) returnTypeName;
        ClassName returnParameterizedClassName = returnParameterizedTypeName.rawType;
        // return List (no listener)
        AssertKripton.assertTrueOrInvalidMethodSignException(returnParameterizedTypeName.typeArguments.size() == 1, method, "return type %s is not supported", returnTypeName);
        TypeName elementName = returnParameterizedTypeName.typeArguments.get(0);
        Class<?> wrapperClazz = Class.forName(returnParameterizedClassName.toString());
        if (PaginatedResult.class.isAssignableFrom(wrapperClazz)) {
            // method must have pageSize, statically or dynamically
            // defined
            AssertKripton.assertTrueOrInvalidMethodSignException(method.hasDynamicPageSizeConditions() || pageSize > 0, method, "use of PaginatedResult requires 'pageSize' attribute or a @%s annotated parameter", returnTypeName, BindSqlPageSize.class.getSimpleName());
            // paged result
            AssertKripton.assertTrueOrInvalidMethodSignException(TypeUtility.isEquals(elementName, entity.getName().toString()), method, "return type %s is not supported", returnTypeName);
            selectResultType = SelectBuilderUtility.SelectType.PAGED_RESULT;
            // set typeName of paginatedResult
            method.paginatedResultName = "paginatedResult";
        } else if (Collection.class.isAssignableFrom(wrapperClazz)) {
            if (TypeUtility.isEquals(elementName, entity.getName().toString())) {
                // entity list
                selectResultType = SelectBuilderUtility.SelectType.LIST_BEAN;
            } else if (SQLTransformer.isSupportedJDKType(elementName) || TypeUtility.isByteArray(elementName)) {
                // scalar list
                selectResultType = SelectBuilderUtility.SelectType.LIST_SCALAR;
            } else {
                AssertKripton.failWithInvalidMethodSignException(true, method, "%s is invalid return type", method.getReturnClass());
            }
        }
    } else if (TypeUtility.isEquals(returnTypeName, entity)) {
        // return one element (no listener)
        selectResultType = SelectBuilderUtility.SelectType.BEAN;
    } else if (SQLTransformer.isSupportedJDKType(returnTypeName) || TypeUtility.isByteArray(returnTypeName)) {
        // return single value string, int, long, short, double, float,
        // String (no listener)
        selectResultType = SelectBuilderUtility.SelectType.SCALAR;
    }
    AssertKripton.assertTrueOrInvalidMethodSignException(selectResultType != null, method, "'%s' as return type is not supported", returnTypeName);
    // generate select method
    selectResultType.generate(builder, method);
    if (method.hasLiveData()) {
        // generate
        selectResultType.generateLiveData(builder, method);
    }
    if (method.contentProviderEntryPathEnabled) {
        // we need to generate UPDATE or DELETE for content provider to
        generateSelectForContentProvider(builder, method, selectResultType);
    }
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) OnReadBeanListener(com.abubusoft.kripton.android.sqlite.OnReadBeanListener) Cursor(android.database.Cursor) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) SelectType(com.abubusoft.kripton.processor.sqlite.SelectBuilderUtility.SelectType) ClassName(com.squareup.javapoet.ClassName) Collection(java.util.Collection) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) BindSqlSelect(com.abubusoft.kripton.android.annotation.BindSqlSelect) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) BindSqlPageSize(com.abubusoft.kripton.android.annotation.BindSqlPageSize)

Example 10 with ModelAnnotation

use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.

the class InsertBeanHelper method getConflictAlgorithmType.

public static ConflictAlgorithmType getConflictAlgorithmType(SQLiteModelMethod method) {
    ModelAnnotation annotation = method.getAnnotation(BindSqlInsert.class);
    String value = annotation.getAttribute(AnnotationAttributeType.CONFLICT_ALGORITHM_TYPE);
    if (value != null && value.indexOf(".") > -1) {
        value = value.substring(value.lastIndexOf(".") + 1);
    }
    ConflictAlgorithmType conflictAlgorithmType = ConflictAlgorithmType.valueOf(value);
    return conflictAlgorithmType;
}
Also used : ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) ConflictAlgorithmType(com.abubusoft.kripton.android.sqlite.ConflictAlgorithmType)

Aggregations

ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)15 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)5 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)5 TypeName (com.squareup.javapoet.TypeName)5 ArrayList (java.util.ArrayList)5 Element (javax.lang.model.element.Element)5 TypeElement (javax.lang.model.element.TypeElement)4 BindEntity (com.abubusoft.kripton.processor.bind.model.BindEntity)3 BindProperty (com.abubusoft.kripton.processor.bind.model.BindProperty)3 InvalidDefinition (com.abubusoft.kripton.processor.exceptions.InvalidDefinition)3 BindSqlSelect (com.abubusoft.kripton.android.annotation.BindSqlSelect)2 BindSqlUpdate (com.abubusoft.kripton.android.annotation.BindSqlUpdate)2 BindDisabled (com.abubusoft.kripton.annotation.BindDisabled)2 One (com.abubusoft.kripton.common.One)2 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)2 GeneratedTypeElement (com.abubusoft.kripton.processor.element.GeneratedTypeElement)2 InvalidMethodSignException (com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)2 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)2 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2