Search in sources :

Example 1 with CarPersonRelationPutResolver

use of com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarPersonRelationPutResolver in project storio by pushtorefresh.

the class CarRelationsPutResolver method performPut.

@Override
@NonNull
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull Car object) {
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    try {
        final PutResult putResult = super.performPut(storIOSQLite, object);
        // noinspection ConstantConditions
        final long carId = putResult.wasInserted() ? putResult.insertedId() : object.id();
        storIOSQLite.delete().byQuery(DeleteQuery.builder().table(PersonCarRelationTable.TABLE).where(COLUMN_CAR_ID + " = ?").whereArgs(carId).build()).prepare().executeAsBlocking();
        final List<Person> persons = object.persons();
        if (persons != null) {
            storIOSQLite.put().objects(persons).withPutResolver(personStorIOSQLitePutResolver).prepare().executeAsBlocking();
            final List<ContentValues> contentValuesList = new ArrayList<ContentValues>(persons.size());
            for (Person person : persons) {
                final ContentValues cv = new ContentValues(2);
                cv.put(COLUMN_CAR_ID, carId);
                cv.put(COLUMN_PERSON_ID, person.id());
                contentValuesList.add(cv);
            }
            storIOSQLite.put().contentValues(contentValuesList).withPutResolver(carPersonRelationPutResolver).prepare().executeAsBlocking();
        }
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        lowLevel.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) ArrayList(java.util.ArrayList) PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) Person(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 2 with CarPersonRelationPutResolver

use of com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarPersonRelationPutResolver in project storio by pushtorefresh.

the class PersonRelationsPutResolver method performPut.

@Override
@NonNull
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull Person object) {
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    try {
        final PutResult putResult = super.performPut(storIOSQLite, object);
        // noinspection ConstantConditions
        final long personId = putResult.wasInserted() ? putResult.insertedId() : object.id();
        storIOSQLite.delete().byQuery(DeleteQuery.builder().table(PersonCarRelationTable.TABLE).where(COLUMN_PERSON_ID + " = ?").whereArgs(personId).build()).prepare().executeAsBlocking();
        final List<Car> cars = object.cars();
        if (cars != null) {
            final PutResults<Car> carsPutResults = storIOSQLite.put().objects(cars).withPutResolver(carStorIOSQLitePutResolver).prepare().executeAsBlocking();
            final List<ContentValues> contentValuesList = new ArrayList<ContentValues>(cars.size());
            for (Car car : cars) {
                final PutResult carPutResult = carsPutResults.results().get(car);
                // noinspection ConstantConditions
                final long carId = carPutResult.wasInserted() ? carPutResult.insertedId() : car.id();
                final ContentValues cv = new ContentValues(2);
                cv.put(COLUMN_PERSON_ID, personId);
                cv.put(PersonCarRelationTable.COLUMN_CAR_ID, carId);
                contentValuesList.add(cv);
            }
            storIOSQLite.put().contentValues(contentValuesList).withPutResolver(carPersonRelationPutResolver).prepare().executeAsBlocking();
        }
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        lowLevel.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) Car(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car) ArrayList(java.util.ArrayList) PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 3 with CarPersonRelationPutResolver

use of com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarPersonRelationPutResolver in project storio by pushtorefresh.

the class DbModule method provideStorIOSQLite.

// We suggest to keep one instance of StorIO (SQLite or ContentResolver)
// It's thread safe and so on, so just share it.
// But if you need you can have multiple instances of StorIO
// (SQLite or ContentResolver) with different settings such as type mapping, logging and so on.
// But keep in mind that different instances of StorIOSQLite won't share notifications!
@Provides
@NonNull
@Singleton
public StorIOSQLite provideStorIOSQLite(@NonNull SQLiteOpenHelper sqLiteOpenHelper) {
    final CarStorIOSQLitePutResolver carStorIOSQLitePutResolver = new CarStorIOSQLitePutResolver();
    final CarStorIOSQLiteGetResolver carStorIOSQLiteGetResolver = new CarStorIOSQLiteGetResolver();
    final PersonStorIOSQLitePutResolver personStorIOSQLitePutResolver = new PersonStorIOSQLitePutResolver();
    final PersonStorIOSQLiteGetResolver personStorIOSQLiteGetResolver = new PersonStorIOSQLiteGetResolver();
    final CarPersonRelationPutResolver carPersonRelationPutResolver = new CarPersonRelationPutResolver();
    return DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).addTypeMapping(Tweet.class, new TweetSQLiteTypeMapping()).addTypeMapping(User.class, new UserSQLiteTypeMapping()).addTypeMapping(TweetWithUser.class, SQLiteTypeMapping.<TweetWithUser>builder().putResolver(new TweetWithUserPutResolver()).getResolver(new TweetWithUserGetResolver()).deleteResolver(new TweetWithUserDeleteResolver()).build()).addTypeMapping(Person.class, SQLiteTypeMapping.<Person>builder().putResolver(new PersonRelationsPutResolver(carStorIOSQLitePutResolver, carPersonRelationPutResolver)).getResolver(new PersonRelationsGetResolver(carStorIOSQLiteGetResolver)).deleteResolver(new PersonRelationsDeleteResolver()).build()).addTypeMapping(Car.class, SQLiteTypeMapping.<Car>builder().putResolver(new CarRelationsPutResolver(personStorIOSQLitePutResolver, carPersonRelationPutResolver)).getResolver(new CarRelationsGetResolver(personStorIOSQLiteGetResolver)).deleteResolver(new CarRelationsDeleteResolver()).build()).build();
}
Also used : TweetWithUserDeleteResolver(com.pushtorefresh.storio3.sample.db.resolvers.TweetWithUserDeleteResolver) PersonStorIOSQLitePutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.PersonStorIOSQLitePutResolver) CarPersonRelationPutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarPersonRelationPutResolver) Tweet(com.pushtorefresh.storio3.sample.db.entities.Tweet) TweetSQLiteTypeMapping(com.pushtorefresh.storio3.sample.db.entities.TweetSQLiteTypeMapping) CarStorIOSQLitePutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.CarStorIOSQLitePutResolver) CarRelationsDeleteResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarRelationsDeleteResolver) CarStorIOSQLiteGetResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.CarStorIOSQLiteGetResolver) TweetWithUser(com.pushtorefresh.storio3.sample.db.entities.TweetWithUser) TweetWithUserGetResolver(com.pushtorefresh.storio3.sample.db.resolvers.TweetWithUserGetResolver) PersonRelationsPutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.PersonRelationsPutResolver) Car(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car) PersonStorIOSQLiteGetResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.PersonStorIOSQLiteGetResolver) PersonRelationsDeleteResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.PersonRelationsDeleteResolver) CarRelationsPutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarRelationsPutResolver) CarRelationsGetResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarRelationsGetResolver) TweetWithUserPutResolver(com.pushtorefresh.storio3.sample.db.resolvers.TweetWithUserPutResolver) PersonRelationsGetResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.PersonRelationsGetResolver) UserSQLiteTypeMapping(com.pushtorefresh.storio3.sample.db.entities.UserSQLiteTypeMapping) Singleton(javax.inject.Singleton) NonNull(android.support.annotation.NonNull) Provides(dagger.Provides)

Aggregations

NonNull (android.support.annotation.NonNull)3 ContentValues (android.content.ContentValues)2 Car (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car)2 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)2 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)2 ArrayList (java.util.ArrayList)2 Tweet (com.pushtorefresh.storio3.sample.db.entities.Tweet)1 TweetSQLiteTypeMapping (com.pushtorefresh.storio3.sample.db.entities.TweetSQLiteTypeMapping)1 TweetWithUser (com.pushtorefresh.storio3.sample.db.entities.TweetWithUser)1 UserSQLiteTypeMapping (com.pushtorefresh.storio3.sample.db.entities.UserSQLiteTypeMapping)1 TweetWithUserDeleteResolver (com.pushtorefresh.storio3.sample.db.resolvers.TweetWithUserDeleteResolver)1 TweetWithUserGetResolver (com.pushtorefresh.storio3.sample.db.resolvers.TweetWithUserGetResolver)1 TweetWithUserPutResolver (com.pushtorefresh.storio3.sample.db.resolvers.TweetWithUserPutResolver)1 CarStorIOSQLiteGetResolver (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.CarStorIOSQLiteGetResolver)1 CarStorIOSQLitePutResolver (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.CarStorIOSQLitePutResolver)1 Person (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person)1 PersonStorIOSQLiteGetResolver (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.PersonStorIOSQLiteGetResolver)1 PersonStorIOSQLitePutResolver (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.PersonStorIOSQLitePutResolver)1 CarPersonRelationPutResolver (com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarPersonRelationPutResolver)1 CarRelationsDeleteResolver (com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarRelationsDeleteResolver)1