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);
}
});
}
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);
}
});
}
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);
}
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);
}
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());
}
Aggregations