]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
adopt Room for displaying account lists
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / MobileLedgerProfile.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.model;
19
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.res.Resources;
23 import android.database.Cursor;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.os.AsyncTask;
26 import android.os.Bundle;
27 import android.text.TextUtils;
28 import android.util.SparseArray;
29
30 import androidx.annotation.Nullable;
31 import androidx.room.Transaction;
32
33 import net.ktnx.mobileledger.App;
34 import net.ktnx.mobileledger.R;
35 import net.ktnx.mobileledger.async.DbOpQueue;
36 import net.ktnx.mobileledger.dao.AccountDAO;
37 import net.ktnx.mobileledger.dao.OptionDAO;
38 import net.ktnx.mobileledger.dao.TransactionDAO;
39 import net.ktnx.mobileledger.db.DB;
40 import net.ktnx.mobileledger.json.API;
41 import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
42 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
43 import net.ktnx.mobileledger.utils.Logger;
44 import net.ktnx.mobileledger.utils.Misc;
45 import net.ktnx.mobileledger.utils.SimpleDate;
46
47 import org.jetbrains.annotations.Contract;
48
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Locale;
53 import java.util.Map;
54 import java.util.Objects;
55
56 import static net.ktnx.mobileledger.utils.Logger.debug;
57
58 public final class MobileLedgerProfile {
59     // N.B. when adding new fields, update the copy-constructor below
60     private final long id;
61     private String name;
62     private boolean permitPosting;
63     private boolean showCommentsByDefault;
64     private boolean showCommodityByDefault;
65     private String defaultCommodity;
66     private String preferredAccountsFilter;
67     private String url;
68     private boolean authEnabled;
69     private String authUserName;
70     private String authPassword;
71     private int themeHue;
72     private int orderNo = -1;
73     private API apiVersion = API.auto;
74     private FutureDates futureDates = FutureDates.None;
75     private boolean accountsLoaded;
76     private boolean transactionsLoaded;
77     private HledgerVersion detectedVersion;
78     // N.B. when adding new fields, update the copy-constructor below
79     transient private AccountAndTransactionListSaver accountAndTransactionListSaver;
80     public MobileLedgerProfile(long id) {
81         this.id = id;
82     }
83     public MobileLedgerProfile(MobileLedgerProfile origin) {
84         id = origin.id;
85         name = origin.name;
86         permitPosting = origin.permitPosting;
87         showCommentsByDefault = origin.showCommentsByDefault;
88         showCommodityByDefault = origin.showCommodityByDefault;
89         preferredAccountsFilter = origin.preferredAccountsFilter;
90         url = origin.url;
91         authEnabled = origin.authEnabled;
92         authUserName = origin.authUserName;
93         authPassword = origin.authPassword;
94         themeHue = origin.themeHue;
95         orderNo = origin.orderNo;
96         futureDates = origin.futureDates;
97         apiVersion = origin.apiVersion;
98         defaultCommodity = origin.defaultCommodity;
99         accountsLoaded = origin.accountsLoaded;
100         transactionsLoaded = origin.transactionsLoaded;
101         if (origin.detectedVersion != null)
102             detectedVersion = new HledgerVersion(origin.detectedVersion);
103     }
104     // loads all profiles into Data.profiles
105     // returns the profile with the given UUID
106     public static MobileLedgerProfile loadAllFromDB(long currentProfileId) {
107         MobileLedgerProfile result = null;
108         ArrayList<MobileLedgerProfile> list = new ArrayList<>();
109         SQLiteDatabase db = App.getDatabase();
110         try (Cursor cursor = db.rawQuery("SELECT id, name, url, use_authentication, auth_user, " +
111                                          "auth_password, permit_posting, theme, order_no, " +
112                                          "preferred_accounts_filter, future_dates, api_version, " +
113                                          "show_commodity_by_default, default_commodity, " +
114                                          "show_comments_by_default, detected_version_pre_1_19, " +
115                                          "detected_version_major, detected_version_minor FROM " +
116                                          "profiles order by order_no", null))
117         {
118             while (cursor.moveToNext()) {
119                 MobileLedgerProfile item = new MobileLedgerProfile(cursor.getLong(0));
120                 item.setName(cursor.getString(1));
121                 item.setUrl(cursor.getString(2));
122                 item.setAuthEnabled(cursor.getInt(3) == 1);
123                 item.setAuthUserName(cursor.getString(4));
124                 item.setAuthPassword(cursor.getString(5));
125                 item.setPostingPermitted(cursor.getInt(6) == 1);
126                 item.setThemeId(cursor.getInt(7));
127                 item.orderNo = cursor.getInt(8);
128                 item.setPreferredAccountsFilter(cursor.getString(9));
129                 item.setFutureDates(cursor.getInt(10));
130                 item.setApiVersion(cursor.getInt(11));
131                 item.setShowCommodityByDefault(cursor.getInt(12) == 1);
132                 item.setDefaultCommodity(cursor.getString(13));
133                 item.setShowCommentsByDefault(cursor.getInt(14) == 1);
134                 {
135                     boolean pre_1_20 = cursor.getInt(15) == 1;
136                     int major = cursor.getInt(16);
137                     int minor = cursor.getInt(17);
138
139                     if (!pre_1_20 && major == 0 && minor == 0) {
140                         item.detectedVersion = null;
141                     }
142                     else if (pre_1_20) {
143                         item.detectedVersion = new HledgerVersion(true);
144                     }
145                     else {
146                         item.detectedVersion = new HledgerVersion(major, minor);
147                     }
148                 }
149                 list.add(item);
150                 if (item.getId() == currentProfileId)
151                     result = item;
152             }
153         }
154         Data.profiles.postValue(list);
155         return result;
156     }
157     public static void storeProfilesOrder() {
158         SQLiteDatabase db = App.getDatabase();
159         db.beginTransactionNonExclusive();
160         try {
161             int orderNo = 0;
162             for (MobileLedgerProfile p : Objects.requireNonNull(Data.profiles.getValue())) {
163                 db.execSQL("update profiles set order_no=? where uuid=?",
164                         new Object[]{orderNo, p.getId()});
165                 p.orderNo = orderNo;
166                 orderNo++;
167             }
168             db.setTransactionSuccessful();
169         }
170         finally {
171             db.endTransaction();
172         }
173     }
174     static public void startEditProfileActivity(Context context, MobileLedgerProfile profile) {
175         Intent intent = new Intent(context, ProfileDetailActivity.class);
176         Bundle args = new Bundle();
177         if (profile != null) {
178             int index = Data.getProfileIndex(profile);
179             if (index != -1)
180                 intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
181         }
182         intent.putExtras(args);
183         context.startActivity(intent, args);
184     }
185     public HledgerVersion getDetectedVersion() {
186         return detectedVersion;
187     }
188     public void setDetectedVersion(HledgerVersion detectedVersion) {
189         this.detectedVersion = detectedVersion;
190     }
191     @Contract(value = "null -> false", pure = true)
192     @Override
193     public boolean equals(@Nullable Object obj) {
194         if (obj == null)
195             return false;
196         if (obj == this)
197             return true;
198         if (obj.getClass() != this.getClass())
199             return false;
200
201         MobileLedgerProfile p = (MobileLedgerProfile) obj;
202         if (id != p.id)
203             return false;
204         if (!name.equals(p.name))
205             return false;
206         if (permitPosting != p.permitPosting)
207             return false;
208         if (showCommentsByDefault != p.showCommentsByDefault)
209             return false;
210         if (showCommodityByDefault != p.showCommodityByDefault)
211             return false;
212         if (!Objects.equals(defaultCommodity, p.defaultCommodity))
213             return false;
214         if (!Objects.equals(preferredAccountsFilter, p.preferredAccountsFilter))
215             return false;
216         if (!Objects.equals(url, p.url))
217             return false;
218         if (authEnabled != p.authEnabled)
219             return false;
220         if (!Objects.equals(authUserName, p.authUserName))
221             return false;
222         if (!Objects.equals(authPassword, p.authPassword))
223             return false;
224         if (themeHue != p.themeHue)
225             return false;
226         if (apiVersion != p.apiVersion)
227             return false;
228         if (!Objects.equals(detectedVersion, p.detectedVersion))
229             return false;
230         return futureDates == p.futureDates;
231     }
232     public boolean getShowCommentsByDefault() {
233         return showCommentsByDefault;
234     }
235     public void setShowCommentsByDefault(boolean newValue) {
236         this.showCommentsByDefault = newValue;
237     }
238     public boolean getShowCommodityByDefault() {
239         return showCommodityByDefault;
240     }
241     public void setShowCommodityByDefault(boolean showCommodityByDefault) {
242         this.showCommodityByDefault = showCommodityByDefault;
243     }
244     public String getDefaultCommodity() {
245         return defaultCommodity;
246     }
247     public void setDefaultCommodity(String defaultCommodity) {
248         this.defaultCommodity = defaultCommodity;
249     }
250     public void setDefaultCommodity(CharSequence defaultCommodity) {
251         if (defaultCommodity == null)
252             this.defaultCommodity = null;
253         else
254             this.defaultCommodity = String.valueOf(defaultCommodity);
255     }
256     public API getApiVersion() {
257         return apiVersion;
258     }
259     public void setApiVersion(API apiVersion) {
260         this.apiVersion = apiVersion;
261     }
262     public void setApiVersion(int apiVersion) {
263         this.apiVersion = API.valueOf(apiVersion);
264     }
265     public FutureDates getFutureDates() {
266         return futureDates;
267     }
268     public void setFutureDates(int anInt) {
269         futureDates = FutureDates.valueOf(anInt);
270     }
271     public void setFutureDates(FutureDates futureDates) {
272         this.futureDates = futureDates;
273     }
274     public String getPreferredAccountsFilter() {
275         return preferredAccountsFilter;
276     }
277     public void setPreferredAccountsFilter(String preferredAccountsFilter) {
278         this.preferredAccountsFilter = preferredAccountsFilter;
279     }
280     public void setPreferredAccountsFilter(CharSequence preferredAccountsFilter) {
281         setPreferredAccountsFilter(String.valueOf(preferredAccountsFilter));
282     }
283     public boolean isPostingPermitted() {
284         return permitPosting;
285     }
286     public void setPostingPermitted(boolean permitPosting) {
287         this.permitPosting = permitPosting;
288     }
289     public long getId() {
290         return id;
291     }
292     public String getName() {
293         return name;
294     }
295     public void setName(CharSequence text) {
296         setName(String.valueOf(text));
297     }
298     public void setName(String name) {
299         this.name = name;
300     }
301     public String getUrl() {
302         return url;
303     }
304     public void setUrl(CharSequence text) {
305         setUrl(String.valueOf(text));
306     }
307     public void setUrl(String url) {
308         this.url = url;
309     }
310     public boolean isAuthEnabled() {
311         return authEnabled;
312     }
313     public void setAuthEnabled(boolean authEnabled) {
314         this.authEnabled = authEnabled;
315     }
316     public String getAuthUserName() {
317         return authUserName;
318     }
319     public void setAuthUserName(CharSequence text) {
320         setAuthUserName(String.valueOf(text));
321     }
322     public void setAuthUserName(String authUserName) {
323         this.authUserName = authUserName;
324     }
325     public String getAuthPassword() {
326         return authPassword;
327     }
328     public void setAuthPassword(CharSequence text) {
329         setAuthPassword(String.valueOf(text));
330     }
331     public void setAuthPassword(String authPassword) {
332         this.authPassword = authPassword;
333     }
334     public void storeInDB() {
335         SQLiteDatabase db = App.getDatabase();
336         db.beginTransactionNonExclusive();
337         try {
338 //            debug("profiles", String.format("Storing profile in DB: uuid=%s, name=%s, " +
339 //                                            "url=%s, permit_posting=%s, authEnabled=%s, " +
340 //                                            "themeHue=%d", uuid, name, url,
341 //                    permitPosting ? "TRUE" : "FALSE", authEnabled ? "TRUE" : "FALSE", themeHue));
342             db.execSQL("REPLACE INTO profiles(id, name, permit_posting, url, " +
343                        "use_authentication, auth_user, auth_password, theme, order_no, " +
344                        "preferred_accounts_filter, future_dates, api_version, " +
345                        "show_commodity_by_default, default_commodity, show_comments_by_default," +
346                        "detected_version_pre_1_19, detected_version_major, " +
347                        "detected_version_minor) " +
348                        "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
349                     new Object[]{id, name, permitPosting, url, authEnabled,
350                                  authEnabled ? authUserName : null,
351                                  authEnabled ? authPassword : null, themeHue, orderNo,
352                                  preferredAccountsFilter, futureDates.toInt(), apiVersion.toInt(),
353                                  showCommodityByDefault, defaultCommodity, showCommentsByDefault,
354                                  (detectedVersion != null) && detectedVersion.isPre_1_20_1(),
355                                  (detectedVersion == null) ? 0 : detectedVersion.getMajor(),
356                                  (detectedVersion == null) ? 0 : detectedVersion.getMinor()
357                     });
358             db.setTransactionSuccessful();
359         }
360         finally {
361             db.endTransaction();
362         }
363     }
364     public void storeAccount(SQLiteDatabase db, int generation, LedgerAccount acc,
365                              boolean storeUiFields) {
366         // replace into is a bad idea because it would reset hidden to its default value
367         // we like the default, but for new accounts only
368         String sql = "update accounts set generation = ?";
369         List<Object> params = new ArrayList<>();
370         params.add(generation);
371         if (storeUiFields) {
372             sql += ", expanded=?";
373             params.add(acc.isExpanded() ? 1 : 0);
374         }
375         sql += " where profile_id=? and name=?";
376         params.add(id);
377         params.add(acc.getName());
378         db.execSQL(sql, params.toArray());
379
380         db.execSQL("insert into accounts(profile_id, name, name_upper, parent_name, level, " +
381                    "expanded, generation) select ?,?,?,?,?,0,? where (select changes() = 0)",
382                 new Object[]{id, acc.getName(), acc.getName().toUpperCase(), acc.getParentName(),
383                              acc.getLevel(), generation
384                 });
385 //        debug("accounts", String.format("Stored account '%s' in DB [%s]", acc.getName(), uuid));
386     }
387     public void storeAccountValue(SQLiteDatabase db, int generation, String name, String currency,
388                                   Float amount) {
389         if (!TextUtils.isEmpty(currency)) {
390             boolean exists;
391             try (Cursor c = db.rawQuery("select 1 from currencies where name=?",
392                     new String[]{currency}))
393             {
394                 exists = c.moveToFirst();
395             }
396             if (!exists) {
397                 db.execSQL(
398                         "insert into currencies(id, name, position, has_gap) values((select max" +
399                         "(id) from currencies)+1, ?, ?, ?)", new Object[]{currency,
400                                                                           Objects.requireNonNull(
401                                                                                   Data.currencySymbolPosition.getValue()).toString(),
402                                                                           Data.currencyGap.getValue()
403                         });
404             }
405         }
406
407         long accId = findAddAccount(db, name);
408
409         db.execSQL("replace into account_values(account_id, " +
410                    "currency, value, generation) values(?, ?, ?, ?);",
411                 new Object[]{accId, Misc.emptyIsNull(currency), amount, generation});
412     }
413     private long findAddAccount(SQLiteDatabase db, String accountName) {
414         try (Cursor c = db.rawQuery("select id from accounts where profile_id=? and name=?",
415                 new String[]{String.valueOf(id), accountName}))
416         {
417             if (c.moveToFirst())
418                 return c.getLong(0);
419
420         }
421
422         try (Cursor c = db.rawQuery(
423                 "insert into accounts(profile_id, name, name_upper) values(?, ?, ?) returning id",
424                 new String[]{String.valueOf(id), accountName, accountName.toUpperCase()}))
425         {
426             c.moveToFirst();
427             return c.getInt(0);
428         }
429     }
430     public void storeTransaction(SQLiteDatabase db, int generation, LedgerTransaction tr) {
431         tr.fillDataHash();
432 //        Logger.debug("storeTransaction", String.format(Locale.US, "ID %d", tr.getId()));
433         SimpleDate d = tr.getDate();
434         db.execSQL("UPDATE transactions SET year=?, month=?, day=?, description=?, comment=?, " +
435                    "data_hash=?, generation=? WHERE profile_id=? AND ledger_id=?",
436                 new Object[]{d.year, d.month, d.day, tr.getDescription(), tr.getComment(),
437                              tr.getDataHash(), generation, id, tr.getId()
438                 });
439         db.execSQL(
440                 "INSERT INTO transactions(profile_id, ledger_id, year, month, day, description, " +
441                 "comment, data_hash, generation) " +
442                 "select ?,?,?,?,?,?,?,?,? WHERE (select changes() = 0)",
443                 new Object[]{id, tr.getId(), tr.getDate().year, tr.getDate().month,
444                              tr.getDate().day, tr.getDescription(), tr.getComment(),
445                              tr.getDataHash(), generation
446                 });
447
448         int accountOrderNo = 1;
449         for (LedgerTransactionAccount item : tr.getAccounts()) {
450             db.execSQL("UPDATE transaction_accounts SET account_name=?, amount=?, currency=?, " +
451                        "comment=?, generation=? " + "WHERE transaction_id=? AND order_no=?",
452                     new Object[]{item.getAccountName(), item.getAmount(),
453                                  Misc.nullIsEmpty(item.getCurrency()), item.getComment(),
454                                  generation, tr.getId(), accountOrderNo
455                     });
456             db.execSQL("INSERT INTO transaction_accounts(transaction_id, " +
457                        "order_no, account_name, amount, currency, comment, generation) " +
458                        "select ?, ?, ?, ?, ?, ?, ? WHERE (select changes() = 0)",
459                     new Object[]{tr.getId(), accountOrderNo, item.getAccountName(),
460                                  item.getAmount(), Misc.nullIsEmpty(item.getCurrency()),
461                                  item.getComment(), generation
462                     });
463
464             accountOrderNo++;
465         }
466 //        debug("profile", String.format("Transaction %d stored", tr.getId()));
467     }
468     public String getOption(String name, String default_value) {
469         SQLiteDatabase db = App.getDatabase();
470         try (Cursor cursor = db.rawQuery(
471                 "select value from options where profile_id = ? and name=?",
472                 new String[]{String.valueOf(id), name}))
473         {
474             if (cursor.moveToFirst()) {
475                 String result = cursor.getString(0);
476
477                 if (result == null) {
478                     debug("profile", "returning default value for " + name);
479                     result = default_value;
480                 }
481                 else
482                     debug("profile", String.format("option %s=%s", name, result));
483
484                 return result;
485             }
486             else
487                 return default_value;
488         }
489         catch (Exception e) {
490             debug("db", "returning default value for " + name, e);
491             return default_value;
492         }
493     }
494     public long getLongOption(String name, long default_value) {
495         long longResult;
496         String result = getOption(name, "");
497         if ((result == null) || result.isEmpty()) {
498             debug("profile", String.format("Returning default value for option %s", name));
499             longResult = default_value;
500         }
501         else {
502             try {
503                 longResult = Long.parseLong(result);
504                 debug("profile", String.format("option %s=%s", name, result));
505             }
506             catch (Exception e) {
507                 debug("profile", String.format("Returning default value for option %s", name), e);
508                 longResult = default_value;
509             }
510         }
511
512         return longResult;
513     }
514     public void setOption(String name, String value) {
515         debug("profile", String.format("setting option %s=%s", name, value));
516         DbOpQueue.add("insert or replace into options(profile_id, name, value) values(?, ?, ?);",
517                 new String[]{String.valueOf(id), name, value});
518     }
519     public void setLongOption(String name, long value) {
520         setOption(name, String.valueOf(value));
521     }
522     public void removeFromDB() {
523         SQLiteDatabase db = App.getDatabase();
524         debug("db", String.format(Locale.ROOT, "removing profile %d from DB", id));
525         db.beginTransactionNonExclusive();
526         try {
527             Object[] id_param = new Object[]{id};
528             db.execSQL("delete from transactions where profile_id=?", id_param);
529             db.execSQL("delete from accounts where profile=?", id_param);
530             db.execSQL("delete from options where profile=?", id_param);
531             db.execSQL("delete from profiles where id=?", id_param);
532             db.setTransactionSuccessful();
533         }
534         finally {
535             db.endTransaction();
536         }
537     }
538     public LedgerTransaction loadTransaction(int transactionId) {
539         LedgerTransaction tr = new LedgerTransaction(transactionId, this.id);
540         tr.loadData(App.getDatabase());
541
542         return tr;
543     }
544     public int getThemeHue() {
545 //        debug("profile", String.format("Profile.getThemeHue() returning %d", themeHue));
546         return this.themeHue;
547     }
548     public void setThemeHue(Object o) {
549         setThemeId(Integer.parseInt(String.valueOf(o)));
550     }
551     public void setThemeId(int themeHue) {
552 //        debug("profile", String.format("Profile.setThemeHue(%d) called", themeHue));
553         this.themeHue = themeHue;
554     }
555     public int getNextTransactionsGeneration(SQLiteDatabase db) {
556         try (Cursor c = db.rawQuery(
557                 "SELECT generation FROM transactions WHERE profile_id=? LIMIT 1",
558                 new String[]{String.valueOf(id)}))
559         {
560             if (c.moveToFirst())
561                 return c.getInt(0) + 1;
562         }
563         return 1;
564     }
565     private int getNextAccountsGeneration(SQLiteDatabase db) {
566         try (Cursor c = db.rawQuery("SELECT generation FROM accounts WHERE profile_id=? LIMIT 1",
567                 new String[]{String.valueOf(id)}))
568         {
569             if (c.moveToFirst())
570                 return c.getInt(0) + 1;
571         }
572         return 1;
573     }
574     private void deleteNotPresentAccounts(SQLiteDatabase db, int generation) {
575         Logger.debug("db/benchmark", "Deleting obsolete accounts");
576         db.execSQL("DELETE FROM account_values WHERE (select a.profile_id from accounts a where a" +
577                    ".id=account_values.account_id)=? AND generation <> ?",
578                 new Object[]{id, generation});
579         db.execSQL("DELETE FROM accounts WHERE profile_id=? AND generation <> ?",
580                 new Object[]{id, generation});
581         Logger.debug("db/benchmark", "Done deleting obsolete accounts");
582     }
583     private void deleteNotPresentTransactions(SQLiteDatabase db, int generation) {
584         Logger.debug("db/benchmark", "Deleting obsolete transactions");
585         db.execSQL(
586                 "DELETE FROM transaction_accounts WHERE (select t.profile_id from transactions t " +
587                 "where t.id=transaction_accounts.transaction_id)=? AND generation" + " <> ?",
588                 new Object[]{id, generation});
589         db.execSQL("DELETE FROM transactions WHERE profile_id=? AND generation <> ?",
590                 new Object[]{id, generation});
591         Logger.debug("db/benchmark", "Done deleting obsolete transactions");
592     }
593     @Transaction
594     public void wipeAllDataSync() {
595         OptionDAO optDao = DB.get()
596                              .getOptionDAO();
597         optDao.deleteSync(optDao.allForProfileSync(id));
598
599         AccountDAO accDao = DB.get()
600                               .getAccountDAO();
601         accDao.deleteSync(accDao.allForProfileSync(id));
602
603         TransactionDAO trnDao = DB.get()
604                                   .getTransactionDAO();
605         trnDao.deleteSync(trnDao.allForProfileSync(id));
606     }
607     public void wipeAllData() {
608         AsyncTask.execute(this::wipeAllDataSync);
609     }
610     public List<Currency> getCurrencies() {
611         SQLiteDatabase db = App.getDatabase();
612
613         ArrayList<Currency> result = new ArrayList<>();
614
615         try (Cursor c = db.rawQuery("SELECT c.id, c.name, c.position, c.has_gap FROM currencies c",
616                 new String[]{}))
617         {
618             while (c.moveToNext()) {
619                 Currency currency = new Currency(c.getInt(0), c.getString(1),
620                         Currency.Position.valueOf(c.getString(2)), c.getInt(3) == 1);
621                 result.add(currency);
622             }
623         }
624
625         return result;
626     }
627     Currency loadCurrencyByName(String name) {
628         SQLiteDatabase db = App.getDatabase();
629         Currency result = tryLoadCurrencyByName(db, name);
630         if (result == null)
631             throw new RuntimeException(String.format("Unable to load currency '%s'", name));
632         return result;
633     }
634     private Currency tryLoadCurrencyByName(SQLiteDatabase db, String name) {
635         try (Cursor cursor = db.rawQuery(
636                 "SELECT c.id, c.name, c.position, c.has_gap FROM currencies c WHERE c.name=?",
637                 new String[]{name}))
638         {
639             if (cursor.moveToFirst()) {
640                 return new Currency(cursor.getInt(0), cursor.getString(1),
641                         Currency.Position.valueOf(cursor.getString(2)), cursor.getInt(3) == 1);
642             }
643             return null;
644         }
645     }
646     public void storeAccountAndTransactionListAsync(List<LedgerAccount> accounts,
647                                                     List<LedgerTransaction> transactions) {
648         if (accountAndTransactionListSaver != null)
649             accountAndTransactionListSaver.interrupt();
650
651         accountAndTransactionListSaver =
652                 new AccountAndTransactionListSaver(this, accounts, transactions);
653         accountAndTransactionListSaver.start();
654     }
655     private Currency tryLoadCurrencyById(SQLiteDatabase db, int id) {
656         try (Cursor cursor = db.rawQuery(
657                 "SELECT c.id, c.name, c.position, c.has_gap FROM currencies c WHERE c.id=?",
658                 new String[]{String.valueOf(id)}))
659         {
660             if (cursor.moveToFirst()) {
661                 return new Currency(cursor.getInt(0), cursor.getString(1),
662                         Currency.Position.valueOf(cursor.getString(2)), cursor.getInt(3) == 1);
663             }
664             return null;
665         }
666     }
667     public Currency loadCurrencyById(int id) {
668         SQLiteDatabase db = App.getDatabase();
669         Currency result = tryLoadCurrencyById(db, id);
670         if (result == null)
671             throw new RuntimeException(String.format("Unable to load currency with id '%d'", id));
672         return result;
673     }
674
675     public enum FutureDates {
676         None(0), OneWeek(7), TwoWeeks(14), OneMonth(30), TwoMonths(60), ThreeMonths(90),
677         SixMonths(180), OneYear(365), All(-1);
678         private static final SparseArray<FutureDates> map = new SparseArray<>();
679
680         static {
681             for (FutureDates item : FutureDates.values()) {
682                 map.put(item.value, item);
683             }
684         }
685
686         private final int value;
687         FutureDates(int value) {
688             this.value = value;
689         }
690         public static FutureDates valueOf(int i) {
691             return map.get(i, None);
692         }
693         public int toInt() {
694             return this.value;
695         }
696         public String getText(Resources resources) {
697             switch (value) {
698                 case 7:
699                     return resources.getString(R.string.future_dates_7);
700                 case 14:
701                     return resources.getString(R.string.future_dates_14);
702                 case 30:
703                     return resources.getString(R.string.future_dates_30);
704                 case 60:
705                     return resources.getString(R.string.future_dates_60);
706                 case 90:
707                     return resources.getString(R.string.future_dates_90);
708                 case 180:
709                     return resources.getString(R.string.future_dates_180);
710                 case 365:
711                     return resources.getString(R.string.future_dates_365);
712                 case -1:
713                     return resources.getString(R.string.future_dates_all);
714                 default:
715                     return resources.getString(R.string.future_dates_none);
716             }
717         }
718     }
719
720     private static class AccountAndTransactionListSaver extends Thread {
721         private final MobileLedgerProfile profile;
722         private final List<LedgerAccount> accounts;
723         private final List<LedgerTransaction> transactions;
724         AccountAndTransactionListSaver(MobileLedgerProfile profile, List<LedgerAccount> accounts,
725                                        List<LedgerTransaction> transactions) {
726             this.accounts = accounts;
727             this.transactions = transactions;
728             this.profile = profile;
729         }
730         public int getNextDescriptionsGeneration(SQLiteDatabase db) {
731             int generation = 1;
732             try (Cursor c = db.rawQuery("SELECT generation FROM description_history LIMIT 1",
733                     null))
734             {
735                 if (c.moveToFirst()) {
736                     generation = c.getInt(0) + 1;
737                 }
738             }
739             return generation;
740         }
741         void deleteNotPresentDescriptions(SQLiteDatabase db, int generation) {
742             Logger.debug("db/benchmark", "Deleting obsolete descriptions");
743             db.execSQL("DELETE FROM description_history WHERE generation <> ?",
744                     new Object[]{generation});
745             db.execSQL("DELETE FROM description_history WHERE generation <> ?",
746                     new Object[]{generation});
747             Logger.debug("db/benchmark", "Done deleting obsolete descriptions");
748         }
749         @Override
750         public void run() {
751             SQLiteDatabase db = App.getDatabase();
752             db.beginTransactionNonExclusive();
753             try {
754                 int accountsGeneration = profile.getNextAccountsGeneration(db);
755                 if (isInterrupted())
756                     return;
757
758                 int transactionsGeneration = profile.getNextTransactionsGeneration(db);
759                 if (isInterrupted())
760                     return;
761
762                 for (LedgerAccount acc : accounts) {
763                     profile.storeAccount(db, accountsGeneration, acc, false);
764                     if (isInterrupted())
765                         return;
766                     for (LedgerAmount amt : acc.getAmounts()) {
767                         profile.storeAccountValue(db, accountsGeneration, acc.getName(),
768                                 amt.getCurrency(), amt.getAmount());
769                         if (isInterrupted())
770                             return;
771                     }
772                 }
773
774                 for (LedgerTransaction tr : transactions) {
775                     profile.storeTransaction(db, transactionsGeneration, tr);
776                     if (isInterrupted())
777                         return;
778                 }
779
780                 profile.deleteNotPresentTransactions(db, transactionsGeneration);
781                 if (isInterrupted()) {
782                     return;
783                 }
784                 profile.deleteNotPresentAccounts(db, accountsGeneration);
785                 if (isInterrupted())
786                     return;
787
788                 Map<String, Boolean> unique = new HashMap<>();
789
790                 debug("descriptions", "Starting refresh");
791                 int descriptionsGeneration = getNextDescriptionsGeneration(db);
792                 try (Cursor c = db.rawQuery("SELECT distinct description from transactions",
793                         null))
794                 {
795                     while (c.moveToNext()) {
796                         String description = c.getString(0);
797                         String descriptionUpper = description.toUpperCase();
798                         if (unique.containsKey(descriptionUpper))
799                             continue;
800
801                         storeDescription(db, descriptionsGeneration, description, descriptionUpper);
802
803                         unique.put(descriptionUpper, true);
804                     }
805                 }
806                 deleteNotPresentDescriptions(db, descriptionsGeneration);
807
808                 db.setTransactionSuccessful();
809             }
810             finally {
811                 db.endTransaction();
812             }
813         }
814         private void storeDescription(SQLiteDatabase db, int generation, String description,
815                                       String descriptionUpper) {
816             db.execSQL("UPDATE description_history SET description=?, generation=? WHERE " +
817                        "description_upper=?", new Object[]{description, generation, descriptionUpper
818             });
819             db.execSQL(
820                     "INSERT INTO description_history(description, description_upper, generation) " +
821                     "select ?,?,? WHERE (select changes() = 0)",
822                     new Object[]{description, descriptionUpper, generation
823                     });
824         }
825     }
826 }