]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
fix many lint errors/warnings
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.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.async;
19
20 import android.annotation.SuppressLint;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23 import android.os.OperationCanceledException;
24
25 import androidx.annotation.NonNull;
26
27 import net.ktnx.mobileledger.App;
28 import net.ktnx.mobileledger.err.HTTPException;
29 import net.ktnx.mobileledger.json.v1_15.AccountListParser;
30 import net.ktnx.mobileledger.json.v1_15.ParsedBalance;
31 import net.ktnx.mobileledger.json.v1_15.ParsedLedgerAccount;
32 import net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction;
33 import net.ktnx.mobileledger.json.v1_15.TransactionListParser;
34 import net.ktnx.mobileledger.model.Data;
35 import net.ktnx.mobileledger.model.LedgerAccount;
36 import net.ktnx.mobileledger.model.LedgerTransaction;
37 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
38 import net.ktnx.mobileledger.model.MobileLedgerProfile;
39 import net.ktnx.mobileledger.ui.MainModel;
40 import net.ktnx.mobileledger.utils.NetworkUtil;
41
42 import java.io.BufferedReader;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.InputStreamReader;
46 import java.net.HttpURLConnection;
47 import java.net.MalformedURLException;
48 import java.net.URLDecoder;
49 import java.nio.charset.StandardCharsets;
50 import java.text.ParseException;
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.HashMap;
54 import java.util.List;
55 import java.util.Locale;
56 import java.util.Objects;
57 import java.util.regex.Matcher;
58 import java.util.regex.Pattern;
59
60
61 public class RetrieveTransactionsTask extends
62         AsyncTask<Void, RetrieveTransactionsTask.Progress, RetrieveTransactionsTask.Result> {
63     private static final int MATCHING_TRANSACTIONS_LIMIT = 150;
64     private static final Pattern reComment = Pattern.compile("^\\s*;");
65     private static final Pattern reTransactionStart = Pattern.compile(
66             "<tr class=\"title\" " + "id=\"transaction-(\\d+)" + "\"><td class=\"date" +
67             "\"[^\"]*>([\\d.-]+)</td>");
68     private static final Pattern reTransactionDescription =
69             Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
70     private static final Pattern reTransactionDetails = Pattern.compile(
71             "^\\s+" + "([!*]\\s+)?" + "(\\S[\\S\\s]+\\S)\\s\\s+" + "(?:([^\\d\\s+\\-]+)\\s*)?" +
72             "([-+]?\\d[\\d,.]*)" + "(?:\\s*([^\\d\\s+\\-]+)\\s*$)?");
73     private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
74     private static final Pattern reDecimalPoint = Pattern.compile("\\.\\d\\d?$");
75     private static final Pattern reDecimalComma = Pattern.compile(",\\d\\d?$");
76     // %3A is '='
77     private final Pattern reAccountName =
78             Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
79     private final Pattern reAccountValue = Pattern.compile(
80             "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
81     private final MainModel mainModel;
82     private final MobileLedgerProfile profile;
83     private final List<LedgerAccount> prevAccounts;
84     private int expectedPostingsCount = -1;
85     public RetrieveTransactionsTask(@NonNull MainModel mainModel,
86                                     @NonNull MobileLedgerProfile profile,
87                                     List<LedgerAccount> accounts) {
88         this.mainModel = mainModel;
89         this.profile = profile;
90         this.prevAccounts = accounts;
91     }
92     private static void L(String msg) {
93         //debug("transaction-parser", msg);
94     }
95     static LedgerTransactionAccount parseTransactionAccountLine(String line) {
96         Matcher m = reTransactionDetails.matcher(line);
97         if (m.find()) {
98             String postingStatus = m.group(1);
99             String acc_name = m.group(2);
100             String currencyPre = m.group(3);
101             String amount = Objects.requireNonNull(m.group(4));
102             String currencyPost = m.group(5);
103
104             String currency = null;
105             if ((currencyPre != null) && (currencyPre.length() > 0)) {
106                 if ((currencyPost != null) && (currencyPost.length() > 0))
107                     return null;
108                 currency = currencyPre;
109             }
110             else if ((currencyPost != null) && (currencyPost.length() > 0)) {
111                 currency = currencyPost;
112             }
113
114             amount = amount.replace(',', '.');
115
116             return new LedgerTransactionAccount(acc_name, Float.parseFloat(amount), currency, null);
117         }
118         else {
119             return null;
120         }
121     }
122     @Override
123     protected void onProgressUpdate(Progress... values) {
124         super.onProgressUpdate(values);
125         Data.backgroundTaskProgress.postValue(values[0]);
126     }
127     @Override
128     protected void onPostExecute(Result result) {
129         super.onPostExecute(result);
130         Progress progress = new Progress();
131         progress.setState(ProgressState.FINISHED);
132         progress.setError(result.error);
133         onProgressUpdate(progress);
134     }
135     @Override
136     protected void onCancelled() {
137         super.onCancelled();
138         Progress progress = new Progress();
139         progress.setState(ProgressState.FINISHED);
140         onProgressUpdate(progress);
141     }
142     private void retrieveTransactionListLegacy(List<LedgerAccount> accounts,
143                                                List<LedgerTransaction> transactions)
144             throws IOException, HTTPException {
145         Progress progress = Progress.indeterminate();
146         progress.setState(ProgressState.RUNNING);
147         progress.setTotal(expectedPostingsCount);
148         int maxTransactionId = -1;
149         HashMap<String, LedgerAccount> map = new HashMap<>();
150         LedgerAccount lastAccount = null;
151         ArrayList<LedgerAccount> syntheticAccounts = new ArrayList<>();
152
153         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
154         http.setAllowUserInteraction(false);
155         publishProgress(progress);
156         if (http.getResponseCode() != 200)
157             throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
158
159         try (InputStream resp = http.getInputStream()) {
160             if (http.getResponseCode() != 200)
161                 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
162
163             int matchedTransactionsCount = 0;
164
165             ParserState state = ParserState.EXPECTING_ACCOUNT;
166             String line;
167             BufferedReader buf =
168                     new BufferedReader(new InputStreamReader(resp, StandardCharsets.UTF_8));
169
170             int processedTransactionCount = 0;
171             int transactionId = 0;
172             LedgerTransaction transaction = null;
173             LINES:
174             while ((line = buf.readLine()) != null) {
175                 throwIfCancelled();
176                 Matcher m;
177                 m = reComment.matcher(line);
178                 if (m.find()) {
179                     // TODO: comments are ignored for now
180 //                            Log.v("transaction-parser", "Ignoring comment");
181                     continue;
182                 }
183                 //L(String.format("State is %d", updating));
184                 switch (state) {
185                     case EXPECTING_ACCOUNT:
186                         if (line.equals("<h2>General Journal</h2>")) {
187                             state = ParserState.EXPECTING_TRANSACTION;
188                             L("→ expecting transaction");
189                             continue;
190                         }
191                         m = reAccountName.matcher(line);
192                         if (m.find()) {
193                             String acct_encoded = m.group(1);
194                             String accName = URLDecoder.decode(acct_encoded, "UTF-8");
195                             accName = accName.replace("\"", "");
196                             L(String.format("found account: %s", accName));
197
198                             lastAccount = map.get(accName);
199                             if (lastAccount != null) {
200                                 L(String.format("ignoring duplicate account '%s'", accName));
201                                 continue;
202                             }
203                             String parentAccountName = LedgerAccount.extractParentName(accName);
204                             LedgerAccount parentAccount;
205                             if (parentAccountName != null) {
206                                 parentAccount = ensureAccountExists(parentAccountName, map,
207                                         syntheticAccounts);
208                             }
209                             else {
210                                 parentAccount = null;
211                             }
212                             lastAccount = new LedgerAccount(profile, accName, parentAccount);
213
214                             accounts.add(lastAccount);
215                             map.put(accName, lastAccount);
216
217                             state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
218                             L("→ expecting account amount");
219                         }
220                         break;
221
222                     case EXPECTING_ACCOUNT_AMOUNT:
223                         m = reAccountValue.matcher(line);
224                         boolean match_found = false;
225                         while (m.find()) {
226                             throwIfCancelled();
227
228                             match_found = true;
229                             String value = Objects.requireNonNull(m.group(1));
230                             String currency = m.group(2);
231                             if (currency == null)
232                                 currency = "";
233
234                             {
235                                 Matcher tmpM = reDecimalComma.matcher(value);
236                                 if (tmpM.find()) {
237                                     value = value.replace(".", "");
238                                     value = value.replace(',', '.');
239                                 }
240
241                                 tmpM = reDecimalPoint.matcher(value);
242                                 if (tmpM.find()) {
243                                     value = value.replace(",", "");
244                                     value = value.replace(" ", "");
245                                 }
246                             }
247                             L("curr=" + currency + ", value=" + value);
248                             final float val = Float.parseFloat(value);
249                             lastAccount.addAmount(val, currency);
250                             for (LedgerAccount syn : syntheticAccounts) {
251                                 L(String.format(Locale.ENGLISH, "propagating %s %1.2f to %s",
252                                         currency, val, syn.getName()));
253                                 syn.addAmount(val, currency);
254                             }
255                         }
256
257                         if (match_found) {
258                             syntheticAccounts.clear();
259                             state = ParserState.EXPECTING_ACCOUNT;
260                             L("→ expecting account");
261                         }
262
263                         break;
264
265                     case EXPECTING_TRANSACTION:
266                         if (!line.isEmpty() && (line.charAt(0) == ' '))
267                             continue;
268                         m = reTransactionStart.matcher(line);
269                         if (m.find()) {
270                             transactionId = Integer.parseInt(Objects.requireNonNull(m.group(1)));
271                             state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
272                             L(String.format(Locale.ENGLISH,
273                                     "found transaction %d → expecting description", transactionId));
274                             progress.setProgress(++processedTransactionCount);
275                             if (maxTransactionId < transactionId)
276                                 maxTransactionId = transactionId;
277                             if ((progress.isIndeterminate()) ||
278                                 (progress.getTotal() < transactionId))
279                                 progress.setTotal(transactionId);
280                             publishProgress(progress);
281                         }
282                         m = reEnd.matcher(line);
283                         if (m.find()) {
284                             L("--- transaction value complete ---");
285                             break LINES;
286                         }
287                         break;
288
289                     case EXPECTING_TRANSACTION_DESCRIPTION:
290                         if (!line.isEmpty() && (line.charAt(0) == ' '))
291                             continue;
292                         m = reTransactionDescription.matcher(line);
293                         if (m.find()) {
294                             if (transactionId == 0)
295                                 throw new TransactionParserException(
296                                         "Transaction Id is 0 while expecting description");
297
298                             String date = Objects.requireNonNull(m.group(1));
299                             try {
300                                 int equalsIndex = date.indexOf('=');
301                                 if (equalsIndex >= 0)
302                                     date = date.substring(equalsIndex + 1);
303                                 transaction =
304                                         new LedgerTransaction(transactionId, date, m.group(2));
305                             }
306                             catch (ParseException e) {
307                                 throw new TransactionParserException(
308                                         String.format("Error parsing date '%s'", date));
309                             }
310                             state = ParserState.EXPECTING_TRANSACTION_DETAILS;
311                             L(String.format(Locale.ENGLISH,
312                                     "transaction %d created for %s (%s) →" + " expecting details",
313                                     transactionId, date, m.group(2)));
314                         }
315                         break;
316
317                     case EXPECTING_TRANSACTION_DETAILS:
318                         if (line.isEmpty()) {
319                             // transaction data collected
320
321                             transaction.finishLoading();
322                             transactions.add(transaction);
323
324                             state = ParserState.EXPECTING_TRANSACTION;
325                             L(String.format("transaction %s parsed → expecting transaction",
326                                     transaction.getId()));
327
328 // sounds like a good idea, but transaction-1 may not be the first one chronologically
329 // for example, when you add the initial seeding transaction after entering some others
330 //                                            if (transactionId == 1) {
331 //                                                L("This was the initial transaction.
332 //                                                Terminating " +
333 //                                                  "parser");
334 //                                                break LINES;
335 //                                            }
336                         }
337                         else {
338                             LedgerTransactionAccount lta = parseTransactionAccountLine(line);
339                             if (lta != null) {
340                                 transaction.addAccount(lta);
341                                 L(String.format(Locale.ENGLISH, "%d: %s = %s", transaction.getId(),
342                                         lta.getAccountName(), lta.getAmount()));
343                             }
344                             else
345                                 throw new IllegalStateException(
346                                         String.format("Can't parse transaction %d details: %s",
347                                                 transactionId, line));
348                         }
349                         break;
350                     default:
351                         throw new RuntimeException(
352                                 String.format("Unknown parser updating %s", state.name()));
353                 }
354             }
355
356             throwIfCancelled();
357         }
358     }
359     private @NonNull
360     LedgerAccount ensureAccountExists(String accountName, HashMap<String, LedgerAccount> map,
361                                       ArrayList<LedgerAccount> createdAccounts) {
362         LedgerAccount acc = map.get(accountName);
363
364         if (acc != null)
365             return acc;
366
367         String parentName = LedgerAccount.extractParentName(accountName);
368         LedgerAccount parentAccount;
369         if (parentName != null) {
370             parentAccount = ensureAccountExists(parentName, map, createdAccounts);
371         }
372         else {
373             parentAccount = null;
374         }
375
376         acc = new LedgerAccount(profile, accountName, parentAccount);
377         createdAccounts.add(acc);
378         return acc;
379     }
380     private List<LedgerAccount> retrieveAccountList() throws IOException, HTTPException {
381         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
382         http.setAllowUserInteraction(false);
383         switch (http.getResponseCode()) {
384             case 200:
385                 break;
386             case 404:
387                 return null;
388             default:
389                 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
390         }
391         publishProgress(Progress.indeterminate());
392         SQLiteDatabase db = App.getDatabase();
393         ArrayList<LedgerAccount> list = new ArrayList<>();
394         HashMap<String, LedgerAccount> map = new HashMap<>();
395         HashMap<String, LedgerAccount> currentMap = new HashMap<>();
396         for (LedgerAccount acc : prevAccounts)
397             currentMap.put(acc.getName(), acc);
398         throwIfCancelled();
399         try (InputStream resp = http.getInputStream()) {
400             throwIfCancelled();
401             if (http.getResponseCode() != 200)
402                 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
403
404             AccountListParser parser = new AccountListParser(resp);
405             expectedPostingsCount = 0;
406
407             while (true) {
408                 throwIfCancelled();
409                 ParsedLedgerAccount parsedAccount = parser.nextAccount();
410                 if (parsedAccount == null) {
411                     break;
412                 }
413                 expectedPostingsCount += parsedAccount.getAnumpostings();
414                 final String accName = parsedAccount.getAname();
415                 LedgerAccount acc = map.get(accName);
416                 if (acc != null)
417                     throw new RuntimeException(
418                             String.format("Account '%s' already present", acc.getName()));
419                 String parentName = LedgerAccount.extractParentName(accName);
420                 ArrayList<LedgerAccount> createdParents = new ArrayList<>();
421                 LedgerAccount parent;
422                 if (parentName == null) {
423                     parent = null;
424                 }
425                 else {
426                     parent = ensureAccountExists(parentName, map, createdParents);
427                     parent.setHasSubAccounts(true);
428                 }
429                 acc = new LedgerAccount(profile, accName, parent);
430                 list.add(acc);
431                 map.put(accName, acc);
432
433                 String lastCurrency = null;
434                 float lastCurrencyAmount = 0;
435                 for (ParsedBalance b : parsedAccount.getAibalance()) {
436                     throwIfCancelled();
437                     final String currency = b.getAcommodity();
438                     final float amount = b.getAquantity()
439                                           .asFloat();
440                     if (currency.equals(lastCurrency)) {
441                         lastCurrencyAmount += amount;
442                     }
443                     else {
444                         if (lastCurrency != null) {
445                             acc.addAmount(lastCurrencyAmount, lastCurrency);
446                         }
447                         lastCurrency = currency;
448                         lastCurrencyAmount = amount;
449                     }
450                 }
451                 if (lastCurrency != null) {
452                     acc.addAmount(lastCurrencyAmount, lastCurrency);
453                 }
454                 for (LedgerAccount p : createdParents)
455                     acc.propagateAmountsTo(p);
456             }
457             throwIfCancelled();
458         }
459
460         // the current account tree may have changed, update the new-to be tree to match
461         for (LedgerAccount acc : list) {
462             LedgerAccount prevData = currentMap.get(acc.getName());
463             if (prevData != null) {
464                 acc.setExpanded(prevData.isExpanded());
465                 acc.setAmountsExpanded(prevData.amountsExpanded());
466             }
467         }
468
469         return list;
470     }
471     private List<LedgerTransaction> retrieveTransactionList()
472             throws IOException, ParseException, HTTPException {
473         Progress progress = new Progress();
474         progress.setTotal(expectedPostingsCount);
475
476         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
477         http.setAllowUserInteraction(false);
478         publishProgress(progress);
479         switch (http.getResponseCode()) {
480             case 200:
481                 break;
482             case 404:
483                 return null;
484             default:
485                 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
486         }
487         ArrayList<LedgerTransaction> trList = new ArrayList<>();
488         try (InputStream resp = http.getInputStream()) {
489             throwIfCancelled();
490
491             TransactionListParser parser = new TransactionListParser(resp);
492
493             int processedPostings = 0;
494
495             while (true) {
496                 throwIfCancelled();
497                 ParsedLedgerTransaction parsedTransaction = parser.nextTransaction();
498                 throwIfCancelled();
499                 if (parsedTransaction == null)
500                     break;
501
502                 LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
503                 trList.add(transaction);
504
505                 progress.setProgress(processedPostings += transaction.getAccounts()
506                                                                      .size());
507 //                Logger.debug("trParser",
508 //                        String.format(Locale.US, "Parsed transaction %d - %s", transaction
509 //                        .getId(),
510 //                                transaction.getDescription()));
511 //                for (LedgerTransactionAccount acc : transaction.getAccounts()) {
512 //                    Logger.debug("trParser",
513 //                            String.format(Locale.US, "  %s", acc.getAccountName()));
514 //                }
515                 publishProgress(progress);
516             }
517
518             throwIfCancelled();
519         }
520
521         // json interface returns transactions if file order and the rest of the machinery
522         // expects them in reverse chronological order
523         Collections.sort(trList, (o1, o2) -> {
524             int res = o2.getDate()
525                         .compareTo(o1.getDate());
526             if (res != 0)
527                 return res;
528             return Integer.compare(o2.getId(), o1.getId());
529         });
530         return trList;
531     }
532
533     @SuppressLint("DefaultLocale")
534     @Override
535     protected Result doInBackground(Void... params) {
536         Data.backgroundTaskStarted();
537         List<LedgerAccount> accounts;
538         List<LedgerTransaction> transactions;
539         try {
540             accounts = retrieveAccountList();
541             if (accounts == null)
542                 transactions = null;
543             else
544                 transactions = retrieveTransactionList();
545             if (accounts == null || transactions == null) {
546                 accounts = new ArrayList<>();
547                 transactions = new ArrayList<>();
548                 retrieveTransactionListLegacy(accounts, transactions);
549             }
550             mainModel.setAndStoreAccountAndTransactionListFromWeb(accounts, transactions);
551
552             return new Result(accounts, transactions);
553         }
554         catch (MalformedURLException e) {
555             e.printStackTrace();
556             return new Result("Invalid server URL");
557         }
558         catch (HTTPException e) {
559             e.printStackTrace();
560             return new Result(String.format("HTTP error %d: %s", e.getResponseCode(),
561                     e.getResponseMessage()));
562         }
563         catch (IOException e) {
564             e.printStackTrace();
565             return new Result(e.getLocalizedMessage());
566         }
567         catch (ParseException e) {
568             e.printStackTrace();
569             return new Result("Network error");
570         }
571         catch (OperationCanceledException e) {
572             e.printStackTrace();
573             return new Result("Operation cancelled");
574         }
575         finally {
576             Data.backgroundTaskFinished();
577         }
578     }
579     private void throwIfCancelled() {
580         if (isCancelled())
581             throw new OperationCanceledException(null);
582     }
583     private enum ParserState {
584         EXPECTING_ACCOUNT, EXPECTING_ACCOUNT_AMOUNT, EXPECTING_TRANSACTION,
585         EXPECTING_TRANSACTION_DESCRIPTION, EXPECTING_TRANSACTION_DETAILS
586     }
587
588     public enum ProgressState {STARTING, RUNNING, FINISHED}
589
590     public static class Progress {
591         private int progress;
592         private int total;
593         private ProgressState state = ProgressState.RUNNING;
594         private String error = null;
595         private boolean indeterminate;
596         Progress() {
597             indeterminate = true;
598         }
599         Progress(int progress, int total) {
600             this.indeterminate = false;
601             this.progress = progress;
602             this.total = total;
603         }
604         public static Progress indeterminate() {
605             return new Progress();
606         }
607         public static Progress finished(String error) {
608             Progress p = new Progress();
609             p.setState(ProgressState.FINISHED);
610             p.setError(error);
611             return p;
612         }
613         public int getProgress() {
614             ensureState(ProgressState.RUNNING);
615             return progress;
616         }
617         protected void setProgress(int progress) {
618             this.progress = progress;
619             this.state = ProgressState.RUNNING;
620         }
621         public int getTotal() {
622             ensureState(ProgressState.RUNNING);
623             return total;
624         }
625         protected void setTotal(int total) {
626             this.total = total;
627             state = ProgressState.RUNNING;
628             indeterminate = total == -1;
629         }
630         private void ensureState(ProgressState wanted) {
631             if (state != wanted)
632                 throw new IllegalStateException(
633                         String.format("Bad state: %s, expected %s", state, wanted));
634         }
635         public ProgressState getState() {
636             return state;
637         }
638         public void setState(ProgressState state) {
639             this.state = state;
640         }
641         public String getError() {
642             ensureState(ProgressState.FINISHED);
643             return error;
644         }
645         public void setError(String error) {
646             this.error = error;
647             state = ProgressState.FINISHED;
648         }
649         public boolean isIndeterminate() {
650             return indeterminate;
651         }
652         public void setIndeterminate(boolean indeterminate) {
653             this.indeterminate = indeterminate;
654         }
655     }
656
657     private static class TransactionParserException extends IllegalStateException {
658         TransactionParserException(String message) {
659             super(message);
660         }
661     }
662
663     public static class Result {
664         public String error;
665         public List<LedgerAccount> accounts;
666         public List<LedgerTransaction> transactions;
667         Result(String error) {
668             this.error = error;
669         }
670         Result(List<LedgerAccount> accounts, List<LedgerTransaction> transactions) {
671             this.accounts = accounts;
672             this.transactions = transactions;
673         }
674     }
675 }