]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java
bfc2a562d156d0e795213b7aefc52af98fecbd6d
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TemplateHeaderDAO.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.dao;
19
20 import android.os.AsyncTask;
21 import android.os.Handler;
22 import android.os.Looper;
23
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.lifecycle.LiveData;
27 import androidx.lifecycle.Observer;
28 import androidx.room.Dao;
29 import androidx.room.Delete;
30 import androidx.room.Insert;
31 import androidx.room.Query;
32 import androidx.room.Transaction;
33 import androidx.room.Update;
34
35 import net.ktnx.mobileledger.db.DB;
36 import net.ktnx.mobileledger.db.TemplateAccount;
37 import net.ktnx.mobileledger.db.TemplateHeader;
38 import net.ktnx.mobileledger.db.TemplateWithAccounts;
39
40 import java.util.List;
41
42 @Dao
43 public abstract class TemplateHeaderDAO {
44     @Insert()
45     public abstract long insertSync(TemplateHeader item);
46
47     public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
48         AsyncTask.execute(() -> {
49             insertSync(item);
50             if (callback != null) {
51                 new Handler(Looper.getMainLooper()).post(callback);
52             }
53         });
54     }
55
56     @Update
57     public abstract void updateSync(TemplateHeader... items);
58
59     @Delete
60     public abstract void deleteSync(TemplateHeader item);
61
62     public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
63         AsyncTask.execute(() -> {
64             deleteSync(item);
65             new Handler(Looper.getMainLooper()).post(callback);
66         });
67     }
68
69     @Query("SELECT * FROM templates ORDER BY UPPER(name)")
70     public abstract LiveData<List<TemplateHeader>> getTemplates();
71
72     @Query("SELECT * FROM templates WHERE id = :id")
73     public abstract LiveData<TemplateHeader> getTemplate(Long id);
74
75     public void getTemplateAsync(@NonNull Long id,
76                                  @NonNull AsyncResultCallback<TemplateHeader> callback) {
77         LiveData<TemplateHeader> resultReceiver = getTemplate(id);
78         resultReceiver.observeForever(new Observer<TemplateHeader>() {
79             @Override
80             public void onChanged(TemplateHeader h) {
81                 if (h == null)
82                     return;
83
84                 resultReceiver.removeObserver(this);
85                 callback.onResult(h);
86             }
87         });
88     }
89
90     @Transaction
91     @Query("SELECT * FROM templates WHERE id = :id")
92     public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
93
94     @Transaction
95     @Query("SELECT * FROM templates WHERE id = :id")
96     public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
97
98     @Transaction
99     public void insertSync(TemplateWithAccounts templateWithAccounts) {
100         long template_id = insertSync(templateWithAccounts.header);
101         for (TemplateAccount acc : templateWithAccounts.accounts) {
102             acc.setTemplateId(template_id);
103             DB.get()
104               .getTemplateAccountDAO()
105               .insertSync(acc);
106         }
107     }
108
109     public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
110             AsyncResultCallback<TemplateWithAccounts> callback) {
111         LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
112         resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
113             @Override
114             public void onChanged(TemplateWithAccounts result) {
115                 if (result == null)
116                     return;
117
118                 resultReceiver.removeObserver(this);
119                 callback.onResult(result);
120             }
121         });
122     }
123     public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
124         AsyncTask.execute(() -> {
125             insertSync(item);
126             if (callback != null) {
127                 new Handler(Looper.getMainLooper()).post(callback);
128             }
129         });
130     }
131     public void duplicateTemplateWitAccounts(@NonNull Long id, @Nullable
132             AsyncResultCallback<TemplateWithAccounts> callback) {
133         AsyncTask.execute(() -> {
134             TemplateWithAccounts src = getTemplateWithAccountsSync(id);
135             TemplateWithAccounts dup = src.createDuplicate();
136             dup.header.setName(dup.header.getName());
137             dup.header.setId(insertSync(dup.header));
138             TemplateAccountDAO accDao = DB.get()
139                                           .getTemplateAccountDAO();
140             for (TemplateAccount dupAcc : dup.accounts) {
141                 dupAcc.setTemplateId(dup.header.getId());
142                 dupAcc.setId(accDao.insertSync(dupAcc));
143             }
144             if (callback != null)
145                 new Handler(Looper.getMainLooper()).post(() -> callback.onResult(dup));
146         });
147     }
148
149 }