]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / AccountDAO.java
1 /*
2  * Copyright © 2024 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.Transaction;
29 import androidx.room.Update;
30
31 import net.ktnx.mobileledger.db.Account;
32 import net.ktnx.mobileledger.db.AccountValue;
33 import net.ktnx.mobileledger.db.AccountWithAmounts;
34 import net.ktnx.mobileledger.db.DB;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39
40 @Dao
41 public abstract class AccountDAO extends BaseDAO<Account> {
42     static public List<String> unbox(List<AccountNameContainer> list) {
43         ArrayList<String> result = new ArrayList<>(list.size());
44         for (AccountNameContainer item : list) {
45             result.add(item.name);
46         }
47
48         return result;
49     }
50
51     @Insert(onConflict = OnConflictStrategy.REPLACE)
52     public abstract long insertSync(Account item);
53
54     @Insert(onConflict = OnConflictStrategy.REPLACE)
55     public abstract void insertSync(List<Account> items);
56
57     @Transaction
58     public void insertSync(@NonNull AccountWithAmounts accountWithAmounts) {
59         final AccountValueDAO valueDAO = DB.get()
60                                            .getAccountValueDAO();
61         Account account = accountWithAmounts.account;
62         account.setId(insertSync(account));
63         for (AccountValue value : accountWithAmounts.amounts) {
64             value.setAccountId(account.getId());
65             value.setGeneration(account.getGeneration());
66             value.setId(valueDAO.insertSync(value));
67         }
68     }
69     @Update
70     public abstract void updateSync(Account item);
71
72     @Delete
73     public abstract void deleteSync(Account item);
74
75     @Delete
76     public abstract void deleteSync(List<Account> items);
77
78     @Query("DELETE FROM accounts")
79     public abstract void deleteAllSync();
80
81     @Query("SELECT * FROM accounts WHERE profile_id=:profileId AND IIF(:includeZeroBalances=1, 1," +
82            " (EXISTS(SELECT 1 FROM account_values av WHERE av.account_id=accounts.id AND av.value" +
83            " <> 0) OR EXISTS(SELECT 1 FROM accounts a WHERE a.parent_name = accounts.name))) " +
84            "ORDER BY name")
85     public abstract LiveData<List<Account>> getAll(long profileId, boolean includeZeroBalances);
86
87     @Transaction
88     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND IIF(:includeZeroBalances=1, " +
89            "1, (EXISTS(SELECT 1 FROM account_values av WHERE av.account_id=accounts.id AND av" +
90            ".value <> 0) OR EXISTS(SELECT 1 FROM accounts a WHERE a.parent_name = accounts.name))" +
91            ") ORDER BY name")
92     public abstract LiveData<List<AccountWithAmounts>> getAllWithAmounts(long profileId,
93                                                                          boolean includeZeroBalances);
94
95     @Query("SELECT * FROM accounts WHERE id=:id")
96     public abstract Account getByIdSync(long id);
97
98     //    not useful for now
99 //    @Transaction
100 //    @Query("SELECT * FROM patterns")
101 //    List<PatternWithAccounts> getPatternsWithAccounts();
102     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
103     public abstract LiveData<Account> getByName(long profileId, @NonNull String accountName);
104
105     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
106     public abstract Account getByNameSync(long profileId, @NonNull String accountName);
107
108     @Transaction
109     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
110     public abstract LiveData<AccountWithAmounts> getByNameWithAmounts(long profileId,
111                                                                       @NonNull String accountName);
112
113     @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
114            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
115            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
116            "               ELSE 9 END AS ordering " + "FROM accounts " +
117            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
118            "ORDER BY ordering, name_upper, rowid ")
119     public abstract LiveData<List<AccountNameContainer>> lookupNamesInProfileByName(long profileId,
120                                                                                     @NonNull
121                                                                                             String term);
122
123     @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
124            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
125            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
126            "               ELSE 9 END AS ordering " + "FROM accounts " +
127            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
128            "ORDER BY ordering, name_upper, rowid ")
129     public abstract List<AccountNameContainer> lookupNamesInProfileByNameSync(long profileId,
130                                                                               @NonNull String term);
131
132     @Transaction
133     @Query("SELECT * FROM accounts " +
134            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
135            "ORDER BY  CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
136            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
137            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
138            "               ELSE 9 END, name_upper, rowid ")
139     public abstract List<AccountWithAmounts> lookupWithAmountsInProfileByNameSync(long profileId,
140                                                                                   @NonNull String term);
141
142     @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
143            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
144            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
145            "               ELSE 9 END AS ordering " + "FROM accounts " +
146            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
147     public abstract LiveData<List<AccountNameContainer>> lookupNamesByName(@NonNull String term);
148
149     @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
150            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
151            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
152            "               ELSE 9 END AS ordering " + "FROM accounts " +
153            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
154     public abstract List<AccountNameContainer> lookupNamesByNameSync(@NonNull String term);
155
156     @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
157     public abstract List<Account> allForProfileSync(long profileId);
158
159     @Query("SELECT generation FROM accounts WHERE profile_id = :profileId LIMIT 1")
160     protected abstract AccountGenerationContainer getGenerationPOJOSync(long profileId);
161     public long getGenerationSync(long profileId) {
162         AccountGenerationContainer result = getGenerationPOJOSync(profileId);
163
164         if (result == null)
165             return 0;
166         return result.generation;
167     }
168     @Query("DELETE FROM accounts WHERE profile_id = :profileId AND generation <> " +
169            ":currentGeneration")
170     public abstract void purgeOldAccountsSync(long profileId, long currentGeneration);
171
172     @Query("DELETE FROM account_values WHERE EXISTS (SELECT 1 FROM accounts a WHERE a" +
173            ".id=account_values.account_id AND a.profile_id=:profileId) AND generation <> " +
174            ":currentGeneration")
175     public abstract void purgeOldAccountValuesSync(long profileId, long currentGeneration);
176     @Transaction
177     public void storeAccountsSync(List<AccountWithAmounts> accounts, long profileId) {
178         long generation = getGenerationSync(profileId) + 1;
179
180         for (AccountWithAmounts rec : accounts) {
181             rec.account.setGeneration(generation);
182             rec.account.setProfileId(profileId);
183             insertSync(rec);
184         }
185         purgeOldAccountsSync(profileId, generation);
186         purgeOldAccountValuesSync(profileId, generation);
187     }
188
189     static public class AccountNameContainer {
190         @ColumnInfo
191         public String name;
192         @ColumnInfo
193         public int ordering;
194     }
195
196     static class AccountGenerationContainer {
197         @ColumnInfo
198         long generation;
199         public AccountGenerationContainer(long generation) {
200             this.generation = generation;
201         }
202     }
203 }