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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
|
package it.niedermann.nextcloud.deck.persistence.sync.adapters.db;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import androidx.annotation.AnyThread;
import androidx.annotation.ColorInt;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;
import androidx.lifecycle.LiveData;
import androidx.sqlite.db.SimpleSQLiteQuery;
import org.jetbrains.annotations.NotNull;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.model.AccessControl;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.Attachment;
import it.niedermann.nextcloud.deck.model.Board;
import it.niedermann.nextcloud.deck.model.Card;
import it.niedermann.nextcloud.deck.model.JoinBoardWithLabel;
import it.niedermann.nextcloud.deck.model.JoinCardWithLabel;
import it.niedermann.nextcloud.deck.model.JoinCardWithUser;
import it.niedermann.nextcloud.deck.model.Label;
import it.niedermann.nextcloud.deck.model.Stack;
import it.niedermann.nextcloud.deck.model.User;
import it.niedermann.nextcloud.deck.model.appwidgets.StackWidgetModel;
import it.niedermann.nextcloud.deck.model.enums.DBStatus;
import it.niedermann.nextcloud.deck.model.enums.EDueType;
import it.niedermann.nextcloud.deck.model.full.FullBoard;
import it.niedermann.nextcloud.deck.model.full.FullCard;
import it.niedermann.nextcloud.deck.model.full.FullCardWithProjects;
import it.niedermann.nextcloud.deck.model.full.FullSingleCardWidgetModel;
import it.niedermann.nextcloud.deck.model.full.FullStack;
import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity;
import it.niedermann.nextcloud.deck.model.interfaces.IRemoteEntity;
import it.niedermann.nextcloud.deck.model.internal.FilterInformation;
import it.niedermann.nextcloud.deck.model.ocs.Activity;
import it.niedermann.nextcloud.deck.model.ocs.comment.DeckComment;
import it.niedermann.nextcloud.deck.model.ocs.comment.Mention;
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.filter.EWidgetType;
import it.niedermann.nextcloud.deck.model.widget.filter.FilterWidget;
import it.niedermann.nextcloud.deck.model.widget.filter.FilterWidgetAccount;
import it.niedermann.nextcloud.deck.model.widget.filter.FilterWidgetBoard;
import it.niedermann.nextcloud.deck.model.widget.filter.FilterWidgetLabel;
import it.niedermann.nextcloud.deck.model.widget.filter.FilterWidgetProject;
import it.niedermann.nextcloud.deck.model.widget.filter.FilterWidgetSort;
import it.niedermann.nextcloud.deck.model.widget.filter.FilterWidgetStack;
import it.niedermann.nextcloud.deck.model.widget.filter.FilterWidgetUser;
import it.niedermann.nextcloud.deck.model.widget.filter.dto.FilterWidgetCard;
import it.niedermann.nextcloud.deck.model.widget.singlecard.SingleCardWidgetModel;
import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper;
import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData;
import it.niedermann.nextcloud.deck.ui.upcomingcards.UpcomingCardsAdapterItem;
import it.niedermann.nextcloud.deck.ui.widget.singlecard.SingleCardWidget;
import static androidx.lifecycle.Transformations.distinctUntilChanged;
public class DataBaseAdapter {
private final DeckDatabase db;
@NonNull
private final Context context;
public DataBaseAdapter(@NonNull Context applicationContext) {
this(applicationContext, DeckDatabase.getInstance(applicationContext));
}
private DataBaseAdapter(@NonNull Context applicationContext, @NonNull DeckDatabase db) {
this.context = applicationContext;
this.db = db;
}
@NonNull
public Context getContext() {
return context;
}
private <T extends AbstractRemoteEntity> void markAsEditedIfNeeded(T entity, boolean setStatus) {
if (!setStatus) return;
entity.setLastModifiedLocal(Instant.now());
entity.setStatusEnum(DBStatus.LOCAL_EDITED);
}
private <T extends AbstractRemoteEntity> void markAsDeletedIfNeeded(T entity, boolean setStatus) {
if (!setStatus) return;
entity.setStatusEnum(DBStatus.LOCAL_DELETED);
entity.setLastModifiedLocal(Instant.now());
}
public LiveData<Boolean> hasAccounts() {
return LiveDataHelper.postCustomValue(db.getAccountDao().countAccounts(), data -> data != null && data > 0);
}
public LiveData<Board> getBoardByRemoteId(long accountId, long remoteId) {
return distinctUntilChanged(db.getBoardDao().getBoardByRemoteId(accountId, remoteId));
}
public Board getBoardByRemoteIdDirectly(long accountId, long remoteId) {
return db.getBoardDao().getBoardByRemoteIdDirectly(accountId, remoteId);
}
public FullBoard getFullBoardByRemoteIdDirectly(long accountId, long remoteId) {
return db.getBoardDao().getFullBoardByRemoteIdDirectly(accountId, remoteId);
}
public FullBoard getFullBoardByLocalIdDirectly(long accountId, long localId) {
return db.getBoardDao().getFullBoardByLocalIdDirectly(accountId, localId);
}
public LiveData<Stack> getStackByRemoteId(long accountId, long localBoardId, long remoteId) {
return distinctUntilChanged(db.getStackDao().getStackByRemoteId(accountId, localBoardId, remoteId));
}
public Stack getStackByLocalIdDirectly(final long localStackId) {
return db.getStackDao().getStackByLocalIdDirectly(localStackId);
}
public FullStack getFullStackByLocalIdDirectly(final long localStackId) {
return db.getStackDao().getFullStackByLocalIdDirectly(localStackId);
}
public FullStack getFullStackByRemoteIdDirectly(long accountId, long localBoardId, long remoteId) {
return db.getStackDao().getFullStackByRemoteIdDirectly(accountId, localBoardId, remoteId);
}
public LiveData<Card> getCardByRemoteID(long accountId, long remoteId) {
return distinctUntilChanged(db.getCardDao().getCardByRemoteId(accountId, remoteId));
}
public FullCard getFullCardByRemoteIdDirectly(long accountId, long remoteId) {
FullCard card = db.getCardDao().getFullCardByRemoteIdDirectly(accountId, remoteId);
filterRelationsForCard(card);
return card;
}
public FullCard getFullCardByLocalIdDirectly(long accountId, long localId) {
return db.getCardDao().getFullCardByLocalIdDirectly(accountId, localId);
}
public void filterRelationsForCard(@Nullable FullCard card) {
if (card != null) {
if (card.getLabels() != null && !card.getLabels().isEmpty()) {
List<Long> filteredIDs = db.getJoinCardWithLabelDao().filterDeleted(card.getLocalId(), getLocalIDs(card.getLabels()));
card.setLabels(db.getLabelDao().getLabelsByIdsDirectly(filteredIDs));
}
if (card.getAssignedUsers() != null && !card.getAssignedUsers().isEmpty()) {
List<Long> filteredIDs = db.getJoinCardWithUserDao().filterDeleted(card.getLocalId(), getLocalIDs(card.getAssignedUsers()));
card.setAssignedUsers(db.getUserDao().getUsersByIdsDirectly(filteredIDs));
}
}
}
private <T> List<Long> getLocalIDs(@NonNull List<? extends AbstractRemoteEntity> remoteEntityList) {
ArrayList<Long> ids = new ArrayList<>(remoteEntityList.size());
for (AbstractRemoteEntity entity : remoteEntityList) {
ids.add(entity.getLocalId());
}
return ids;
}
public void readRelationsForACL(@Nullable List<AccessControl> acl) {
if (acl != null) {
for (AccessControl accessControl : acl) {
readRelationsForACL(accessControl);
}
}
}
public void readRelationsForACL(@Nullable AccessControl acl) {
if (acl != null) {
if (acl.getUserId() != null) {
acl.setUser(db.getUserDao().getUserByLocalIdDirectly(acl.getUserId()));
}
}
}
private void filterRelationsForCard(@Nullable Collection<FullCard> card) {
if (card == null) {
return;
}
for (FullCard c : card) {
filterRelationsForCard(c);
}
}
@WorkerThread
public Card getCardByRemoteIdDirectly(long accountId, long remoteId) {
return db.getCardDao().getCardByRemoteIdDirectly(accountId, remoteId);
}
public LiveData<List<FullCard>> getFullCardsForStack(long accountId, long localStackId, FilterInformation filter) {
if (filter == null) {
return LiveDataHelper.interceptLiveData(db.getCardDao().getFullCardsForStack(accountId, localStackId), this::filterRelationsForCard);
}
return LiveDataHelper.interceptLiveData(db.getCardDao().getFilteredFullCardsForStack(getQueryForFilter(filter, accountId, localStackId)), this::filterRelationsForCard);
}
private void fillSqlWithEntityListValues(StringBuilder query, List<Object> args, @NonNull List<? extends IRemoteEntity> entities) {
List<Long> idList = entities.stream().map(IRemoteEntity::getLocalId).collect(Collectors.toList());
fillSqlWithListValues(query, args, idList);
}
private void fillSqlWithListValues(StringBuilder query, List<Object> args, @NonNull List<?> values) {
for (int i = 0; i < values.size(); i++) {
if (i > 0) {
query.append(", ");
}
query.append("?");
args.add(values.get(i));
}
}
@WorkerThread
public List<FullCard> getFullCardsForStackDirectly(long accountId, long localStackId, @Nullable FilterInformation filter) {
return filter == null
? db.getCardDao().getFullCardsForStackDirectly(accountId, localStackId)
: db.getCardDao().getFilteredFullCardsForStackDirectly(getQueryForFilter(filter, accountId, localStackId));
}
@AnyThread
private SimpleSQLiteQuery getQueryForFilter(FilterInformation filter, long accountId, long localStackId) {
return getQueryForFilter(filter, Collections.singletonList(accountId), Collections.singletonList(localStackId));
}
@AnyThread
private SimpleSQLiteQuery getQueryForFilter(FilterInformation filter, List<Long> accountIds, List<Long> localStackIds) {
List<Object> args = new ArrayList<>();
StringBuilder query = new StringBuilder("SELECT * FROM card c WHERE 1=1 ");
if (accountIds != null && !accountIds.isEmpty()) {
query.append("and accountId in (");
fillSqlWithListValues(query, args, accountIds);
query.append(") ");
}
if (localStackIds != null && !localStackIds.isEmpty()) {
query.append("and stackId in (");
fillSqlWithListValues(query, args, localStackIds);
query.append(") ");
}
if (!filter.getLabels().isEmpty()) {
query.append("and (exists(select 1 from joincardwithlabel j where c.localId = cardId and labelId in (");
fillSqlWithEntityListValues(query, args, filter.getLabels());
query.append(") and j.status<>3) ");
if (filter.isNoAssignedLabel()) {
query.append("or not exists(select 1 from joincardwithlabel j where c.localId = cardId and j.status<>3)) ");
} else {
query.append(") ");
}
} else if (filter.isNoAssignedLabel()) {
query.append("and not exists(select 1 from joincardwithlabel j where c.localId = cardId and j.status<>3) ");
}
if (!filter.getUsers().isEmpty()) {
query.append("and (exists(select 1 from joincardwithuser j where c.localId = cardId and userId in (");
fillSqlWithEntityListValues(query, args, filter.getUsers());
query.append(") and j.status<>3) ");
if (filter.isNoAssignedUser()) {
query.append("or not exists(select 1 from joincardwithuser j where c.localId = cardId and j.status<>3)) ");
} else {
query.append(") ");
}
} else if (filter.isNoAssignedUser()) {
query.append("and not exists(select 1 from joincardwithuser j where c.localId = cardId and j.status<>3) ");
}
if (!filter.getProjects().isEmpty()) {
query.append("and (exists(select 1 from joincardwithproject j where c.localId = cardId and projectId in (");
fillSqlWithEntityListValues(query, args, filter.getProjects());
query.append(") and j.status<>3) ");
if (filter.isNoAssignedProject()) {
query.append("or not exists(select 1 from joincardwithproject j where c.localId = cardId and j.status<>3)) ");
} else {
query.append(") ");
}
} else if (filter.isNoAssignedProject()) {
query.append("and not exists(select 1 from joincardwithproject j where c.localId = cardId and j.status<>3) ");
}
if (filter.getDueType() != EDueType.NO_FILTER) {
switch (filter.getDueType()) {
case NO_DUE:
query.append("and c.dueDate is null");
break;
case OVERDUE:
query.append("and datetime(c.duedate/1000, 'unixepoch', 'localtime') <= datetime('now', 'localtime')");
break;
case TODAY:
query.append("and datetime(c.duedate/1000, 'unixepoch', 'localtime') between datetime('now', 'localtime') and datetime('now', '+24 hour', 'localtime')");
break;
case WEEK:
query.append("and datetime(c.duedate/1000, 'unixepoch', 'localtime') between datetime('now', 'localtime') and datetime('now', '+7 day', 'localtime')");
break;
case MONTH:
query.append("and datetime(c.duedate/1000, 'unixepoch', 'localtime') between datetime('now', 'localtime') and datetime('now', '+30 day', 'localtime')");
break;
default:
throw new IllegalArgumentException("You need to add your new EDueType value\"" + filter.getDueType() + "\" here!");
}
}
if (filter.getFilterText() != null && !filter.getFilterText().isEmpty()) {
query.append(" and (c.description like ? or c.title like ?) ");
String filterText = "%"+filter.getFilterText()+"%";
args.add(filterText);
args.add(filterText);
}
if (filter.getArchiveStatus() != FilterInformation.EArchiveStatus.ALL) {
query.append(" and c.archived = ").append(filter.getArchiveStatus() == FilterInformation.EArchiveStatus.ARCHIVED ? 1 : 0);
}
query.append(" and status<>3 order by accountId asc, stackId asc, `order`, createdAt asc;");
return new SimpleSQLiteQuery(query.toString(), args.toArray());
}
@WorkerThread
public User getUserByUidDirectly(long accountId, String uid) {
return db.getUserDao().getUserByUidDirectly(accountId, uid);
}
@WorkerThread
public long createUser(long accountId, User user) {
user.setAccountId(accountId);
long newId = db.getUserDao().insert(user);
new Thread(() -> {
Account account = db.getAccountDao().getAccountByIdDirectly(accountId);
if (account.getUserName().equals(user.getUid())) {
for (FilterWidget widget : getFilterWidgetsByType(EWidgetType.UPCOMING_WIDGET)) {
for (FilterWidgetAccount widgetAccount : widget.getAccounts()) {
if (widgetAccount.getAccountId() == accountId && widgetAccount.getUsers().isEmpty()) {
FilterWidgetUser u = new FilterWidgetUser();
u.setFilterAccountId(widgetAccount.getId());
u.setUserId(newId);
widgetAccount.getUsers().add(u);
updateFilterWidgetDirectly(widget);
}
}
}
}
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.USER, newId);
}).start();
return newId;
}
@WorkerThread
public void updateUser(long accountId, User user, boolean setStatus) {
markAsEditedIfNeeded(user, setStatus);
user.setAccountId(accountId);
db.getUserDao().update(user);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.USER, user.getLocalId());
}
@UiThread
public LiveData<Label> getLabelByRemoteId(long accountId, long remoteId) {
return distinctUntilChanged(db.getLabelDao().getLabelByRemoteId(accountId, remoteId));
}
@WorkerThread
public Label getLabelByRemoteIdDirectly(long accountId, long remoteId) {
return db.getLabelDao().getLabelByRemoteIdDirectly(accountId, remoteId);
}
@WorkerThread
public long createLabelDirectly(long accountId, @NonNull Label label) {
label.setAccountId(accountId);
long newId = db.getLabelDao().insert(label);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.LABEL, newId);
return newId;
}
public void createJoinCardWithLabel(long localLabelId, long localCardId) {
createJoinCardWithLabel(localLabelId, localCardId, DBStatus.UP_TO_DATE);
}
public void createJoinCardWithLabel(long localLabelId, long localCardId, DBStatus status) {
JoinCardWithLabel existing = db.getJoinCardWithLabelDao().getJoin(localLabelId, localCardId);
if (existing != null && existing.getStatusEnum() == DBStatus.LOCAL_DELETED) {
// readded!
existing.setStatusEnum(DBStatus.LOCAL_EDITED);
db.getJoinCardWithLabelDao().update(existing);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.LABEL, existing.getLabelId());
} else {
JoinCardWithLabel join = new JoinCardWithLabel();
join.setCardId(localCardId);
join.setLabelId(localLabelId);
join.setStatus(status.getId());
db.getJoinCardWithLabelDao().insert(join);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.LABEL, join.getLabelId());
}
}
public void deleteJoinedLabelsForCard(long localCardId) {
db.getJoinCardWithLabelDao().deleteByCardId(localCardId);
}
public void deleteJoinedLabelForCard(long localCardId, long localLabelId) {
db.getJoinCardWithLabelDao().setDbStatus(localCardId, localLabelId, DBStatus.LOCAL_DELETED.getId());
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.LABEL, localLabelId);
}
public void deleteJoinedUserForCard(long localCardId, long localUserId) {
db.getJoinCardWithUserDao().setDbStatus(localCardId, localUserId, DBStatus.LOCAL_DELETED.getId());
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.USER, localUserId);
}
public void deleteJoinedLabelForCardPhysically(long localCardId, long localLabelId) {
db.getJoinCardWithLabelDao().deleteByCardIdAndLabelId(localCardId, localLabelId);
}
public void deleteJoinedUserForCardPhysically(long localCardId, long localUserId) {
db.getJoinCardWithUserDao().deleteByCardIdAndUserIdPhysically(localCardId, localUserId);
}
public void createJoinCardWithUser(long localUserId, long localCardId) {
createJoinCardWithUser(localUserId, localCardId, DBStatus.UP_TO_DATE);
}
public void createJoinCardWithUser(long localUserId, long localCardId, DBStatus status) {
JoinCardWithUser existing = db.getJoinCardWithUserDao().getJoin(localUserId, localCardId);
if (existing != null && existing.getStatusEnum() == DBStatus.LOCAL_DELETED) {
// readded!
existing.setStatusEnum(DBStatus.LOCAL_EDITED);
db.getJoinCardWithUserDao().update(existing);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.USER, localUserId);
} else if (existing != null) {
return;
} else {
JoinCardWithUser join = new JoinCardWithUser();
join.setCardId(localCardId);
join.setUserId(localUserId);
join.setStatus(status.getId());
db.getJoinCardWithUserDao().insert(join);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.USER, localUserId);
}
}
public void deleteJoinedUsersForCard(long localCardId) {
db.getJoinCardWithUserDao().deleteByCardId(localCardId);
}
public void createJoinBoardWithLabel(long localBoardId, long localLabelId) {
JoinBoardWithLabel join = new JoinBoardWithLabel();
join.setBoardId(localBoardId);
join.setLabelId(localLabelId);
db.getJoinBoardWithLabelDao().insert(join);
}
public void deleteJoinedLabelsForBoard(Long localBoardId) {
db.getJoinBoardWithLabelDao().deleteByBoardId(localBoardId);
}
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);
relation.setMemberId(localGroupMemberId);
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);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.LABEL, label.getLocalId());
}
public void deleteLabel(Label label, boolean setStatus) {
markAsDeletedIfNeeded(label, setStatus);
db.getLabelDao().update(label);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.LABEL, label.getLocalId());
}
public void deleteLabelPhysically(Label label) {
db.getLabelDao().delete(label);
}
public WrappedLiveData<Account> createAccount(Account account) {
return LiveDataHelper.wrapInLiveData(() -> {
long id = db.getAccountDao().insert(account);
new Thread(() -> {
DeckLog.verbose("Adding new created", Account.class.getSimpleName(), " with ", id, " to all instances of ", EWidgetType.UPCOMING_WIDGET.name());
for (FilterWidget widget : getFilterWidgetsByType(EWidgetType.UPCOMING_WIDGET)) {
widget.getAccounts().add(new FilterWidgetAccount(id, false));
updateFilterWidgetDirectly(widget);
}
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.ACCOUNT, id);
}).start();
return readAccountDirectly(id);
});
}
public void deleteAccount(long id) {
db.getAccountDao().deleteById(id);
notifyAllWidgets();
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.ACCOUNT, id);
}
public void updateAccount(Account account) {
db.getAccountDao().update(account);
}
public LiveData<Account> readAccount(long id) {
return distinctUntilChanged(db.getAccountDao().getAccountById(id));
}
public LiveData<Account> readAccount(String name) {
return LiveDataHelper.wrapInLiveData(() -> db.getAccountDao().getAccountByNameDirectly(name));
// return distinctUntilChanged(db.getAccountDao().getAccountByName(name));
}
@WorkerThread
public Account readAccountDirectly(long id) {
return db.getAccountDao().getAccountByIdDirectly(id);
}
public LiveData<List<Account>> readAccounts() {
return distinctUntilChanged(db.getAccountDao().getAllAccounts());
}
public LiveData<List<Board>> getBoards(long accountId) {
return distinctUntilChanged(db.getBoardDao().getBoardsForAccount(accountId));
}
public LiveData<List<Board>> getBoards(long accountId, boolean archived) {
return distinctUntilChanged(
archived
? db.getBoardDao().getArchivedBoardsForAccount(accountId)
: db.getBoardDao().getNonArchivedBoardsForAccount(accountId));
}
public LiveData<List<Board>> getBoardsWithEditPermission(long accountId) {
return distinctUntilChanged(db.getBoardDao().getBoardsWithEditPermissionsForAccount(accountId));
}
public WrappedLiveData<Board> createBoard(long accountId, @NonNull Board board) {
return LiveDataHelper.wrapInLiveData(() -> {
board.setAccountId(accountId);
long id = db.getBoardDao().insert(board);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.BOARD, id);
return db.getBoardDao().getBoardByLocalIdDirectly(id);
});
}
@WorkerThread
public long createBoardDirectly(long accountId, @NonNull Board board) {
board.setAccountId(accountId);
long id = db.getBoardDao().insert(board);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.BOARD, id);
return id;
}
public void deleteBoard(Board board, boolean setStatus) {
markAsDeletedIfNeeded(board, setStatus);
db.getBoardDao().update(board);
notifyAllWidgets();
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.BOARD, board.getLocalId());
}
public void deleteBoardPhysically(Board board) {
db.getBoardDao().delete(board);
notifyAllWidgets();
}
public void updateBoard(Board board, boolean setStatus) {
markAsEditedIfNeeded(board, setStatus);
db.getBoardDao().update(board);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.BOARD, board.getLocalId());
}
public LiveData<List<Stack>> getStacksForBoard(long accountId, long localBoardId) {
return distinctUntilChanged(db.getStackDao().getStacksForBoard(accountId, localBoardId));
}
@WorkerThread
public List<FullStack> getFullStacksForBoardDirectly(long accountId, long localBoardId) {
return db.getStackDao().getFullStacksForBoardDirectly(accountId, localBoardId);
}
@MainThread
public LiveData<FullStack> getStack(long accountId, long localStackId) {
return distinctUntilChanged(db.getStackDao().getFullStack(accountId, localStackId));
}
@WorkerThread
public long createStack(long accountId, Stack stack) {
stack.setAccountId(accountId);
long id = db.getStackDao().insert(stack);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.STACK, id);
return id;
}
@WorkerThread
public void deleteStack(Stack stack, boolean setStatus) {
markAsDeletedIfNeeded(stack, setStatus);
db.getStackDao().update(stack);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.STACK, stack.getLocalId());
notifyAllWidgets();
}
@WorkerThread
public void deleteStackPhysically(Stack stack) {
db.getStackDao().delete(stack);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.STACK, stack.getLocalId());
notifyAllWidgets();
}
@WorkerThread
public void updateStack(Stack stack, boolean setStatus) {
markAsEditedIfNeeded(stack, setStatus);
db.getStackDao().update(stack);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.STACK, stack.getLocalId());
// if (db.getStackWidgetModelDao().containsStackLocalId(stack.getLocalId())) {
// DeckLog.info("Notifying " + StackWidget.class.getSimpleName() + " about card changes for \"" + stack.getTitle() + "\"");
// // FIXME StackWidget.notifyDatasetChanged(context);
// }
}
@WorkerThread
public Card getCardByLocalIdDirectly(long accountId, long localCardId) {
return db.getCardDao().getCardByLocalIdDirectly(accountId, localCardId);
}
@AnyThread
public LiveData<FullCard> getCardByLocalId(long accountId, long localCardId) {
return LiveDataHelper.interceptLiveData(db.getCardDao().getFullCardByLocalId(accountId, localCardId), this::filterRelationsForCard);
}
@AnyThread
public LiveData<FullCardWithProjects> getCardWithProjectsByLocalId(long accountId, long localCardId) {
return LiveDataHelper.interceptLiveData(db.getCardDao().getFullCardWithProjectsByLocalId(accountId, localCardId), this::filterRelationsForCard);
}
@WorkerThread
public List<FullCard> getLocallyChangedCardsDirectly(long accountId) {
return db.getCardDao().getLocallyChangedCardsDirectly(accountId);
}
@WorkerThread
public List<FullCard> getLocallyChangedCardsByLocalStackIdDirectly(long accountId, long localStackId) {
return db.getCardDao().getLocallyChangedCardsByLocalStackIdDirectly(accountId, localStackId);
}
@WorkerThread
public long createCardDirectly(long accountId, Card card) {
card.setAccountId(accountId);
long newCardId = db.getCardDao().insert(card);
notifyStackWidgetsIfNeeded(card.getTitle(), card.getStackId());
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.STACK, card.getStackId());
return newCardId;
}
@WorkerThread
public int getHighestCardOrderInStack(long localStackId) {
return db.getCardDao().getHighestOrderInStack(localStackId);
}
@WorkerThread
public int getHighestStackOrderInBoard(long localBoardId) {
return db.getStackDao().getHighestStackOrderInBoard(localBoardId);
}
@WorkerThread
public void deleteCard(Card card, boolean setStatus) {
markAsDeletedIfNeeded(card, setStatus);
if (setStatus) {
db.getCardDao().update(card);
} else {
deleteCardPhysically(card);
}
notifyStackWidgetsIfNeeded(card.getTitle(), card.getStackId());
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.STACK, card.getStackId());
}
@WorkerThread
public void deleteCardPhysically(Card card) {
db.getCardDao().delete(card);
}
@WorkerThread
public void updateCard(@NonNull Card card, boolean setStatus) {
markAsEditedIfNeeded(card, setStatus);
Long originalStackLocalId = db.getCardDao().getLocalStackIdByLocalCardId(card.getLocalId());
db.getCardDao().update(card);
if (db.getSingleCardWidgetModelDao().containsCardLocalId(card.getLocalId())) {
DeckLog.info("Notifying", SingleCardWidget.class.getSimpleName(), "about card changes for", card.getTitle());
SingleCardWidget.notifyDatasetChanged(context);
}
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.STACK, originalStackLocalId);
notifyStackWidgetsIfNeeded(card.getTitle(), card.getStackId(), originalStackLocalId);
}
private void notifyStackWidgetsIfNeeded(String cardTitle, long... affectedStackIds) {
// if (db.getStackWidgetModelDao().containsStackLocalId(affectedStackIds)) {
// DeckLog.info("Notifying " + StackWidget.class.getSimpleName() + " about card changes for \"" + cardTitle + "\"");
// // FIXME StackWidget.notifyDatasetChanged(context);
// }
}
@WorkerThread
public long createAccessControl(long accountId, @NonNull AccessControl entity) {
entity.setAccountId(accountId);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.ACCOUNT, accountId);
return db.getAccessControlDao().insert(entity);
}
@WorkerThread
public AccessControl getAccessControlByRemoteIdDirectly(long accountId, Long id) {
return db.getAccessControlDao().getAccessControlByRemoteIdDirectly(accountId, id);
}
public LiveData<List<AccessControl>> getAccessControlByLocalBoardId(long accountId, Long localBoardId) {
return LiveDataHelper.interceptLiveData(db.getAccessControlDao().getAccessControlByLocalBoardId(accountId, localBoardId), this::readRelationsForACL);
}
public List<AccessControl> getAccessControlByLocalBoardIdDirectly(long accountId, Long localBoardId) {
return db.getAccessControlDao().getAccessControlByLocalBoardIdDirectly(accountId, localBoardId);
}
public void updateAccessControl(AccessControl entity, boolean setStatus) {
markAsEditedIfNeeded(entity, setStatus);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.ACCOUNT, entity.getAccountId());
db.getAccessControlDao().update(entity);
}
public void deleteAccessControl(AccessControl entity, boolean setStatus) {
markAsDeletedIfNeeded(entity, setStatus);
if (setStatus) {
db.getAccessControlDao().update(entity);
} else {
db.getAccessControlDao().delete(entity);
}
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.ACCOUNT, entity.getAccountId());
}
public LiveData<FullBoard> getFullBoardById(Long accountId, Long localId) {
return distinctUntilChanged(db.getBoardDao().getFullBoardById(accountId, localId));
}
@WorkerThread
public Board getBoardByLocalIdDirectly(long localId) {
return db.getBoardDao().getBoardByLocalIdDirectly(localId);
}
public LiveData<User> getUserByLocalId(long accountId, long localId) {
return db.getUserDao().getUserByLocalId(accountId, localId);
}
public LiveData<User> getUserByUid(long accountId, String uid) {
return db.getUserDao().getUserByUid(accountId, uid);
}
public LiveData<List<User>> getUsersForAccount(final long accountId) {
return db.getUserDao().getUsersForAccount(accountId);
}
public LiveData<List<User>> searchUserByUidOrDisplayName(final long accountId, final long boardId, final long notYetAssignedToLocalCardId, final String searchTerm) {
validateSearchTerm(searchTerm);
return db.getUserDao().searchUserByUidOrDisplayName(accountId, boardId, notYetAssignedToLocalCardId, "%" + searchTerm.trim() + "%");
}
public List<User> searchUserByUidOrDisplayNameForACLDirectly(final long accountId, final long notYetAssignedToACL, final String searchTerm) {
validateSearchTerm(searchTerm);
return db.getUserDao().searchUserByUidOrDisplayNameForACLDirectly(accountId, notYetAssignedToACL, "%" + searchTerm.trim() + "%");
}
public LiveData<List<Label>> searchNotYetAssignedLabelsByTitle(final long accountId, final long boardId, final long notYetAssignedToLocalCardId, String searchTerm) {
validateSearchTerm(searchTerm);
return db.getLabelDao().searchNotYetAssignedLabelsByTitle(accountId, boardId, notYetAssignedToLocalCardId, "%" + searchTerm.trim() + "%");
}
public LiveData<List<User>> findProposalsForUsersToAssign(final long accountId, long boardId, long notAssignedToLocalCardId, final int topX) {
return db.getUserDao().findProposalsForUsersToAssign(accountId, boardId, notAssignedToLocalCardId, topX);
}
public LiveData<List<User>> findProposalsForUsersToAssignForACL(final long accountId, long boardId, final int topX) {
return db.getUserDao().findProposalsForUsersToAssignForACL(accountId, boardId, topX);
}
public LiveData<List<Label>> findProposalsForLabelsToAssign(final long accountId, final long boardId, long notAssignedToLocalCardId) {
return db.getLabelDao().findProposalsForLabelsToAssign(accountId, boardId, notAssignedToLocalCardId);
}
@WorkerThread
public Attachment getAttachmentByRemoteIdDirectly(long accountId, Long id) {
return db.getAttachmentDao().getAttachmentByRemoteIdDirectly(accountId, id);
}
@WorkerThread
public Attachment getAttachmentByLocalIdDirectly(long accountId, Long id) {
return db.getAttachmentDao().getAttachmentByLocalIdDirectly(accountId, id);
}
@WorkerThread
public List<Attachment> getAttachmentsForLocalCardIdDirectly(long accountId, Long localCardId) {
return db.getAttachmentDao().getAttachmentsForLocalCardIdDirectly(accountId, localCardId);
}
@WorkerThread
public List<Attachment> getLocallyChangedAttachmentsByLocalCardIdDirectly(long accountId, Long localCardId) {
return db.getAttachmentDao().getLocallyChangedAttachmentsByLocalCardIdDirectly(accountId, localCardId);
}
@WorkerThread
public List<Attachment> getLocallyChangedAttachmentsDirectly(long accountId) {
return db.getAttachmentDao().getLocallyChangedAttachmentsDirectly(accountId);
}
@WorkerThread
public List<Attachment> getLocallyChangedAttachmentsForStackDirectly(long localStackId) {
return db.getAttachmentDao().getLocallyChangedAttachmentsForStackDirectly(localStackId);
}
public long createAttachment(long accountId, @NonNull Attachment attachment) {
attachment.setAccountId(accountId);
attachment.setCreatedAt(Instant.now());
return db.getAttachmentDao().insert(attachment);
}
public void updateAttachment(long accountId, @NonNull Attachment attachment, boolean setStatus) {
markAsEditedIfNeeded(attachment, setStatus);
attachment.setAccountId(accountId);
db.getAttachmentDao().update(attachment);
}
public void deleteAttachment(long accountId, Attachment attachment, boolean setStatus) {
attachment.setAccountId(accountId);
if (setStatus) {
markAsDeletedIfNeeded(attachment, setStatus);
db.getAttachmentDao().update(attachment);
} else {
db.getAttachmentDao().delete(attachment);
}
}
private void validateSearchTerm(@Nullable String searchTerm) {
if (searchTerm == null || searchTerm.trim().length() < 1) {
throw new IllegalArgumentException("please provide a proper search term! \"" + searchTerm + "\" doesn't seem right...");
}
}
@WorkerThread
public Account getAccountByIdDirectly(long accountId) {
return db.getAccountDao().getAccountByIdDirectly(accountId);
}
@WorkerThread
public List<Account> getAllAccountsDirectly() {
return db.getAccountDao().getAllAccountsDirectly();
}
@WorkerThread
public User getUserByLocalIdDirectly(long localUserId) {
return db.getUserDao().getUserByLocalIdDirectly(localUserId);
}
public void setStatusForJoinCardWithUser(long localCardId, long localUserId, int status) {
db.getJoinCardWithUserDao().setDbStatus(localCardId, localUserId, status);
}
public void setStatusForJoinCardWithLabel(long localCardId, long localLabelId, int status) {
db.getJoinCardWithLabelDao().setDbStatus(localCardId, localLabelId, status);
}
@WorkerThread
public Label getLabelByLocalIdDirectly(long localLabelId) {
return db.getLabelDao().getLabelsByIdDirectly(localLabelId);
}
public LiveData<Label> getLabelByLocalId(long localLabelId) {
return db.getLabelDao().getLabelByLocalId(localLabelId);
}
public List<FullBoard> getLocallyChangedBoards(long accountId) {
return db.getBoardDao().getLocallyChangedBoardsDirectly(accountId);
}
public List<FullBoard> getAllFullBoards(long accountId) {
return db.getBoardDao().getAllFullBoards(accountId);
}
public List<FullStack> getLocallyChangedStacksForBoard(long accountId, long localBoardId) {
return db.getStackDao().getLocallyChangedStacksForBoardDirectly(accountId, localBoardId);
}
public List<FullStack> getLocallyChangedStacks(long accountId) {
return db.getStackDao().getLocallyChangedStacksDirectly(accountId);
}
public List<Label> getLocallyChangedLabels(long accountId) {
return db.getLabelDao().getLocallyChangedLabelsDirectly(accountId);
}
@WorkerThread
public Board getBoardByLocalCardIdDirectly(long localCardId) {
return db.getBoardDao().getBoardByLocalCardIdDirectly(localCardId);
}
@WorkerThread
public Long getBoardLocalIdByLocalCardIdDirectly(long localCardId) {
return db.getBoardDao().getBoardLocalIdByLocalCardIdDirectly(localCardId);
}
@WorkerThread
public FullBoard getFullBoardByLocalCardIdDirectly(long localCardId) {
return db.getBoardDao().getFullBoardByLocalCardIdDirectly(localCardId);
}
public JoinCardWithLabel getJoinCardWithLabel(Long localLabelId, Long localCardId) {
return db.getJoinCardWithLabelDao().getJoin(localLabelId, localCardId);
}
public JoinCardWithUser getJoinCardWithUser(Long localUserId, Long localCardId) {
return db.getJoinCardWithUserDao().getJoin(localUserId, localCardId);
}
public List<JoinCardWithLabel> getAllDeletedJoinsWithRemoteIDs() {
return db.getJoinCardWithLabelDao().getAllDeletedJoinsWithRemoteIDs();
}
public List<JoinCardWithLabel> getAllChangedLabelJoins() {
return db.getJoinCardWithLabelDao().getAllChangedJoins();
}
public List<JoinCardWithLabel> getAllChangedLabelJoinsForStack(Long localStackId) {
return db.getJoinCardWithLabelDao().getAllChangedJoinsForStack(localStackId);
}
public JoinCardWithLabel getAllChangedLabelJoinsWithRemoteIDs(Long localCardId, Long localLabelId) {
return db.getJoinCardWithLabelDao().getRemoteIdsForJoin(localCardId, localLabelId);
}
public List<JoinCardWithUser> getAllChangedUserJoinsWithRemoteIDs() {
return db.getJoinCardWithUserDao().getChangedJoinsWithRemoteIDs();
}
public List<JoinCardWithUser> getAllChangedUserJoinsWithRemoteIDsForStack(Long localStackId) {
return db.getJoinCardWithUserDao().getChangedJoinsWithRemoteIDsForStack(localStackId);
}
public void deleteJoinedLabelForCardPhysicallyByRemoteIDs(Long accountId, Long remoteCardId, Long remoteLabelId) {
db.getJoinCardWithLabelDao().deleteJoinedLabelForCardPhysicallyByRemoteIDs(accountId, remoteCardId, remoteLabelId);
}
public void deleteJoinedUserForCardPhysicallyByRemoteIDs(Long accountId, Long remoteCardId, String userUid) {
db.getJoinCardWithUserDao().deleteJoinedUserForCardPhysicallyByRemoteIDs(accountId, remoteCardId, userUid);
}
public LiveData<List<Activity>> getActivitiesForCard(Long localCardId) {
return db.getActivityDao().getActivitiesForCard(localCardId);
}
public long createActivity(long accountId, Activity activity) {
activity.setAccountId(accountId);
return db.getActivityDao().insert(activity);
}
@WorkerThread
public Activity getActivityByRemoteIdDirectly(long accountId, long remoteActivityId) {
return db.getActivityDao().getActivityByRemoteIdDirectly(accountId, remoteActivityId);
}
public void updateActivity(Activity activity, boolean setStatus) {
markAsEditedIfNeeded(activity, setStatus);
db.getActivityDao().update(activity);
}
public void deleteActivity(Activity activity) {
db.getActivityDao().delete(activity);
}
public List<AccessControl> getLocallyChangedAccessControl(long accountId, long boardId) {
return db.getAccessControlDao().getLocallyChangedAccessControl(accountId, boardId);
}
public List<Long> getBoardIDsOfLocallyChangedAccessControl(long accountId) {
return db.getAccessControlDao().getBoardIDsOfLocallyChangedAccessControl(accountId);
}
public LiveData<List<DeckComment>> getCommentsForLocalCardId(long localCardId) {
return LiveDataHelper.interceptLiveData(db.getCommentDao().getCommentByLocalCardId(localCardId), (list) -> {
for (DeckComment deckComment : list) {
deckComment.setMentions(db.getMentionDao().getMentionsForCommentIdDirectly(deckComment.getLocalId()));
}
});
}
public LiveData<List<FullDeckComment>> getFullCommentsForLocalCardId(long localCardId) {
return LiveDataHelper.interceptLiveData(db.getCommentDao().getFullCommentByLocalCardId(localCardId), (list) -> {
for (FullDeckComment deckComment : list) {
deckComment.getComment().setMentions(db.getMentionDao().getMentionsForCommentIdDirectly(deckComment.getLocalId()));
if (deckComment.getParent() != null) {
deckComment.getParent().setMentions(db.getMentionDao().getMentionsForCommentIdDirectly(deckComment.getComment().getParentId()));
}
}
});
}
@WorkerThread
public DeckComment getCommentByRemoteIdDirectly(long accountId, Long remoteCommentId) {
return db.getCommentDao().getCommentByRemoteIdDirectly(accountId, remoteCommentId);
}
@WorkerThread
public DeckComment getCommentByLocalIdDirectly(long accountId, Long localCommentId) {
return db.getCommentDao().getCommentByLocalIdDirectly(accountId, localCommentId);
}
public long createComment(long accountId, DeckComment comment) {
comment.setAccountId(accountId);
return db.getCommentDao().insert(comment);
}
public void updateComment(DeckComment comment, boolean setStatus) {
markAsEditedIfNeeded(comment, setStatus);
db.getCommentDao().update(comment);
}
public void deleteComment(DeckComment comment, boolean setStatus) {
markAsDeletedIfNeeded(comment, setStatus);
if (setStatus) {
db.getCommentDao().update(comment);
} else {
db.getCommentDao().delete(comment);
}
}
@WorkerThread
public List<DeckComment> getLocallyChangedCommentsByLocalCardIdDirectly(long accountId, long localCardId) {
return db.getCommentDao().getLocallyChangedCommentsByLocalCardIdDirectly(accountId, localCardId);
}
public void clearMentionsForCommentId(long commentID) {
db.getMentionDao().clearMentionsForCommentId(commentID);
}
public long createMention(Mention mention) {
return db.getMentionDao().insert(mention);
}
@WorkerThread
public List<DeckComment> getCommentByLocalCardIdDirectly(Long localCardId) {
return db.getCommentDao().getCommentByLocalCardIdDirectly(localCardId);
}
@WorkerThread
public List<Card> getCardsWithLocallyChangedCommentsDirectly(Long accountId) {
return db.getCardDao().getCardsWithLocallyChangedCommentsDirectly(accountId);
}
@WorkerThread
public List<Card> getCardsWithLocallyChangedCommentsForStackDirectly(Long localStackId) {
return db.getCardDao().getCardsWithLocallyChangedCommentsForStackDirectly(localStackId);
}
@WorkerThread
public Long getLocalStackIdByRemoteStackIdDirectly(long accountId, Long stackId) {
return db.getStackDao().getLocalStackIdByRemoteStackIdDirectly(accountId, stackId);
}
public LiveData<Long> getLocalBoardIdByCardRemoteIdAndAccountId(long cardRemoteId, long accountId) {
return db.getBoardDao().getLocalBoardIdByCardRemoteIdAndAccountId(cardRemoteId, accountId);
}
public LiveData<Integer> countCardsInStack(long accountId, long localStackId) {
return db.getCardDao().countCardsInStack(accountId, localStackId);
}
public LiveData<Integer> countCardsWithLabel(long localLabelId) {
return db.getJoinCardWithLabelDao().countCardsWithLabel(localLabelId);
}
@WorkerThread
public Label getLabelByBoardIdAndTitleDirectly(long boardId, String title) {
return db.getLabelDao().getLabelByBoardIdAndTitleDirectly(boardId, title);
}
public LiveData<List<FullBoard>> getFullBoards(long accountId, boolean archived) {
return db.getBoardDao().getArchivedFullBoards(accountId, (archived ? 1 : 0));
}
public LiveData<Boolean> hasArchivedBoards(long accountId) {
return LiveDataHelper.postCustomValue(distinctUntilChanged(db.getBoardDao().countArchivedBoards(accountId)), data -> data != null && data > 0);
}
@WorkerThread
public Long getRemoteCommentIdForLocalIdDirectly(Long localCommentId) {
return db.getCommentDao().getRemoteCommentIdForLocalIdDirectly(localCommentId);
}
@WorkerThread
public Long getLocalCommentIdForRemoteIdDirectly(long accountId, Long remoteCommentId) {
return db.getCommentDao().getLocalCommentIdForRemoteIdDirectly(accountId, remoteCommentId);
}
// -------------------
// Widgets
// -------------------
@WorkerThread
public long createSingleCardWidget(int widgetId, long accountId, long boardLocalId, long cardLocalId) {
SingleCardWidgetModel model = new SingleCardWidgetModel();
model.setWidgetId(widgetId);
model.setAccountId(accountId);
model.setBoardId(boardLocalId);
model.setCardId(cardLocalId);
return db.getSingleCardWidgetModelDao().insert(model);
}
public FullSingleCardWidgetModel getFullSingleCardWidgetModel(int widgetId) {
FullSingleCardWidgetModel model = db.getSingleCardWidgetModelDao().getFullCardByRemoteIdDirectly(widgetId);
if (model != null) {
model.setFullCard(db.getCardDao().getFullCardByLocalIdDirectly(model.getAccount().getId(), model.getModel().getCardId()));
}
return model;
}
public void deleteSingleCardWidget(int widgetId) {
SingleCardWidgetModel model = new SingleCardWidgetModel();
model.setWidgetId(widgetId);
db.getSingleCardWidgetModelDao().delete(model);
}
public void createStackWidget(int appWidgetId, long accountId, long stackId, boolean darkTheme) {
StackWidgetModel model = new StackWidgetModel();
model.setAppWidgetId(appWidgetId);
model.setAccountId(accountId);
model.setStackId(stackId);
model.setDarkTheme(darkTheme);
// db.getStackWidgetModelDao().insert(model);
}
public StackWidgetModel getStackWidgetModelDirectly(int appWidgetId) {
// return db.getStackWidgetModelDao().getStackWidgetByAppWidgetIdDirectly(appWidgetId);
return null;
}
public int createFilterWidgetDirectly(@NonNull FilterWidget filterWidget) {
db.getFilterWidgetDao().insert(filterWidget);
insertFilterWidgetDecendants(filterWidget);
return filterWidget.getId();
}
private void insertFilterWidgetDecendants(FilterWidget filterWidget) {
long widgetId = filterWidget.getId();
for (FilterWidgetAccount account : filterWidget.getAccounts()) {
account.setFilterWidgetId(widgetId);
long accountId = db.getFilterWidgetAccountDao().insert(account);
for (FilterWidgetUser user : account.getUsers()) {
user.setFilterAccountId(accountId);
db.getFilterWidgetUserDao().insert(user);
}
for (FilterWidgetProject project : account.getProjects()) {
project.setFilterAccountId(accountId);
db.getFilterWidgetProjectDao().insert(project);
}
for (FilterWidgetBoard board : account.getBoards()) {
board.setFilterAccountId(accountId);
long boardId = db.getFilterWidgetBoardDao().insert(board);
for (FilterWidgetStack stack : board.getStacks()) {
stack.setFilterBoardId(boardId);
db.getFilterWidgetStackDao().insert(stack);
}
for (FilterWidgetLabel label : board.getLabels()) {
label.setFilterBoardId(boardId);
db.getFilterWidgetLabelDao().insert(label);
}
}
}
for (FilterWidgetSort sort : filterWidget.getSorts()) {
sort.setFilterWidgetId(widgetId);
db.getFilterWidgetSortDao().insert(sort);
}
}
public void deleteFilterWidgetDirectly(Integer filterWidgetId) {
db.getFilterWidgetDao().delete(filterWidgetId);
}
public void updateFilterWidgetDirectly(FilterWidget filterWidget) {
db.getFilterWidgetSortDao().deleteByFilterWidgetId(filterWidget.getId());
db.getFilterWidgetAccountDao().deleteByFilterWidgetId(filterWidget.getId());
db.getFilterWidgetDao().update(filterWidget);
insertFilterWidgetDecendants(filterWidget);
}
public FilterWidget getFilterWidgetByIdDirectly(Integer filterWidgetId) {
FilterWidget filterWidget = db.getFilterWidgetDao().getFilterWidgetByIdDirectly(filterWidgetId);
if (filterWidget == null) {
throw new NoSuchElementException("No widget with id " + filterWidgetId + " configured.");
}
filterWidget.setSorts(db.getFilterWidgetSortDao().getFilterWidgetSortByFilterWidgetIdDirectly(filterWidgetId));
filterWidget.setAccounts(db.getFilterWidgetAccountDao().getFilterWidgetAccountsByFilterWidgetIdDirectly(filterWidgetId));
for (FilterWidgetAccount account : filterWidget.getAccounts()) {
account.setBoards(db.getFilterWidgetBoardDao().getFilterWidgetBoardsByFilterWidgetAccountIdDirectly(account.getId()));
account.setUsers(db.getFilterWidgetUserDao().getFilterWidgetUsersByFilterWidgetAccountIdDirectly(account.getId()));
account.setProjects(db.getFilterWidgetProjectDao().getFilterWidgetProjectsByFilterWidgetAccountIdDirectly(account.getId()));
for (FilterWidgetBoard board : account.getBoards()) {
board.setLabels(db.getFilterWidgetLabelDao().getFilterWidgetLabelsByFilterWidgetBoardIdDirectly(board.getId()));
board.setStacks(db.getFilterWidgetStackDao().getFilterWidgetStacksByFilterWidgetBoardIdDirectly(board.getId()));
}
}
return filterWidget;
}
public void notifyFilterWidgetsAboutChangedEntity(@NonNull FilterWidget.EChangedEntityType type, Long entityId) {
new Thread(() -> {
final List<EWidgetType> widgetTypesToNotify = db.getFilterWidgetDao().getChangedListTypesByEntity(type.toString(), entityId);
for (EWidgetType t : widgetTypesToNotify) {
DeckLog.info("Notifying", t.getWidgetClass().getSimpleName(), "about entity change:", type.name(), "with ID", entityId);
context.sendBroadcast(new Intent(context, t.getWidgetClass()).setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE));
}
}).start();
}
public LiveData<List<UpcomingCardsAdapterItem>> getCardsForUpcomingCard() {
LiveData<List<FullCard>> upcomingCardsLiveData = db.getCardDao().getUpcomingCards();
return LiveDataHelper.postCustomValue(upcomingCardsLiveData, this::cardResultsToUpcomingCardsAdapterItems);
}
public List<UpcomingCardsAdapterItem> getCardsForUpcomingCardForWidget() {
return cardResultsToUpcomingCardsAdapterItems(db.getCardDao().getUpcomingCardsDirectly());
}
@NotNull
private List<UpcomingCardsAdapterItem> cardResultsToUpcomingCardsAdapterItems(List<FullCard> cardsResult) {
filterRelationsForCard(cardsResult);
List<UpcomingCardsAdapterItem> result = new ArrayList<>(cardsResult.size());
Map<Long, Account> accountCache = new HashMap<>();
for (FullCard fullCard : cardsResult) {
Board board = db.getBoardDao().getBoardByLocalCardIdDirectly(fullCard.getLocalId());
Account account = accountCache.get(fullCard.getAccountId());
if (account == null) {
account = db.getAccountDao().getAccountByIdDirectly(fullCard.getAccountId());
accountCache.put(fullCard.getAccountId(), account);
}
result.add(new UpcomingCardsAdapterItem(fullCard, account, board.getLocalId(), board.getId(), board.isPermissionEdit()));
}
return result;
}
public List<FilterWidgetCard> getCardsForFilterWidget(@NonNull Integer filterWidgetId) {
FilterWidget filterWidget = getFilterWidgetByIdDirectly(filterWidgetId);
FilterInformation filter = new FilterInformation();
Set<FullCard> cardsResult = new HashSet<>();
if (filterWidget.getDueType() != null) {
filter.setDueType(filterWidget.getDueType());
} else filter.setDueType(EDueType.NO_FILTER);
if (filterWidget.getAccounts().isEmpty()) {
cardsResult.addAll(db.getCardDao().getFilteredFullCardsForStackDirectly(getQueryForFilter(filter, null, null)));
} else {
for (FilterWidgetAccount account : filterWidget.getAccounts()) {
filter.setNoAssignedUser(account.isIncludeNoUser());
List<User> users = new ArrayList<>();
if (!account.getUsers().isEmpty()) {
for (FilterWidgetUser user : account.getUsers()) {
User u = new User();
u.setLocalId(user.getUserId());
users.add(u);
}
}
filter.setUsers(users);
filter.setNoAssignedProject(account.isIncludeNoProject());
List<OcsProject> projects = new ArrayList<>();
if (!account.getProjects().isEmpty()) {
for (FilterWidgetProject project : account.getProjects()) {
OcsProject u = new OcsProject();
u.setLocalId(project.getProjectId());
projects.add(u);
}
}
filter.setProjects(projects);
if (!account.getBoards().isEmpty()) {
for (FilterWidgetBoard board : account.getBoards()) {
filter.setNoAssignedLabel(board.isIncludeNoLabel());
List<Long> stacks;
for (FilterWidgetLabel label : board.getLabels()) {
Label l = new Label();
l.setLocalId(label.getLabelId());
filter.addLabel(l);
}
if (board.getStacks().isEmpty()) {
stacks = db.getStackDao().getLocalStackIdsByLocalBoardIdDirectly(board.getBoardId());
} else {
stacks = new ArrayList<>();
for (FilterWidgetStack stack : board.getStacks()) {
stacks.add(stack.getStackId());
}
}
cardsResult.addAll(db.getCardDao().getFilteredFullCardsForStackDirectly(getQueryForFilter(filter, Collections.singletonList(account.getAccountId()), stacks)));
}
} else {
cardsResult.addAll(db.getCardDao().getFilteredFullCardsForStackDirectly(getQueryForFilter(filter, Collections.singletonList(account.getAccountId()), null)));
}
}
}
handleWidgetTypeExtras(filterWidget, cardsResult);
filterRelationsForCard(cardsResult);
List<FilterWidgetCard> result = new ArrayList<>(cardsResult.size());
Map<Long, Board> boardCache = new HashMap<>();
Map<Long, Stack> stackCache = new HashMap<>();
for (FullCard fullCard : cardsResult) {
Long stackId = fullCard.getCard().getStackId();
Stack stack = stackCache.get(stackId);
if (stack == null) {
stack = db.getStackDao().getStackByLocalIdDirectly(stackId);
stackCache.put(stackId, stack);
}
Board board = boardCache.get(stack.getBoardId());
if (board == null) {
board = db.getBoardDao().getBoardByLocalIdDirectly(stackId);
boardCache.put(stackId, board);
}
result.add(new FilterWidgetCard(fullCard, stack, board));
}
return result;
}
private void handleWidgetTypeExtras(FilterWidget filterWidget, Collection<FullCard> cardsResult) {
if (filterWidget.getWidgetType() == EWidgetType.UPCOMING_WIDGET) {
// https://github.com/stefan-niedermann/nextcloud-deck/issues/819 "no due" cards are only shown if they are on a shared board
for (FullCard fullCard : new ArrayList<>(cardsResult)) {
if (fullCard.getCard().getDueDate() == null && !db.getStackDao().isStackOnSharedBoardDirectly(fullCard.getCard().getStackId())) {
cardsResult.remove(fullCard);
}
}
List<Long> accountIds = null;
if (!filterWidget.getAccounts().isEmpty()) {
accountIds = filterWidget.getAccounts().stream().map(FilterWidgetAccount::getAccountId).collect(Collectors.toList());
}
// https://github.com/stefan-niedermann/nextcloud-deck/issues/822 exclude archived cards and boards
List<Long> archivedStacks = db.getStackDao().getLocalStackIdsInArchivedBoardsByAccountIdsDirectly(accountIds);
for (Long archivedStack : archivedStacks) {
List<FullCard> archivedCards = cardsResult.stream()
.filter(c -> c.getCard().isArchived() || archivedStack.equals(c.getCard().getStackId()))
.collect(Collectors.toList());
cardsResult.removeAll(archivedCards);
}
// https://github.com/stefan-niedermann/nextcloud-deck/issues/800 all cards within non-shared boards need to be included
cardsResult.addAll(db.getCardDao().getFullCardsForNonSharedBoardsWithDueDateForUpcomingCardsWidgetDirectly(accountIds));
}
}
public List<FilterWidget> getFilterWidgetsByType(EWidgetType type) {
List<Integer> ids = db.getFilterWidgetDao().getFilterWidgetIdsByType(type.getId());
List<FilterWidget> widgets = new ArrayList<>(ids.size());
for (Integer id : ids) {
widgets.add(getFilterWidgetByIdDirectly(id));
}
return widgets;
}
public boolean filterWidgetExists(int id) {
return db.getFilterWidgetDao().filterWidgetExists(id);
}
public void deleteStackWidget(int appWidgetId) {
StackWidgetModel model = new StackWidgetModel();
model.setAppWidgetId(appWidgetId);
// db.getStackWidgetModelDao().delete(model);
}
public LiveData<List<Account>> readAccountsForHostWithReadAccessToBoard(String host, long boardRemoteId) {
return db.getAccountDao().readAccountsForHostWithReadAccessToBoard("%" + host + "%", boardRemoteId);
}
public List<Account> readAccountsForHostWithReadAccessToBoardDirectly(String host, long boardRemoteId) {
return db.getAccountDao().readAccountsForHostWithReadAccessToBoardDirectly("%" + host + "%", boardRemoteId);
}
public Board getBoardForAccountByNameDirectly(long account, String title) {
return db.getBoardDao().getBoardForAccountByNameDirectly(account, title);
}
public OcsProject getProjectByRemoteIdDirectly(long accountId, Long remoteId) {
return db.getOcsProjectDao().getProjectByRemoteIdDirectly(accountId, remoteId);
}
public Long createProjectDirectly(long accountId, OcsProject entity) {
entity.setAccountId(accountId);
return db.getOcsProjectDao().insert(entity);
}
public void deleteProjectResourcesForProjectIdDirectly(Long localProjectId) {
db.getOcsProjectResourceDao().deleteByProjectId(localProjectId);
}
public void updateProjectDirectly(long accountId, OcsProject entity) {
entity.setAccountId(accountId);
db.getOcsProjectDao().update(entity);
}
public void deleteProjectDirectly(OcsProject ocsProject) {
db.getOcsProjectResourceDao().deleteByProjectId(ocsProject.getLocalId());
db.getOcsProjectDao().delete(ocsProject);
}
public Long createProjectResourceDirectly(Long accountId, OcsProjectResource resource) {
resource.setAccountId(accountId);
return db.getOcsProjectResourceDao().insert(resource);
}
public int countProjectResourcesInProjectDirectly(Long projectLocalId) {
return db.getOcsProjectResourceDao().countProjectResourcesInProjectDirectly(projectLocalId);
}
public LiveData<Integer> countProjectResourcesInProject(Long projectLocalId) {
return db.getOcsProjectResourceDao().countProjectResourcesInProject(projectLocalId);
}
public LiveData<List<OcsProjectResource>> getResourcesByLocalProjectId(Long projectLocalId) {
return db.getOcsProjectResourceDao().getResourcesByLocalProjectId(projectLocalId);
}
public void assignCardToProjectIfMissng(Long accountId, Long localProjectId, Long remoteCardId) {
Card card = db.getCardDao().getCardByRemoteIdDirectly(accountId, remoteCardId);
if (card != null) {
JoinCardWithProject existing = db.getJoinCardWithOcsProjectDao().getAssignmentByCardIdAndProjectIdDirectly(card.getLocalId(), localProjectId);
if (existing == null) {
JoinCardWithProject assignment = new JoinCardWithProject();
assignment.setStatus(DBStatus.UP_TO_DATE.getId());
assignment.setCardId(card.getLocalId());
assignment.setProjectId(localProjectId);
long id = db.getJoinCardWithOcsProjectDao().insert(assignment);
notifyFilterWidgetsAboutChangedEntity(FilterWidget.EChangedEntityType.PROJECT, id);
}
}
}
private void notifyAllWidgets() {
SingleCardWidget.notifyDatasetChanged(context);
/// FIXME StackWidget.notifyDatasetChanged(context);
// UpcomingWidget.notifyDatasetChanged(context);
}
@ColorInt
public Integer getBoardColorDirectly(long accountId, long localBoardId) {
return db.getBoardDao().getBoardColorByLocalIdDirectly(accountId, localBoardId);
}
public void deleteProjectResourcesByCardIdExceptGivenProjectIdsDirectly(long accountId, Long localCardId, List<Long> remoteProjectIDs) {
db.getJoinCardWithOcsProjectDao().deleteProjectResourcesByCardIdExceptGivenProjectIdsDirectly(accountId, localCardId, remoteProjectIDs);
}
public void deleteProjectResourcesByCardIdDirectly(Long localCardId) {
db.getJoinCardWithOcsProjectDao().deleteProjectResourcesByCardIdDirectly(localCardId);
}
}
|