]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java
fix search previous transactions by description
[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 androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22 import androidx.lifecycle.LiveData;
23 import androidx.lifecycle.Observer;
24 import androidx.room.Dao;
25 import androidx.room.Delete;
26 import androidx.room.Insert;
27 import androidx.room.Query;
28 import androidx.room.Transaction;
29 import androidx.room.Update;
30
31 import net.ktnx.mobileledger.db.DB;
32 import net.ktnx.mobileledger.db.TemplateAccount;
33 import net.ktnx.mobileledger.db.TemplateHeader;
34 import net.ktnx.mobileledger.db.TemplateWithAccounts;
35 import net.ktnx.mobileledger.utils.Misc;
36
37 import java.util.List;
38
39 @Dao
40 public abstract class TemplateHeaderDAO {
41     @Insert()
42     public abstract long insertSync(TemplateHeader item);
43
44     public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
45         BaseDAO.runAsync(() -> {
46             insertSync(item);
47             if (callback != null)
48                 Misc.onMainThread(callback);
49         });
50     }
51
52     @Update
53     public abstract void updateSync(TemplateHeader... items);
54
55     @Delete
56     public abstract void deleteSync(TemplateHeader item);
57
58     public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
59         BaseDAO.runAsync(() -> {
60             deleteSync(item);
61             Misc.onMainThread(callback);
62         });
63     }
64
65     @Query("SELECT * FROM templates ORDER BY is_fallback, UPPER(name)")
66     public abstract LiveData<List<TemplateHeader>> getTemplates();
67
68     @Query("SELECT * FROM templates WHERE id = :id")
69     public abstract LiveData<TemplateHeader> getTemplate(Long id);
70
71     @Query("SELECT * FROM templates WHERE id = :id")
72     public abstract TemplateHeader getTemplateSync(Long id);
73
74     public void getTemplateAsync(@NonNull Long id,
75                                  @NonNull AsyncResultCallback<TemplateHeader> callback) {
76         LiveData<TemplateHeader> resultReceiver = getTemplate(id);
77         resultReceiver.observeForever(new Observer<TemplateHeader>() {
78             @Override
79             public void onChanged(TemplateHeader h) {
80                 if (h == null)
81                     return;
82
83                 resultReceiver.removeObserver(this);
84                 callback.onResult(h);
85             }
86         });
87     }
88
89     @Transaction
90     @Query("SELECT * FROM templates WHERE id = :id")
91     public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
92
93     @Transaction
94     @Query("SELECT * FROM templates WHERE id = :id")
95     public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
96
97     @Transaction
98     @Query("SELECT * FROM templates WHERE uuid = :uuid")
99     public abstract TemplateWithAccounts getTemplateWithAccountsByUuidSync(String uuid);
100
101     @Transaction
102     @Query("SELECT * FROM templates")
103     public abstract List<TemplateWithAccounts> getAllTemplatesWithAccountsSync();
104
105     @Transaction
106     public void insertSync(TemplateWithAccounts templateWithAccounts) {
107         long template_id = insertSync(templateWithAccounts.header);
108         for (TemplateAccount acc : templateWithAccounts.accounts) {
109             acc.setTemplateId(template_id);
110             DB.get()
111               .getTemplateAccountDAO()
112               .insertSync(acc);
113         }
114     }
115
116     public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
117             AsyncResultCallback<TemplateWithAccounts> callback) {
118         LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
119         resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
120             @Override
121             public void onChanged(TemplateWithAccounts result) {
122                 if (result == null)
123                     return;
124
125                 resultReceiver.removeObserver(this);
126                 callback.onResult(result);
127             }
128         });
129     }
130     public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
131         BaseDAO.runAsync(() -> {
132             insertSync(item);
133             if (callback != null)
134                 Misc.onMainThread(callback);
135         });
136     }
137     public void duplicateTemplateWitAccounts(@NonNull Long id, @Nullable
138             AsyncResultCallback<TemplateWithAccounts> callback) {
139         BaseDAO.runAsync(() -> {
140             TemplateWithAccounts src = getTemplateWithAccountsSync(id);
141             TemplateWithAccounts dup = src.createDuplicate();
142             dup.header.setName(dup.header.getName());
143             dup.header.setId(insertSync(dup.header));
144             TemplateAccountDAO accDao = DB.get()
145                                           .getTemplateAccountDAO();
146             for (TemplateAccount dupAcc : dup.accounts) {
147                 dupAcc.setTemplateId(dup.header.getId());
148                 dupAcc.setId(accDao.insertSync(dupAcc));
149             }
150             if (callback != null)
151                 Misc.onMainThread(() -> callback.onResult(dup));
152         });
153     }
154
155 }