Search in sources :

Example 1 with ModelConfig

use of com.amplifyframework.core.model.annotations.ModelConfig in project amplify-android by aws-amplify.

the class ModelSchema method fromModelClass.

/**
 * Construct the ModelSchema from the {@link Model} class.
 *
 * @param clazz the instance of a model class
 * @return the ModelSchema object.
 * @throws AmplifyException If the conversion fails
 */
@NonNull
public static ModelSchema fromModelClass(@NonNull Class<? extends Model> clazz) throws AmplifyException {
    try {
        final List<Field> classFields = FieldFinder.findModelFieldsIn(clazz);
        final TreeMap<String, ModelField> fields = new TreeMap<>();
        final TreeMap<String, ModelAssociation> associations = new TreeMap<>();
        final TreeMap<String, ModelIndex> indexes = new TreeMap<>();
        final List<AuthRule> authRules = new ArrayList<>();
        // Set the model name and plural name (null if not provided)
        ModelConfig modelConfig = clazz.getAnnotation(ModelConfig.class);
        final String modelName = clazz.getSimpleName();
        final String modelPluralName = modelConfig != null && !modelConfig.pluralName().isEmpty() ? modelConfig.pluralName() : null;
        final String listPluralName = modelConfig != null && !modelConfig.listPluralName().isEmpty() ? modelConfig.listPluralName() : null;
        final String syncPluralName = modelConfig != null && !modelConfig.syncPluralName().isEmpty() ? modelConfig.syncPluralName() : null;
        if (modelConfig != null) {
            for (com.amplifyframework.core.model.annotations.AuthRule ruleAnnotation : modelConfig.authRules()) {
                authRules.add(new AuthRule(ruleAnnotation));
            }
        }
        for (Annotation annotation : clazz.getAnnotations()) {
            if (annotation.annotationType().isAssignableFrom(Indexes.class)) {
                Indexes indexesAnnotation = (Indexes) annotation;
                for (Index indexAnnotation : indexesAnnotation.value()) {
                    ModelIndex modelIndex = createModelIndex(indexAnnotation);
                    indexes.put(modelIndex.getIndexName(), modelIndex);
                }
            } else if (annotation.annotationType().isAssignableFrom(Index.class)) {
                ModelIndex modelIndex = createModelIndex((Index) annotation);
                indexes.put(modelIndex.getIndexName(), modelIndex);
            }
        }
        for (Field field : classFields) {
            final ModelField modelField = createModelField(field);
            if (modelField != null) {
                fields.put(field.getName(), modelField);
            }
            final ModelAssociation modelAssociation = createModelAssociation(field);
            if (modelAssociation != null) {
                associations.put(field.getName(), modelAssociation);
            }
        }
        return ModelSchema.builder().name(modelName).pluralName(modelPluralName).listPluralName(listPluralName).syncPluralName(syncPluralName).authRules(authRules).fields(fields).associations(associations).indexes(indexes).modelClass(clazz).build();
    } catch (Exception exception) {
        throw new AmplifyException("Error in constructing a ModelSchema.", exception, AmplifyException.TODO_RECOVERY_SUGGESTION);
    }
}
Also used : AmplifyException(com.amplifyframework.AmplifyException) ArrayList(java.util.ArrayList) Index(com.amplifyframework.core.model.annotations.Index) TreeMap(java.util.TreeMap) Indexes(com.amplifyframework.core.model.annotations.Indexes) Annotation(java.lang.annotation.Annotation) AmplifyException(com.amplifyframework.AmplifyException) Field(java.lang.reflect.Field) ModelConfig(com.amplifyframework.core.model.annotations.ModelConfig) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)1 AmplifyException (com.amplifyframework.AmplifyException)1 Index (com.amplifyframework.core.model.annotations.Index)1 Indexes (com.amplifyframework.core.model.annotations.Indexes)1 ModelConfig (com.amplifyframework.core.model.annotations.ModelConfig)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1