]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
rework account list management to be fully asynchronous
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / MobileLedgerProfile.java
1 /*
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.
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.res.Resources;
21 import android.database.Cursor;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.os.Build;
24 import android.text.TextUtils;
25 import android.util.SparseArray;
26
27 import androidx.annotation.Nullable;
28 import androidx.lifecycle.LiveData;
29 import androidx.lifecycle.MutableLiveData;
30
31 import net.ktnx.mobileledger.App;
32 import net.ktnx.mobileledger.R;
33 import net.ktnx.mobileledger.async.DbOpQueue;
34 import net.ktnx.mobileledger.async.SendTransactionTask;
35 import net.ktnx.mobileledger.utils.LockHolder;
36 import net.ktnx.mobileledger.utils.Locker;
37 import net.ktnx.mobileledger.utils.Logger;
38 import net.ktnx.mobileledger.utils.MLDB;
39 import net.ktnx.mobileledger.utils.Misc;
40
41 import org.jetbrains.annotations.Contract;
42
43 import java.util.ArrayList;
44 import java.util.Calendar;
45 import java.util.Date;
46 import java.util.HashMap;
47 import java.util.Iterator;
48 import java.util.List;
49 import java.util.Locale;
50 import java.util.Map;
51 import java.util.Objects;
52
53 import static net.ktnx.mobileledger.utils.Logger.debug;
54
55 public final class MobileLedgerProfile {
56     private final MutableLiveData<List<LedgerAccount>> displayedAccounts;
57     private final MutableLiveData<List<LedgerTransaction>> allTransactions;
58     private final MutableLiveData<List<LedgerTransaction>> displayedTransactions;
59     // N.B. when adding new fields, update the copy-constructor below
60     private final String uuid;
61     private final Locker accountsLocker = new Locker();
62     private List<LedgerAccount> allAccounts;
63     private String name;
64     private boolean permitPosting;
65     private boolean showCommentsByDefault;
66     private boolean showCommodityByDefault;
67     private String defaultCommodity;
68     private String preferredAccountsFilter;
69     private String url;
70     private boolean authEnabled;
71     private String authUserName;
72     private String authPassword;
73     private int themeHue;
74     private int orderNo = -1;
75     private SendTransactionTask.API apiVersion = SendTransactionTask.API.auto;
76     private Calendar firstTransactionDate;
77     private Calendar lastTransactionDate;
78     private FutureDates futureDates = FutureDates.None;
79     private boolean accountsLoaded;
80     private boolean transactionsLoaded;
81     // N.B. when adding new fields, update the copy-constructor below
82     transient private AccountListLoader loader = null;
83     transient private Thread displayedAccountsUpdater;
84     transient private AccountListSaver accountListSaver;
85     transient private TransactionListSaver transactionListSaver;
86     transient private AccountAndTransactionListSaver accountAndTransactionListSaver;
87     private Map<String, LedgerAccount> accountMap = new HashMap<>();
88     public MobileLedgerProfile(String uuid) {
89         this.uuid = uuid;
90         allAccounts = new ArrayList<>();
91         displayedAccounts = new MutableLiveData<>();
92         allTransactions = new MutableLiveData<>(new ArrayList<>());
93         displayedTransactions = new MutableLiveData<>(new ArrayList<>());
94     }
95     public MobileLedgerProfile(MobileLedgerProfile origin) {
96         uuid = origin.uuid;
97         name = origin.name;
98         permitPosting = origin.permitPosting;
99         showCommentsByDefault = origin.showCommentsByDefault;
100         showCommodityByDefault = origin.showCommodityByDefault;
101         preferredAccountsFilter = origin.preferredAccountsFilter;
102         url = origin.url;
103         authEnabled = origin.authEnabled;
104         authUserName = origin.authUserName;
105         authPassword = origin.authPassword;
106         themeHue = origin.themeHue;
107         orderNo = origin.orderNo;
108         futureDates = origin.futureDates;
109         apiVersion = origin.apiVersion;
110         defaultCommodity = origin.defaultCommodity;
111         firstTransactionDate = origin.firstTransactionDate;
112         lastTransactionDate = origin.lastTransactionDate;
113         displayedAccounts = origin.displayedAccounts;
114         allAccounts = origin.allAccounts;
115         accountMap = origin.accountMap;
116         displayedTransactions = origin.displayedTransactions;
117         allTransactions = origin.allTransactions;
118         accountsLoaded = origin.accountsLoaded;
119         transactionsLoaded = origin.transactionsLoaded;
120     }
121     // loads all profiles into Data.profiles
122     // returns the profile with the given UUID
123     public static MobileLedgerProfile loadAllFromDB(@Nullable String currentProfileUUID) {
124         MobileLedgerProfile result = null;
125         ArrayList<MobileLedgerProfile> list = new ArrayList<>();
126         SQLiteDatabase db = App.getDatabase();
127         try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
128                                          "auth_password, permit_posting, theme, order_no, " +
129                                          "preferred_accounts_filter, future_dates, api_version, " +
130                                          "show_commodity_by_default, default_commodity, " +
131                                          "show_comments_by_default FROM " +
132                                          "profiles order by order_no", null))
133         {
134             while (cursor.moveToNext()) {
135                 MobileLedgerProfile item = new MobileLedgerProfile(cursor.getString(0));
136                 item.setName(cursor.getString(1));
137                 item.setUrl(cursor.getString(2));
138                 item.setAuthEnabled(cursor.getInt(3) == 1);
139                 item.setAuthUserName(cursor.getString(4));
140                 item.setAuthPassword(cursor.getString(5));
141                 item.setPostingPermitted(cursor.getInt(6) == 1);
142                 item.setThemeId(cursor.getInt(7));
143                 item.orderNo = cursor.getInt(8);
144                 item.setPreferredAccountsFilter(cursor.getString(9));
145                 item.setFutureDates(cursor.getInt(10));
146                 item.setApiVersion(cursor.getInt(11));
147                 item.setShowCommodityByDefault(cursor.getInt(12) == 1);
148                 item.setDefaultCommodity(cursor.getString(13));
149                 item.setShowCommentsByDefault(cursor.getInt(14) == 1);
150                 list.add(item);
151                 if (item.getUuid()
152                         .equals(currentProfileUUID))
153                     result = item;
154             }
155         }
156         Data.profiles.setValue(list);
157         return result;
158     }
159     public static void storeProfilesOrder() {
160         SQLiteDatabase db = App.getDatabase();
161         db.beginTransactionNonExclusive();
162         try {
163             int orderNo = 0;
164             for (MobileLedgerProfile p : Data.profiles.getValue()) {
165                 db.execSQL("update profiles set order_no=? where uuid=?",
166                         new Object[]{orderNo, p.getUuid()});
167                 p.orderNo = orderNo;
168                 orderNo++;
169             }
170             db.setTransactionSuccessful();
171         }
172         finally {
173             db.endTransaction();
174         }
175     }
176     public static ArrayList<LedgerAccount> mergeAccountLists(List<LedgerAccount> oldList,
177                                                              List<LedgerAccount> newList) {
178         LedgerAccount oldAcc, newAcc;
179         ArrayList<LedgerAccount> merged = new ArrayList<>();
180
181         Iterator<LedgerAccount> oldIterator = oldList.iterator();
182         Iterator<LedgerAccount> newIterator = newList.iterator();
183
184         while (true) {
185             if (!oldIterator.hasNext()) {
186                 // the rest of the incoming are new
187                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
188                     newIterator.forEachRemaining(merged::add);
189                 }
190                 else {
191                     while (newIterator.hasNext())
192                         merged.add(newIterator.next());
193                 }
194                 break;
195             }
196             oldAcc = oldIterator.next();
197
198             if (!newIterator.hasNext()) {
199                 // no more incoming accounts. ignore the rest of the old
200                 break;
201             }
202             newAcc = newIterator.next();
203
204             // ignore now missing old items
205             if (oldAcc.getName()
206                       .compareTo(newAcc.getName()) < 0)
207                 continue;
208
209             // add newly found items
210             if (oldAcc.getName()
211                       .compareTo(newAcc.getName()) > 0)
212             {
213                 merged.add(newAcc);
214                 continue;
215             }
216
217             // two items with same account names; merge UI-controlled fields
218             oldAcc.setExpanded(newAcc.isExpanded());
219             oldAcc.setAmountsExpanded(newAcc.amountsExpanded());
220             merged.add(oldAcc);
221         }
222
223         return merged;
224     }
225     public void mergeAccountList(List<LedgerAccount> newList) {
226
227         try (LockHolder l = accountsLocker.lockForWriting()) {
228             allAccounts = mergeAccountLists(allAccounts, newList);
229             updateAccountsMap(allAccounts);
230         }
231     }
232     public LiveData<List<LedgerAccount>> getDisplayedAccounts() {
233         return displayedAccounts;
234     }
235     @Contract(value = "null -> false", pure = true)
236     @Override
237     public boolean equals(@Nullable Object obj) {
238         if (obj == null)
239             return false;
240         if (obj == this)
241             return true;
242         if (obj.getClass() != this.getClass())
243             return false;
244
245         MobileLedgerProfile p = (MobileLedgerProfile) obj;
246         if (!uuid.equals(p.uuid))
247             return false;
248         if (!name.equals(p.name))
249             return false;
250         if (permitPosting != p.permitPosting)
251             return false;
252         if (showCommentsByDefault != p.showCommentsByDefault)
253             return false;
254         if (showCommodityByDefault != p.showCommodityByDefault)
255             return false;
256         if (!Objects.equals(defaultCommodity, p.defaultCommodity))
257             return false;
258         if (!Objects.equals(preferredAccountsFilter, p.preferredAccountsFilter))
259             return false;
260         if (!Objects.equals(url, p.url))
261             return false;
262         if (authEnabled != p.authEnabled)
263             return false;
264         if (!Objects.equals(authUserName, p.authUserName))
265             return false;
266         if (!Objects.equals(authPassword, p.authPassword))
267             return false;
268         if (themeHue != p.themeHue)
269             return false;
270         if (apiVersion != p.apiVersion)
271             return false;
272         if (!Objects.equals(firstTransactionDate, p.firstTransactionDate))
273             return false;
274         if (!Objects.equals(lastTransactionDate, p.lastTransactionDate))
275             return false;
276         return futureDates == p.futureDates;
277     }
278     synchronized public void scheduleAccountListReload() {
279         Logger.debug("async-acc", "scheduleAccountListReload() enter");
280         if ((loader != null) && loader.isAlive()) {
281             Logger.debug("async-acc", "returning early - loader already active");
282             return;
283         }
284
285         Logger.debug("async-acc", "Starting AccountListLoader");
286         loader = new AccountListLoader(this);
287         loader.start();
288     }
289     synchronized public void abortAccountListReload() {
290         if (loader == null)
291             return;
292         loader.interrupt();
293         loader = null;
294     }
295     public boolean getShowCommentsByDefault() {
296         return showCommentsByDefault;
297     }
298     public void setShowCommentsByDefault(boolean newValue) {
299         this.showCommentsByDefault = newValue;
300     }
301     public boolean getShowCommodityByDefault() {
302         return showCommodityByDefault;
303     }
304     public void setShowCommodityByDefault(boolean showCommodityByDefault) {
305         this.showCommodityByDefault = showCommodityByDefault;
306     }
307     public String getDefaultCommodity() {
308         return defaultCommodity;
309     }
310     public void setDefaultCommodity(String defaultCommodity) {
311         this.defaultCommodity = defaultCommodity;
312     }
313     public void setDefaultCommodity(CharSequence defaultCommodity) {
314         if (defaultCommodity == null)
315             this.defaultCommodity = null;
316         else
317             this.defaultCommodity = String.valueOf(defaultCommodity);
318     }
319     public SendTransactionTask.API getApiVersion() {
320         return apiVersion;
321     }
322     public void setApiVersion(SendTransactionTask.API apiVersion) {
323         this.apiVersion = apiVersion;
324     }
325     public void setApiVersion(int apiVersion) {
326         this.apiVersion = SendTransactionTask.API.valueOf(apiVersion);
327     }
328     public FutureDates getFutureDates() {
329         return futureDates;
330     }
331     public void setFutureDates(int anInt) {
332         futureDates = FutureDates.valueOf(anInt);
333     }
334     public void setFutureDates(FutureDates futureDates) {
335         this.futureDates = futureDates;
336     }
337     public String getPreferredAccountsFilter() {
338         return preferredAccountsFilter;
339     }
340     public void setPreferredAccountsFilter(String preferredAccountsFilter) {
341         this.preferredAccountsFilter = preferredAccountsFilter;
342     }
343     public void setPreferredAccountsFilter(CharSequence preferredAccountsFilter) {
344         setPreferredAccountsFilter(String.valueOf(preferredAccountsFilter));
345     }
346     public boolean isPostingPermitted() {
347         return permitPosting;
348     }
349     public void setPostingPermitted(boolean permitPosting) {
350         this.permitPosting = permitPosting;
351     }
352     public String getUuid() {
353         return uuid;
354     }
355     public String getName() {
356         return name;
357     }
358     public void setName(CharSequence text) {
359         setName(String.valueOf(text));
360     }
361     public void setName(String name) {
362         this.name = name;
363     }
364     public String getUrl() {
365         return url;
366     }
367     public void setUrl(CharSequence text) {
368         setUrl(String.valueOf(text));
369     }
370     public void setUrl(String url) {
371         this.url = url;
372     }
373     public boolean isAuthEnabled() {
374         return authEnabled;
375     }
376     public void setAuthEnabled(boolean authEnabled) {
377         this.authEnabled = authEnabled;
378     }
379     public String getAuthUserName() {
380         return authUserName;
381     }
382     public void setAuthUserName(CharSequence text) {
383         setAuthUserName(String.valueOf(text));
384     }
385     public void setAuthUserName(String authUserName) {
386         this.authUserName = authUserName;
387     }
388     public String getAuthPassword() {
389         return authPassword;
390     }
391     public void setAuthPassword(CharSequence text) {
392         setAuthPassword(String.valueOf(text));
393     }
394     public void setAuthPassword(String authPassword) {
395         this.authPassword = authPassword;
396     }
397     public void storeInDB() {
398         SQLiteDatabase db = App.getDatabase();
399         db.beginTransactionNonExclusive();
400         try {
401 //            debug("profiles", String.format("Storing profile in DB: uuid=%s, name=%s, " +
402 //                                            "url=%s, permit_posting=%s, authEnabled=%s, " +
403 //                                            "themeHue=%d", uuid, name, url,
404 //                    permitPosting ? "TRUE" : "FALSE", authEnabled ? "TRUE" : "FALSE", themeHue));
405             db.execSQL("REPLACE INTO profiles(uuid, name, permit_posting, url, " +
406                        "use_authentication, auth_user, auth_password, theme, order_no, " +
407                        "preferred_accounts_filter, future_dates, api_version, " +
408                        "show_commodity_by_default, default_commodity, show_comments_by_default) " +
409                        "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
410                     new Object[]{uuid, name, permitPosting, url, authEnabled,
411                                  authEnabled ? authUserName : null,
412                                  authEnabled ? authPassword : null, themeHue, orderNo,
413                                  preferredAccountsFilter, futureDates.toInt(), apiVersion.toInt(),
414                                  showCommodityByDefault, defaultCommodity, showCommentsByDefault
415                     });
416             db.setTransactionSuccessful();
417         }
418         finally {
419             db.endTransaction();
420         }
421     }
422     public void storeAccount(SQLiteDatabase db, LedgerAccount acc, boolean storeUiFields) {
423         // replace into is a bad idea because it would reset hidden to its default value
424         // we like the default, but for new accounts only
425         String sql = "update accounts set keep = 1";
426         List<Object> params = new ArrayList<>();
427         if (storeUiFields) {
428             sql += ", expanded=?";
429             params.add(acc.isExpanded() ? 1 : 0);
430         }
431         sql += " where profile=? and name=?";
432         params.add(uuid);
433         params.add(acc.getName());
434         db.execSQL(sql, params.toArray());
435
436         db.execSQL("insert into accounts(profile, name, name_upper, parent_name, level, " +
437                    "expanded, keep) " + "select ?,?,?,?,?,0,1 where (select changes() = 0)",
438                 new Object[]{uuid, acc.getName(), acc.getName().toUpperCase(), acc.getParentName(),
439                              acc.getLevel()
440                 });
441 //        debug("accounts", String.format("Stored account '%s' in DB [%s]", acc.getName(), uuid));
442     }
443     public void storeAccountValue(SQLiteDatabase db, String name, String currency, Float amount) {
444         db.execSQL("replace into account_values(profile, account, " +
445                    "currency, value, keep) values(?, ?, ?, ?, 1);",
446                 new Object[]{uuid, name, Misc.emptyIsNull(currency), amount});
447     }
448     public void storeTransaction(SQLiteDatabase db, LedgerTransaction tr) {
449         tr.fillDataHash();
450         db.execSQL("DELETE from transactions WHERE profile=? and id=?",
451                 new Object[]{uuid, tr.getId()});
452         db.execSQL("DELETE from transaction_accounts WHERE profile = ? and transaction_id=?",
453                 new Object[]{uuid, tr.getId()});
454
455         db.execSQL("INSERT INTO transactions(profile, id, year, month, day, description, " +
456                    "comment, data_hash, keep) values(?,?,?,?,?,?,?,?,1)",
457                 new Object[]{uuid, tr.getId(), tr.getDate().year, tr.getDate().month,
458                              tr.getDate().day, tr.getDescription(), tr.getComment(),
459                              tr.getDataHash()
460                 });
461
462         for (LedgerTransactionAccount item : tr.getAccounts()) {
463             db.execSQL("INSERT INTO transaction_accounts(profile, transaction_id, " +
464                        "account_name, amount, currency, comment) values(?, ?, ?, ?, ?, ?)",
465                     new Object[]{uuid, tr.getId(), item.getAccountName(), item.getAmount(),
466                                  Misc.nullIsEmpty(item.getCurrency()), item.getComment()
467                     });
468         }
469 //        debug("profile", String.format("Transaction %d stored", tr.getId()));
470     }
471     public String getOption(String name, String default_value) {
472         SQLiteDatabase db = App.getDatabase();
473         try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
474                 new String[]{uuid, name}))
475         {
476             if (cursor.moveToFirst()) {
477                 String result = cursor.getString(0);
478
479                 if (result == null) {
480                     debug("profile", "returning default value for " + name);
481                     result = default_value;
482                 }
483                 else
484                     debug("profile", String.format("option %s=%s", name, result));
485
486                 return result;
487             }
488             else
489                 return default_value;
490         }
491         catch (Exception e) {
492             debug("db", "returning default value for " + name, e);
493             return default_value;
494         }
495     }
496     public long getLongOption(String name, long default_value) {
497         long longResult;
498         String result = getOption(name, "");
499         if ((result == null) || result.isEmpty()) {
500             debug("profile", String.format("Returning default value for option %s", name));
501             longResult = default_value;
502         }
503         else {
504             try {
505                 longResult = Long.parseLong(result);
506                 debug("profile", String.format("option %s=%s", name, result));
507             }
508             catch (Exception e) {
509                 debug("profile", String.format("Returning default value for option %s", name), e);
510                 longResult = default_value;
511             }
512         }
513
514         return longResult;
515     }
516     public void setOption(String name, String value) {
517         debug("profile", String.format("setting option %s=%s", name, value));
518         DbOpQueue.add("insert or replace into options(profile, name, value) values(?, ?, ?);",
519                 new String[]{uuid, name, value});
520     }
521     public void setLongOption(String name, long value) {
522         setOption(name, String.valueOf(value));
523     }
524     public void removeFromDB() {
525         SQLiteDatabase db = App.getDatabase();
526         debug("db", String.format("removing profile %s from DB", uuid));
527         db.beginTransactionNonExclusive();
528         try {
529             Object[] uuid_param = new Object[]{uuid};
530             db.execSQL("delete from profiles where uuid=?", uuid_param);
531             db.execSQL("delete from accounts where profile=?", uuid_param);
532             db.execSQL("delete from account_values where profile=?", uuid_param);
533             db.execSQL("delete from transactions where profile=?", uuid_param);
534             db.execSQL("delete from transaction_accounts where profile=?", uuid_param);
535             db.execSQL("delete from options where profile=?", uuid_param);
536             db.setTransactionSuccessful();
537         }
538         finally {
539             db.endTransaction();
540         }
541     }
542     public LedgerTransaction loadTransaction(int transactionId) {
543         LedgerTransaction tr = new LedgerTransaction(transactionId, this.uuid);
544         tr.loadData(App.getDatabase());
545
546         return tr;
547     }
548     public int getThemeHue() {
549 //        debug("profile", String.format("Profile.getThemeHue() returning %d", themeHue));
550         return this.themeHue;
551     }
552     public void setThemeHue(Object o) {
553         setThemeId(Integer.parseInt(String.valueOf(o)));
554     }
555     public void setThemeId(int themeHue) {
556 //        debug("profile", String.format("Profile.setThemeHue(%d) called", themeHue));
557         this.themeHue = themeHue;
558     }
559     public void markTransactionsAsNotPresent(SQLiteDatabase db) {
560         db.execSQL("UPDATE transactions set keep=0 where profile=?", new String[]{uuid});
561
562     }
563     private void markAccountsAsNotPresent(SQLiteDatabase db) {
564         db.execSQL("update account_values set keep=0 where profile=?;", new String[]{uuid});
565         db.execSQL("update accounts set keep=0 where profile=?;", new String[]{uuid});
566
567     }
568     private void deleteNotPresentAccounts(SQLiteDatabase db) {
569         db.execSQL("delete from account_values where keep=0 and profile=?", new String[]{uuid});
570         db.execSQL("delete from accounts where keep=0 and profile=?", new String[]{uuid});
571     }
572     private void markTransactionAsPresent(SQLiteDatabase db, LedgerTransaction transaction) {
573         db.execSQL("UPDATE transactions SET keep = 1 WHERE profile = ? and id=?",
574                 new Object[]{uuid, transaction.getId()
575                 });
576     }
577     private void markTransactionsBeforeTransactionAsPresent(SQLiteDatabase db,
578                                                             LedgerTransaction transaction) {
579         db.execSQL("UPDATE transactions SET keep=1 WHERE profile = ? and id < ?",
580                 new Object[]{uuid, transaction.getId()
581                 });
582
583     }
584     private void deleteNotPresentTransactions(SQLiteDatabase db) {
585         db.execSQL("DELETE FROM transactions WHERE profile=? AND keep = 0", new String[]{uuid});
586     }
587     private void setLastUpdateStamp() {
588         debug("db", "Updating transaction value stamp");
589         Date now = new Date();
590         setLongOption(MLDB.OPT_LAST_SCRAPE, now.getTime());
591         Data.lastUpdateDate.postValue(now);
592     }
593     public void wipeAllData() {
594         SQLiteDatabase db = App.getDatabase();
595         db.beginTransaction();
596         try {
597             String[] pUuid = new String[]{uuid};
598             db.execSQL("delete from options where profile=?", pUuid);
599             db.execSQL("delete from accounts where profile=?", pUuid);
600             db.execSQL("delete from account_values where profile=?", pUuid);
601             db.execSQL("delete from transactions where profile=?", pUuid);
602             db.execSQL("delete from transaction_accounts where profile=?", pUuid);
603             db.setTransactionSuccessful();
604             debug("wipe", String.format(Locale.ENGLISH, "Profile %s wiped out", pUuid[0]));
605         }
606         finally {
607             db.endTransaction();
608         }
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.getInt(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.getInt(2)), cursor.getInt(3) == 1);
642             }
643             return null;
644         }
645     }
646     public Calendar getFirstTransactionDate() {
647         return firstTransactionDate;
648     }
649     public Calendar getLastTransactionDate() {
650         return lastTransactionDate;
651     }
652     private void applyTransactionFilter(List<LedgerTransaction> list) {
653         final String accFilter = Data.accountFilter.getValue();
654         if (TextUtils.isEmpty(accFilter)) {
655             displayedTransactions.postValue(list);
656         }
657         else {
658             ArrayList<LedgerTransaction> newList = new ArrayList<>();
659             for (LedgerTransaction tr : list) {
660                 if (tr.hasAccountNamedLike(accFilter))
661                     newList.add(tr);
662             }
663             displayedTransactions.postValue(newList);
664         }
665     }
666     synchronized public void storeAccountListAsync(List<LedgerAccount> list,
667                                                    boolean storeUiFields) {
668         if (accountListSaver != null)
669             accountListSaver.interrupt();
670         accountListSaver = new AccountListSaver(this, list, storeUiFields);
671         accountListSaver.start();
672     }
673     public void setAndStoreAccountListFromWeb(ArrayList<LedgerAccount> list) {
674         SQLiteDatabase db = App.getDatabase();
675         db.beginTransactionNonExclusive();
676         try {
677             markAccountsAsNotPresent(db);
678             for (LedgerAccount acc : list) {
679                 storeAccount(db, acc, false);
680                 for (LedgerAmount amt : acc.getAmounts()) {
681                     storeAccountValue(db, acc.getName(), amt.getCurrency(), amt.getAmount());
682                 }
683             }
684             deleteNotPresentAccounts(db);
685             setLastUpdateStamp();
686             db.setTransactionSuccessful();
687         }
688         finally {
689             db.endTransaction();
690         }
691
692         mergeAccountList(list);
693         updateDisplayedAccounts();
694     }
695     public synchronized Locker lockAccountsForWriting() {
696         accountsLocker.lockForWriting();
697         return accountsLocker;
698     }
699     public void setAndStoreTransactionList(ArrayList<LedgerTransaction> list) {
700         storeTransactionListAsync(this, list);
701         SQLiteDatabase db = App.getDatabase();
702         db.beginTransactionNonExclusive();
703         try {
704             markTransactionsAsNotPresent(db);
705             for (LedgerTransaction tr : list)
706                 storeTransaction(db, tr);
707             deleteNotPresentTransactions(db);
708             setLastUpdateStamp();
709             db.setTransactionSuccessful();
710         }
711         finally {
712             db.endTransaction();
713         }
714
715         allTransactions.postValue(list);
716     }
717     private void storeTransactionListAsync(MobileLedgerProfile mobileLedgerProfile,
718                                            List<LedgerTransaction> list) {
719         if (transactionListSaver != null)
720             transactionListSaver.interrupt();
721
722         transactionListSaver = new TransactionListSaver(this, list);
723         transactionListSaver.start();
724     }
725     public void setAndStoreAccountAndTransactionListFromWeb(List<LedgerAccount> accounts,
726                                                             List<LedgerTransaction> transactions) {
727         storeAccountAndTransactionListAsync(accounts, transactions, false);
728
729         mergeAccountList(accounts);
730         updateDisplayedAccounts();
731
732         allTransactions.postValue(transactions);
733     }
734     private void storeAccountAndTransactionListAsync(List<LedgerAccount> accounts,
735                                                      List<LedgerTransaction> transactions,
736                                                      boolean storeAccUiFields) {
737         if (accountAndTransactionListSaver != null)
738             accountAndTransactionListSaver.interrupt();
739
740         accountAndTransactionListSaver =
741                 new AccountAndTransactionListSaver(this, accounts, transactions, storeAccUiFields);
742         accountAndTransactionListSaver.start();
743     }
744     synchronized public void updateDisplayedAccounts() {
745         if (displayedAccountsUpdater != null) {
746             displayedAccountsUpdater.interrupt();
747         }
748         displayedAccountsUpdater = new AccountListDisplayedFilter(this, allAccounts);
749         displayedAccountsUpdater.start();
750     }
751     public List<LedgerAccount> getAllAccounts() {
752         return allAccounts;
753     }
754     private void updateAccountsMap(List<LedgerAccount> newAccounts) {
755         accountMap.clear();
756         for (LedgerAccount acc : newAccounts) {
757             accountMap.put(acc.getName(), acc);
758         }
759     }
760     @Nullable
761     public LedgerAccount locateAccount(String name) {
762         return accountMap.get(name);
763     }
764
765     public enum FutureDates {
766         None(0), OneWeek(7), TwoWeeks(14), OneMonth(30), TwoMonths(60), ThreeMonths(90),
767         SixMonths(180), OneYear(365), All(-1);
768         private static SparseArray<FutureDates> map = new SparseArray<>();
769
770         static {
771             for (FutureDates item : FutureDates.values()) {
772                 map.put(item.value, item);
773             }
774         }
775
776         private int value;
777         FutureDates(int value) {
778             this.value = value;
779         }
780         public static FutureDates valueOf(int i) {
781             return map.get(i, None);
782         }
783         public int toInt() {
784             return this.value;
785         }
786         public String getText(Resources resources) {
787             switch (value) {
788                 case 7:
789                     return resources.getString(R.string.future_dates_7);
790                 case 14:
791                     return resources.getString(R.string.future_dates_14);
792                 case 30:
793                     return resources.getString(R.string.future_dates_30);
794                 case 60:
795                     return resources.getString(R.string.future_dates_60);
796                 case 90:
797                     return resources.getString(R.string.future_dates_90);
798                 case 180:
799                     return resources.getString(R.string.future_dates_180);
800                 case 365:
801                     return resources.getString(R.string.future_dates_365);
802                 case -1:
803                     return resources.getString(R.string.future_dates_all);
804                 default:
805                     return resources.getString(R.string.future_dates_none);
806             }
807         }
808     }
809
810     static class AccountListLoader extends Thread {
811         MobileLedgerProfile profile;
812         AccountListLoader(MobileLedgerProfile profile) {
813             this.profile = profile;
814         }
815         @Override
816         public void run() {
817             Logger.debug("async-acc", "AccountListLoader::run() entered");
818             String profileUUID = profile.getUuid();
819             ArrayList<LedgerAccount> list = new ArrayList<>();
820             HashMap<String, LedgerAccount> map = new HashMap<>();
821
822             String sql = "SELECT a.name, a.expanded, a.amounts_expanded";
823             sql += " from accounts a WHERE a.profile = ?";
824             sql += " ORDER BY a.name";
825
826             SQLiteDatabase db = App.getDatabase();
827             Logger.debug("async-acc", "AccountListLoader::run() connected to DB");
828             try (Cursor cursor = db.rawQuery(sql, new String[]{profileUUID})) {
829                 Logger.debug("async-acc", "AccountListLoader::run() executed query");
830                 while (cursor.moveToNext()) {
831                     if (isInterrupted())
832                         return;
833
834                     final String accName = cursor.getString(0);
835 //                    debug("accounts",
836 //                            String.format("Read account '%s' from DB [%s]", accName,
837 //                            profileUUID));
838                     String parentName = LedgerAccount.extractParentName(accName);
839                     LedgerAccount parent;
840                     if (parentName != null) {
841                         parent = map.get(parentName);
842                         if (parent == null)
843                             throw new IllegalStateException(
844                                     String.format("Can't load account '%s': parent '%s' not loaded",
845                                             accName, parentName));
846                         parent.setHasSubAccounts(true);
847                     }
848                     else
849                         parent = null;
850
851                     LedgerAccount acc = new LedgerAccount(profile, accName, parent);
852                     acc.setExpanded(cursor.getInt(1) == 1);
853                     acc.setAmountsExpanded(cursor.getInt(2) == 1);
854                     acc.setHasSubAccounts(false);
855
856                     try (Cursor c2 = db.rawQuery(
857                             "SELECT value, currency FROM account_values WHERE profile = ?" + " " +
858                             "AND account = ?", new String[]{profileUUID, accName}))
859                     {
860                         while (c2.moveToNext()) {
861                             acc.addAmount(c2.getFloat(0), c2.getString(1));
862                         }
863                     }
864
865                     list.add(acc);
866                     map.put(accName, acc);
867                 }
868                 Logger.debug("async-acc", "AccountListLoader::run() query execution done");
869             }
870
871             if (isInterrupted())
872                 return;
873
874             Logger.debug("async-acc", "AccountListLoader::run() posting new list");
875             profile.allAccounts = list;
876             profile.updateAccountsMap(list);
877             profile.updateDisplayedAccounts();
878         }
879     }
880
881     static class AccountListDisplayedFilter extends Thread {
882         private final MobileLedgerProfile profile;
883         private final List<LedgerAccount> list;
884         AccountListDisplayedFilter(MobileLedgerProfile profile, List<LedgerAccount> list) {
885             this.profile = profile;
886             this.list = list;
887         }
888         @Override
889         public void run() {
890             List<LedgerAccount> newDisplayed = new ArrayList<>();
891             Logger.debug("dFilter", "waiting for synchronized block");
892             Logger.debug("dFilter", String.format(Locale.US,
893                     "entered synchronized block (about to examine %d accounts)", list.size()));
894             for (LedgerAccount a : list) {
895                 if (isInterrupted()) {
896                     return;
897                 }
898
899                 if (a.isVisible()) {
900                     newDisplayed.add(a);
901                 }
902             }
903             if (!isInterrupted()) {
904                 profile.displayedAccounts.postValue(newDisplayed);
905             }
906             Logger.debug("dFilter", "left synchronized block");
907         }
908     }
909
910     private static class AccountListSaver extends Thread {
911         private final MobileLedgerProfile profile;
912         private final List<LedgerAccount> list;
913         private final boolean storeUiFields;
914         AccountListSaver(MobileLedgerProfile profile, List<LedgerAccount> list,
915                          boolean storeUiFields) {
916             this.list = list;
917             this.profile = profile;
918             this.storeUiFields = storeUiFields;
919         }
920         @Override
921         public void run() {
922             SQLiteDatabase db = App.getDatabase();
923             db.beginTransactionNonExclusive();
924             try {
925                 profile.markAccountsAsNotPresent(db);
926                 if (isInterrupted())
927                     return;
928                 for (LedgerAccount acc : list) {
929                     profile.storeAccount(db, acc, storeUiFields);
930                     if (isInterrupted())
931                         return;
932                 }
933                 profile.deleteNotPresentAccounts(db);
934                 if (isInterrupted())
935                     return;
936                 profile.setLastUpdateStamp();
937                 db.setTransactionSuccessful();
938             }
939             finally {
940                 db.endTransaction();
941             }
942         }
943     }
944
945     private static class TransactionListSaver extends Thread {
946         private final MobileLedgerProfile profile;
947         private final List<LedgerTransaction> list;
948         TransactionListSaver(MobileLedgerProfile profile, List<LedgerTransaction> list) {
949             this.list = list;
950             this.profile = profile;
951         }
952         @Override
953         public void run() {
954             SQLiteDatabase db = App.getDatabase();
955             db.beginTransactionNonExclusive();
956             try {
957                 profile.markTransactionsAsNotPresent(db);
958                 if (isInterrupted())
959                     return;
960                 for (LedgerTransaction tr : list) {
961                     profile.storeTransaction(db, tr);
962                     if (isInterrupted())
963                         return;
964                 }
965                 profile.deleteNotPresentTransactions(db);
966                 if (isInterrupted())
967                     return;
968                 profile.setLastUpdateStamp();
969                 db.setTransactionSuccessful();
970             }
971             finally {
972                 db.endTransaction();
973             }
974         }
975     }
976
977     private static class AccountAndTransactionListSaver extends Thread {
978         private final MobileLedgerProfile profile;
979         private final List<LedgerAccount> accounts;
980         private final List<LedgerTransaction> transactions;
981         private final boolean storeAccUiFields;
982         AccountAndTransactionListSaver(MobileLedgerProfile profile, List<LedgerAccount> accounts,
983                                        List<LedgerTransaction> transactions,
984                                        boolean storeAccUiFields) {
985             this.accounts = accounts;
986             this.transactions = transactions;
987             this.profile = profile;
988             this.storeAccUiFields = storeAccUiFields;
989         }
990         @Override
991         public void run() {
992             SQLiteDatabase db = App.getDatabase();
993             db.beginTransactionNonExclusive();
994             try {
995                 profile.markAccountsAsNotPresent(db);
996                 if (isInterrupted())
997                     return;
998
999                 profile.markTransactionsAsNotPresent(db);
1000                 if (isInterrupted()) {
1001                     return;
1002                 }
1003
1004                 for (LedgerAccount acc : accounts) {
1005                     profile.storeAccount(db, acc, storeAccUiFields);
1006                     if (isInterrupted())
1007                         return;
1008                 }
1009
1010                 for (LedgerTransaction tr : transactions) {
1011                     profile.storeTransaction(db, tr);
1012                     if (isInterrupted()) {
1013                         return;
1014                     }
1015                 }
1016
1017                 profile.deleteNotPresentAccounts(db);
1018                 if (isInterrupted()) {
1019                     return;
1020                 }
1021                 profile.deleteNotPresentTransactions(db);
1022                 if (isInterrupted())
1023                     return;
1024
1025                 profile.setLastUpdateStamp();
1026
1027                 db.setTransactionSuccessful();
1028             }
1029             finally {
1030                 db.endTransaction();
1031             }
1032         }
1033     }
1034 }