]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
03f43533fa05641d8505f233323614f95613fd6f
[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 LIMIT 1")
86     public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
87             @NonNull String description, @NonNull String accountTerm);
88
89     @Query("SELECT * from transactions WHERE profile_id = :profileId ORDER BY " +
90            "year desc, month desc, day desc, id desc")
91     public abstract List<Transaction> allForProfileSync(long profileId);
92
93     @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
94     protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
95
96     @androidx.room.Transaction
97     @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
98     public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
99
100     @androidx.room.Transaction
101     @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
102            " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
103            "transaction_accounts ta ON ta.transaction_id=tr.id WHERE ta.account_name LIKE " +
104            ":accountName||'%' AND ta.amount <> 0 AND tr.profile_id = :profileId ORDER BY tr.year " +
105            "desc, tr.month desc, tr.day desc, tr.id desc")
106     public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
107                                                                                  String accountName);
108
109     public long getGenerationSync(long profileId) {
110         TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
111
112         if (result == null)
113             return 0;
114         return result.generation;
115     }
116     @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
117            ":currentGeneration")
118     public abstract void purgeOldTransactionsSync(long profileId, long currentGeneration);
119     static class TransactionGenerationContainer {
120         @ColumnInfo
121         long generation;
122         public TransactionGenerationContainer(long generation) {
123             this.generation = generation;
124         }
125     }
126
127     static public class DescriptionContainer {
128         @ColumnInfo
129         public String description;
130         @ColumnInfo
131         public int ordering;
132     }
133 }