Search in sources :

Example 1 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultStorIOContentResolverTest method shouldUseSpecifiedTypeMappingFinder.

@Test
public void shouldUseSpecifiedTypeMappingFinder() throws NoSuchFieldException, IllegalAccessException {
    TypeMappingFinder typeMappingFinder = mock(TypeMappingFinder.class);
    DefaultStorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(mock(ContentResolver.class)).typeMappingFinder(typeMappingFinder).build();
    assertThat(getTypeMappingFinder(storIOContentResolver)).isEqualTo(typeMappingFinder);
}
Also used : TypeMappingFinder(com.pushtorefresh.storio.TypeMappingFinder) ContentResolver(android.content.ContentResolver) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 2 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultStorIOContentResolverTest method shouldReturnSameContentResolver.

@Test
public void shouldReturnSameContentResolver() {
    ContentResolver contentResolver = mock(ContentResolver.class);
    StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(contentResolver).build();
    assertThat(storIOContentResolver.lowLevel().contentResolver()).isSameAs(contentResolver);
}
Also used : StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) ContentResolver(android.content.ContentResolver) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 3 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultPutResolver method performPut.

/**
     * {@inheritDoc}
     */
@NonNull
@Override
public PutResult performPut(@NonNull StorIOContentResolver storIOContentResolver, @NonNull T object) {
    final UpdateQuery updateQuery = mapToUpdateQuery(object);
    final Query query = Query.builder().uri(updateQuery.uri()).where(nullableString(updateQuery.where())).whereArgs((Object[]) nullableArrayOfStringsFromListOfStrings(updateQuery.whereArgs())).build();
    final StorIOContentResolver.LowLevel lowLevel = storIOContentResolver.lowLevel();
    final Cursor cursor = lowLevel.query(query);
    try {
        final ContentValues contentValues = mapToContentValues(object);
        if (cursor.getCount() == 0) {
            final InsertQuery insertQuery = mapToInsertQuery(object);
            final Uri insertedUri = lowLevel.insert(insertQuery, contentValues);
            return PutResult.newInsertResult(insertedUri, insertQuery.uri());
        } else {
            final int numberOfRowsUpdated = lowLevel.update(updateQuery, contentValues);
            return PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.uri());
        }
    } finally {
        cursor.close();
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio.contentresolver.queries.Query) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Uri(android.net.Uri) NonNull(android.support.annotation.NonNull)

Example 4 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class PreparedPutCollectionOfObjects method executeAsBlocking.

/**
     * Executes Put Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null result of Put Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public PutResults<T> executeAsBlocking() {
    try {
        final StorIOContentResolver.LowLevel lowLevel = storIOContentResolver.lowLevel();
        // Nullable
        final List<SimpleImmutableEntry<T, PutResolver<T>>> objectsAndPutResolvers;
        if (explicitPutResolver != null) {
            objectsAndPutResolvers = null;
        } else {
            objectsAndPutResolvers = new ArrayList<SimpleImmutableEntry<T, PutResolver<T>>>(objects.size());
            for (final T object : objects) {
                final ContentResolverTypeMapping<T> typeMapping = (ContentResolverTypeMapping<T>) lowLevel.typeMapping(object.getClass());
                if (typeMapping == null) {
                    throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + "object = " + object + ", object.class = " + object.getClass() + "," + "ContentProvider was not affected by this operation, please add type mapping for this type");
                }
                objectsAndPutResolvers.add(new SimpleImmutableEntry<T, PutResolver<T>>(object, typeMapping.putResolver()));
            }
        }
        final Map<T, PutResult> results = new HashMap<T, PutResult>(objects.size());
        if (explicitPutResolver != null) {
            for (final T object : objects) {
                final PutResult putResult = explicitPutResolver.performPut(storIOContentResolver, object);
                results.put(object, putResult);
            }
        } else {
            for (final SimpleImmutableEntry<T, PutResolver<T>> objectAndPutResolver : objectsAndPutResolvers) {
                final T object = objectAndPutResolver.getKey();
                final PutResolver<T> putResolver = objectAndPutResolver.getValue();
                final PutResult putResult = putResolver.performPut(storIOContentResolver, object);
                results.put(object, putResult);
            }
        }
        return PutResults.newInstance(results);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put operation. objects = " + objects, exception);
    }
}
Also used : HashMap(java.util.HashMap) ContentResolverTypeMapping(com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping) StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 5 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class PreparedDeleteCollectionOfObjects method executeAsBlocking.

/**
     * Executes Delete Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null results of Delete Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public DeleteResults<T> executeAsBlocking() {
    try {
        final StorIOContentResolver.LowLevel lowLevel = storIOContentResolver.lowLevel();
        // Nullable
        final List<SimpleImmutableEntry> objectsAndDeleteResolvers;
        if (explicitDeleteResolver != null) {
            objectsAndDeleteResolvers = null;
        } else {
            objectsAndDeleteResolvers = new ArrayList<SimpleImmutableEntry>(objects.size());
            for (final T object : objects) {
                final ContentResolverTypeMapping<T> typeMapping = (ContentResolverTypeMapping<T>) lowLevel.typeMapping(object.getClass());
                if (typeMapping == null) {
                    throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + "object = " + object + ", object.class = " + object.getClass() + "," + "ContentProvider was not affected by this operation, please add type mapping for this type");
                }
                objectsAndDeleteResolvers.add(new SimpleImmutableEntry(object, typeMapping.deleteResolver()));
            }
        }
        final Map<T, DeleteResult> results = new HashMap<T, DeleteResult>(objects.size());
        if (explicitDeleteResolver != null) {
            for (final T object : objects) {
                final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOContentResolver, object);
                results.put(object, deleteResult);
            }
        } else {
            for (final SimpleImmutableEntry<T, DeleteResolver<T>> objectAndDeleteResolver : objectsAndDeleteResolvers) {
                final T object = objectAndDeleteResolver.getKey();
                final DeleteResolver<T> deleteResolver = objectAndDeleteResolver.getValue();
                final DeleteResult deleteResult = deleteResolver.performDelete(storIOContentResolver, object);
                results.put(object, deleteResult);
            }
        }
        return DeleteResults.newInstance(results);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Delete operation. objects = " + objects, exception);
    }
}
Also used : HashMap(java.util.HashMap) ContentResolverTypeMapping(com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping) StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Aggregations

StorIOContentResolver (com.pushtorefresh.storio.contentresolver.StorIOContentResolver)23 Test (org.junit.Test)20 Query (com.pushtorefresh.storio.contentresolver.queries.Query)11 NonNull (android.support.annotation.NonNull)9 ContentResolver (android.content.ContentResolver)8 Cursor (android.database.Cursor)6 Uri (android.net.Uri)6 ContentValues (android.content.ContentValues)5 StorIOException (com.pushtorefresh.storio.StorIOException)5 InsertQuery (com.pushtorefresh.storio.contentresolver.queries.InsertQuery)5 UpdateQuery (com.pushtorefresh.storio.contentresolver.queries.UpdateQuery)5 TypeMappingFinder (com.pushtorefresh.storio.TypeMappingFinder)4 GetResolver (com.pushtorefresh.storio.contentresolver.operations.get.GetResolver)3 WorkerThread (android.support.annotation.WorkerThread)2 ContentResolverTypeMapping (com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping)2 TypeMappingFinderImpl (com.pushtorefresh.storio.internal.TypeMappingFinderImpl)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 HashMap (java.util.HashMap)2 TestSubscriber (rx.observers.TestSubscriber)2 Handler (android.os.Handler)1