diff options
Diffstat (limited to 'app/src')
6 files changed, 108 insertions, 18 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DataBaseAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DataBaseAdapter.java index 1e34e2b1b..b30309e19 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DataBaseAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DataBaseAdapter.java @@ -1,5 +1,6 @@ package it.niedermann.nextcloud.deck.persistence.sync.adapters.db; +import android.arch.persistence.room.Room; import android.content.Context; import org.greenrobot.greendao.query.QueryBuilder; @@ -30,17 +31,18 @@ import it.niedermann.nextcloud.deck.persistence.DeckDaoSession; public class DataBaseAdapter implements IDatabaseOnlyAdapter { + private interface DataAccessor <T> { T getData(); } - private DaoSession db; + private DeckDatabase db; + private Context applicationContext; public DataBaseAdapter(Context applicationContext) { this.applicationContext = applicationContext; - this.db = DeckDaoSession.getInstance(applicationContext).session(); - QueryBuilder.LOG_SQL = true; //FIXME: remove this. + this.db = DeckDatabase.getInstance(applicationContext); } private <T> void respond(IResponseCallback<T> responseCallback, DataAccessor<T> r){ @@ -49,13 +51,12 @@ public class DataBaseAdapter implements IDatabaseOnlyAdapter { @Override public boolean hasAccounts() { - return db.getAccountDao().count()>0; + return db.getAccountDao().countAccounts()>0; } @Override public Board getBoard(long accountId, long remoteId) { - QueryBuilder<Board> qb = db.getBoardDao().queryBuilder(); - return qb.where(BoardDao.Properties.AccountId.eq(accountId), BoardDao.Properties.Id.eq(remoteId)).unique(); + return db.getBoardDao().getBoardByRemoteId(accountId, remoteId); } @Override @@ -72,8 +73,7 @@ public class DataBaseAdapter implements IDatabaseOnlyAdapter { @Override public User getUser(long accountId, long remoteId) { - QueryBuilder<User> qb = db.getUserDao().queryBuilder(); - return qb.where(UserDao.Properties.AccountId.eq(accountId), UserDao.Properties.Id.eq(remoteId)).unique(); + return db.getUserDao().getUsersByRemoteId(accountId, remoteId); } @Override @@ -160,7 +160,7 @@ public class DataBaseAdapter implements IDatabaseOnlyAdapter { @Override public void updateAccount(Account account) { - db.update(account); + db.getAccountDao().update(account); } @Override diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java index 5a18c1c95..92fe723a7 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java @@ -1,7 +1,9 @@ package it.niedermann.nextcloud.deck.persistence.sync.adapters.db; import android.arch.persistence.room.Database; +import android.arch.persistence.room.Room; import android.arch.persistence.room.RoomDatabase; +import android.content.Context; import it.niedermann.nextcloud.deck.model.Account; import it.niedermann.nextcloud.deck.model.Board; @@ -16,6 +18,8 @@ import it.niedermann.nextcloud.deck.model.Label; import it.niedermann.nextcloud.deck.model.Permission; import it.niedermann.nextcloud.deck.model.Stack; import it.niedermann.nextcloud.deck.model.User; +import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.AccountDao; +import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.BoardDao; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.UserDao; @Database( @@ -39,10 +43,28 @@ import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.UserDao; ) public abstract class DeckDatabase extends RoomDatabase { -// public abstract AccountDao getAccountDao(); -// -// public abstract BoardDao getBoardDao(); -// + + private static final String DECK_DB_NAME = "NC_DECK_DB.db"; + private static volatile DeckDatabase instance; + + static synchronized DeckDatabase getInstance(Context context) { + if (instance == null) { + instance = create(context); + } + return instance; + } + + private static DeckDatabase create(final Context context) { + return Room.databaseBuilder( + context, + DeckDatabase.class, + DECK_DB_NAME).build(); + } + + public abstract AccountDao getAccountDao(); + + public abstract BoardDao getBoardDao(); + // public abstract CardDao getCardDao(); // // public abstract JoinBoardWithLabelDao getJoinBoardWithLabelDao(); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/AccountDao.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/AccountDao.java new file mode 100644 index 000000000..41cf2d74b --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/AccountDao.java @@ -0,0 +1,23 @@ +package it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao; + +import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Delete; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.Query; +import android.arch.persistence.room.Update; + +import java.util.Set; + +import it.niedermann.nextcloud.deck.model.Account; +import it.niedermann.nextcloud.deck.model.User; + +@Dao +public interface AccountDao extends GenericDao<Account> { + + @Query("SELECT * FROM account") + Set<Account> getAccounts(); + + @Query("SELECT count(*) FROM account") + int countAccounts(); + +}
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/BoardDao.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/BoardDao.java new file mode 100644 index 000000000..155a51b00 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/BoardDao.java @@ -0,0 +1,24 @@ +package it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao; + +import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Delete; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.Query; +import android.arch.persistence.room.Update; + +import java.util.List; +import java.util.Set; + +import it.niedermann.nextcloud.deck.model.Board; +import it.niedermann.nextcloud.deck.model.User; + +@Dao +public interface BoardDao extends GenericDao<Board> { + + @Query("SELECT * FROM board WHERE accountId = :accountId") + Set<Board> getBoardsForAccount(final long accountId); + + @Query("SELECT * FROM board WHERE accountId = :accountId and id = :remoteId") + Board getBoardByRemoteId(final long accountId, final long remoteId); + +}
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/GenericDao.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/GenericDao.java new file mode 100644 index 000000000..298ac51a3 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/GenericDao.java @@ -0,0 +1,17 @@ +package it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao; + +import android.arch.persistence.room.Delete; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.Update; + +public interface GenericDao <T>{ + + @Insert + long insert(T entity); + @Insert + long [] insert(T... entity); + @Update + void update(T... entity); + @Delete + void delete(T... entity); +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserDao.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserDao.java index a12fa665a..5e8d38d38 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserDao.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserDao.java @@ -1,19 +1,23 @@ package it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao; import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Delete; import android.arch.persistence.room.Insert; import android.arch.persistence.room.Query; +import android.arch.persistence.room.Update; import java.util.List; +import java.util.Set; import it.niedermann.nextcloud.deck.model.User; @Dao -public interface UserDao { - @Insert - void insert(User user); +public interface UserDao extends GenericDao<User> { - @Query("SELECT * FROM user WHERE user.accountId = :accountId") - List<User> getUsersForAccount(final long accountId); + @Query("SELECT * FROM user WHERE accountId = :accountId") + Set<User> getUsersForAccount(final long accountId); + + @Query("SELECT * FROM user WHERE accountId = :accountId and id = :remoteId") + User getUsersByRemoteId(final long accountId, final long remoteId); }
\ No newline at end of file |