Search in sources :

Example 26 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class AppSyncRequestFactory method buildUpdateRequest.

static <M extends Model> AppSyncGraphQLRequest<ModelWithMetadata<M>> buildUpdateRequest(ModelSchema schema, M model, Integer version, QueryPredicate predicate, AuthModeStrategyType strategyType) throws DataStoreException {
    try {
        Map<String, Object> inputMap = new HashMap<>();
        inputMap.put("_version", version);
        inputMap.putAll(getMapOfFieldNameAndValues(schema, model));
        return buildMutation(schema, inputMap, predicate, MutationType.UPDATE, strategyType);
    } catch (AmplifyException amplifyException) {
        throw new DataStoreException("Failed to get fields for model.", amplifyException, "Validate your model file.");
    }
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) AmplifyException(com.amplifyframework.AmplifyException) HashMap(java.util.HashMap)

Example 27 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class AppSyncRequestFactory method parsePredicate.

static Map<String, Object> parsePredicate(QueryPredicate queryPredicate) throws DataStoreException {
    if (QueryPredicates.all().equals(queryPredicate)) {
        return Collections.singletonMap("id", Collections.singletonMap("ne", null));
    }
    if (QueryPredicates.none().equals(queryPredicate)) {
        // id cannot be null, so match none
        return Collections.singletonMap("id", Collections.singletonMap("eq", null));
    }
    if (queryPredicate instanceof QueryPredicateOperation) {
        QueryPredicateOperation<?> qpo = (QueryPredicateOperation<?>) queryPredicate;
        QueryOperator<?> op = qpo.operator();
        return Collections.singletonMap(qpo.field(), Collections.singletonMap(appSyncOpType(op.type()), appSyncOpValue(op)));
    } else if (queryPredicate instanceof QueryPredicateGroup) {
        QueryPredicateGroup qpg = (QueryPredicateGroup) queryPredicate;
        if (QueryPredicateGroup.Type.NOT.equals(qpg.type())) {
            try {
                return Collections.singletonMap("not", parsePredicate(qpg.predicates().get(0)));
            } catch (IndexOutOfBoundsException exception) {
                throw new DataStoreException("Predicate group of type NOT must include a value to negate.", exception, "Check if you created a NOT condition in your Predicate with no included value.");
            }
        } else {
            List<Map<String, Object>> predicates = new ArrayList<>();
            for (QueryPredicate predicate : qpg.predicates()) {
                predicates.add(parsePredicate(predicate));
            }
            return Collections.singletonMap(qpg.type().toString().toLowerCase(Locale.getDefault()), predicates);
        }
    } else {
        throw new DataStoreException("Tried to parse an unsupported QueryPredicate", "Try changing to one of the supported values: QueryPredicateOperation, QueryPredicateGroup.");
    }
}
Also used : QueryPredicateOperation(com.amplifyframework.core.model.query.predicate.QueryPredicateOperation) DataStoreException(com.amplifyframework.datastore.DataStoreException) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) QueryPredicateGroup(com.amplifyframework.core.model.query.predicate.QueryPredicateGroup) ArrayList(java.util.ArrayList) List(java.util.List)

Example 28 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class AppSyncRequestFactory method buildSyncRequest.

/**
 * Builds the query document for base and delta sync.
 * If you provide lastSyncTime, it builds a delta sync, where the delta is computed
 * against the provided time. Otherwise, if you provide a null lastSyncTime, a
 * request doc is generated for a base sync.
 * @param modelSchema Schema Class for which we want to sync.
 * @param lastSync The last time synced. If not provided, do a base query.
 *                 If provided, do a delta query.
 * @param <T> The type of objects we are syncing
 * @return A string which contains a GraphQL query doc for an base/delta sync
 * @throws DataStoreException On Failure to inspect
 */
@NonNull
static <T> AppSyncGraphQLRequest<T> buildSyncRequest(@NonNull final ModelSchema modelSchema, @Nullable final Long lastSync, @Nullable final Integer limit, @NonNull final QueryPredicate predicate, @NonNull final AuthModeStrategyType strategyType) throws DataStoreException {
    try {
        AppSyncGraphQLRequest.Builder builder = AppSyncGraphQLRequest.builder().modelClass(modelSchema.getModelClass()).modelSchema(modelSchema).operation(QueryType.SYNC).requestAuthorizationStrategyType(strategyType).requestOptions(new DataStoreGraphQLRequestOptions()).responseType(TypeMaker.getParameterizedType(PaginatedResult.class, ModelWithMetadata.class, modelSchema.getModelClass()));
        if (lastSync != null) {
            builder.variable("lastSync", "AWSTimestamp", lastSync);
        }
        if (limit != null) {
            builder.variable("limit", "Int", limit);
        }
        if (!QueryPredicates.all().equals(predicate)) {
            String filterType = "Model" + Casing.capitalizeFirst(modelSchema.getName()) + "FilterInput";
            QueryPredicate syncPredicate = predicate;
            if (!(syncPredicate instanceof QueryPredicateGroup)) {
                // When a filter is provided, wrap it with a predicate group of type AND.  By doing this, it enables
                // AppSync to optimize the request by performing a DynamoDB query instead of a scan.  If the
                // provided syncPredicate is already a QueryPredicateGroup, this is not needed.  If the provided
                // group is of type AND, the optimization will occur.  If the top level group is OR or NOT, the
                // optimization is not possible anyway.
                syncPredicate = QueryPredicateGroup.andOf(syncPredicate);
            }
            builder.variable("filter", filterType, parsePredicate(syncPredicate));
        }
        return builder.build();
    } catch (AmplifyException amplifyException) {
        throw new DataStoreException("Failed to get fields for model.", amplifyException, "Validate your model file.");
    }
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) AmplifyException(com.amplifyframework.AmplifyException) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) AppSyncGraphQLRequest(com.amplifyframework.api.aws.AppSyncGraphQLRequest) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicateGroup(com.amplifyframework.core.model.query.predicate.QueryPredicateGroup) NonNull(androidx.annotation.NonNull)

Example 29 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class AppSyncRequestFactory method buildDeletionRequest.

static <M extends Model> AppSyncGraphQLRequest<ModelWithMetadata<M>> buildDeletionRequest(ModelSchema schema, M model, Integer version, QueryPredicate predicate, AuthModeStrategyType strategyType) throws DataStoreException {
    try {
        Map<String, Object> inputMap = new HashMap<>();
        inputMap.put("_version", version);
        inputMap.putAll(getDeleteMutationInputMap(schema, model));
        return buildMutation(schema, inputMap, predicate, MutationType.DELETE, strategyType);
    } catch (AmplifyException amplifyException) {
        throw new DataStoreException("Failed to get fields for model.", amplifyException, "Validate your model file.");
    }
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) AmplifyException(com.amplifyframework.AmplifyException) HashMap(java.util.HashMap)

Example 30 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class Orchestrator method startObservingStorageChanges.

/**
 * Start observing the local storage adapter for changes;
 * enqueue them into the mutation outbox.
 */
private void startObservingStorageChanges() throws DataStoreException {
    LOG.info("Starting to observe local storage changes.");
    try {
        mutationOutbox.load().andThen(Completable.create(emitter -> {
            storageObserver.startObservingStorageChanges(emitter::onComplete);
            LOG.info("Setting currentState to LOCAL_ONLY");
            currentState.set(State.LOCAL_ONLY);
        })).blockingAwait();
    } catch (Throwable throwable) {
        throw new DataStoreException("Timed out while starting to observe storage changes.", throwable, AmplifyException.REPORT_BUG_TO_AWS_SUGGESTION);
    }
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException)

Aggregations

DataStoreException (com.amplifyframework.datastore.DataStoreException)89 Test (org.junit.Test)52 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)36 Consumer (com.amplifyframework.core.Consumer)32 List (java.util.List)32 Cancelable (com.amplifyframework.core.async.Cancelable)31 Model (com.amplifyframework.core.model.Model)31 ArrayList (java.util.ArrayList)31 AmplifyException (com.amplifyframework.AmplifyException)29 ModelSchema (com.amplifyframework.core.model.ModelSchema)28 Collections (java.util.Collections)28 Action (com.amplifyframework.core.Action)27 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)27 TimeUnit (java.util.concurrent.TimeUnit)25 Post (com.amplifyframework.testmodels.commentsblog.Post)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)23 Arrays (java.util.Arrays)23 Assert.assertEquals (org.junit.Assert.assertEquals)23 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)22 DataStoreQuerySnapshot (com.amplifyframework.datastore.DataStoreQuerySnapshot)21