Search in sources :

Example 1 with InvalidNameException

use of com.abubusoft.kripton.processor.exceptions.InvalidNameException in project kripton by xcesco.

the class BindDataSourceSubProcessor method createSQLEntityFromDao.

/**
 * <p>
 * Create bean's definition for each dao definition contained in dataSource
 * </p>
 *
 * @param dataSource
 * @param daoName
 */
private boolean createSQLEntityFromDao(final SQLiteDatabaseSchema schema, Element dataSource, String daoName) {
    TypeElement daoElement = globalDaoElements.get(daoName);
    if (daoElement == null) {
        String msg = String.format("Data source %s references a DAO %s without @BindDao annotation", dataSource.toString(), daoName);
        throw (new InvalidNameException(msg));
    }
    ModelProperty property;
    String beanName = AnnotationUtility.extractAsClassName(daoElement, BindDao.class, AnnotationAttributeType.VALUE);
    if (!StringUtils.hasText(beanName)) {
        return false;
    }
    final TypeElement beanElement = globalBeanElements.get(beanName);
    // this.isGeneratedEntity(beanName);
    AssertKripton.asserTrueOrMissedAnnotationOnClassException(beanElement != null, daoElement, beanName);
    // create equivalent entity in the domain of bind processor
    final BindEntity bindEntity = BindEntityBuilder.parse(null, beanElement);
    // assert: bean is present
    final SQLiteEntity currentEntity = new SQLiteEntity(schema, bindEntity);
    if (schema.contains(currentEntity.getName())) {
        // bean already defined in datasource
        return true;
    }
    final boolean bindAllFields = AnnotationUtility.getAnnotationAttributeAsBoolean(currentEntity, BindType.class, AnnotationAttributeType.ALL_FIELDS, Boolean.TRUE);
    {
        PropertyUtility.buildProperties(elementUtils, currentEntity, new PropertyFactory<SQLiteEntity, SQLProperty>() {

            @Override
            public SQLProperty createProperty(SQLiteEntity entity, Element propertyElement) {
                return new SQLProperty(entity, propertyElement, AnnotationUtility.buildAnnotationList(propertyElement));
            }
        }, propertyAnnotationFilter, new PropertyCreatedListener<SQLiteEntity, SQLProperty>() {

            @Override
            public boolean onProperty(SQLiteEntity entity, SQLProperty property) {
                if (property.hasAnnotation(BindDisabled.class)) {
                    if (bindAllFields) {
                        return false;
                    } else {
                        throw new InvalidDefinition("@BindDisabled can not be used with @BindType(allField=false)");
                    }
                }
                ModelAnnotation annotationBindColumn = property.getAnnotation(BindColumn.class);
                if (annotationBindColumn != null && AnnotationUtility.extractAsBoolean(property, annotationBindColumn, AnnotationAttributeType.ENABLED) == false) {
                    return false;
                }
                if (!bindAllFields && annotationBindColumn == null) {
                    return false;
                }
                if (annotationBindColumn != null) {
                    property.setNullable(AnnotationUtility.extractAsBoolean(property, annotationBindColumn, AnnotationAttributeType.NULLABLE));
                    ColumnType columnType = ColumnType.valueOf(AnnotationUtility.extractAsEnumerationValue(property, annotationBindColumn, AnnotationAttributeType.COLUMN_TYPE));
                    property.columnType = columnType;
                    property.setPrimaryKey(columnType == ColumnType.PRIMARY_KEY);
                    String foreignClassName = annotationBindColumn.getAttributeAsClassName(AnnotationAttributeType.FOREIGN_KEY);
                    property.foreignClassName = foreignClassName;
                    if (property.hasForeignKeyClassName() && property.columnType == ColumnType.PRIMARY_KEY) {
                        AssertKripton.failIncompatibleAttributesInAnnotationException("In class '%s' property '%s' can not be defined as PRIMARY KEY and FOREIGN KEY", bindEntity.getElement().asType(), property.getName());
                    }
                    ForeignKeyAction onDeleteAction = ForeignKeyAction.valueOf(AnnotationUtility.extractAsEnumerationValue(property, annotationBindColumn, AnnotationAttributeType.ON_DELETE));
                    ForeignKeyAction onUpdateAction = ForeignKeyAction.valueOf(AnnotationUtility.extractAsEnumerationValue(property, annotationBindColumn, AnnotationAttributeType.ON_UPDATE));
                    if (!property.hasForeignKeyClassName() && onDeleteAction != ForeignKeyAction.NO_ACTION) {
                        String msg = String.format("In class '%s', property '%s' defines 'onDelete' attribute but it is not a foreign key", bindEntity.getElement().asType(), property.getName());
                        AssertKripton.failIncompatibleAttributesInAnnotationException(msg);
                    }
                    if (!property.hasForeignKeyClassName() && onUpdateAction != ForeignKeyAction.NO_ACTION) {
                        String msg = String.format("In class '%s', property '%s' defines 'onUpdate' attribute but it is not a foreign key", bindEntity.getElement().asType(), property.getName());
                        AssertKripton.failIncompatibleAttributesInAnnotationException(msg);
                    }
                    property.onDeleteAction = onDeleteAction;
                    property.onUpdateAction = onUpdateAction;
                } else {
                    // primary key is set in other places
                    property.setNullable(true);
                    // ColumnType columnType = ColumnType.STANDARD;
                    property.columnType = ColumnType.STANDARD;
                }
                if (bindEntity.contains(property.getName())) {
                    BindProperty bindProperty = bindEntity.get(property.getName());
                    if (bindProperty.isBindedArray() || bindProperty.isBindedCollection() || bindProperty.isBindedMap() || bindProperty.isBindedObject()) {
                        property.bindProperty = bindProperty;
                    }
                } else {
                    throw (new KriptonRuntimeException(String.format("In class '%s' property '%s' has a wrong definition for create SQLite DataSource", bindEntity.getElement().asType(), property.getName())));
                }
                String columnName = null;
                if (annotationBindColumn != null) {
                    columnName = annotationBindColumn.getAttribute(AnnotationAttributeType.VALUE);
                }
                if (!StringUtils.hasText(columnName)) {
                    columnName = property.getName();
                }
                // convert column typeName from field typeName to table:
                // fieldName
                // to field_name
                property.columnName = schema.columnNameConverter.convert(columnName);
                return true;
            }
        });
    }
    // just to fix that property id can be the default PK without
    // annotation.
    // this operation force primary key flag for property
    SQLProperty primaryKey = currentEntity.getPrimaryKey();
    if (primaryKey != null) {
        primaryKey.setPrimaryKey(true);
        primaryKey.columnType = ColumnType.PRIMARY_KEY;
        primaryKey.setNullable(false);
    }
    if (currentEntity.getCollection().size() == 0) {
        String msg = String.format("Class '%s', used in %s database definition, has no property!", currentEntity.getName(), dataSource.getSimpleName().toString());
        throw (new PropertyNotFoundException(msg));
    }
    if (currentEntity.countPrimaryKeys() > 1) {
        throw (new TooManySQLPrimaryKeyFoundException(currentEntity));
    }
    // check primary key
    property = currentEntity.getPrimaryKey();
    if (property == null)
        throw (new SQLPrimaryKeyNotFoundException(currentEntity));
    if (!property.isType(Long.TYPE, Long.class))
        throw (new SQLPrimaryKeyNotValidTypeException(currentEntity, property));
    // add entity to schema after properties definition!
    schema.addEntity(currentEntity);
    return true;
}
Also used : PropertyCreatedListener(com.abubusoft.kripton.processor.core.reflect.PropertyUtility.PropertyCreatedListener) ColumnType(com.abubusoft.kripton.android.ColumnType) PropertyNotFoundException(com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException) TypeElement(javax.lang.model.element.TypeElement) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) 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) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException) TooManySQLPrimaryKeyFoundException(com.abubusoft.kripton.processor.exceptions.TooManySQLPrimaryKeyFoundException) BindEntity(com.abubusoft.kripton.processor.bind.model.BindEntity) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) InvalidNameException(com.abubusoft.kripton.processor.exceptions.InvalidNameException) InvalidDefinition(com.abubusoft.kripton.processor.exceptions.InvalidDefinition) PropertyFactory(com.abubusoft.kripton.processor.core.reflect.PropertyFactory) ForeignKeyAction(com.abubusoft.kripton.android.sqlite.ForeignKeyAction) ModelProperty(com.abubusoft.kripton.processor.core.ModelProperty) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) SQLPrimaryKeyNotValidTypeException(com.abubusoft.kripton.processor.exceptions.SQLPrimaryKeyNotValidTypeException) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty) SQLPrimaryKeyNotFoundException(com.abubusoft.kripton.processor.exceptions.SQLPrimaryKeyNotFoundException)

Example 2 with InvalidNameException

use of com.abubusoft.kripton.processor.exceptions.InvalidNameException in project kripton by xcesco.

the class BindDataSourceSubProcessor method createDataSource.

/**
 * @param databaseSchema
 * @return databaseSchema
 */
protected SQLiteDatabaseSchema createDataSource(Element databaseSchema) {
    if (databaseSchema.getKind() != ElementKind.INTERFACE) {
        String msg = String.format("Class %s: only interfaces can be annotated with @%s annotation", databaseSchema.getSimpleName().toString(), BindDataSource.class.getSimpleName());
        throw (new InvalidKindForAnnotationException(msg));
    }
    if (!databaseSchema.getSimpleName().toString().endsWith(BindDataSourceBuilder.SUFFIX)) {
        String msg = String.format("Interface %s marked with @%s annotation must have a typeName with suffix \"" + BindDataSourceBuilder.SUFFIX + "\" to be used with @BindDataSource", databaseSchema.getSimpleName().toString(), BindDataSource.class.getSimpleName());
        throw (new InvalidNameException(msg));
    }
    // go ahead to dataSource analysis
    // ASSERT: daoElement and beanElement is element for dao and bean
    // associated
    String schemaFileName = AnnotationUtility.extractAsString(databaseSchema, BindDataSource.class, AnnotationAttributeType.FILENAME);
    int schemaVersion = AnnotationUtility.extractAsInt(databaseSchema, BindDataSource.class, AnnotationAttributeType.VERSION);
    boolean generateLog = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_LOG);
    boolean generateSchema = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_SCHEMA);
    boolean generateAsyncTask = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_ASYNC_TASK);
    boolean generateCursorWrapper = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_CURSOR_WRAPPER);
    boolean generateRx = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_RX);
    // get all dao used within SQLDatabaseSchema annotation
    List<String> daoIntoDataSource = AnnotationUtility.extractAsClassNameArray(elementUtils, databaseSchema, BindDataSource.class, AnnotationAttributeType.DAO_SET);
    String configCursorFactory = NoCursorFactory.class.getName();
    String configDatabaseErrorHandler = NoDatabaseErrorHandler.class.getName();
    String configDatabaseLifecycleHandler = NoDatabaseLifecycleHandler.class.getName();
    boolean configInMemory = false;
    boolean configLogEnabled = true;
    String configPopulatorClass = NoPopulator.class.getName();
    // manage for annotated data-source options
    BindDataSourceOptions dataSourceOptionsAnnotation = databaseSchema.getAnnotation(BindDataSourceOptions.class);
    if (dataSourceOptionsAnnotation != null) {
        configInMemory = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.IN_MEMORY);
        configLogEnabled = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.LOG_ENABLED);
        configPopulatorClass = AnnotationUtility.extractAsClassName(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.POPULATOR);
        configCursorFactory = AnnotationUtility.extractAsClassName(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.CURSOR_FACTORY);
        configDatabaseLifecycleHandler = AnnotationUtility.extractAsClassName(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.DATABASE_LIFECYCLE_HANDLER);
    }
    SQLiteDatabaseSchema schema = new SQLiteDatabaseSchema((TypeElement) databaseSchema, schemaFileName, schemaVersion, generateSchema, generateLog, generateAsyncTask, generateCursorWrapper, generateRx, daoIntoDataSource, configCursorFactory, configDatabaseErrorHandler, configDatabaseLifecycleHandler, configInMemory, configLogEnabled, configPopulatorClass);
    // manage for content provider generation
    BindContentProvider contentProviderAnnotation = databaseSchema.getAnnotation(BindContentProvider.class);
    if (contentProviderAnnotation != null) {
        schema.generateContentProvider = true;
        schema.contentProvider = new SQLiteModelContentProvider();
        schema.contentProvider.authority = contentProviderAnnotation.authority();
    } else {
        schema.generateContentProvider = false;
    }
    return schema;
}
Also used : SQLiteDatabaseSchema(com.abubusoft.kripton.processor.sqlite.model.SQLiteDatabaseSchema) BindDataSource(com.abubusoft.kripton.android.annotation.BindDataSource) InvalidNameException(com.abubusoft.kripton.processor.exceptions.InvalidNameException) BindDataSourceOptions(com.abubusoft.kripton.android.annotation.BindDataSourceOptions) BindContentProvider(com.abubusoft.kripton.android.annotation.BindContentProvider) InvalidKindForAnnotationException(com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException) SQLiteModelContentProvider(com.abubusoft.kripton.processor.sqlite.model.SQLiteModelContentProvider)

Aggregations

InvalidNameException (com.abubusoft.kripton.processor.exceptions.InvalidNameException)2 ColumnType (com.abubusoft.kripton.android.ColumnType)1 BindContentProvider (com.abubusoft.kripton.android.annotation.BindContentProvider)1 BindDataSource (com.abubusoft.kripton.android.annotation.BindDataSource)1 BindDataSourceOptions (com.abubusoft.kripton.android.annotation.BindDataSourceOptions)1 ForeignKeyAction (com.abubusoft.kripton.android.sqlite.ForeignKeyAction)1 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)1 BindEntity (com.abubusoft.kripton.processor.bind.model.BindEntity)1 BindProperty (com.abubusoft.kripton.processor.bind.model.BindProperty)1 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)1 ModelProperty (com.abubusoft.kripton.processor.core.ModelProperty)1 PropertyFactory (com.abubusoft.kripton.processor.core.reflect.PropertyFactory)1 PropertyCreatedListener (com.abubusoft.kripton.processor.core.reflect.PropertyUtility.PropertyCreatedListener)1 GeneratedTypeElement (com.abubusoft.kripton.processor.element.GeneratedTypeElement)1 InvalidDefinition (com.abubusoft.kripton.processor.exceptions.InvalidDefinition)1 InvalidKindForAnnotationException (com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException)1 PropertyNotFoundException (com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException)1 SQLPrimaryKeyNotFoundException (com.abubusoft.kripton.processor.exceptions.SQLPrimaryKeyNotFoundException)1 SQLPrimaryKeyNotValidTypeException (com.abubusoft.kripton.processor.exceptions.SQLPrimaryKeyNotValidTypeException)1 TooManySQLPrimaryKeyFoundException (com.abubusoft.kripton.processor.exceptions.TooManySQLPrimaryKeyFoundException)1