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