]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
fix ordering of filtered transaction items
[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.Transaction;
31 import net.ktnx.mobileledger.db.TransactionWithAccounts;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 @Dao
37 public abstract class TransactionDAO extends BaseDAO<Transaction> {
38     static public List<String> unbox(List<DescriptionContainer> list) {
39         ArrayList<String> result = new ArrayList<>(list.size());
40         for (DescriptionContainer item : list) {
41             result.add(item.description);
42         }
43
44         return result;
45     }
46     @Insert(onConflict = OnConflictStrategy.REPLACE)
47     public abstract long insertSync(Transaction item);
48
49     @Update
50     public abstract void updateSync(Transaction item);
51
52     @Delete
53     public abstract void deleteSync(Transaction item);
54
55     @Delete
56     public abstract void deleteSync(List<Transaction> items);
57
58     @Query("SELECT * FROM transactions WHERE id = :id")
59     public abstract LiveData<Transaction> getById(long id);
60
61     @androidx.room.Transaction
62     @Query("SELECT * FROM transactions WHERE id = :transactionId")
63     public abstract LiveData<TransactionWithAccounts> getByIdWithAccounts(long transactionId);
64
65     @androidx.room.Transaction
66     @Query("SELECT * FROM transactions WHERE id = :transactionId")
67     public abstract TransactionWithAccounts getByIdWithAccountsSync(long transactionId);
68
69     @Query("SELECT DISTINCT description, CASE WHEN description_upper LIKE :term||'%%' THEN 1 " +
70            "               WHEN description_upper LIKE '%%:'||:term||'%%' THEN 2 " +
71            "               WHEN description_upper LIKE '%% '||:term||'%%' THEN 3 " +
72            "               ELSE 9 END AS ordering " + "FROM description_history " +
73            "WHERE description_upper LIKE '%%'||:term||'%%' " +
74            "ORDER BY ordering, description_upper, rowid ")
75     public abstract List<DescriptionContainer> lookupDescriptionSync(@NonNull String term);
76
77     @androidx.room.Transaction
78     @Query("SELECT * from transactions WHERE description = :description ORDER BY year desc, month" +
79            " desc, day desc LIMIT 1")
80     public abstract TransactionWithAccounts getFirstByDescriptionSync(@NonNull String description);
81
82     @androidx.room.Transaction
83     @Query("SELECT * from transactions tr JOIN transaction_accounts t_a ON t_a.transaction_id = " +
84            "tr.id WHERE tr.description = :description AND t_a.account_name LIKE " +
85            "'%'||:accountTerm||'%' ORDER BY year desc, month desc, day desc, tr.ledger_id desc " +
86            "LIMIT 1")
87     public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
88             @NonNull String description, @NonNull String accountTerm);
89
90     @Query("SELECT * from transactions WHERE profile_id = :profileId ORDER BY " +
91            "year desc, month desc, day desc, ledger_id desc")
92     public abstract List<Transaction> allForProfileSync(long profileId);
93
94     @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
95     protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
96
97     @androidx.room.Transaction
98     @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
99     public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
100
101     @androidx.room.Transaction
102     @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
103            " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
104            "transaction_accounts ta ON ta.transaction_id=tr.id WHERE ta.account_name LIKE " +
105            ":accountName||'%' AND ta.amount <> 0 AND tr.profile_id = :profileId ORDER BY tr.year " +
106            "desc, tr.month desc, tr.day desc, tr.ledger_id desc")
107     public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
108                                                                                  String accountName);
109
110     public long getGenerationSync(long profileId) {
111         TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
112
113         if (result == null)
114             return 0;
115         return result.generation;
116     }
117     @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
118            ":currentGeneration")
119     public abstract void purgeOldTransactionsSync(long profileId, long currentGeneration);
120     static class TransactionGenerationContainer {
121         @ColumnInfo
122         long generation;
123         public TransactionGenerationContainer(long generation) {
124             this.generation = generation;
125         }
126     }
127
128     static public class DescriptionContainer {
129         @ColumnInfo
130         public String description;
131         @ColumnInfo
132         public int ordering;
133     }
134 }