]> 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 © 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.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 ORDER BY name")
82     public abstract LiveData<List<Account>> getAll(long profileId);
83
84     @Transaction
85     @Query("SELECT * FROM accounts WHERE profile_id = :profileId ORDER BY name")
86     public abstract LiveData<List<AccountWithAmounts>> getAllWithAmounts(long profileId);
87
88     @Query("SELECT * FROM accounts WHERE id=:id")
89     public abstract Account getByIdSync(long id);
90
91     //    not useful for now
92 //    @Transaction
93 //    @Query("SELECT * FROM patterns")
94 //    List<PatternWithAccounts> getPatternsWithAccounts();
95     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
96     public abstract LiveData<Account> getByName(long profileId, @NonNull String accountName);
97
98     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
99     public abstract Account getByNameSync(long profileId, @NonNull String accountName);
100
101     @Transaction
102     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
103     public abstract LiveData<AccountWithAmounts> getByNameWithAmounts(long profileId,
104                                                                       @NonNull String accountName);
105
106     @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
107            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
108            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
109            "               ELSE 9 END AS ordering " + "FROM accounts " +
110            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
111            "ORDER BY ordering, name_upper, rowid ")
112     public abstract LiveData<List<AccountNameContainer>> lookupNamesInProfileByName(long profileId,
113                                                                                     @NonNull
114                                                                                             String term);
115
116     @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
117            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
118            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
119            "               ELSE 9 END AS ordering " + "FROM accounts " +
120            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
121            "ORDER BY ordering, name_upper, rowid ")
122     public abstract List<AccountNameContainer> lookupNamesInProfileByNameSync(long profileId,
123                                                                               @NonNull String term);
124
125     @Transaction
126     @Query("SELECT * FROM accounts " +
127            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
128            "ORDER BY  CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
129            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
130            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
131            "               ELSE 9 END, name_upper, rowid ")
132     public abstract List<AccountWithAmounts> lookupWithAmountsInProfileByNameSync(long profileId,
133                                                                                   @NonNull String term);
134
135     @Query("SELECT DISTINCT name, 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 AS ordering " + "FROM accounts " +
139            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
140     public abstract LiveData<List<AccountNameContainer>> lookupNamesByName(@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 List<AccountNameContainer> lookupNamesByNameSync(@NonNull String term);
148
149     @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
150     public abstract List<Account> allForProfileSync(long profileId);
151
152     @Query("SELECT generation FROM accounts WHERE profile_id = :profileId LIMIT 1")
153     protected abstract AccountGenerationContainer getGenerationPOJOSync(long profileId);
154     public long getGenerationSync(long profileId) {
155         AccountGenerationContainer result = getGenerationPOJOSync(profileId);
156
157         if (result == null)
158             return 0;
159         return result.generation;
160     }
161     @Query("DELETE FROM accounts WHERE profile_id = :profileId AND generation <> " +
162            ":currentGeneration")
163     public abstract void purgeOldAccountsSync(long profileId, long currentGeneration);
164
165     @Query("DELETE FROM account_values WHERE EXISTS (SELECT 1 FROM accounts a WHERE a" +
166            ".id=account_values.account_id AND a.profile_id=:profileId) AND generation <> " +
167            ":currentGeneration")
168     public abstract void purgeOldAccountValuesSync(long profileId, long currentGeneration);
169     @Transaction
170     public void storeAccountsSync(List<AccountWithAmounts> accounts, long profileId) {
171         long generation = getGenerationSync(profileId) + 1;
172
173         for (AccountWithAmounts rec : accounts) {
174             rec.account.setGeneration(generation);
175             rec.account.setProfileId(profileId);
176             insertSync(rec);
177         }
178         purgeOldAccountsSync(profileId, generation);
179         purgeOldAccountValuesSync(profileId, generation);
180     }
181
182     static public class AccountNameContainer {
183         @ColumnInfo
184         public String name;
185         @ColumnInfo
186         public int ordering;
187     }
188
189     static class AccountGenerationContainer {
190         @ColumnInfo
191         long generation;
192         public AccountGenerationContainer(long generation) {
193             this.generation = generation;
194         }
195     }
196 }