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 public void getTemplateAsync(@NonNull Long id,
74 @NonNull AsyncResultCallback<TemplateHeader> callback) {
75 LiveData<TemplateHeader> resultReceiver = getTemplate(id);
76 resultReceiver.observeForever(new Observer<TemplateHeader>() {
78 public void onChanged(TemplateHeader h) {
82 resultReceiver.removeObserver(this);
89 @Query("SELECT * FROM templates WHERE id = :id")
90 public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
93 @Query("SELECT * FROM templates WHERE id = :id")
94 public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
97 public void insertSync(TemplateWithAccounts templateWithAccounts) {
98 long template_id = insertSync(templateWithAccounts.header);
99 for (TemplateAccount acc : templateWithAccounts.accounts) {
100 acc.setTemplateId(template_id);
102 .getTemplateAccountDAO()
107 public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
108 AsyncResultCallback<TemplateWithAccounts> callback) {
109 LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
110 resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
112 public void onChanged(TemplateWithAccounts result) {
116 resultReceiver.removeObserver(this);
117 callback.onResult(result);
121 public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
122 AsyncTask.execute(() -> {
124 if (callback != null)
125 Misc.onMainThread(callback);
128 public void duplicateTemplateWitAccounts(@NonNull Long id, @Nullable
129 AsyncResultCallback<TemplateWithAccounts> callback) {
130 AsyncTask.execute(() -> {
131 TemplateWithAccounts src = getTemplateWithAccountsSync(id);
132 TemplateWithAccounts dup = src.createDuplicate();
133 dup.header.setName(dup.header.getName());
134 dup.header.setId(insertSync(dup.header));
135 TemplateAccountDAO accDao = DB.get()
136 .getTemplateAccountDAO();
137 for (TemplateAccount dupAcc : dup.accounts) {
138 dupAcc.setTemplateId(dup.header.getId());
139 dupAcc.setId(accDao.insertSync(dupAcc));
141 if (callback != null)
142 Misc.onMainThread(() -> callback.onResult(dup));