Search in sources :

Example 36 with One

use of com.abubusoft.kripton.common.One in project kripton by xcesco.

the class JQLBuilder method extractFieldsFromAnnotation.

/**
 * @param method
 * @param annotationClazz
 * @param includePrimaryKey
 * @param dao
 * @param entity
 * @return
 */
private static <A extends Annotation> LinkedHashSet<String> extractFieldsFromAnnotation(final SQLiteModelMethod method, Class<A> annotationClazz, final boolean includePrimaryKey) {
    final SQLiteDaoDefinition dao = method.getParent();
    final SQLiteEntity entity = method.getParent().getEntity();
    List<String> annotatedFieldValues = AnnotationUtility.extractAsStringArray(method.getElement(), annotationClazz, AnnotationAttributeType.FIELDS);
    List<String> annotatedExcludedFieldValues = AnnotationUtility.extractAsStringArray(method.getElement(), annotationClazz, AnnotationAttributeType.EXCLUDED_FIELDS);
    CollectionUtils.trim(annotatedFieldValues);
    CollectionUtils.trim(annotatedExcludedFieldValues);
    final One<Integer> count = new One<>(0);
    // extract properties from managed bean
    final Set<String> allFields = new LinkedHashSet<>();
    forEachFields(dao, new OnPropertyListener() {

        @Override
        public void onProperty(SQLProperty item) {
            if (!item.isPrimaryKey() || (item.isPrimaryKey() && includePrimaryKey)) {
                allFields.add(item.getName());
            }
            if (TypeUtility.isEquals(item.getPropertyType().getTypeName(), typeName(entity.getElement()))) {
                count.value0++;
            }
        }
    });
    // properties from selected set.
    if (annotatedFieldValues.size() == 0 && annotatedExcludedFieldValues.size() == 0) {
        // if no fields was selected, select all
        annotatedFieldValues.clear();
        annotatedFieldValues.addAll(allFields);
    } else if (annotatedExcludedFieldValues.size() > 0) {
        for (String fieldName : annotatedExcludedFieldValues) {
            if (!entity.contains(fieldName)) {
                AssertKripton.failUnknownPropertyInJQLException(method, annotationClazz, AnnotationAttributeType.EXCLUDED_FIELDS, fieldName);
            }
        }
        allFields.removeAll(annotatedExcludedFieldValues);
        annotatedFieldValues.clear();
        annotatedFieldValues.addAll(allFields);
    }
    LinkedHashSet<String> result = new LinkedHashSet<>();
    result.addAll(annotatedFieldValues);
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) One(com.abubusoft.kripton.common.One) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)

Aggregations

One (com.abubusoft.kripton.common.One)36 ArrayList (java.util.ArrayList)13 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)12 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)10 Elements (javax.lang.model.util.Elements)9 Where_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext)8 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)8 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)7 JQLReplaceVariableStatementListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl)7 JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)7 Pair (com.abubusoft.kripton.common.Pair)5 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)5 Bind_parameterContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext)4 Projected_columnsContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext)4 TypeName (com.squareup.javapoet.TypeName)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Test (org.junit.Test)4 BaseAndroidTest (base.BaseAndroidTest)3 TransactionResult (com.abubusoft.kripton.android.sqlite.TransactionResult)3