Search in sources :

Example 16 with DataStoreException

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

the class SQLiteStorageAdapter method save.

/**
 * {@inheritDoc}
 */
@Override
public <T extends Model> void save(@NonNull T item, @NonNull StorageItemChange.Initiator initiator, @NonNull QueryPredicate predicate, @NonNull Consumer<StorageItemChange<T>> onSuccess, @NonNull Consumer<DataStoreException> onError) {
    Objects.requireNonNull(item);
    Objects.requireNonNull(initiator);
    Objects.requireNonNull(predicate);
    Objects.requireNonNull(onSuccess);
    Objects.requireNonNull(onError);
    threadPool.submit(() -> {
        try {
            final ModelSchema modelSchema = schemaRegistry.getModelSchemaForModelClass(item.getModelName());
            final StorageItemChange.Type writeType;
            SerializedModel patchItem = null;
            if (sqlQueryProcessor.modelExists(item, QueryPredicates.all())) {
                // if data exists already, then UPDATE the row
                writeType = StorageItemChange.Type.UPDATE;
                // Check if existing data meets the condition, only if a condition other than all() was provided.
                if (!QueryPredicates.all().equals(predicate) && !sqlQueryProcessor.modelExists(item, predicate)) {
                    throw new DataStoreException("Save failed because condition did not match existing model instance.", "The save will continue to fail until the model instance is updated.");
                }
                if (initiator == StorageItemChange.Initiator.DATA_STORE_API) {
                    // When saving items via the DataStore API, compute a SerializedModel of the changed model.
                    // This is not necessary when save
                    // is initiated by the sync engine, so skip it for optimization to avoid the extra SQL query.
                    patchItem = SerializedModel.create(item, modelSchema);
                }
            } else if (!QueryPredicates.all().equals(predicate)) {
                // insert not permitted with a condition
                throw new DataStoreException("Conditional update must be performed against an already existing data. " + "Insertion is not permitted while using a predicate.", "Please save without specifying a predicate.");
            } else {
                // if data doesn't exist yet, then INSERT a new row
                writeType = StorageItemChange.Type.CREATE;
            }
            // execute local save
            writeData(item, writeType);
            // publish successful save
            StorageItemChange<T> change = StorageItemChange.<T>builder().item(item).patchItem(patchItem != null ? patchItem : SerializedModel.create(item, modelSchema)).modelSchema(modelSchema).type(writeType).predicate(predicate).initiator(initiator).build();
            itemChangeSubject.onNext(change);
            onSuccess.accept(change);
        } catch (DataStoreException dataStoreException) {
            onError.accept(dataStoreException);
        } catch (Exception someOtherTypeOfException) {
            String modelToString = item.getModelName() + "[id=" + item.getId() + "]";
            DataStoreException dataStoreException = new DataStoreException("Error in saving the model: " + modelToString, someOtherTypeOfException, "See attached exception for details.");
            onError.accept(dataStoreException);
        }
    });
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) DataStoreException(com.amplifyframework.datastore.DataStoreException) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) SerializedModel(com.amplifyframework.core.model.SerializedModel) AmplifyException(com.amplifyframework.AmplifyException) DataStoreException(com.amplifyframework.datastore.DataStoreException)

Example 17 with DataStoreException

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

the class SQLiteStorageAdapter method delete.

/**
 * {@inheritDoc}
 */
@Override
public <T extends Model> void delete(@NonNull Class<T> itemClass, @NonNull StorageItemChange.Initiator initiator, @NonNull QueryPredicate predicate, @NonNull Action onSuccess, @NonNull Consumer<DataStoreException> onError) {
    Objects.requireNonNull(itemClass);
    Objects.requireNonNull(initiator);
    Objects.requireNonNull(predicate);
    Objects.requireNonNull(onSuccess);
    Objects.requireNonNull(onError);
    threadPool.submit(() -> {
        final ModelSchema modelSchema = schemaRegistry.getModelSchemaForModelClass(itemClass);
        QueryOptions options = Where.matches(predicate);
        try (Cursor cursor = sqlCommandProcessor.rawQuery(sqlCommandFactory.queryFor(modelSchema, options))) {
            final SQLiteTable sqliteTable = SQLiteTable.fromSchema(modelSchema);
            final String primaryKeyName = sqliteTable.getPrimaryKey().getAliasedName();
            // identify items that meet the predicate
            List<T> items = new ArrayList<>();
            if (cursor != null && cursor.moveToFirst()) {
                int index = cursor.getColumnIndexOrThrow(primaryKeyName);
                do {
                    String id = cursor.getString(index);
                    String dummyJson = gson.toJson(Collections.singletonMap("id", id));
                    T dummyItem = gson.fromJson(dummyJson, itemClass);
                    items.add(dummyItem);
                } while (cursor.moveToNext());
            }
            // identify every model to delete as a result of this operation
            List<Model> modelsToDelete = new ArrayList<>(items);
            List<Model> cascadedModels = sqliteModelTree.descendantsOf(items);
            modelsToDelete.addAll(cascadedModels);
            // execute local deletions
            sqlCommandProcessor.execute(sqlCommandFactory.deleteFor(modelSchema, predicate));
            // publish every deletion
            for (Model model : modelsToDelete) {
                ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(model.getModelName());
                itemChangeSubject.onNext(StorageItemChange.builder().item(model).patchItem(SerializedModel.create(model, schema)).modelSchema(schema).type(StorageItemChange.Type.DELETE).predicate(QueryPredicates.all()).initiator(initiator).build());
            }
            onSuccess.call();
        } catch (DataStoreException dataStoreException) {
            onError.accept(dataStoreException);
        } catch (Exception someOtherTypeOfException) {
            DataStoreException dataStoreException = new DataStoreException("Error in deleting models.", someOtherTypeOfException, "See attached exception for details.");
            onError.accept(dataStoreException);
        }
    });
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) ArrayList(java.util.ArrayList) SQLiteTable(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable) Cursor(android.database.Cursor) QueryOptions(com.amplifyframework.core.model.query.QueryOptions) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) AmplifyException(com.amplifyframework.AmplifyException) DataStoreException(com.amplifyframework.datastore.DataStoreException) ModelSchema(com.amplifyframework.core.model.ModelSchema) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model)

Example 18 with DataStoreException

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

the class SQLiteStorageAdapter method clear.

/**
 * {@inheritDoc}
 */
@Override
public synchronized void clear(@NonNull Action onComplete, @NonNull Consumer<DataStoreException> onError) {
    try {
        LOG.debug("Shutting down thread pool for the storage adapter.");
        threadPool.shutdown();
        if (!threadPool.awaitTermination(THREAD_POOL_TERMINATE_TIMEOUT, TimeUnit.MILLISECONDS)) {
            threadPool.shutdownNow();
        }
        LOG.debug("Storage adapter thread pool shutdown.");
    } catch (InterruptedException exception) {
        LOG.warn("Storage adapter thread pool was interrupted during shutdown.", exception);
    }
    sqliteStorageHelper.close();
    databaseConnectionHandle.close();
    LOG.debug("Clearing DataStore.");
    if (!context.deleteDatabase(databaseName)) {
        DataStoreException dataStoreException = new DataStoreException("Error while trying to clear data from the local DataStore storage.", "See attached exception for details.");
        onError.accept(dataStoreException);
    }
    LOG.debug("DataStore cleared. Re-initializing storage adapter.");
    // Re-initialize the adapter.
    initialize(context, schemaList -> onComplete.call(), exception -> onError.accept(new DataStoreException("Error occurred while trying to re-initialize the storage adapter", String.valueOf(exception.getMessage()))), dataStoreConfiguration);
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException)

Example 19 with DataStoreException

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

the class AppSyncMockingTest method mockFailureForSync.

/**
 * When mockFailure() is called on the SyncConfigurator, the AppSync mock
 * will emit the provided failure.
 * @throws DataStoreException On failure to get a SyncConfigurator via sync()
 */
@Test
public void mockFailureForSync() throws DataStoreException {
    DataStoreException failure = new DataStoreException("Foo", "Bar");
    AppSyncMocking.sync(appSync).mockFailure(failure);
    GraphQLRequest<PaginatedResult<ModelWithMetadata<BlogOwner>>> request = appSync.buildSyncRequest(schema, null, 100, QueryPredicates.all());
    Single.create(emitter -> appSync.sync(request, emitter::onSuccess, emitter::onError)).test().awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS).assertError(failure);
}
Also used : Arrays(java.util.Arrays) Single(io.reactivex.rxjava3.core.Single) SyncConfigurator(com.amplifyframework.datastore.appsync.AppSyncMocking.SyncConfigurator) AmplifyException(com.amplifyframework.AmplifyException) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) Observable(io.reactivex.rxjava3.core.Observable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Temporal(com.amplifyframework.core.model.temporal.Temporal) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) NoOpAction(com.amplifyframework.core.NoOpAction) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) Collections(java.util.Collections) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) DataStoreException(com.amplifyframework.datastore.DataStoreException) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 20 with DataStoreException

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

the class ModelProviderLocatorTest method locateThrowingModelProvider.

/**
 * When the ModelProviderLocator attempts to locate a ModelProvider whose getInstance() method
 * throws an exception, the locator should bubble up that exception as DataStoreException.
 */
@Test
public void locateThrowingModelProvider() {
    String className = Objects.requireNonNull(ThrowingModelProvider.class.getName());
    DataStoreException actualException = assertThrows(DataStoreException.class, () -> ModelProviderLocator.locate(className));
    assertEquals("An exception was thrown from " + ThrowingModelProvider.class.getName() + "getInstance" + " while invoking via reflection.", actualException.getMessage());
    assertEquals("This is not expected to occur. Contact AWS.", actualException.getRecoverySuggestion());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) RandomString(com.amplifyframework.testutils.random.RandomString) Test(org.junit.Test)

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