2 * Copyright © 2020 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.model;
20 import android.content.res.Resources;
21 import android.database.Cursor;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.text.TextUtils;
24 import android.util.SparseArray;
26 import androidx.annotation.Nullable;
28 import net.ktnx.mobileledger.App;
29 import net.ktnx.mobileledger.R;
30 import net.ktnx.mobileledger.async.DbOpQueue;
31 import net.ktnx.mobileledger.async.SendTransactionTask;
32 import net.ktnx.mobileledger.utils.Logger;
33 import net.ktnx.mobileledger.utils.Misc;
34 import net.ktnx.mobileledger.utils.SimpleDate;
36 import org.jetbrains.annotations.Contract;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Locale;
43 import java.util.Objects;
45 import static net.ktnx.mobileledger.utils.Logger.debug;
47 public final class MobileLedgerProfile {
48 // N.B. when adding new fields, update the copy-constructor below
49 private final String uuid;
51 private boolean permitPosting;
52 private boolean showCommentsByDefault;
53 private boolean showCommodityByDefault;
54 private String defaultCommodity;
55 private String preferredAccountsFilter;
57 private boolean authEnabled;
58 private String authUserName;
59 private String authPassword;
61 private int orderNo = -1;
62 private SendTransactionTask.API apiVersion = SendTransactionTask.API.auto;
63 private FutureDates futureDates = FutureDates.None;
64 private boolean accountsLoaded;
65 private boolean transactionsLoaded;
66 // N.B. when adding new fields, update the copy-constructor below
67 transient private AccountAndTransactionListSaver accountAndTransactionListSaver;
68 public MobileLedgerProfile(String uuid) {
71 public MobileLedgerProfile(MobileLedgerProfile origin) {
74 permitPosting = origin.permitPosting;
75 showCommentsByDefault = origin.showCommentsByDefault;
76 showCommodityByDefault = origin.showCommodityByDefault;
77 preferredAccountsFilter = origin.preferredAccountsFilter;
79 authEnabled = origin.authEnabled;
80 authUserName = origin.authUserName;
81 authPassword = origin.authPassword;
82 themeHue = origin.themeHue;
83 orderNo = origin.orderNo;
84 futureDates = origin.futureDates;
85 apiVersion = origin.apiVersion;
86 defaultCommodity = origin.defaultCommodity;
87 accountsLoaded = origin.accountsLoaded;
88 transactionsLoaded = origin.transactionsLoaded;
90 // loads all profiles into Data.profiles
91 // returns the profile with the given UUID
92 public static MobileLedgerProfile loadAllFromDB(@Nullable String currentProfileUUID) {
93 MobileLedgerProfile result = null;
94 ArrayList<MobileLedgerProfile> list = new ArrayList<>();
95 SQLiteDatabase db = App.getDatabase();
96 try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
97 "auth_password, permit_posting, theme, order_no, " +
98 "preferred_accounts_filter, future_dates, api_version, " +
99 "show_commodity_by_default, default_commodity, " +
100 "show_comments_by_default FROM " +
101 "profiles order by order_no", null))
103 while (cursor.moveToNext()) {
104 MobileLedgerProfile item = new MobileLedgerProfile(cursor.getString(0));
105 item.setName(cursor.getString(1));
106 item.setUrl(cursor.getString(2));
107 item.setAuthEnabled(cursor.getInt(3) == 1);
108 item.setAuthUserName(cursor.getString(4));
109 item.setAuthPassword(cursor.getString(5));
110 item.setPostingPermitted(cursor.getInt(6) == 1);
111 item.setThemeId(cursor.getInt(7));
112 item.orderNo = cursor.getInt(8);
113 item.setPreferredAccountsFilter(cursor.getString(9));
114 item.setFutureDates(cursor.getInt(10));
115 item.setApiVersion(cursor.getInt(11));
116 item.setShowCommodityByDefault(cursor.getInt(12) == 1);
117 item.setDefaultCommodity(cursor.getString(13));
118 item.setShowCommentsByDefault(cursor.getInt(14) == 1);
121 .equals(currentProfileUUID))
125 Data.profiles.setValue(list);
128 public static void storeProfilesOrder() {
129 SQLiteDatabase db = App.getDatabase();
130 db.beginTransactionNonExclusive();
133 for (MobileLedgerProfile p : Objects.requireNonNull(Data.profiles.getValue())) {
134 db.execSQL("update profiles set order_no=? where uuid=?",
135 new Object[]{orderNo, p.getUuid()});
139 db.setTransactionSuccessful();
145 @Contract(value = "null -> false", pure = true)
147 public boolean equals(@Nullable Object obj) {
152 if (obj.getClass() != this.getClass())
155 MobileLedgerProfile p = (MobileLedgerProfile) obj;
156 if (!uuid.equals(p.uuid))
158 if (!name.equals(p.name))
160 if (permitPosting != p.permitPosting)
162 if (showCommentsByDefault != p.showCommentsByDefault)
164 if (showCommodityByDefault != p.showCommodityByDefault)
166 if (!Objects.equals(defaultCommodity, p.defaultCommodity))
168 if (!Objects.equals(preferredAccountsFilter, p.preferredAccountsFilter))
170 if (!Objects.equals(url, p.url))
172 if (authEnabled != p.authEnabled)
174 if (!Objects.equals(authUserName, p.authUserName))
176 if (!Objects.equals(authPassword, p.authPassword))
178 if (themeHue != p.themeHue)
180 if (apiVersion != p.apiVersion)
182 return futureDates == p.futureDates;
184 public boolean getShowCommentsByDefault() {
185 return showCommentsByDefault;
187 public void setShowCommentsByDefault(boolean newValue) {
188 this.showCommentsByDefault = newValue;
190 public boolean getShowCommodityByDefault() {
191 return showCommodityByDefault;
193 public void setShowCommodityByDefault(boolean showCommodityByDefault) {
194 this.showCommodityByDefault = showCommodityByDefault;
196 public String getDefaultCommodity() {
197 return defaultCommodity;
199 public void setDefaultCommodity(String defaultCommodity) {
200 this.defaultCommodity = defaultCommodity;
202 public void setDefaultCommodity(CharSequence defaultCommodity) {
203 if (defaultCommodity == null)
204 this.defaultCommodity = null;
206 this.defaultCommodity = String.valueOf(defaultCommodity);
208 public SendTransactionTask.API getApiVersion() {
211 public void setApiVersion(SendTransactionTask.API apiVersion) {
212 this.apiVersion = apiVersion;
214 public void setApiVersion(int apiVersion) {
215 this.apiVersion = SendTransactionTask.API.valueOf(apiVersion);
217 public FutureDates getFutureDates() {
220 public void setFutureDates(int anInt) {
221 futureDates = FutureDates.valueOf(anInt);
223 public void setFutureDates(FutureDates futureDates) {
224 this.futureDates = futureDates;
226 public String getPreferredAccountsFilter() {
227 return preferredAccountsFilter;
229 public void setPreferredAccountsFilter(String preferredAccountsFilter) {
230 this.preferredAccountsFilter = preferredAccountsFilter;
232 public void setPreferredAccountsFilter(CharSequence preferredAccountsFilter) {
233 setPreferredAccountsFilter(String.valueOf(preferredAccountsFilter));
235 public boolean isPostingPermitted() {
236 return permitPosting;
238 public void setPostingPermitted(boolean permitPosting) {
239 this.permitPosting = permitPosting;
241 public String getUuid() {
244 public String getName() {
247 public void setName(CharSequence text) {
248 setName(String.valueOf(text));
250 public void setName(String name) {
253 public String getUrl() {
256 public void setUrl(CharSequence text) {
257 setUrl(String.valueOf(text));
259 public void setUrl(String url) {
262 public boolean isAuthEnabled() {
265 public void setAuthEnabled(boolean authEnabled) {
266 this.authEnabled = authEnabled;
268 public String getAuthUserName() {
271 public void setAuthUserName(CharSequence text) {
272 setAuthUserName(String.valueOf(text));
274 public void setAuthUserName(String authUserName) {
275 this.authUserName = authUserName;
277 public String getAuthPassword() {
280 public void setAuthPassword(CharSequence text) {
281 setAuthPassword(String.valueOf(text));
283 public void setAuthPassword(String authPassword) {
284 this.authPassword = authPassword;
286 public void storeInDB() {
287 SQLiteDatabase db = App.getDatabase();
288 db.beginTransactionNonExclusive();
290 // debug("profiles", String.format("Storing profile in DB: uuid=%s, name=%s, " +
291 // "url=%s, permit_posting=%s, authEnabled=%s, " +
292 // "themeHue=%d", uuid, name, url,
293 // permitPosting ? "TRUE" : "FALSE", authEnabled ? "TRUE" : "FALSE", themeHue));
294 db.execSQL("REPLACE INTO profiles(uuid, name, permit_posting, url, " +
295 "use_authentication, auth_user, auth_password, theme, order_no, " +
296 "preferred_accounts_filter, future_dates, api_version, " +
297 "show_commodity_by_default, default_commodity, show_comments_by_default) " +
298 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
299 new Object[]{uuid, name, permitPosting, url, authEnabled,
300 authEnabled ? authUserName : null,
301 authEnabled ? authPassword : null, themeHue, orderNo,
302 preferredAccountsFilter, futureDates.toInt(), apiVersion.toInt(),
303 showCommodityByDefault, defaultCommodity, showCommentsByDefault
305 db.setTransactionSuccessful();
311 public void storeAccount(SQLiteDatabase db, int generation, LedgerAccount acc,
312 boolean storeUiFields) {
313 // replace into is a bad idea because it would reset hidden to its default value
314 // we like the default, but for new accounts only
315 String sql = "update accounts set generation = ?";
316 List<Object> params = new ArrayList<>();
317 params.add(generation);
319 sql += ", expanded=?";
320 params.add(acc.isExpanded() ? 1 : 0);
322 sql += " where profile=? and name=?";
324 params.add(acc.getName());
325 db.execSQL(sql, params.toArray());
327 db.execSQL("insert into accounts(profile, name, name_upper, parent_name, level, " +
328 "expanded, generation) select ?,?,?,?,?,0,? where (select changes() = 0)",
329 new Object[]{uuid, acc.getName(), acc.getName().toUpperCase(), acc.getParentName(),
330 acc.getLevel(), generation
332 // debug("accounts", String.format("Stored account '%s' in DB [%s]", acc.getName(), uuid));
334 public void storeAccountValue(SQLiteDatabase db, int generation, String name, String currency,
336 if (!TextUtils.isEmpty(currency)) {
338 try (Cursor c = db.rawQuery("select 1 from currencies where name=?",
339 new String[]{currency}))
341 exists = c.moveToFirst();
345 "insert into currencies(id, name, position, has_gap) values((select max" +
346 "(id) from currencies)+1, ?, ?, ?)", new Object[]{currency,
347 Objects.requireNonNull(
348 Data.currencySymbolPosition.getValue()).toString(),
349 Data.currencyGap.getValue()
354 db.execSQL("replace into account_values(profile, account, " +
355 "currency, value, generation) values(?, ?, ?, ?, ?);",
356 new Object[]{uuid, name, Misc.emptyIsNull(currency), amount, generation});
358 public void storeTransaction(SQLiteDatabase db, int generation, LedgerTransaction tr) {
360 // Logger.debug("storeTransaction", String.format(Locale.US, "ID %d", tr.getId()));
361 SimpleDate d = tr.getDate();
362 db.execSQL("UPDATE transactions SET year=?, month=?, day=?, description=?, comment=?, " +
363 "data_hash=?, generation=? WHERE profile=? AND id=?",
364 new Object[]{d.year, d.month, d.day, tr.getDescription(), tr.getComment(),
365 tr.getDataHash(), generation, uuid, tr.getId()
367 db.execSQL("INSERT INTO transactions(profile, id, year, month, day, description, " +
368 "comment, data_hash, generation) " +
369 "select ?,?,?,?,?,?,?,?,? WHERE (select changes() = 0)",
370 new Object[]{uuid, tr.getId(), tr.getDate().year, tr.getDate().month,
371 tr.getDate().day, tr.getDescription(), tr.getComment(),
372 tr.getDataHash(), generation
375 int accountOrderNo = 1;
376 for (LedgerTransactionAccount item : tr.getAccounts()) {
377 db.execSQL("UPDATE transaction_accounts SET account_name=?, amount=?, currency=?, " +
378 "comment=?, generation=? " +
379 "WHERE profile=? AND transaction_id=? AND order_no=?",
380 new Object[]{item.getAccountName(), item.getAmount(),
381 Misc.nullIsEmpty(item.getCurrency()), item.getComment(),
382 generation, uuid, tr.getId(), accountOrderNo
384 db.execSQL("INSERT INTO transaction_accounts(profile, transaction_id, " +
385 "order_no, account_name, amount, currency, comment, generation) " +
386 "select ?, ?, ?, ?, ?, ?, ?, ? WHERE (select changes() = 0)",
387 new Object[]{uuid, tr.getId(), accountOrderNo, item.getAccountName(),
388 item.getAmount(), Misc.nullIsEmpty(item.getCurrency()),
389 item.getComment(), generation
394 // debug("profile", String.format("Transaction %d stored", tr.getId()));
396 public String getOption(String name, String default_value) {
397 SQLiteDatabase db = App.getDatabase();
398 try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
399 new String[]{uuid, name}))
401 if (cursor.moveToFirst()) {
402 String result = cursor.getString(0);
404 if (result == null) {
405 debug("profile", "returning default value for " + name);
406 result = default_value;
409 debug("profile", String.format("option %s=%s", name, result));
414 return default_value;
416 catch (Exception e) {
417 debug("db", "returning default value for " + name, e);
418 return default_value;
421 public long getLongOption(String name, long default_value) {
423 String result = getOption(name, "");
424 if ((result == null) || result.isEmpty()) {
425 debug("profile", String.format("Returning default value for option %s", name));
426 longResult = default_value;
430 longResult = Long.parseLong(result);
431 debug("profile", String.format("option %s=%s", name, result));
433 catch (Exception e) {
434 debug("profile", String.format("Returning default value for option %s", name), e);
435 longResult = default_value;
441 public void setOption(String name, String value) {
442 debug("profile", String.format("setting option %s=%s", name, value));
443 DbOpQueue.add("insert or replace into options(profile, name, value) values(?, ?, ?);",
444 new String[]{uuid, name, value});
446 public void setLongOption(String name, long value) {
447 setOption(name, String.valueOf(value));
449 public void removeFromDB() {
450 SQLiteDatabase db = App.getDatabase();
451 debug("db", String.format("removing profile %s from DB", uuid));
452 db.beginTransactionNonExclusive();
454 Object[] uuid_param = new Object[]{uuid};
455 db.execSQL("delete from transaction_accounts where profile=?", uuid_param);
456 db.execSQL("delete from transactions where profile=?", uuid_param);
457 db.execSQL("delete from account_values where profile=?", uuid_param);
458 db.execSQL("delete from accounts where profile=?", uuid_param);
459 db.execSQL("delete from options where profile=?", uuid_param);
460 db.execSQL("delete from profiles where uuid=?", uuid_param);
461 db.setTransactionSuccessful();
467 public LedgerTransaction loadTransaction(int transactionId) {
468 LedgerTransaction tr = new LedgerTransaction(transactionId, this.uuid);
469 tr.loadData(App.getDatabase());
473 public int getThemeHue() {
474 // debug("profile", String.format("Profile.getThemeHue() returning %d", themeHue));
475 return this.themeHue;
477 public void setThemeHue(Object o) {
478 setThemeId(Integer.parseInt(String.valueOf(o)));
480 public void setThemeId(int themeHue) {
481 // debug("profile", String.format("Profile.setThemeHue(%d) called", themeHue));
482 this.themeHue = themeHue;
484 public int getNextTransactionsGeneration(SQLiteDatabase db) {
486 try (Cursor c = db.rawQuery("SELECT generation FROM transactions WHERE profile=? LIMIT 1",
489 if (c.moveToFirst()) {
490 generation = c.getInt(0) + 1;
495 private int getNextAccountsGeneration(SQLiteDatabase db) {
497 try (Cursor c = db.rawQuery("SELECT generation FROM accounts WHERE profile=? LIMIT 1",
500 if (c.moveToFirst()) {
501 generation = c.getInt(0) + 1;
506 private void deleteNotPresentAccounts(SQLiteDatabase db, int generation) {
507 Logger.debug("db/benchmark", "Deleting obsolete accounts");
508 db.execSQL("DELETE FROM account_values WHERE profile=? AND generation <> ?",
509 new Object[]{uuid, generation});
510 db.execSQL("DELETE FROM accounts WHERE profile=? AND generation <> ?",
511 new Object[]{uuid, generation});
512 Logger.debug("db/benchmark", "Done deleting obsolete accounts");
514 private void deleteNotPresentTransactions(SQLiteDatabase db, int generation) {
515 Logger.debug("db/benchmark", "Deleting obsolete transactions");
516 db.execSQL("DELETE FROM transaction_accounts WHERE profile=? AND generation <> ?",
517 new Object[]{uuid, generation});
518 db.execSQL("DELETE FROM transactions WHERE profile=? AND generation <> ?",
519 new Object[]{uuid, generation});
520 Logger.debug("db/benchmark", "Done deleting obsolete transactions");
522 public void wipeAllData() {
523 SQLiteDatabase db = App.getDatabase();
524 db.beginTransaction();
526 String[] pUuid = new String[]{uuid};
527 db.execSQL("delete from options where profile=?", pUuid);
528 db.execSQL("delete from accounts where profile=?", pUuid);
529 db.execSQL("delete from account_values where profile=?", pUuid);
530 db.execSQL("delete from transactions where profile=?", pUuid);
531 db.execSQL("delete from transaction_accounts where profile=?", pUuid);
532 db.setTransactionSuccessful();
533 debug("wipe", String.format(Locale.ENGLISH, "Profile %s wiped out", pUuid[0]));
539 public List<Currency> getCurrencies() {
540 SQLiteDatabase db = App.getDatabase();
542 ArrayList<Currency> result = new ArrayList<>();
544 try (Cursor c = db.rawQuery("SELECT c.id, c.name, c.position, c.has_gap FROM currencies c",
547 while (c.moveToNext()) {
548 Currency currency = new Currency(c.getInt(0), c.getString(1),
549 Currency.Position.valueOf(c.getString(2)), c.getInt(3) == 1);
550 result.add(currency);
556 Currency loadCurrencyByName(String name) {
557 SQLiteDatabase db = App.getDatabase();
558 Currency result = tryLoadCurrencyByName(db, name);
560 throw new RuntimeException(String.format("Unable to load currency '%s'", name));
563 private Currency tryLoadCurrencyByName(SQLiteDatabase db, String name) {
564 try (Cursor cursor = db.rawQuery(
565 "SELECT c.id, c.name, c.position, c.has_gap FROM currencies c WHERE c.name=?",
568 if (cursor.moveToFirst()) {
569 return new Currency(cursor.getInt(0), cursor.getString(1),
570 Currency.Position.valueOf(cursor.getString(2)), cursor.getInt(3) == 1);
575 public void storeAccountAndTransactionListAsync(List<LedgerAccount> accounts,
576 List<LedgerTransaction> transactions) {
577 if (accountAndTransactionListSaver != null)
578 accountAndTransactionListSaver.interrupt();
580 accountAndTransactionListSaver =
581 new AccountAndTransactionListSaver(this, accounts, transactions);
582 accountAndTransactionListSaver.start();
585 public enum FutureDates {
586 None(0), OneWeek(7), TwoWeeks(14), OneMonth(30), TwoMonths(60), ThreeMonths(90),
587 SixMonths(180), OneYear(365), All(-1);
588 private static final SparseArray<FutureDates> map = new SparseArray<>();
591 for (FutureDates item : FutureDates.values()) {
592 map.put(item.value, item);
597 FutureDates(int value) {
600 public static FutureDates valueOf(int i) {
601 return map.get(i, None);
606 public String getText(Resources resources) {
609 return resources.getString(R.string.future_dates_7);
611 return resources.getString(R.string.future_dates_14);
613 return resources.getString(R.string.future_dates_30);
615 return resources.getString(R.string.future_dates_60);
617 return resources.getString(R.string.future_dates_90);
619 return resources.getString(R.string.future_dates_180);
621 return resources.getString(R.string.future_dates_365);
623 return resources.getString(R.string.future_dates_all);
625 return resources.getString(R.string.future_dates_none);
630 private static class AccountAndTransactionListSaver extends Thread {
631 private final MobileLedgerProfile profile;
632 private final List<LedgerAccount> accounts;
633 private final List<LedgerTransaction> transactions;
634 AccountAndTransactionListSaver(MobileLedgerProfile profile, List<LedgerAccount> accounts,
635 List<LedgerTransaction> transactions) {
636 this.accounts = accounts;
637 this.transactions = transactions;
638 this.profile = profile;
640 public int getNextDescriptionsGeneration(SQLiteDatabase db) {
642 try (Cursor c = db.rawQuery("SELECT generation FROM description_history LIMIT 1",
645 if (c.moveToFirst()) {
646 generation = c.getInt(0) + 1;
651 void deleteNotPresentDescriptions(SQLiteDatabase db, int generation) {
652 Logger.debug("db/benchmark", "Deleting obsolete descriptions");
653 db.execSQL("DELETE FROM description_history WHERE generation <> ?",
654 new Object[]{generation});
655 db.execSQL("DELETE FROM description_history WHERE generation <> ?",
656 new Object[]{generation});
657 Logger.debug("db/benchmark", "Done deleting obsolete descriptions");
661 SQLiteDatabase db = App.getDatabase();
662 db.beginTransactionNonExclusive();
664 int accountsGeneration = profile.getNextAccountsGeneration(db);
668 int transactionsGeneration = profile.getNextTransactionsGeneration(db);
672 for (LedgerAccount acc : accounts) {
673 profile.storeAccount(db, accountsGeneration, acc, false);
676 for (LedgerAmount amt : acc.getAmounts()) {
677 profile.storeAccountValue(db, accountsGeneration, acc.getName(),
678 amt.getCurrency(), amt.getAmount());
684 for (LedgerTransaction tr : transactions) {
685 profile.storeTransaction(db, transactionsGeneration, tr);
690 profile.deleteNotPresentTransactions(db, transactionsGeneration);
691 if (isInterrupted()) {
694 profile.deleteNotPresentAccounts(db, accountsGeneration);
698 Map<String, Boolean> unique = new HashMap<>();
700 debug("descriptions", "Starting refresh");
701 int descriptionsGeneration = getNextDescriptionsGeneration(db);
702 try (Cursor c = db.rawQuery("SELECT distinct description from transactions",
705 while (c.moveToNext()) {
706 String description = c.getString(0);
707 String descriptionUpper = description.toUpperCase();
708 if (unique.containsKey(descriptionUpper))
711 storeDescription(db, descriptionsGeneration, description, descriptionUpper);
713 unique.put(descriptionUpper, true);
716 deleteNotPresentDescriptions(db, descriptionsGeneration);
718 db.setTransactionSuccessful();
724 private void storeDescription(SQLiteDatabase db, int generation, String description,
725 String descriptionUpper) {
726 db.execSQL("UPDATE description_history SET description=?, generation=? WHERE " +
727 "description_upper=?", new Object[]{description, generation, descriptionUpper
730 "INSERT INTO description_history(description, description_upper, generation) " +
731 "select ?,?,? WHERE (select changes() = 0)",
732 new Object[]{description, descriptionUpper, generation