Search in sources :

Example 41 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty 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

SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)41 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)23 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)22 TypeName (com.squareup.javapoet.TypeName)18 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)14 ArrayList (java.util.ArrayList)12 One (com.abubusoft.kripton.common.One)10 Pair (com.abubusoft.kripton.common.Pair)9 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)7 JQLProjection (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection)7 InvalidMethodSignException (com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)6 ModelProperty (com.abubusoft.kripton.processor.core.ModelProperty)5 LinkedHashSet (java.util.LinkedHashSet)5 SQLiteStatement (android.database.sqlite.SQLiteStatement)4 SQLTypeAdapterUtils (com.abubusoft.kripton.common.SQLTypeAdapterUtils)4 PropertyNotFoundException (com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException)4 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)4 BaseProcessorTest (base.BaseProcessorTest)3 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)3 JQL (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL)3