]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
additional transaction deletion methods
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TransactionDAO.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 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;
29
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;
35
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Locale;
39
40 @Dao
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);
46         }
47
48         return result;
49     }
50     @Insert(onConflict = OnConflictStrategy.REPLACE)
51     public abstract long insertSync(Transaction item);
52
53     @Update
54     public abstract void updateSync(Transaction item);
55
56     @Delete
57     public abstract void deleteSync(Transaction item);
58
59     @Delete
60     public abstract void deleteSync(Transaction... items);
61
62     @Delete
63     public abstract void deleteSync(List<Transaction> items);
64
65     @Query("SELECT * FROM transactions WHERE id = :id")
66     public abstract LiveData<Transaction> getById(long id);
67
68     @androidx.room.Transaction
69     @Query("SELECT * FROM transactions WHERE id = :transactionId")
70     public abstract LiveData<TransactionWithAccounts> getByIdWithAccounts(long transactionId);
71
72     @androidx.room.Transaction
73     @Query("SELECT * FROM transactions WHERE id = :transactionId")
74     public abstract TransactionWithAccounts getByIdWithAccountsSync(long transactionId);
75
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);
83
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);
88
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);
97
98     @Query("SELECT * from transactions WHERE profile_id = :profileId")
99     public abstract List<Transaction> getAllForProfileUnorderedSync(long profileId);
100
101     @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
102     protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
103
104     @androidx.room.Transaction
105     @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
106     public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
107
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,
115                                                                                  String accountName);
116
117     public long getGenerationSync(long profileId) {
118         TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
119
120         if (result == null)
121             return 0;
122         return result.generation;
123     }
124     @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
125            ":currentGeneration")
126     public abstract int purgeOldTransactionsSync(long profileId, long currentGeneration);
127
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;
134
135         for (TransactionWithAccounts tr : list) {
136             tr.transaction.setGeneration(generation);
137             tr.transaction.setProfileId(profileId);
138
139             storeSync(tr);
140         }
141
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));
145
146         removed = purgeOldTransactionAccountsSync(profileId, generation);
147         Logger.debug("Transaction",
148                 String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
149     }
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();
155
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;
162         }
163         else
164             transaction.setId(insertSync(transaction));
165
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(trAcc);
174                 trAcc = existingAcc;
175             }
176             else
177                 trAcc.setId(trAccDao.insertSync(trAcc));
178         }
179     }
180     @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId")
181     public abstract Transaction getByLedgerId(long profileId, long ledgerId);
182     static class TransactionGenerationContainer {
183         @ColumnInfo
184         long generation;
185         public TransactionGenerationContainer(long generation) {
186             this.generation = generation;
187         }
188     }
189
190     static public class DescriptionContainer {
191         @ColumnInfo
192         public String description;
193         @ColumnInfo
194         public int ordering;
195     }
196 }