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.
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.
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/>.
18 package net.ktnx.mobileledger.dao;
20 import android.os.AsyncTask;
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;
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;
39 import java.util.List;
42 public abstract class TemplateHeaderDAO {
44 public abstract long insertSync(TemplateHeader item);
46 public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
47 AsyncTask.execute(() -> {
50 Misc.onMainThread(callback);
55 public abstract void updateSync(TemplateHeader... items);
58 public abstract void deleteSync(TemplateHeader item);
60 public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
61 AsyncTask.execute(() -> {
63 Misc.onMainThread(callback);
67 @Query("SELECT * FROM templates ORDER BY is_fallback, UPPER(name)")
68 public abstract LiveData<List<TemplateHeader>> getTemplates();
70 @Query("SELECT * FROM templates WHERE id = :id")
71 public abstract LiveData<TemplateHeader> getTemplate(Long id);
73 @Query("SELECT * FROM templates WHERE id = :id")
74 public abstract TemplateHeader getTemplateSync(Long id);
76 public void getTemplateAsync(@NonNull Long id,
77 @NonNull AsyncResultCallback<TemplateHeader> callback) {
78 LiveData<TemplateHeader> resultReceiver = getTemplate(id);
79 resultReceiver.observeForever(new Observer<TemplateHeader>() {
81 public void onChanged(TemplateHeader h) {
85 resultReceiver.removeObserver(this);
92 @Query("SELECT * FROM templates WHERE id = :id")
93 public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
96 @Query("SELECT * FROM templates WHERE id = :id")
97 public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
100 @Query("SELECT * FROM templates WHERE uuid = :uuid")
101 public abstract TemplateWithAccounts getTemplateWithAccountsByUuidSync(String uuid);
104 @Query("SELECT * FROM templates")
105 public abstract List<TemplateWithAccounts> getAllTemplatesWithAccountsSync();
108 public void insertSync(TemplateWithAccounts templateWithAccounts) {
109 long template_id = insertSync(templateWithAccounts.header);
110 for (TemplateAccount acc : templateWithAccounts.accounts) {
111 acc.setTemplateId(template_id);
113 .getTemplateAccountDAO()
118 public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
119 AsyncResultCallback<TemplateWithAccounts> callback) {
120 LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
121 resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
123 public void onChanged(TemplateWithAccounts result) {
127 resultReceiver.removeObserver(this);
128 callback.onResult(result);
132 public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
133 AsyncTask.execute(() -> {
135 if (callback != null)
136 Misc.onMainThread(callback);
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));
152 if (callback != null)
153 Misc.onMainThread(() -> callback.onResult(dup));