1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
package it.niedermann.nextcloud.deck.persistence.sync.helpers.providers;
import android.annotation.SuppressLint;
import android.util.Pair;
import androidx.lifecycle.MutableLiveData;
import com.nextcloud.android.sso.api.ParsedResponse;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.api.ResponseCallback;
import it.niedermann.nextcloud.deck.model.AccessControl;
import it.niedermann.nextcloud.deck.model.Board;
import it.niedermann.nextcloud.deck.model.Label;
import it.niedermann.nextcloud.deck.model.User;
import it.niedermann.nextcloud.deck.model.full.FullBoard;
import it.niedermann.nextcloud.deck.model.full.FullStack;
import it.niedermann.nextcloud.deck.persistence.sync.adapters.ServerAdapter;
import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.DataBaseAdapter;
import it.niedermann.nextcloud.deck.persistence.sync.helpers.SyncHelper;
import it.niedermann.nextcloud.deck.persistence.sync.helpers.util.AsyncUtil;
public class BoardDataProvider extends AbstractSyncDataProvider<FullBoard> {
private int progressTotal = 0;
private int progressDone = 0;
private MutableLiveData<Pair<Integer, Integer>> progress = null;
public BoardDataProvider() {
super(null);
}
public BoardDataProvider(MutableLiveData<Pair<Integer, Integer>> progress) {
this();
this.progress = progress;
}
@Override
public void getAllFromServer(ServerAdapter serverAdapter, DataBaseAdapter dataBaseAdapter, long accountId, ResponseCallback<List<FullBoard>> responder, Instant lastSync) {
serverAdapter.getBoards(new ResponseCallback<>(responder.getAccount()) {
@Override
public void onResponse(ParsedResponse<List<FullBoard>> response) {
progressTotal = response.getResponse().size();
updateProgress();
String etag = response.getHeaders().get("ETag");
if (etag != null && !etag.equals(account.getBoardsEtag())) {
account.setBoardsEtag(etag);
dataBaseAdapter.updateAccount(account);
}
responder.onResponse(response.getResponse());
}
@SuppressLint("MissingSuperCall")
@Override
public void onError(Throwable throwable) {
responder.onError(throwable);
}
});
}
private void updateProgress() {
if (progress != null) {
DeckLog.log("New progress post", progressDone, progressTotal);
progress.postValue(Pair.create(progressDone, progressTotal));
}
}
@Override
protected boolean removeChild(AbstractSyncDataProvider<?> child) {
boolean isRemoved = super.removeChild(child);
if (isRemoved && child.getClass() == StackDataProvider.class) {
progressDone ++;
updateProgress();
}
return isRemoved;
}
@Override
public FullBoard getSingleFromDB(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard entity) {
return dataBaseAdapter.getFullBoardByRemoteIdDirectly(accountId, entity.getEntity().getId());
}
@Override
public long createInDB(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard entity) {
handleOwner(dataBaseAdapter, accountId, entity);
final long localId = dataBaseAdapter.createBoardDirectly(accountId, entity.getBoard());
entity.getBoard().setLocalId(localId);
handleUsers(dataBaseAdapter, accountId, entity);
return localId;
}
private void handleOwner(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard entity) {
if (entity.getOwner() != null) {
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) {
handleDefaultLabels(dataBaseAdapter, entity);
handleOwner(dataBaseAdapter, accountId, entity);
dataBaseAdapter.updateBoard(entity.getBoard(), setStatus);
handleUsers(dataBaseAdapter, accountId, entity);
}
private void handleDefaultLabels(DataBaseAdapter dataBaseAdapter, FullBoard entity) {
// ## merge labels (created at board creation):
// the server creates four default labels. if a board is copied, they will also be copied. At sync, after creating the board, the labels are already there.
// this merges the created default ones with the ones i already have.
if (entity != null && entity.getLabels() != null) {
for (Label label : entity.getLabels()) {
// does this label exist and unknown to server yet?
Label existing = dataBaseAdapter.getLabelByBoardIdAndTitleDirectly(entity.getLocalId(), label.getTitle());
if (existing != null && existing.getId() == null) {
// take our label and lets say it IS the same as on server (but use the local color, no matter what the server says)
existing.setId(label.getId());
dataBaseAdapter.updateLabel(existing, false);
}
}
}
}
@Override
public void updateInDB(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard entity) {
updateInDB(dataBaseAdapter, accountId, entity, false);
}
@Override
public void goDeeper(SyncHelper syncHelper, FullBoard existingEntity, FullBoard entityFromServer, ResponseCallback<Boolean> callback) {
List<Label> labels = entityFromServer.getLabels();
if (labels != null && !labels.isEmpty()) {
syncHelper.doSyncFor(new LabelDataProvider(this, existingEntity.getBoard(), labels));
}
List<AccessControl> acl = entityFromServer.getParticipants();
if (acl != null && !acl.isEmpty()) {
for (AccessControl ac : acl) {
ac.setBoardId(existingEntity.getLocalId());
}
syncHelper.doSyncFor(new AccessControlDataProvider(this, existingEntity, acl));
}
if (entityFromServer.getStacks() != null && !entityFromServer.getStacks().isEmpty()) {
syncHelper.doSyncFor(new StackDataProvider(this, existingEntity));
}
}
@Override
public void createOnServer(ServerAdapter serverAdapter, DataBaseAdapter dataBaseAdapter, long accountId, ResponseCallback<FullBoard> responder, FullBoard entity) {
serverAdapter.createBoard(entity.getBoard(), responder);
}
@Override
public List<FullBoard> getAllChangedFromDB(DataBaseAdapter dataBaseAdapter, long accountId, Instant lastSync) {
return dataBaseAdapter.getLocallyChangedBoards(accountId);
}
@Override
public void goDeeperForUpSync(SyncHelper syncHelper, ServerAdapter serverAdapter, DataBaseAdapter dataBaseAdapter, ResponseCallback<Boolean> callback) {
Long accountId = callback.getAccount().getId();
List<Label> locallyChangedLabels = dataBaseAdapter.getLocallyChangedLabels(accountId);
AsyncUtil.awaitAsyncWork(locallyChangedLabels.size(), (countDownLatch) -> {
for (Label label : locallyChangedLabels) {
Board board = dataBaseAdapter.getBoardByLocalIdDirectly(label.getBoardId());
label.setBoardId(board.getId());
syncHelper.doUpSyncFor(new LabelDataProvider(this, board, Collections.singletonList(label)), countDownLatch);
}
});
List<Long> localBoardIDsWithChangedACL = dataBaseAdapter.getBoardIDsOfLocallyChangedAccessControl(accountId);
for (Long boardId : localBoardIDsWithChangedACL) {
syncHelper.doUpSyncFor(new AccessControlDataProvider(this, dataBaseAdapter.getFullBoardByLocalIdDirectly(accountId, boardId), new ArrayList<>()));
}
Set<Long> syncedBoards = new HashSet<>();
List<FullStack> locallyChangedStacks = dataBaseAdapter.getLocallyChangedStacks(accountId);
if (locallyChangedStacks.size() < 1) {
// no changed stacks? maybe cards! So we have to go deeper!
new StackDataProvider(this, null).goDeeperForUpSync(syncHelper, serverAdapter, dataBaseAdapter, callback);
} else {
for (FullStack locallyChangedStack : locallyChangedStacks) {
long boardId = locallyChangedStack.getStack().getBoardId();
boolean added = syncedBoards.add(boardId);
if (added) {
FullBoard board = dataBaseAdapter.getFullBoardByLocalIdDirectly(accountId, boardId);
locallyChangedStack.getStack().setBoardId(board.getId());
syncHelper.doUpSyncFor(new StackDataProvider(this, board));
}
}
}
}
@Override
public void updateOnServer(ServerAdapter serverAdapter, DataBaseAdapter dataBaseAdapter, long accountId, ResponseCallback<FullBoard> callback, FullBoard entity) {
serverAdapter.updateBoard(entity.getBoard(), callback);
}
@Override
public void deleteInDB(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard fullBoard) {
dataBaseAdapter.deleteBoard(fullBoard.getBoard(), true);
}
@Override
public void deletePhysicallyInDB(DataBaseAdapter dataBaseAdapter, long accountId, FullBoard fullBoard) {
dataBaseAdapter.deleteBoardPhysically(fullBoard.getBoard());
}
@Override
public void deleteOnServer(ServerAdapter serverAdapter, long accountId, ResponseCallback<Void> callback, FullBoard entity, DataBaseAdapter dataBaseAdapter) {
serverAdapter.deleteBoard(entity.getBoard(), callback);
}
@Override
public void handleDeletes(ServerAdapter serverAdapter, DataBaseAdapter dataBaseAdapter, long accountId, List<FullBoard> entitiesFromServer) {
List<FullBoard> localBoards = dataBaseAdapter.getAllFullBoards(accountId);
List<FullBoard> delta = findDelta(entitiesFromServer, localBoards);
for (FullBoard board : delta) {
if (board.getId() == null) {
// not pushed up yet so:
continue;
}
dataBaseAdapter.deleteBoardPhysically(board.getBoard());
}
}
}
|