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.
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.
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/>.
18 package net.ktnx.mobileledger.dao;
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;
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;
36 import java.util.ArrayList;
37 import java.util.List;
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);
51 @Insert(onConflict = OnConflictStrategy.REPLACE)
52 public abstract long insertSync(Account item);
54 @Insert(onConflict = OnConflictStrategy.REPLACE)
55 public abstract void insertSync(List<Account> items);
58 public void insertSync(@NonNull AccountWithAmounts accountWithAmounts) {
59 final AccountValueDAO valueDAO = DB.get()
60 .getAccountValueDAO();
61 Account account = accountWithAmounts.account;
62 Account existingAccount = getByNameSync(account.getProfileId(), account.getName());
63 if (existingAccount != null) {
64 existingAccount.setGeneration(account.getGeneration());
65 account = existingAccount;
69 long accountId = insertSync(account);
70 account.setId(accountId);
72 for (AccountValue value : accountWithAmounts.amounts) {
73 value.setAccountId(account.getId());
74 value.setGeneration(account.getGeneration());
75 value.setId(valueDAO.insertSync(value));
79 public abstract void updateSync(Account item);
82 public abstract void deleteSync(Account item);
85 public abstract void deleteSync(List<Account> items);
87 @Query("SELECT * FROM accounts WHERE profile_id=:profileId")
88 public abstract LiveData<List<Account>> getAll(long profileId);
91 @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
92 public abstract LiveData<List<AccountWithAmounts>> getAllWithAmounts(long profileId);
94 @Query("SELECT * FROM accounts WHERE id=:id")
95 public abstract Account getByIdSync(long id);
99 // @Query("SELECT * FROM patterns")
100 // List<PatternWithAccounts> getPatternsWithAccounts();
101 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
102 public abstract LiveData<Account> getByName(long profileId, @NonNull String accountName);
104 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
105 public abstract Account getByNameSync(long profileId, @NonNull String accountName);
108 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
109 public abstract LiveData<AccountWithAmounts> getByNameWithAmounts(long profileId,
110 @NonNull String accountName);
112 @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
113 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
114 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
115 " ELSE 9 END AS ordering " + "FROM accounts " +
116 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
117 "ORDER BY ordering, name_upper, rowid ")
118 public abstract LiveData<List<AccountNameContainer>> lookupInProfileByName(long profileId,
122 @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
123 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
124 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
125 " ELSE 9 END AS ordering " + "FROM accounts " +
126 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
127 "ORDER BY ordering, name_upper, rowid ")
128 public abstract List<AccountNameContainer> lookupInProfileByNameSync(long profileId,
129 @NonNull String term);
131 @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
132 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
133 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
134 " ELSE 9 END AS ordering " + "FROM accounts " +
135 "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
136 public abstract LiveData<List<AccountNameContainer>> lookupByName(@NonNull String term);
138 @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
139 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
140 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
141 " ELSE 9 END AS ordering " + "FROM accounts " +
142 "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
143 public abstract List<AccountNameContainer> lookupByNameSync(@NonNull String term);
145 @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
146 public abstract List<Account> allForProfileSync(long profileId);
148 @Query("SELECT generation FROM accounts WHERE profile_id = :profileId LIMIT 1")
149 protected abstract AccountGenerationContainer getGenerationPOJOSync(long profileId);
150 public long getGenerationSync(long profileId) {
151 AccountGenerationContainer result = getGenerationPOJOSync(profileId);
155 return result.generation;
157 @Query("DELETE FROM accounts WHERE profile_id = :profileId AND generation <> " +
158 ":currentGeneration")
159 public abstract void purgeOldAccountsSync(long profileId, long currentGeneration);
161 @Query("DELETE FROM account_values WHERE EXISTS (SELECT 1 FROM accounts a WHERE a" +
162 ".id=account_values.account_id AND a.profile_id=:profileId) AND generation <> " +
163 ":currentGeneration")
164 public abstract void purgeOldAccountValuesSync(long profileId, long currentGeneration);
166 public void storeAccountsSync(List<AccountWithAmounts> accounts, long profileId) {
167 long generation = getGenerationSync(profileId) + 1;
169 for (AccountWithAmounts rec : accounts) {
170 rec.account.setGeneration(generation);
171 rec.account.setProfileId(profileId);
174 purgeOldAccountsSync(profileId, generation);
175 purgeOldAccountValuesSync(profileId, generation);
178 static public class AccountNameContainer {
185 static class AccountGenerationContainer {
188 public AccountGenerationContainer(long generation) {
189 this.generation = generation;