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 androidx.annotation.NonNull;
21 import androidx.lifecycle.LiveData;
22 import androidx.room.ColumnInfo;
23 import androidx.room.Dao;
24 import androidx.room.Delete;
25 import androidx.room.Insert;
26 import androidx.room.OnConflictStrategy;
27 import androidx.room.Query;
28 import androidx.room.Update;
30 import net.ktnx.mobileledger.db.DB;
31 import net.ktnx.mobileledger.db.Transaction;
32 import net.ktnx.mobileledger.db.TransactionAccount;
33 import net.ktnx.mobileledger.db.TransactionWithAccounts;
34 import net.ktnx.mobileledger.utils.Logger;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Locale;
41 public abstract class TransactionDAO extends BaseDAO<Transaction> {
42 static public List<String> unbox(List<DescriptionContainer> list) {
43 ArrayList<String> result = new ArrayList<>(list.size());
44 for (DescriptionContainer item : list) {
45 result.add(item.description);
50 @Insert(onConflict = OnConflictStrategy.REPLACE)
51 public abstract long insertSync(Transaction item);
54 public abstract void updateSync(Transaction item);
57 public abstract void deleteSync(Transaction item);
60 public abstract void deleteSync(Transaction... items);
63 public abstract void deleteSync(List<Transaction> items);
65 @Query("SELECT * FROM transactions WHERE id = :id")
66 public abstract LiveData<Transaction> getById(long id);
68 @androidx.room.Transaction
69 @Query("SELECT * FROM transactions WHERE id = :transactionId")
70 public abstract LiveData<TransactionWithAccounts> getByIdWithAccounts(long transactionId);
72 @androidx.room.Transaction
73 @Query("SELECT * FROM transactions WHERE id = :transactionId")
74 public abstract TransactionWithAccounts getByIdWithAccountsSync(long transactionId);
76 @Query("SELECT DISTINCT description, CASE WHEN description_upper LIKE :term||'%%' THEN 1 " +
77 " WHEN description_upper LIKE '%%:'||:term||'%%' THEN 2 " +
78 " WHEN description_upper LIKE '%% '||:term||'%%' THEN 3 " +
79 " ELSE 9 END AS ordering " + "FROM description_history " +
80 "WHERE description_upper LIKE '%%'||:term||'%%' " +
81 "ORDER BY ordering, description_upper, rowid ")
82 public abstract List<DescriptionContainer> lookupDescriptionSync(@NonNull String term);
84 @androidx.room.Transaction
85 @Query("SELECT * from transactions WHERE description = :description ORDER BY year desc, month" +
86 " desc, day desc LIMIT 1")
87 public abstract TransactionWithAccounts getFirstByDescriptionSync(@NonNull String description);
89 @androidx.room.Transaction
90 @Query("SELECT tr.id, tr.profile_id, tr.ledger_id, tr.description, tr.data_hash, tr.comment, " +
91 "tr.year, tr.month, tr.day, tr.generation from transactions tr JOIN " +
92 "transaction_accounts t_a ON t_a.transaction_id = tr.id WHERE tr.description = " +
93 ":description AND t_a.account_name LIKE '%'||:accountTerm||'%' ORDER BY year desc, " +
94 "month desc, day desc, tr.ledger_id desc LIMIT 1")
95 public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
96 @NonNull String description, @NonNull String accountTerm);
98 @Query("SELECT * from transactions WHERE profile_id = :profileId")
99 public abstract List<Transaction> getAllForProfileUnorderedSync(long profileId);
101 @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
102 protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
104 @androidx.room.Transaction
105 @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
106 public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
108 @androidx.room.Transaction
109 @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
110 " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
111 "transaction_accounts ta ON ta.transaction_id=tr.id WHERE ta.account_name LIKE " +
112 ":accountName||'%' AND ta.amount <> 0 AND tr.profile_id = :profileId ORDER BY tr.year " +
113 "desc, tr.month desc, tr.day desc, tr.ledger_id desc")
114 public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
117 public long getGenerationSync(long profileId) {
118 TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
122 return result.generation;
124 @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
125 ":currentGeneration")
126 public abstract int purgeOldTransactionsSync(long profileId, long currentGeneration);
128 @Query("DELETE FROM transaction_accounts WHERE EXISTS (SELECT 1 FROM transactions tr WHERE tr" +
129 ".id=transaction_accounts.transaction_id AND tr.profile_id=:profileId) AND generation " +
130 "<> :currentGeneration")
131 public abstract int purgeOldTransactionAccountsSync(long profileId, long currentGeneration);
132 public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
133 long generation = getGenerationSync(profileId) + 1;
135 for (TransactionWithAccounts tr : list) {
136 tr.transaction.setGeneration(generation);
137 tr.transaction.setProfileId(profileId);
142 Logger.debug("Transaction", "Purging old transactions");
143 int removed = purgeOldTransactionsSync(profileId, generation);
144 Logger.debug("Transaction", String.format(Locale.ROOT, "Purged %d transactions", removed));
146 removed = purgeOldTransactionAccountsSync(profileId, generation);
147 Logger.debug("Transaction",
148 String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
150 @Query("DELETE FROM transactions WHERE profile_id = :profileId")
151 public abstract int deleteAllSync(long profileId);
152 private void storeSync(TransactionWithAccounts rec) {
153 TransactionAccountDAO trAccDao = DB.get()
154 .getTransactionAccountDAO();
156 Transaction transaction = rec.transaction;
157 Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId());
158 if (existing != null) {
159 existing.copyDataFrom(transaction);
160 updateSync(existing);
161 transaction = existing;
164 transaction.setId(insertSync(transaction));
166 for (TransactionAccount trAcc : rec.accounts) {
167 trAcc.setTransactionId(transaction.getId());
168 trAcc.setGeneration(transaction.getGeneration());
169 TransactionAccount existingAcc =
170 trAccDao.getByOrderNoSync(trAcc.getTransactionId(), trAcc.getOrderNo());
171 if (existingAcc != null) {
172 existingAcc.copyDataFrom(trAcc);
173 trAccDao.updateSync(existingAcc);
176 trAcc.setId(trAccDao.insertSync(trAcc));
179 @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId")
180 public abstract Transaction getByLedgerId(long profileId, long ledgerId);
181 static class TransactionGenerationContainer {
184 public TransactionGenerationContainer(long generation) {
185 this.generation = generation;
189 static public class DescriptionContainer {
191 public String description;