Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DataBaseAdapter.java « db « adapters « sync « persistence « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96dcbed81ba2ffc4d443dd9279838f18ebadcadd (plain)
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
package it.niedermann.nextcloud.deck.persistence.sync.adapters.db;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.content.Context;

import java.util.List;

import it.niedermann.nextcloud.deck.model.Account;
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.JoinStackWithCard;
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.full.FullBoard;
import it.niedermann.nextcloud.deck.model.full.FullCard;
import it.niedermann.nextcloud.deck.model.full.FullStack;

public class DataBaseAdapter {


    private interface DataAccessor<T> {
        T getData();
    }
    private interface LiveDataWrapper<T> {
        void returnIntoLiveData(MutableLiveData<T> liveData);
    }

    private DeckDatabase db;

    public DataBaseAdapter(Context applicationContext) {
        this.db = DeckDatabase.getInstance(applicationContext);
    }

    public <T> LiveData<T> wrapInLiveData(final LiveDataWrapper<T> liveDataWrapper){
        final MutableLiveData<T> liveData = new MutableLiveData();

        new Thread(() -> liveDataWrapper.returnIntoLiveData(liveData)).start();

        return liveData;
    }

    public LiveData<Boolean> hasAccounts(){
        return wrapInLiveData((MutableLiveData<Boolean> liveData) -> liveData.postValue(db.getAccountDao().countAccounts() > 0));
    }

    
    public LiveData<Board> getBoard(long accountId, long remoteId) {
        return 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 LiveData<Stack> getStackByRemoteId(long accountId, long localBoardId, long remoteId) {
        return db.getStackDao().getStackByRemoteId(accountId, localBoardId, remoteId);
    }

    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 db.getCardDao().getCardByRemoteId(accountId, remoteId);
    }

    public FullCard getFullCardByRemoteIdDirectly(long accountId, long remoteId) {
        return db.getCardDao().getFullCardByRemoteIdDirectly(accountId, remoteId);
    }


    public Card getCardByRemoteIdDirectly(long accountId, long remoteId) {
        return db.getCardDao().getCardByRemoteIdDirectly(accountId, remoteId);
    }

    
    public LiveData<User> getUser(long accountId, long remoteId) {
        return db.getUserDao().getUsersByRemoteId(accountId, remoteId);
    }

    public LiveData<List<FullCard>> getFullCardsForStack(long accountId, long localStackId) {
        return db.getCardDao().getFullCardsForStack(accountId, localStackId);
    }

    public User getUserByRemoteIdDirectly(long accountId, long remoteId) {
        return db.getUserDao().getUserByRemoteIdDirectly(accountId, remoteId);
    }
    public User getUserByUidDirectly(long accountId, String uid) {
        return db.getUserDao().getUserByUidDirectly(accountId, uid);
    }

    
    public long createUser(long accountId, User user) {
        user.setAccountId(accountId);
        return db.getUserDao().insert(user);
    }

    
    public void updateUser(long accountId, User user) {
        user.setAccountId(accountId);
        db.getUserDao().update(user);
    }

    
    public LiveData<Label> getLabelByRemoteId(long accountId, long remoteId) {
        return db.getLabelDao().getLabelByRemoteId(accountId, remoteId);
    }
    public Label getLabelByRemoteIdDirectly(long accountId, long remoteId) {
        return db.getLabelDao().getLabelByRemoteIdDirectly(accountId, remoteId);
    }

    
    public long createLabel(long accountId, Label label) {
        label.setAccountId(accountId);
        return db.getLabelDao().insert(label);
    }

    
    public void createJoinCardWithLabel(long localLabelId, long localCardId) {
        JoinCardWithLabel join = new JoinCardWithLabel();
        join.setCardId(localCardId);
        join.setLabelId(localLabelId);
        db.getJoinCardWithLabelDao().insert(join);
    }

    
    public void deleteJoinedLabelsForCard(long localCardId) {
        db.getJoinCardWithLabelDao().deleteByCardId(localCardId);
    }

    
    public void createJoinCardWithUser(long localUserId, long localCardId) {
        JoinCardWithUser join = new JoinCardWithUser();
        join.setCardId(localCardId);
        join.setUserId(localUserId);
        db.getJoinCardWithUserDao().insert(join);
    }

    
    public void deleteJoinedUsersForCard(long localCardId) {
        db.getJoinCardWithUserDao().deleteByCardId(localCardId);
    }

    
    public void createJoinStackWithCard(long localCardId, long localStackId) {
        JoinStackWithCard join = new JoinStackWithCard();
        join.setCardId(localCardId);
        join.setStackId(localStackId);
        db.getJoinStackWithCardDao().insert(join);
    }

    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 deleteJoinedCardsForStack(long localStackId) {
        db.getJoinStackWithCardDao().deleteByStackId(localStackId);
    }
    public void deleteJoinedCardForStackById(long localCardId) {
        db.getJoinStackWithCardDao().deleteByCardId(localCardId);
    }

    
    public void updateLabel(Label label) {
        db.getLabelDao().update(label);
    }

    public void deleteLabel(Label label) {
        db.getLabelDao().delete(label);
    }

    
    public LiveData<Account> createAccount(String accoutName) {
        return wrapInLiveData((MutableLiveData<Account> liveData) -> {
            Account acc = new Account();
            acc.setName(accoutName);
            long id = db.getAccountDao().insert(acc);
            liveData.postValue(readAccountDirectly(id));
        });
    }

    
    public void deleteAccount(long id) {
        db.getAccountDao().deleteById(id);
    }

    
    public void updateAccount(Account account) {
        db.getAccountDao().update(account);
    }

    
    public LiveData<Account> readAccount(long id) {
        return db.getAccountDao().selectById(id);
    }

    public Account readAccountDirectly(long id) {
        return db.getAccountDao().selectByIdDirectly(id);
    }

    
    public LiveData<List<Account>> readAccounts() {
        return db.getAccountDao().selectAll();
    }

    
    public LiveData<List<Board>> getBoards(long accountId) {
        return db.getBoardDao().getBoardsForAccount(accountId);
    }

    public LiveData<Board> createBoard(long accountId, Board board) {
        return wrapInLiveData((MutableLiveData<Board> liveData) -> {
            board.setAccountId(accountId);
            long id = db.getBoardDao().insert(board);
            Board newBoard = db.getBoardDao().getBoardByIdDirectly(accountId, id);
            liveData.postValue(newBoard);
        });
    }

    
    public void deleteBoard(Board board) {
        db.getBoardDao().delete(board);
    }

    
    public void updateBoard(Board board) {
        db.getBoardDao().update(board);
    }

    
    public LiveData<List<FullStack>> getStacks(long accountId, long localBoardId) {
        return db.getStackDao().getFullStacksForBoard(accountId, localBoardId);
    }



    
    public LiveData<FullStack> getStack(long accountId, long localStackId) {
        return db.getStackDao().getFullStack(accountId, localStackId);
    }

    
    public long createStack(long accountId, Stack stack) {
        stack.setAccountId(accountId);
        return db.getStackDao().insert(stack);

    }

    
    public void deleteStack(Stack stack) {
        db.getStackDao().delete(stack);
    }

    
    public void updateStack(Stack stack) {
        db.getStackDao().update(stack);

    }

    
    public LiveData<FullCard>  getCardByLocalId(long accountId, long localCardId) {
        return db.getCardDao().getFullCardByLocalId(accountId, localCardId);
    }

    
    public long createCard(long accountId, Card card) {
        card.setAccountId(accountId);
        return db.getCardDao().insert(card);

    }

    
    public void deleteCard(Card card) {
        db.getCardDao().delete(card);
    }

    
    public void updateCard(Card card) {
        db.getCardDao().update(card);

    }


}