Search in sources :

Example 1 with ModelField

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

the class AppSyncGraphQLRequestFactory method getMapOfFieldNameAndValues.

private static Map<String, Object> getMapOfFieldNameAndValues(@NonNull ModelSchema schema, @NonNull Model instance) throws AmplifyException {
    if (!instance.getClass().getSimpleName().equals(schema.getName())) {
        throw new AmplifyException("The object provided is not an instance of " + schema.getName() + ".", "Please provide an instance of " + schema.getName() + " that matches the schema type.");
    }
    final Map<String, Object> result = new HashMap<>();
    for (ModelField modelField : schema.getFields().values()) {
        if (modelField.isReadOnly()) {
            // Skip read only fields, since they should not be included on the input object.
            continue;
        }
        String fieldName = modelField.getName();
        Object fieldValue = extractFieldValue(fieldName, instance, schema);
        final ModelAssociation association = schema.getAssociations().get(fieldName);
        if (association == null) {
            result.put(fieldName, fieldValue);
        } else if (association.isOwner()) {
            Model target = (Model) Objects.requireNonNull(fieldValue);
            result.put(association.getTargetName(), target.getId());
        }
    // Ignore if field is associated, but is not a "belongsTo" relationship
    }
    /*
         * If the owner field exists on the model, and the value is null, it should be omitted when performing a
         * mutation because the AppSync server will automatically populate it using the authentication token provided
         * in the request header.  The logic below filters out the owner field if null for this scenario.
         */
    for (AuthRule authRule : schema.getAuthRules()) {
        if (AuthStrategy.OWNER.equals(authRule.getAuthStrategy())) {
            String ownerField = authRule.getOwnerFieldOrDefault();
            if (result.containsKey(ownerField) && result.get(ownerField) == null) {
                result.remove(ownerField);
            }
        }
    }
    return result;
}
Also used : AmplifyException(com.amplifyframework.AmplifyException) ModelAssociation(com.amplifyframework.core.model.ModelAssociation) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) Model(com.amplifyframework.core.model.Model) AuthRule(com.amplifyframework.core.model.AuthRule)

Example 2 with ModelField

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

the class MultiAuthModeStrategy method authTypesFor.

@Override
public AuthorizationTypeIterator authTypesFor(@NonNull ModelSchema modelSchema, @NonNull ModelOperation operation) {
    final List<AuthRule> applicableRules = new ArrayList<>();
    Consumer<List<AuthRule>> filterAuthRules = authRules -> {
        for (AuthRule rule : authRules) {
            if (rule.getOperationsOrDefault().contains(operation)) {
                applicableRules.add(rule);
            }
        }
    };
    filterAuthRules.accept(modelSchema.getAuthRules());
    for (ModelField field : modelSchema.getFields().values()) {
        filterAuthRules.accept(field.getAuthRules());
    }
    return new MultiAuthorizationTypeIterator(applicableRules);
}
Also used : Consumer(com.amplifyframework.core.Consumer) List(java.util.List) AuthorizationTypeIterator(com.amplifyframework.core.model.auth.AuthorizationTypeIterator) MultiAuthorizationTypeIterator(com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator) NonNull(androidx.annotation.NonNull) AuthRule(com.amplifyframework.core.model.AuthRule) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelOperation(com.amplifyframework.core.model.ModelOperation) ModelField(com.amplifyframework.core.model.ModelField) ArrayList(java.util.ArrayList) MultiAuthorizationTypeIterator(com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator) ModelField(com.amplifyframework.core.model.ModelField) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) AuthRule(com.amplifyframework.core.model.AuthRule)

Example 3 with ModelField

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

the class SelectionSetTest method nestedSerializedModelAndSerializedCustomType.

/**
 * Test generating SelectionSet for ModelSchema that nests CustomTypeSchema.
 * @throws AmplifyException if a ModelSchema can't be derived from OwnerAuth.class
 */
@Test
public void nestedSerializedModelAndSerializedCustomType() throws AmplifyException {
    SchemaRegistry schemaRegistry = SchemaRegistry.instance();
    CustomTypeField phoneCountryField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField phoneAreaField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField phoneNumber = CustomTypeField.builder().targetType("String").isRequired(true).build();
    Map<String, CustomTypeField> phoneFields = new HashMap<>();
    phoneFields.put("country", phoneCountryField);
    phoneFields.put("area", phoneAreaField);
    phoneFields.put("number", phoneNumber);
    CustomTypeSchema phoneSchema = CustomTypeSchema.builder().fields(phoneFields).name("Phone").pluralName("Phones").build();
    CustomTypeField addressCityField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField addressPhoneNumberField = CustomTypeField.builder().targetType("Phone").isCustomType(true).build();
    CustomTypeField addressLine1Field = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField addressLine2Field = CustomTypeField.builder().targetType("String").build();
    CustomTypeField addressStateField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField addressPostalCodeField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    Map<String, CustomTypeField> addressFields = new HashMap<>();
    addressFields.put("city", addressCityField);
    addressFields.put("phoneNumber", addressPhoneNumberField);
    addressFields.put("line1", addressLine1Field);
    addressFields.put("line2", addressLine2Field);
    addressFields.put("state", addressStateField);
    addressFields.put("postalCode", addressPostalCodeField);
    CustomTypeSchema addressSchema = CustomTypeSchema.builder().fields(addressFields).name("Address").pluralName("Addresses").build();
    ModelField personAddressField = ModelField.builder().name("address").isCustomType(true).targetType("Address").isRequired(true).build();
    ModelField personNameField = ModelField.builder().name("name").targetType("String").isRequired(true).build();
    ModelField personPhonesField = ModelField.builder().name("phoneNumbers").targetType("Phone").isCustomType(true).isArray(true).build();
    Map<String, ModelField> personFields = new HashMap<>();
    personFields.put("address", personAddressField);
    personFields.put("name", personNameField);
    personFields.put("phones", personPhonesField);
    ModelSchema personSchema = ModelSchema.builder().fields(personFields).name("Person").pluralName("People").modelClass(SerializedModel.class).build();
    // Register custom type schema for usage in SelectionSet
    schemaRegistry.register("Address", addressSchema);
    schemaRegistry.register("Phone", phoneSchema);
    // Register model schema for usage in SelectionSet
    schemaRegistry.register("Person", personSchema);
    SelectionSet selectionSet = SelectionSet.builder().modelClass(SerializedModel.class).modelSchema(personSchema).operation(QueryType.GET).requestOptions(new DefaultGraphQLRequestOptions()).build();
    String result = selectionSet.toString();
    assertEquals(Resources.readAsString("selection-set-nested-serialized-model-serialized-custom-type.txt"), result + "\n");
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) CustomTypeField(com.amplifyframework.core.model.CustomTypeField) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Test(org.junit.Test)

Example 4 with ModelField

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

the class AppSyncRequestFactoryTest method buildSerializedModelNestsSerializedCustomTypeSchemas.

private void buildSerializedModelNestsSerializedCustomTypeSchemas() {
    SchemaRegistry schemaRegistry = SchemaRegistry.instance();
    CustomTypeField addressLine1Field = CustomTypeField.builder().name("line1").isRequired(true).targetType("String").build();
    CustomTypeField addressLine2Field = CustomTypeField.builder().name("line2").isRequired(false).targetType("String").build();
    CustomTypeField addressCityField = CustomTypeField.builder().name("city").isRequired(true).targetType("String").build();
    CustomTypeField addressStateField = CustomTypeField.builder().name("state").isRequired(true).targetType("String").build();
    CustomTypeField addressPostalCodeField = CustomTypeField.builder().name("postalCode").isRequired(true).targetType("String").build();
    Map<String, CustomTypeField> addressFields = new HashMap<>();
    addressFields.put("line1", addressLine1Field);
    addressFields.put("line2", addressLine2Field);
    addressFields.put("city", addressCityField);
    addressFields.put("state", addressStateField);
    addressFields.put("postalCode", addressPostalCodeField);
    CustomTypeSchema addressSchema = CustomTypeSchema.builder().name("Address").pluralName("Addresses").fields(addressFields).build();
    CustomTypeField phoneCountryField = CustomTypeField.builder().name("country").isRequired(true).targetType("String").build();
    CustomTypeField phoneAreaField = CustomTypeField.builder().name("area").isRequired(true).targetType("String").build();
    CustomTypeField phoneNumberField = CustomTypeField.builder().name("number").isRequired(true).targetType("String").build();
    Map<String, CustomTypeField> phoneFields = new HashMap<>();
    phoneFields.put("country", phoneCountryField);
    phoneFields.put("area", phoneAreaField);
    phoneFields.put("number", phoneNumberField);
    CustomTypeSchema phoneSchema = CustomTypeSchema.builder().name("Phone").pluralName("Phones").fields(phoneFields).build();
    CustomTypeField bioEmailField = CustomTypeField.builder().name("email").isRequired(false).targetType("String").build();
    CustomTypeField bioPhoneField = CustomTypeField.builder().name("phone").isCustomType(true).targetType("Phone").build();
    Map<String, CustomTypeField> bioFields = new HashMap<>();
    bioFields.put("email", bioEmailField);
    bioFields.put("phone", bioPhoneField);
    CustomTypeSchema bioSchema = CustomTypeSchema.builder().name("Bio").fields(bioFields).build();
    ModelField personBioField = ModelField.builder().name("bio").targetType("Bio").isCustomType(true).isRequired(true).build();
    ModelField personNameField = ModelField.builder().name("name").targetType("String").isRequired(true).build();
    ModelField personMailingAddressesField = ModelField.builder().name("mailingAddresses").targetType("Address").isCustomType(true).isArray(true).build();
    ModelField personIdField = ModelField.builder().name("id").targetType("String").isRequired(true).build();
    Map<String, ModelField> personFields = new HashMap<>();
    personFields.put("bio", personBioField);
    personFields.put("name", personNameField);
    personFields.put("mailingAddresses", personMailingAddressesField);
    personFields.put("id", personIdField);
    ModelSchema personSchema = ModelSchema.builder().name("Person").pluralName("People").fields(personFields).modelClass(SerializedModel.class).build();
    schemaRegistry.register("Address", addressSchema);
    schemaRegistry.register("Phone", phoneSchema);
    schemaRegistry.register("Bio", bioSchema);
    schemaRegistry.register("Person", personSchema);
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) CustomTypeField(com.amplifyframework.core.model.CustomTypeField) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry)

Example 5 with ModelField

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

the class SQLiteCommandFactory method extractFieldValues.

// extract model field values to save in database
private List<Object> extractFieldValues(@NonNull Model model) throws DataStoreException {
    final String modelName = model.getModelName();
    final ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(modelName);
    final SQLiteTable table = SQLiteTable.fromSchema(schema);
    final SQLiteModelFieldTypeConverter converter = new SQLiteModelFieldTypeConverter(schema, schemaRegistry, gson);
    final Map<String, ModelField> modelFields = schema.getFields();
    final List<Object> bindings = new ArrayList<>();
    for (SQLiteColumn column : table.getSortedColumns()) {
        final ModelField modelField = Objects.requireNonNull(modelFields.get(column.getFieldName()));
        final Object fieldValue = converter.convertValueFromTarget(model, modelField);
        bindings.add(fieldValue);
    }
    return bindings;
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) SQLiteColumn(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteColumn) ArrayList(java.util.ArrayList) SQLiteTable(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable)

Aggregations

ModelField (com.amplifyframework.core.model.ModelField)14 HashMap (java.util.HashMap)9 ModelSchema (com.amplifyframework.core.model.ModelSchema)8 SerializedModel (com.amplifyframework.core.model.SerializedModel)7 ModelAssociation (com.amplifyframework.core.model.ModelAssociation)5 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)4 Test (org.junit.Test)4 AmplifyException (com.amplifyframework.AmplifyException)3 CustomTypeSchema (com.amplifyframework.core.model.CustomTypeSchema)3 ArrayList (java.util.ArrayList)3 NonNull (androidx.annotation.NonNull)2 AuthRule (com.amplifyframework.core.model.AuthRule)2 CustomTypeField (com.amplifyframework.core.model.CustomTypeField)2 DataStoreException (com.amplifyframework.datastore.DataStoreException)2 Field (java.lang.reflect.Field)2 List (java.util.List)2 Map (java.util.Map)2 Consumer (com.amplifyframework.core.Consumer)1 Model (com.amplifyframework.core.model.Model)1 ModelIndex (com.amplifyframework.core.model.ModelIndex)1