use of it.niedermann.nextcloud.deck.model.User in project nextcloud-deck by stefan-niedermann.
the class SyncManager method createBoard.
@AnyThread
public void createBoard(long accountId, @NonNull Board board, @NonNull IResponseCallback<FullBoard> callback) {
executor.submit(() -> {
final Account account = dataBaseAdapter.getAccountByIdDirectly(accountId);
final User owner = dataBaseAdapter.getUserByUidDirectly(accountId, account.getUserName());
if (owner == null) {
callback.onError(new IllegalStateException("Owner is null. This can be the case if the Deck app has never before been opened in the webinterface"));
} else {
final FullBoard fullBoard = new FullBoard();
board.setOwnerId(owner.getLocalId());
fullBoard.setOwner(owner);
fullBoard.setBoard(board);
board.setAccountId(accountId);
fullBoard.setAccountId(accountId);
new DataPropagationHelper(serverAdapter, dataBaseAdapter, executor).createEntity(new BoardDataProvider(), fullBoard, ResponseCallback.from(account, callback));
}
});
}
use of it.niedermann.nextcloud.deck.model.User in project nextcloud-deck by stefan-niedermann.
the class SyncManager method createFullCard.
// public LiveData<FullCard> createCard(long accountId, long localBoardId, long localStackId, Card card) {
//
// MutableLiveData<FullCard> liveData = new MutableLiveData<>();
// executor.submit(() -> {
// Account account = dataBaseAdapter.getAccountByIdDirectly(accountId);
// User owner = dataBaseAdapter.getUserByUidDirectly(accountId, account.getUserName());
// FullStack stack = dataBaseAdapter.getFullStackByLocalIdDirectly(localStackId);
// Board board = dataBaseAdapter.getBoardByLocalIdDirectly(localBoardId);
// card.setStackId(stack.getLocalId());
// FullCard fullCard = new FullCard();
// fullCard.setCard(card);
// fullCard.setOwner(owner);
// fullCard.setAccountId(accountId);
// new DataPropagationHelper(serverAdapter, dataBaseAdapter).createEntity(new CardPropagationDataProvider(null, board, stack), fullCard, new IResponseCallback<FullCard>(account) {
// @Override
// public void onResponse(FullCard response) {
// liveData.postValue(response);
// }
// }, (FullCard entity, FullCard response) -> {
// response.getCard().setUserId(entity.getCard().getUserId());
// response.getCard().setStackId(stack.getLocalId());
// });
// });
// return liveData;
// }
@AnyThread
public void createFullCard(long accountId, long localBoardId, long localStackId, @NonNull FullCard card, @NonNull IResponseCallback<FullCard> callback) {
executor.submit(() -> {
Account account = dataBaseAdapter.getAccountByIdDirectly(accountId);
User owner = dataBaseAdapter.getUserByUidDirectly(accountId, account.getUserName());
FullStack stack = dataBaseAdapter.getFullStackByLocalIdDirectly(localStackId);
Board board = dataBaseAdapter.getBoardByLocalIdDirectly(localBoardId);
card.getCard().setUserId(owner.getLocalId());
card.getCard().setStackId(stack.getLocalId());
card.getCard().setAccountId(accountId);
card.getCard().setStatusEnum(DBStatus.LOCAL_EDITED);
card.getCard().setOrder(dataBaseAdapter.getHighestCardOrderInStack(localStackId) + 1);
long localCardId = dataBaseAdapter.createCardDirectly(accountId, card.getCard());
card.getCard().setLocalId(localCardId);
List<User> assignedUsers = card.getAssignedUsers();
if (assignedUsers != null) {
for (User assignedUser : assignedUsers) {
dataBaseAdapter.createJoinCardWithUser(assignedUser.getLocalId(), localCardId, DBStatus.LOCAL_EDITED);
}
}
List<Label> labels = card.getLabels();
if (labels != null) {
for (Label label : labels) {
dataBaseAdapter.createJoinCardWithLabel(label.getLocalId(), localCardId, DBStatus.LOCAL_EDITED);
}
}
if (card.getAttachments() != null) {
for (Attachment attachment : card.getAttachments()) {
if (attachment.getLocalId() == null) {
attachment.setCardId(localCardId);
dataBaseAdapter.createAttachment(accountId, attachment);
}
}
}
if (serverAdapter.hasInternetConnection()) {
syncHelperFactory.create(serverAdapter, dataBaseAdapter, null).setResponseCallback(new ResponseCallback<>(account) {
@Override
public void onResponse(Boolean response) {
callback.onResponse(card);
}
@SuppressLint("MissingSuperCall")
@Override
public void onError(Throwable throwable) {
if (throwable.getClass() == DeckException.class && ((DeckException) throwable).getHint().equals(DeckException.Hint.DEPENDENCY_NOT_SYNCED_YET)) {
callback.onResponse(card);
} else {
callback.onError(throwable);
}
}
}).doUpSyncFor(new CardDataProvider(null, board, stack));
} else {
callback.onResponse(card);
}
});
}
use of it.niedermann.nextcloud.deck.model.User in project nextcloud-deck by stefan-niedermann.
the class SyncManager method cloneBoard.
/**
* Creates a new {@link Board} and adds the same {@link Label} and {@link Stack} as in the origin {@link Board}.
* Owner of the target {@link Board} will be the {@link User} with the {@link Account} of {@param targetAccountId}.
*
* @param cloneCards determines whether or not the cards in this {@link Board} shall be cloned or not
* Does <strong>not</strong> clone any {@link Card} or {@link AccessControl} from the origin {@link Board}.
*/
@AnyThread
public void cloneBoard(long originAccountId, long originBoardLocalId, long targetAccountId, @ColorInt int targetBoardColor, boolean cloneCards, @NonNull IResponseCallback<FullBoard> callback) {
executor.submit(() -> {
Account originAccount = dataBaseAdapter.getAccountByIdDirectly(originAccountId);
User newOwner = dataBaseAdapter.getUserByUidDirectly(originAccountId, originAccount.getUserName());
if (newOwner == null) {
callback.onError(new DeckException(DeckException.Hint.UNKNOWN_ACCOUNT_USER_ID, "User with Account-UID \"" + originAccount.getUserName() + "\" not found."));
return;
}
FullBoard originalBoard = dataBaseAdapter.getFullBoardByLocalIdDirectly(originAccountId, originBoardLocalId);
String newBoardTitleBaseName = originalBoard.getBoard().getTitle().trim();
int newBoardTitleCopyIndex = 0;
// already a copy?
String regex = " \\(copy [0-9]+\\)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(originalBoard.getBoard().getTitle());
if (matcher.find()) {
String found = matcher.group();
newBoardTitleBaseName = newBoardTitleBaseName.substring(0, newBoardTitleBaseName.length() - found.length());
Matcher indexMatcher = Pattern.compile("[0-9]+").matcher(found);
// noinspection ResultOfMethodCallIgnored
indexMatcher.find();
String oldIndexString = indexMatcher.group();
newBoardTitleCopyIndex = Integer.parseInt(oldIndexString);
}
String newBoardTitle;
do {
newBoardTitleCopyIndex++;
newBoardTitle = newBoardTitleBaseName + " (copy " + newBoardTitleCopyIndex + ")";
} while (dataBaseAdapter.getBoardForAccountByNameDirectly(targetAccountId, newBoardTitle) != null);
originalBoard.setAccountId(targetAccountId);
originalBoard.setId(null);
originalBoard.setLocalId(null);
originalBoard.getBoard().setTitle(newBoardTitle);
originalBoard.getBoard().setColor(String.format("%06X", 0xFFFFFF & targetBoardColor));
originalBoard.getBoard().setOwnerId(newOwner.getLocalId());
originalBoard.setStatusEnum(DBStatus.LOCAL_EDITED);
originalBoard.setOwner(newOwner);
long newBoardId = dataBaseAdapter.createBoardDirectly(originAccountId, originalBoard.getBoard());
originalBoard.setLocalId(newBoardId);
boolean isSameAccount = targetAccountId == originAccountId;
if (isSameAccount) {
List<AccessControl> aclList = originalBoard.getParticipants();
for (AccessControl acl : aclList) {
acl.setLocalId(null);
acl.setId(null);
acl.setBoardId(newBoardId);
dataBaseAdapter.createAccessControl(targetAccountId, acl);
}
}
Map<Long, Long> oldToNewLabelIdsDictionary = new HashMap<>();
for (Label label : originalBoard.getLabels()) {
Long oldLocalId = label.getLocalId();
label.setLocalId(null);
label.setId(null);
label.setAccountId(targetAccountId);
label.setStatusEnum(DBStatus.LOCAL_EDITED);
label.setBoardId(newBoardId);
long newLocalId = dataBaseAdapter.createLabelDirectly(targetAccountId, label);
oldToNewLabelIdsDictionary.put(oldLocalId, newLocalId);
}
List<Stack> oldStacks = originalBoard.getStacks();
for (Stack stack : oldStacks) {
Long oldStackId = stack.getLocalId();
stack.setLocalId(null);
stack.setId(null);
stack.setStatusEnum(DBStatus.LOCAL_EDITED);
stack.setAccountId(targetAccountId);
stack.setBoardId(newBoardId);
long createdStackId = dataBaseAdapter.createStack(targetAccountId, stack);
if (cloneCards) {
List<FullCard> oldCards = dataBaseAdapter.getFullCardsForStackDirectly(originAccountId, oldStackId, null);
for (FullCard oldCard : oldCards) {
Card newCard = oldCard.getCard();
newCard.setId(null);
newCard.setUserId(newOwner.getLocalId());
newCard.setLocalId(null);
newCard.setStackId(createdStackId);
newCard.setAccountId(targetAccountId);
newCard.setStatusEnum(DBStatus.LOCAL_EDITED);
long createdCardId = dataBaseAdapter.createCardDirectly(targetAccountId, newCard);
if (oldCard.getLabels() != null) {
for (Label oldLabel : oldCard.getLabels()) {
Long newLabelId = oldToNewLabelIdsDictionary.get(oldLabel.getLocalId());
if (newLabelId != null) {
dataBaseAdapter.createJoinCardWithLabel(newLabelId, createdCardId, DBStatus.LOCAL_EDITED);
} else
DeckLog.error("ID of created Label is null! Skipping assignment of ", oldLabel.getTitle(), "…");
}
}
if (isSameAccount && oldCard.getAssignedUsers() != null) {
for (User assignedUser : oldCard.getAssignedUsers()) {
dataBaseAdapter.createJoinCardWithUser(assignedUser.getLocalId(), createdCardId, DBStatus.LOCAL_EDITED);
}
}
}
}
}
if (serverAdapter.hasInternetConnection()) {
Account targetAccount = dataBaseAdapter.getAccountByIdDirectly(targetAccountId);
ServerAdapter serverAdapterToUse = this.serverAdapter;
if (originAccountId != targetAccountId) {
serverAdapterToUse = new ServerAdapter(appContext, targetAccount.getName());
}
syncHelperFactory.create(serverAdapterToUse, dataBaseAdapter, null).setResponseCallback(new ResponseCallback<>(targetAccount) {
@Override
public void onResponse(Boolean response) {
callback.onResponse(dataBaseAdapter.getFullBoardByLocalIdDirectly(targetAccountId, newBoardId));
}
@SuppressLint("MissingSuperCall")
@Override
public void onError(Throwable throwable) {
callback.onError(throwable);
}
}).doUpSyncFor(new BoardWithStacksAndLabelsUpSyncDataProvider(dataBaseAdapter.getFullBoardByLocalIdDirectly(targetAccountId, newBoardId)));
} else {
callback.onResponse(dataBaseAdapter.getFullBoardByLocalIdDirectly(targetAccountId, newBoardId));
}
});
}
use of it.niedermann.nextcloud.deck.model.User in project nextcloud-deck by stefan-niedermann.
the class CardDataProvider method fixRelations.
protected void fixRelations(DataBaseAdapter dataBaseAdapter, long accountId, FullCard entity) {
entity.getCard().setStackId(stack.getLocalId());
if (entity.getOwner() != null && !entity.getOwner().isEmpty()) {
User user = entity.getOwner().get(0);
User u = dataBaseAdapter.getUserByUidDirectly(accountId, user.getUid());
if (u == null) {
dataBaseAdapter.createUser(accountId, user);
} else {
user.setLocalId(u.getLocalId());
dataBaseAdapter.updateUser(accountId, user, false);
}
u = dataBaseAdapter.getUserByUidDirectly(accountId, user.getUid());
user.setLocalId(u.getLocalId());
entity.getCard().setUserId(u.getLocalId());
}
}
use of it.niedermann.nextcloud.deck.model.User in project nextcloud-deck by stefan-niedermann.
the class CardDataProvider method goDeeperForUpSync.
@Override
public void goDeeperForUpSync(SyncHelper syncHelper, ServerAdapter serverAdapter, DataBaseAdapter dataBaseAdapter, ResponseCallback<Boolean> callback) {
FullStack stack;
Board board;
List<JoinCardWithLabel> changedLabels;
if (this.stack == null) {
changedLabels = dataBaseAdapter.getAllChangedLabelJoins();
} else {
changedLabels = dataBaseAdapter.getAllChangedLabelJoinsForStack(this.stack.getLocalId());
}
Account account = callback.getAccount();
for (JoinCardWithLabel changedLabelLocal : changedLabels) {
Card card = dataBaseAdapter.getCardByLocalIdDirectly(account.getId(), changedLabelLocal.getCardId());
if (card == null) {
// https://github.com/stefan-niedermann/nextcloud-deck/issues/683#issuecomment-759116820
continue;
}
if (this.stack == null) {
stack = dataBaseAdapter.getFullStackByLocalIdDirectly(card.getStackId());
} else {
stack = this.stack;
}
if (this.board == null) {
board = dataBaseAdapter.getBoardByLocalIdDirectly(stack.getStack().getBoardId());
} else {
board = this.board;
}
JoinCardWithLabel changedLabel = dataBaseAdapter.getAllChangedLabelJoinsWithRemoteIDs(changedLabelLocal.getCardId(), changedLabelLocal.getLabelId());
if (changedLabel.getStatusEnum() == DBStatus.LOCAL_DELETED) {
if (changedLabel.getLabelId() == null || changedLabel.getCardId() == null) {
dataBaseAdapter.deleteJoinedLabelForCardPhysicallyByRemoteIDs(account.getId(), changedLabel.getCardId(), changedLabel.getLabelId());
} else {
serverAdapter.unassignLabelFromCard(board.getId(), stack.getId(), changedLabel.getCardId(), changedLabel.getLabelId(), new ResponseCallback<>(account) {
@Override
public void onResponse(Void response) {
dataBaseAdapter.deleteJoinedLabelForCardPhysicallyByRemoteIDs(account.getId(), changedLabel.getCardId(), changedLabel.getLabelId());
}
});
}
} else if (changedLabel.getStatusEnum() == DBStatus.LOCAL_EDITED) {
if (changedLabel.getLabelId() == null || changedLabel.getCardId() == null) {
// Sync next time, the card should be available on server then.
continue;
} else {
serverAdapter.assignLabelToCard(board.getId(), stack.getId(), changedLabel.getCardId(), changedLabel.getLabelId(), new ResponseCallback<>(account) {
@Override
public void onResponse(Void response) {
Label label = dataBaseAdapter.getLabelByRemoteIdDirectly(account.getId(), changedLabel.getLabelId());
dataBaseAdapter.setStatusForJoinCardWithLabel(card.getLocalId(), label.getLocalId(), DBStatus.UP_TO_DATE.getId());
}
});
}
}
}
List<JoinCardWithUser> changedUsers;
if (this.stack == null) {
changedUsers = dataBaseAdapter.getAllChangedUserJoinsWithRemoteIDs();
} else {
changedUsers = dataBaseAdapter.getAllChangedUserJoinsWithRemoteIDsForStack(this.stack.getLocalId());
}
for (JoinCardWithUser changedUser : changedUsers) {
// not already known to server?
if (changedUser.getCardId() == null) {
// skip for now
continue;
}
Card card = dataBaseAdapter.getCardByRemoteIdDirectly(account.getId(), changedUser.getCardId());
if (card == null) {
// this shouldn't actually happen, but does as it seems. the card cant be found by remote id (exists!) and account-ID.
continue;
}
if (this.stack == null) {
stack = dataBaseAdapter.getFullStackByLocalIdDirectly(card.getStackId());
} else {
stack = this.stack;
}
if (this.board == null) {
board = dataBaseAdapter.getBoardByLocalIdDirectly(stack.getStack().getBoardId());
} else {
board = this.board;
}
User user = dataBaseAdapter.getUserByLocalIdDirectly(changedUser.getUserId());
if (changedUser.getStatusEnum() == DBStatus.LOCAL_DELETED) {
serverAdapter.unassignUserFromCard(board.getId(), stack.getId(), changedUser.getCardId(), user.getUid(), new ResponseCallback<>(account) {
@Override
public void onResponse(Void response) {
dataBaseAdapter.deleteJoinedUserForCardPhysicallyByRemoteIDs(account.getId(), changedUser.getCardId(), user.getUid());
}
});
} else if (changedUser.getStatusEnum() == DBStatus.LOCAL_EDITED) {
serverAdapter.assignUserToCard(board.getId(), stack.getId(), changedUser.getCardId(), user.getUid(), new ResponseCallback<>(account) {
@Override
public void onResponse(Void response) {
dataBaseAdapter.setStatusForJoinCardWithUser(card.getLocalId(), user.getLocalId(), DBStatus.UP_TO_DATE.getId());
}
});
}
}
List<Attachment> attachments;
if (this.stack == null) {
attachments = dataBaseAdapter.getLocallyChangedAttachmentsDirectly(account.getId());
} else {
attachments = dataBaseAdapter.getLocallyChangedAttachmentsForStackDirectly(this.stack.getLocalId());
}
for (Attachment attachment : attachments) {
FullCard card = dataBaseAdapter.getFullCardByLocalIdDirectly(account.getId(), attachment.getCardId());
stack = dataBaseAdapter.getFullStackByLocalIdDirectly(card.getCard().getStackId());
board = dataBaseAdapter.getBoardByLocalIdDirectly(stack.getStack().getBoardId());
syncHelper.doUpSyncFor(new AttachmentDataProvider(this, board, stack.getStack(), card, Collections.singletonList(attachment)));
}
List<Card> cardsWithChangedComments;
if (this.stack == null) {
cardsWithChangedComments = dataBaseAdapter.getCardsWithLocallyChangedCommentsDirectly(account.getId());
} else {
cardsWithChangedComments = dataBaseAdapter.getCardsWithLocallyChangedCommentsForStackDirectly(this.stack.getLocalId());
}
for (Card card : cardsWithChangedComments) {
syncHelper.doUpSyncFor(new DeckCommentsDataProvider(this, card));
}
callback.onResponse(Boolean.TRUE);
}
Aggregations