]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
put all the annotated abstract DAO methods in the beginning
[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     @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
118            ":currentGeneration")
119     public abstract int purgeOldTransactionsSync(long profileId, long currentGeneration);
120
121     @Query("DELETE FROM transaction_accounts WHERE EXISTS (SELECT 1 FROM transactions tr WHERE tr" +
122            ".id=transaction_accounts.transaction_id AND tr.profile_id=:profileId) AND generation " +
123            "<> :currentGeneration")
124     public abstract int purgeOldTransactionAccountsSync(long profileId, long currentGeneration);
125
126     @Query("DELETE FROM transactions WHERE profile_id = :profileId")
127     public abstract int deleteAllSync(long profileId);
128
129     @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId")
130     public abstract Transaction getByLedgerId(long profileId, long ledgerId);
131     public long getGenerationSync(long profileId) {
132         TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
133
134         if (result == null)
135             return 0;
136         return result.generation;
137     }
138     public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
139         long generation = getGenerationSync(profileId) + 1;
140
141         for (TransactionWithAccounts tr : list) {
142             tr.transaction.setGeneration(generation);
143             tr.transaction.setProfileId(profileId);
144
145             storeSync(tr);
146         }
147
148         Logger.debug("Transaction", "Purging old transactions");
149         int removed = purgeOldTransactionsSync(profileId, generation);
150         Logger.debug("Transaction", String.format(Locale.ROOT, "Purged %d transactions", removed));
151
152         removed = purgeOldTransactionAccountsSync(profileId, generation);
153         Logger.debug("Transaction",
154                 String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
155     }
156     private void storeSync(TransactionWithAccounts rec) {
157         TransactionAccountDAO trAccDao = DB.get()
158                                            .getTransactionAccountDAO();
159
160         Transaction transaction = rec.transaction;
161         Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId());
162         if (existing != null) {
163             existing.copyDataFrom(transaction);
164             updateSync(existing);
165             transaction = existing;
166         }
167         else
168             transaction.setId(insertSync(transaction));
169
170         for (TransactionAccount trAcc : rec.accounts) {
171             trAcc.setTransactionId(transaction.getId());
172             trAcc.setGeneration(transaction.getGeneration());
173             TransactionAccount existingAcc =
174                     trAccDao.getByOrderNoSync(trAcc.getTransactionId(), trAcc.getOrderNo());
175             if (existingAcc != null) {
176                 existingAcc.copyDataFrom(trAcc);
177                 trAccDao.updateSync(existingAcc);
178             }
179             else
180                 trAcc.setId(trAccDao.insertSync(trAcc));
181         }
182     }
183     static class TransactionGenerationContainer {
184         @ColumnInfo
185         long generation;
186         public TransactionGenerationContainer(long generation) {
187             this.generation = generation;
188         }
189     }
190
191     static public class DescriptionContainer {
192         @ColumnInfo
193         public String description;
194         @ColumnInfo
195         public int ordering;
196     }
197 }