Search in sources :

Example 1 with BindDaoMany2Many

use of com.abubusoft.kripton.android.annotation.BindDaoMany2Many 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 BindDaoMany2Many

use of com.abubusoft.kripton.android.annotation.BindDaoMany2Many in project kripton by xcesco.

the class M2MEntity method extractEntityManagedByDAO.

/**
 * Works with @BindDaoMany2Many and @BindDao to extract entity name.
 *
 * @param item
 * @return
 */
public static M2MEntity extractEntityManagedByDAO(TypeElement daoElement) {
    ClassName entity1 = null;
    ClassName entity2 = null;
    String prefixId = null;
    String tableName = null;
    String entityName = null;
    PackageElement pkg = null;
    String packageName = null;
    boolean needToCreate = true;
    boolean generatedMethods = true;
    if (daoElement.getAnnotation(BindDaoMany2Many.class) != null) {
        entity1 = TypeUtility.className(AnnotationUtility.extractAsClassName(daoElement, BindDaoMany2Many.class, AnnotationAttributeType.ENTITY_1));
        entity2 = TypeUtility.className(AnnotationUtility.extractAsClassName(daoElement, BindDaoMany2Many.class, AnnotationAttributeType.ENTITY_2));
        prefixId = AnnotationUtility.extractAsString(daoElement, BindDaoMany2Many.class, AnnotationAttributeType.ID_NAME);
        tableName = AnnotationUtility.extractAsString(daoElement, BindDaoMany2Many.class, AnnotationAttributeType.TABLE_NAME);
        generatedMethods = AnnotationUtility.extractAsBoolean(daoElement, BindDaoMany2Many.class, AnnotationAttributeType.METHODS);
        entityName = entity1.simpleName() + entity2.simpleName();
        pkg = BaseProcessor.elementUtils.getPackageOf(daoElement);
        packageName = pkg.isUnnamed() ? null : pkg.getQualifiedName().toString();
    }
    if (daoElement.getAnnotation(BindDao.class) != null) {
        // we have @BindDao
        String derived = AnnotationUtility.extractAsClassName(daoElement, BindDao.class, AnnotationAttributeType.VALUE);
        ClassName clazz = TypeUtility.className(derived);
        packageName = clazz.packageName();
        entityName = clazz.simpleName();
        String tableTemp = AnnotationUtility.extractAsClassName(daoElement, BindDao.class, AnnotationAttributeType.TABLE_NAME);
        if (StringUtils.hasText(tableTemp)) {
            tableName = tableTemp;
        }
        needToCreate = false;
    }
    M2MEntity entity = new M2MEntity(daoElement, packageName, entityName, TypeUtility.className(daoElement.asType().toString()), entity1, entity2, prefixId, tableName, needToCreate, generatedMethods);
    return entity;
}
Also used : BindDao(com.abubusoft.kripton.android.annotation.BindDao) BindDaoMany2Many(com.abubusoft.kripton.android.annotation.BindDaoMany2Many) ClassName(com.squareup.javapoet.ClassName) PackageElement(javax.lang.model.element.PackageElement)

Aggregations

BindDao (com.abubusoft.kripton.android.annotation.BindDao)2 BindDaoMany2Many (com.abubusoft.kripton.android.annotation.BindDaoMany2Many)2 BindContentProvider (com.abubusoft.kripton.android.annotation.BindContentProvider)1 BindContentProviderPath (com.abubusoft.kripton.android.annotation.BindContentProviderPath)1 BindGeneratedDao (com.abubusoft.kripton.android.annotation.BindGeneratedDao)1 M2MEntity (com.abubusoft.kripton.processor.bind.model.many2many.M2MEntity)1 GeneratedTypeElement (com.abubusoft.kripton.processor.element.GeneratedTypeElement)1 DaoDefinitionWithoutAnnotatedMethodException (com.abubusoft.kripton.processor.exceptions.DaoDefinitionWithoutAnnotatedMethodException)1 InvalidBeanTypeException (com.abubusoft.kripton.processor.exceptions.InvalidBeanTypeException)1 InvalidKindForAnnotationException (com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException)1 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)1 ClassName (com.squareup.javapoet.ClassName)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 PackageElement (javax.lang.model.element.PackageElement)1 TypeElement (javax.lang.model.element.TypeElement)1