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(List<Transaction> items);
62 @Query("SELECT * FROM transactions WHERE id = :id")
63 public abstract LiveData<Transaction> getById(long id);
65 @androidx.room.Transaction
66 @Query("SELECT * FROM transactions WHERE id = :transactionId")
67 public abstract LiveData<TransactionWithAccounts> getByIdWithAccounts(long transactionId);
69 @androidx.room.Transaction
70 @Query("SELECT * FROM transactions WHERE id = :transactionId")
71 public abstract TransactionWithAccounts getByIdWithAccountsSync(long transactionId);
73 @Query("SELECT DISTINCT description, CASE WHEN description_upper LIKE :term||'%%' THEN 1 " +
74 " WHEN description_upper LIKE '%%:'||:term||'%%' THEN 2 " +
75 " WHEN description_upper LIKE '%% '||:term||'%%' THEN 3 " +
76 " ELSE 9 END AS ordering " + "FROM description_history " +
77 "WHERE description_upper LIKE '%%'||:term||'%%' " +
78 "ORDER BY ordering, description_upper, rowid ")
79 public abstract List<DescriptionContainer> lookupDescriptionSync(@NonNull String term);
81 @androidx.room.Transaction
82 @Query("SELECT * from transactions WHERE description = :description ORDER BY year desc, month" +
83 " desc, day desc LIMIT 1")
84 public abstract TransactionWithAccounts getFirstByDescriptionSync(@NonNull String description);
86 @androidx.room.Transaction
87 @Query("SELECT * from transactions tr JOIN transaction_accounts t_a ON t_a.transaction_id = " +
88 "tr.id WHERE tr.description = :description AND t_a.account_name LIKE " +
89 "'%'||:accountTerm||'%' ORDER BY year desc, month desc, day desc, tr.ledger_id desc " +
91 public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
92 @NonNull String description, @NonNull String accountTerm);
94 @Query("SELECT * from transactions WHERE profile_id = :profileId ORDER BY " +
95 "year desc, month desc, day desc, ledger_id desc")
96 public abstract List<Transaction> allForProfileSync(long profileId);
98 @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
99 protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
101 @androidx.room.Transaction
102 @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
103 public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
105 @androidx.room.Transaction
106 @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
107 " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
108 "transaction_accounts ta ON ta.transaction_id=tr.id WHERE ta.account_name LIKE " +
109 ":accountName||'%' AND ta.amount <> 0 AND tr.profile_id = :profileId ORDER BY tr.year " +
110 "desc, tr.month desc, tr.day desc, tr.ledger_id desc")
111 public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
114 public long getGenerationSync(long profileId) {
115 TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
119 return result.generation;
121 @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
122 ":currentGeneration")
123 public abstract int purgeOldTransactionsSync(long profileId, long currentGeneration);
125 @Query("DELETE FROM transaction_accounts WHERE EXISTS (SELECT 1 FROM transactions tr WHERE tr" +
126 ".id=transaction_accounts.transaction_id AND tr.profile_id=:profileId) AND generation " +
127 "<> :currentGeneration")
128 public abstract int purgeOldTransactionAccountsSync(long profileId, long currentGeneration);
129 public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
130 long generation = getGenerationSync(profileId) + 1;
132 for (TransactionWithAccounts tr : list) {
133 tr.transaction.setGeneration(generation);
134 tr.transaction.setProfileId(profileId);
139 Logger.debug("Transaction", "Purging old transactions");
140 int removed = purgeOldTransactionsSync(profileId, generation);
141 Logger.debug("Transaction", String.format(Locale.ROOT, "Purged %d transactions", removed));
143 removed = purgeOldTransactionAccountsSync(profileId, generation);
144 Logger.debug("Transaction",
145 String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
147 private void storeSync(TransactionWithAccounts rec) {
148 TransactionAccountDAO trAccDao = DB.get()
149 .getTransactionAccountDAO();
151 Transaction transaction = rec.transaction;
152 Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId());
153 if (existing != null) {
154 existing.copyDataFrom(transaction);
155 updateSync(existing);
156 transaction = existing;
159 transaction.setId(insertSync(transaction));
161 for (TransactionAccount trAcc : rec.accounts) {
162 trAcc.setTransactionId(transaction.getId());
163 trAcc.setGeneration(transaction.getGeneration());
164 TransactionAccount existingAcc =
165 trAccDao.getByOrderNoSync(trAcc.getTransactionId(), trAcc.getOrderNo());
166 if (existingAcc != null) {
167 existingAcc.copyDataFrom(trAcc);
168 trAccDao.updateSync(trAcc);
172 trAcc.setId(trAccDao.insertSync(trAcc));
175 @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId")
176 public abstract Transaction getByLedgerId(long profileId, long ledgerId);
177 static class TransactionGenerationContainer {
180 public TransactionGenerationContainer(long generation) {
181 this.generation = generation;
185 static public class DescriptionContainer {
187 public String description;