diff options
author | desperateCoder <echotodevnull@gmail.com> | 2020-08-05 19:30:57 +0300 |
---|---|---|
committer | Niedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com> | 2020-08-09 17:34:18 +0300 |
commit | ae00d7d9b07e6ef10256e039d485f0daa7402c7d (patch) | |
tree | d150f622dcc85792391fcfc1062afef3c070958b | |
parent | 53be92d4792f6337b046f8377ad3ec509423d9e9 (diff) |
#492 #631 fetch users of boards
8 files changed, 166 insertions, 21 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/api/JsonToEntityParser.java b/app/src/main/java/it/niedermann/nextcloud/deck/api/JsonToEntityParser.java index ab9572687..8b2686499 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/api/JsonToEntityParser.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/api/JsonToEntityParser.java @@ -339,12 +339,19 @@ public class JsonToEntityParser { } } - JsonElement owner = e.get("owner"); - if (owner != null) { - if (owner.isJsonPrimitive()) {//TODO: remove if, let only else! - DeckLog.verbose("owner is Primitive, skipping"); - } else - fullBoard.setOwner(parseUser(owner)); + if (e.has("owner")) { + fullBoard.setOwner(parseUser(e.get("owner"))); + } + if (e.has("users")) { + JsonElement users = e.get("users"); + if (users != null && !users.isJsonNull() && users.isJsonArray()) { + JsonArray usersArray = users.getAsJsonArray(); + List<User> usersList = new ArrayList<>(); + for (JsonElement userJson : usersArray) { + usersList.add(parseUser(userJson)); + } + fullBoard.setUsers(usersList); + } } }, e); return fullBoard; diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/full/FullBoard.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/full/FullBoard.java index 95875d5a3..f9f1643ff 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/full/FullBoard.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/full/FullBoard.java @@ -30,6 +30,8 @@ public class FullBoard implements IRemoteEntity { @Relation(entity = Stack.class, parentColumn = "localId", entityColumn = "boardId") public List<Stack> stacks; + @Ignore + public List<User> users; public User getOwner() { return owner; @@ -55,6 +57,14 @@ public class FullBoard implements IRemoteEntity { this.labels = labels; } + public List<User> getUsers() { + return users; + } + + public void setUsers(List<User> users) { + this.users = users; + } + @Ignore @Override public Board getEntity() { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/relations/UserInBoard.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/relations/UserInBoard.java new file mode 100644 index 000000000..1f167bad1 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/relations/UserInBoard.java @@ -0,0 +1,71 @@ +package it.niedermann.nextcloud.deck.model.relations; + +import androidx.annotation.NonNull; +import androidx.room.Entity; +import androidx.room.ForeignKey; +import androidx.room.Index; + +import it.niedermann.nextcloud.deck.model.Board; +import it.niedermann.nextcloud.deck.model.User; + +@Entity( + primaryKeys = {"userId", "boardId"}, + indices = {@Index("userId"), @Index("boardId"), @Index(name = "unique_idx_user_board", value = {"userId","boardId"}, unique = true)}, + foreignKeys = { + @ForeignKey(entity = User.class, + parentColumns = "localId", + childColumns = "userId", onDelete = ForeignKey.CASCADE), + @ForeignKey(entity = Board.class, + parentColumns = "localId", + childColumns = "boardId", onDelete = ForeignKey.CASCADE) + }) +public class UserInBoard { + @NonNull + private Long userId; + @NonNull + private Long boardId; + + @NonNull + public Long getUserId() { + return userId; + } + + public void setUserId(@NonNull Long userId) { + this.userId = userId; + } + + @NonNull + public Long getBoardId() { + return boardId; + } + + public void setBoardId(@NonNull Long boardId) { + this.boardId = boardId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UserInBoard that = (UserInBoard) o; + + if (!userId.equals(that.userId)) return false; + return boardId.equals(that.boardId); + } + + @Override + public int hashCode() { + int result = userId.hashCode(); + result = 31 * result + boardId.hashCode(); + return result; + } + + @Override + public String toString() { + return "UserInGroup{" + + "userId=" + userId + + ", boardId=" + boardId + + '}'; + } +} 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 b56ec97f8..964626e91 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 @@ -43,6 +43,7 @@ import it.niedermann.nextcloud.deck.model.ocs.comment.full.FullDeckComment; import it.niedermann.nextcloud.deck.model.ocs.projects.JoinCardWithProject; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProject; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProjectResource; +import it.niedermann.nextcloud.deck.model.relations.UserInBoard; import it.niedermann.nextcloud.deck.model.relations.UserInGroup; import it.niedermann.nextcloud.deck.model.widget.singlecard.SingleCardWidgetModel; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper; @@ -369,6 +370,9 @@ public class DataBaseAdapter { public void deleteGroupMembershipsOfGroup(Long localGroupUserId) { db.getUserInGroupDao().deleteByGroupId(localGroupUserId); } + public void deleteBoardMembershipsOfBoard(Long localBoardId) { + db.getUserInBoardDao().deleteByBoardId(localBoardId); + } public void addUserToGroup(Long localGroupUserId, Long localGroupMemberId) { UserInGroup relation = new UserInGroup(); relation.setGroupId(localGroupUserId); @@ -376,6 +380,13 @@ public class DataBaseAdapter { db.getUserInGroupDao().insert(relation); } + public void addUserToBoard(Long localUserId, Long localBoardId) { + UserInBoard relation = new UserInBoard(); + relation.setBoardId(localBoardId); + relation.setUserId(localUserId); + db.getUserInBoardDao().insert(relation); + } + public void updateLabel(Label label, boolean setStatus) { markAsEditedIfNeeded(label, setStatus); db.getLabelDao().update(label); 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 cea525526..3235d840e 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 @@ -36,6 +36,7 @@ import it.niedermann.nextcloud.deck.model.ocs.comment.Mention; import it.niedermann.nextcloud.deck.model.ocs.projects.JoinCardWithProject; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProject; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProjectResource; +import it.niedermann.nextcloud.deck.model.relations.UserInBoard; import it.niedermann.nextcloud.deck.model.relations.UserInGroup; import it.niedermann.nextcloud.deck.model.widget.singlecard.SingleCardWidgetModel; import it.niedermann.nextcloud.deck.persistence.sync.SyncWorker; @@ -56,6 +57,7 @@ import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.MentionDao; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.PermissionDao; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.StackDao; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.UserDao; +import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.UserInBoardDao; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.UserInGroupDao; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.projects.JoinCardWithOcsProjectDao; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.projects.OcsProjectDao; @@ -88,6 +90,7 @@ import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.widgets.Sta OcsProjectResource.class, JoinCardWithProject.class, UserInGroup.class, + UserInBoard.class, }, exportSchema = false, version = 20 @@ -251,6 +254,14 @@ public abstract class DeckDatabase extends RoomDatabase { database.execSQL("CREATE UNIQUE INDEX `unique_idx_group_member` ON `UserInGroup` (`groupId`, `memberId`)"); database.execSQL("CREATE INDEX `index_UserInGroup_groupId` ON `UserInGroup` (`groupId`)"); database.execSQL("CREATE INDEX `index_UserInGroup_memberId` ON `UserInGroup` (`memberId`)"); + + database.execSQL("CREATE TABLE `UserInBoard` (`userId` INTEGER NOT NULL, `boardId` INTEGER NOT NULL, " + + "primary KEY(`userId`, `boardId`), " + + "FOREIGN KEY(`userId`) REFERENCES `User`(`localId`) ON UPDATE NO ACTION ON DELETE CASCADE, " + + "FOREIGN KEY(`boardId`) REFERENCES `Board`(`localId`) ON UPDATE NO ACTION ON DELETE CASCADE)"); + database.execSQL("CREATE UNIQUE INDEX `unique_idx_user_board` ON `UserInBoard` (`userId`, `boardId`)"); + database.execSQL("CREATE INDEX `index_UserInBoard_userId` ON `UserInBoard` (`userId`)"); + database.execSQL("CREATE INDEX `index_UserInBoard_boardId` ON `UserInBoard` (`boardId`)"); } }; @@ -349,4 +360,6 @@ public abstract class DeckDatabase extends RoomDatabase { public abstract JoinCardWithOcsProjectDao getJoinCardWithOcsProjectDao(); public abstract UserInGroupDao getUserInGroupDao(); + + public abstract UserInBoardDao getUserInBoardDao(); }
\ No newline at end of file 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 7b19598a7..76dad1b15 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 @@ -76,13 +76,16 @@ public interface UserDao extends GenericDao<User> { " where ju.userId = u.localId" + " and ju.cardId = :notAssignedToLocalCardId AND status <> 3" + // not LOCAL_DELETED " )" + - " AND" + - " (" + - " EXISTS (" + - " select 1 from accesscontrol" + // v GROUP! - " where (userId = u.localId OR (type = 1 and exists(select 1 from UserInGroup uig where uig.memberId = u.localId and uig.groupId = userId))) " + - " and boardId = :boardId" + - " )" + + " AND ( " + + " EXISTS (" + + " select 1 from userinboard where boardId = :boardId AND userId = u.localId" + + " )" + + " OR" + + " EXISTS (" + + " select 1 from accesscontrol" + // v GROUP! + " where (userId = u.localId OR (type = 1 and exists(select 1 from UserInGroup uig where uig.memberId = u.localId and uig.groupId = userId))) " + + " and boardId = :boardId" + + " )" + " OR" + " EXISTS (" + " select 1 from board where localId = :boardId AND ownerId = u.localId" + diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserInBoardDao.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserInBoardDao.java new file mode 100644 index 000000000..7a0476c53 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserInBoardDao.java @@ -0,0 +1,12 @@ +package it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao; + +import androidx.room.Dao; +import androidx.room.Query; + +import it.niedermann.nextcloud.deck.model.relations.UserInBoard; + +@Dao +public interface UserInBoardDao extends GenericDao<UserInBoard> { + @Query("DELETE FROM userinboard WHERE boardId = :localId") + void deleteByBoardId(long localId); +}
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/BoardDataProvider.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/BoardDataProvider.java index 98163029f..da86d4ba0 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/BoardDataProvider.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/BoardDataProvider.java @@ -39,26 +39,44 @@ public class BoardDataProvider extends AbstractSyncDataProvider<FullBoard> { @Override public long createInDB(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard entity) { handleOwner(dataBaseAdapter, accountId, entity); + handleUsers(dataBaseAdapter, accountId, entity); return dataBaseAdapter.createBoardDirectly(accountId, entity.getBoard()); } private void handleOwner(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard entity) { if (entity.getOwner()!=null) { - User remoteOwner = entity.getOwner(); - User owner = dataBaseAdapter.getUserByUidDirectly(accountId, remoteOwner.getUid()); - if (owner == null){ - dataBaseAdapter.createUser(accountId, remoteOwner); - } else { - dataBaseAdapter.updateUser(accountId, remoteOwner, false); - } - owner = dataBaseAdapter.getUserByUidDirectly(accountId, remoteOwner.getUid()); + User owner = createOrUpdateUser(dataBaseAdapter,accountId, entity.getOwner()); entity.getBoard().setOwnerId(owner.getLocalId()); } } + private void handleUsers(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard entity) { + dataBaseAdapter.deleteBoardMembershipsOfBoard(entity.getLocalId()); + if (entity.getUsers()!=null && !entity.getUsers().isEmpty()) { + for (User user : entity.getUsers()) { + if (user == null) { + continue; + } + User existing = createOrUpdateUser(dataBaseAdapter, accountId, user); + dataBaseAdapter.addUserToBoard(existing.getLocalId(), entity.getLocalId()); + } + } + } + + private User createOrUpdateUser(DataBaseAdapter dataBaseAdapter, long accountId, User remoteUser) { + User owner = dataBaseAdapter.getUserByUidDirectly(accountId, remoteUser.getUid()); + if (owner == null){ + dataBaseAdapter.createUser(accountId, remoteUser); + } else { + dataBaseAdapter.updateUser(accountId, remoteUser, false); + } + return dataBaseAdapter.getUserByUidDirectly(accountId, remoteUser.getUid()); + } + @Override public void updateInDB(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard entity, boolean setStatus) { handleOwner(dataBaseAdapter, accountId, entity); + handleUsers(dataBaseAdapter, accountId, entity); dataBaseAdapter.updateBoard(entity.getBoard(), setStatus); } |