2 * Copyright © 2020 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.async;
20 import android.annotation.SuppressLint;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23 import android.os.OperationCanceledException;
25 import androidx.annotation.NonNull;
27 import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
29 import net.ktnx.mobileledger.App;
30 import net.ktnx.mobileledger.err.HTTPException;
31 import net.ktnx.mobileledger.json.API;
32 import net.ktnx.mobileledger.json.AccountListParser;
33 import net.ktnx.mobileledger.json.ApiNotSupportedException;
34 import net.ktnx.mobileledger.json.TransactionListParser;
35 import net.ktnx.mobileledger.model.Data;
36 import net.ktnx.mobileledger.model.LedgerAccount;
37 import net.ktnx.mobileledger.model.LedgerTransaction;
38 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
39 import net.ktnx.mobileledger.model.MobileLedgerProfile;
40 import net.ktnx.mobileledger.ui.MainModel;
41 import net.ktnx.mobileledger.utils.Logger;
42 import net.ktnx.mobileledger.utils.NetworkUtil;
44 import java.io.BufferedReader;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.InputStreamReader;
48 import java.net.HttpURLConnection;
49 import java.net.MalformedURLException;
50 import java.net.URLDecoder;
51 import java.nio.charset.StandardCharsets;
52 import java.text.ParseException;
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.HashMap;
56 import java.util.List;
57 import java.util.Locale;
58 import java.util.Objects;
59 import java.util.regex.Matcher;
60 import java.util.regex.Pattern;
63 public class RetrieveTransactionsTask extends
64 AsyncTask<Void, RetrieveTransactionsTask.Progress, RetrieveTransactionsTask.Result> {
65 private static final int MATCHING_TRANSACTIONS_LIMIT = 150;
66 private static final Pattern reComment = Pattern.compile("^\\s*;");
67 private static final Pattern reTransactionStart = Pattern.compile(
68 "<tr class=\"title\" " + "id=\"transaction-(\\d+)" + "\"><td class=\"date" +
69 "\"[^\"]*>([\\d.-]+)</td>");
70 private static final Pattern reTransactionDescription =
71 Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
72 private static final Pattern reTransactionDetails = Pattern.compile(
73 "^\\s+" + "([!*]\\s+)?" + "(\\S[\\S\\s]+\\S)\\s\\s+" + "(?:([^\\d\\s+\\-]+)\\s*)?" +
74 "([-+]?\\d[\\d,.]*)" + "(?:\\s*([^\\d\\s+\\-]+)\\s*$)?");
75 private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
76 private static final Pattern reDecimalPoint = Pattern.compile("\\.\\d\\d?$");
77 private static final Pattern reDecimalComma = Pattern.compile(",\\d\\d?$");
79 private final Pattern reAccountName =
80 Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
81 private final Pattern reAccountValue = Pattern.compile(
82 "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
83 private final MainModel mainModel;
84 private final MobileLedgerProfile profile;
85 private final List<LedgerAccount> prevAccounts;
86 private int expectedPostingsCount = -1;
87 public RetrieveTransactionsTask(@NonNull MainModel mainModel,
88 @NonNull MobileLedgerProfile profile,
89 List<LedgerAccount> accounts) {
90 this.mainModel = mainModel;
91 this.profile = profile;
92 this.prevAccounts = accounts;
94 private static void L(String msg) {
95 //debug("transaction-parser", msg);
97 static LedgerTransactionAccount parseTransactionAccountLine(String line) {
98 Matcher m = reTransactionDetails.matcher(line);
100 String postingStatus = m.group(1);
101 String acc_name = m.group(2);
102 String currencyPre = m.group(3);
103 String amount = Objects.requireNonNull(m.group(4));
104 String currencyPost = m.group(5);
106 String currency = null;
107 if ((currencyPre != null) && (currencyPre.length() > 0)) {
108 if ((currencyPost != null) && (currencyPost.length() > 0))
110 currency = currencyPre;
112 else if ((currencyPost != null) && (currencyPost.length() > 0)) {
113 currency = currencyPost;
116 amount = amount.replace(',', '.');
118 return new LedgerTransactionAccount(acc_name, Float.parseFloat(amount), currency, null);
124 public MobileLedgerProfile getProfile() {
128 protected void onProgressUpdate(Progress... values) {
129 super.onProgressUpdate(values);
130 Data.backgroundTaskProgress.postValue(values[0]);
133 protected void onPostExecute(Result result) {
134 super.onPostExecute(result);
135 Progress progress = new Progress();
136 progress.setState(ProgressState.FINISHED);
137 progress.setError(result.error);
138 onProgressUpdate(progress);
141 protected void onCancelled() {
143 Progress progress = new Progress();
144 progress.setState(ProgressState.FINISHED);
145 onProgressUpdate(progress);
147 private void retrieveTransactionListLegacy(List<LedgerAccount> accounts,
148 List<LedgerTransaction> transactions)
149 throws IOException, HTTPException {
150 Progress progress = Progress.indeterminate();
151 progress.setState(ProgressState.RUNNING);
152 progress.setTotal(expectedPostingsCount);
153 int maxTransactionId = -1;
154 HashMap<String, LedgerAccount> map = new HashMap<>();
155 LedgerAccount lastAccount = null;
156 ArrayList<LedgerAccount> syntheticAccounts = new ArrayList<>();
158 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
159 http.setAllowUserInteraction(false);
160 publishProgress(progress);
161 if (http.getResponseCode() != 200)
162 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
164 try (InputStream resp = http.getInputStream()) {
165 if (http.getResponseCode() != 200)
166 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
168 int matchedTransactionsCount = 0;
170 ParserState state = ParserState.EXPECTING_ACCOUNT;
173 new BufferedReader(new InputStreamReader(resp, StandardCharsets.UTF_8));
175 int processedTransactionCount = 0;
176 int transactionId = 0;
177 LedgerTransaction transaction = null;
179 while ((line = buf.readLine()) != null) {
182 m = reComment.matcher(line);
184 // TODO: comments are ignored for now
185 // Log.v("transaction-parser", "Ignoring comment");
188 //L(String.format("State is %d", updating));
190 case EXPECTING_ACCOUNT:
191 if (line.equals("<h2>General Journal</h2>")) {
192 state = ParserState.EXPECTING_TRANSACTION;
193 L("→ expecting transaction");
196 m = reAccountName.matcher(line);
198 String acct_encoded = m.group(1);
199 String accName = URLDecoder.decode(acct_encoded, "UTF-8");
200 accName = accName.replace("\"", "");
201 L(String.format("found account: %s", accName));
203 lastAccount = map.get(accName);
204 if (lastAccount != null) {
205 L(String.format("ignoring duplicate account '%s'", accName));
208 String parentAccountName = LedgerAccount.extractParentName(accName);
209 LedgerAccount parentAccount;
210 if (parentAccountName != null) {
211 parentAccount = ensureAccountExists(parentAccountName, map,
215 parentAccount = null;
217 lastAccount = new LedgerAccount(profile, accName, parentAccount);
219 accounts.add(lastAccount);
220 map.put(accName, lastAccount);
222 state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
223 L("→ expecting account amount");
227 case EXPECTING_ACCOUNT_AMOUNT:
228 m = reAccountValue.matcher(line);
229 boolean match_found = false;
234 String value = Objects.requireNonNull(m.group(1));
235 String currency = m.group(2);
236 if (currency == null)
240 Matcher tmpM = reDecimalComma.matcher(value);
242 value = value.replace(".", "");
243 value = value.replace(',', '.');
246 tmpM = reDecimalPoint.matcher(value);
248 value = value.replace(",", "");
249 value = value.replace(" ", "");
252 L("curr=" + currency + ", value=" + value);
253 final float val = Float.parseFloat(value);
254 lastAccount.addAmount(val, currency);
255 for (LedgerAccount syn : syntheticAccounts) {
256 L(String.format(Locale.ENGLISH, "propagating %s %1.2f to %s",
257 currency, val, syn.getName()));
258 syn.addAmount(val, currency);
263 syntheticAccounts.clear();
264 state = ParserState.EXPECTING_ACCOUNT;
265 L("→ expecting account");
270 case EXPECTING_TRANSACTION:
271 if (!line.isEmpty() && (line.charAt(0) == ' '))
273 m = reTransactionStart.matcher(line);
275 transactionId = Integer.parseInt(Objects.requireNonNull(m.group(1)));
276 state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
277 L(String.format(Locale.ENGLISH,
278 "found transaction %d → expecting description", transactionId));
279 progress.setProgress(++processedTransactionCount);
280 if (maxTransactionId < transactionId)
281 maxTransactionId = transactionId;
282 if ((progress.isIndeterminate()) ||
283 (progress.getTotal() < transactionId))
284 progress.setTotal(transactionId);
285 publishProgress(progress);
287 m = reEnd.matcher(line);
289 L("--- transaction value complete ---");
294 case EXPECTING_TRANSACTION_DESCRIPTION:
295 if (!line.isEmpty() && (line.charAt(0) == ' '))
297 m = reTransactionDescription.matcher(line);
299 if (transactionId == 0)
300 throw new TransactionParserException(
301 "Transaction Id is 0 while expecting description");
303 String date = Objects.requireNonNull(m.group(1));
305 int equalsIndex = date.indexOf('=');
306 if (equalsIndex >= 0)
307 date = date.substring(equalsIndex + 1);
309 new LedgerTransaction(transactionId, date, m.group(2));
311 catch (ParseException e) {
312 throw new TransactionParserException(
313 String.format("Error parsing date '%s'", date));
315 state = ParserState.EXPECTING_TRANSACTION_DETAILS;
316 L(String.format(Locale.ENGLISH,
317 "transaction %d created for %s (%s) →" + " expecting details",
318 transactionId, date, m.group(2)));
322 case EXPECTING_TRANSACTION_DETAILS:
323 if (line.isEmpty()) {
324 // transaction data collected
326 transaction.finishLoading();
327 transactions.add(transaction);
329 state = ParserState.EXPECTING_TRANSACTION;
330 L(String.format("transaction %s parsed → expecting transaction",
331 transaction.getId()));
333 // sounds like a good idea, but transaction-1 may not be the first one chronologically
334 // for example, when you add the initial seeding transaction after entering some others
335 // if (transactionId == 1) {
336 // L("This was the initial transaction.
343 LedgerTransactionAccount lta = parseTransactionAccountLine(line);
345 transaction.addAccount(lta);
346 L(String.format(Locale.ENGLISH, "%d: %s = %s", transaction.getId(),
347 lta.getAccountName(), lta.getAmount()));
350 throw new IllegalStateException(
351 String.format("Can't parse transaction %d details: %s",
352 transactionId, line));
356 throw new RuntimeException(
357 String.format("Unknown parser updating %s", state.name()));
365 public LedgerAccount ensureAccountExists(String accountName, HashMap<String, LedgerAccount> map,
366 ArrayList<LedgerAccount> createdAccounts) {
367 LedgerAccount acc = map.get(accountName);
372 String parentName = LedgerAccount.extractParentName(accountName);
373 LedgerAccount parentAccount;
374 if (parentName != null) {
375 parentAccount = ensureAccountExists(parentName, map, createdAccounts);
378 parentAccount = null;
381 acc = new LedgerAccount(profile, accountName, parentAccount);
382 createdAccounts.add(acc);
385 public void addNumberOfPostings(int number) {
386 expectedPostingsCount += number;
388 private List<LedgerAccount> retrieveAccountList()
389 throws IOException, HTTPException, ApiNotSupportedException {
390 final API apiVersion = profile.getApiVersion();
391 if (apiVersion.equals(API.auto)) {
392 return retrieveAccountListAnyVersion();
394 else if (apiVersion.equals(API.html)) {
396 "Declining using JSON API for /accounts with configured legacy API version");
400 return retrieveAccountListForVersion(apiVersion);
403 private List<LedgerAccount> retrieveAccountListAnyVersion()
404 throws HTTPException, ApiNotSupportedException {
405 for (API ver : API.allVersions) {
407 return retrieveAccountListForVersion(ver);
409 catch (Exception e) {
411 String.format(Locale.US, "Error during account list retrieval using API %s",
412 ver.getDescription()));
415 throw new ApiNotSupportedException();
418 throw new RuntimeException("This should never be reached");
420 private List<LedgerAccount> retrieveAccountListForVersion(API version)
421 throws IOException, HTTPException {
422 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
423 http.setAllowUserInteraction(false);
424 switch (http.getResponseCode()) {
430 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
432 publishProgress(Progress.indeterminate());
433 SQLiteDatabase db = App.getDatabase();
434 ArrayList<LedgerAccount> list = new ArrayList<>();
435 HashMap<String, LedgerAccount> map = new HashMap<>();
436 HashMap<String, LedgerAccount> currentMap = new HashMap<>();
437 for (LedgerAccount acc : prevAccounts)
438 currentMap.put(acc.getName(), acc);
440 try (InputStream resp = http.getInputStream()) {
442 if (http.getResponseCode() != 200)
443 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
445 AccountListParser parser = AccountListParser.forApiVersion(version, resp);
446 expectedPostingsCount = 0;
450 LedgerAccount acc = parser.nextAccount(this, map);
458 // the current account tree may have changed, update the new-to be tree to match
459 for (LedgerAccount acc : list) {
460 LedgerAccount prevData = currentMap.get(acc.getName());
461 if (prevData != null) {
462 acc.setExpanded(prevData.isExpanded());
463 acc.setAmountsExpanded(prevData.amountsExpanded());
469 private List<LedgerTransaction> retrieveTransactionList()
470 throws ParseException, HTTPException, IOException, ApiNotSupportedException {
471 final API apiVersion = profile.getApiVersion();
472 if (apiVersion.equals(API.auto)) {
473 return retrieveTransactionListAnyVersion();
475 else if (apiVersion.equals(API.html)) {
477 "Declining using JSON API for /accounts with configured legacy API version");
481 return retrieveTransactionListForVersion(apiVersion);
485 private List<LedgerTransaction> retrieveTransactionListAnyVersion()
486 throws ApiNotSupportedException {
487 for (API ver : API.allVersions) {
489 return retrieveTransactionListForVersion(ver);
491 catch (Exception | HTTPException e) {
493 String.format(Locale.US, "Error during account list retrieval using API %s",
494 ver.getDescription()));
497 throw new ApiNotSupportedException();
500 throw new RuntimeException("This should never be reached");
502 private List<LedgerTransaction> retrieveTransactionListForVersion(API apiVersion)
503 throws IOException, ParseException, HTTPException {
504 Progress progress = new Progress();
505 progress.setTotal(expectedPostingsCount);
507 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
508 http.setAllowUserInteraction(false);
509 publishProgress(progress);
510 switch (http.getResponseCode()) {
516 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
518 ArrayList<LedgerTransaction> trList = new ArrayList<>();
519 try (InputStream resp = http.getInputStream()) {
522 TransactionListParser parser = TransactionListParser.forApiVersion(apiVersion, resp);
524 int processedPostings = 0;
528 LedgerTransaction transaction = parser.nextTransaction();
530 if (transaction == null)
533 trList.add(transaction);
535 progress.setProgress(processedPostings += transaction.getAccounts()
537 // Logger.debug("trParser",
538 // String.format(Locale.US, "Parsed transaction %d - %s", transaction
540 // transaction.getDescription()));
541 // for (LedgerTransactionAccount acc : transaction.getAccounts()) {
542 // Logger.debug("trParser",
543 // String.format(Locale.US, " %s", acc.getAccountName()));
545 publishProgress(progress);
551 // json interface returns transactions if file order and the rest of the machinery
552 // expects them in reverse chronological order
553 Collections.sort(trList, (o1, o2) -> {
554 int res = o2.getDate()
555 .compareTo(o1.getDate());
558 return Integer.compare(o2.getId(), o1.getId());
563 @SuppressLint("DefaultLocale")
565 protected Result doInBackground(Void... params) {
566 Data.backgroundTaskStarted();
567 List<LedgerAccount> accounts;
568 List<LedgerTransaction> transactions;
570 accounts = retrieveAccountList();
571 // accounts is null in API-version auto-detection and means
572 // requesting 'html' API version via the JSON classes
573 // this can't work, and the null results in the legacy code below
575 if (accounts == null)
578 transactions = retrieveTransactionList();
580 if (accounts == null || transactions == null) {
581 accounts = new ArrayList<>();
582 transactions = new ArrayList<>();
583 retrieveTransactionListLegacy(accounts, transactions);
585 mainModel.setAndStoreAccountAndTransactionListFromWeb(accounts, transactions);
587 return new Result(accounts, transactions);
589 catch (MalformedURLException e) {
591 return new Result("Invalid server URL");
593 catch (HTTPException e) {
595 return new Result(String.format("HTTP error %d: %s", e.getResponseCode(),
596 e.getResponseMessage()));
598 catch (IOException e) {
600 return new Result(e.getLocalizedMessage());
602 catch (RuntimeJsonMappingException e) {
604 return new Result(Result.ERR_JSON_PARSER_ERROR);
606 catch (ParseException e) {
608 return new Result("Network error");
610 catch (OperationCanceledException e) {
612 return new Result("Operation cancelled");
614 catch (ApiNotSupportedException e) {
616 return new Result("Server version not supported");
619 Data.backgroundTaskFinished();
622 public void throwIfCancelled() {
624 throw new OperationCanceledException(null);
626 private enum ParserState {
627 EXPECTING_ACCOUNT, EXPECTING_ACCOUNT_AMOUNT, EXPECTING_TRANSACTION,
628 EXPECTING_TRANSACTION_DESCRIPTION, EXPECTING_TRANSACTION_DETAILS
631 public enum ProgressState {STARTING, RUNNING, FINISHED}
633 public static class Progress {
634 private int progress;
636 private ProgressState state = ProgressState.RUNNING;
637 private String error = null;
638 private boolean indeterminate;
640 indeterminate = true;
642 Progress(int progress, int total) {
643 this.indeterminate = false;
644 this.progress = progress;
647 public static Progress indeterminate() {
648 return new Progress();
650 public static Progress finished(String error) {
651 Progress p = new Progress();
652 p.setState(ProgressState.FINISHED);
656 public int getProgress() {
657 ensureState(ProgressState.RUNNING);
660 protected void setProgress(int progress) {
661 this.progress = progress;
662 this.state = ProgressState.RUNNING;
664 public int getTotal() {
665 ensureState(ProgressState.RUNNING);
668 protected void setTotal(int total) {
670 state = ProgressState.RUNNING;
671 indeterminate = total == -1;
673 private void ensureState(ProgressState wanted) {
675 throw new IllegalStateException(
676 String.format("Bad state: %s, expected %s", state, wanted));
678 public ProgressState getState() {
681 public void setState(ProgressState state) {
684 public String getError() {
685 ensureState(ProgressState.FINISHED);
688 public void setError(String error) {
690 state = ProgressState.FINISHED;
692 public boolean isIndeterminate() {
693 return indeterminate;
695 public void setIndeterminate(boolean indeterminate) {
696 this.indeterminate = indeterminate;
700 private static class TransactionParserException extends IllegalStateException {
701 TransactionParserException(String message) {
706 public static class Result {
707 public static String ERR_JSON_PARSER_ERROR = "err_json_parser";
709 public List<LedgerAccount> accounts;
710 public List<LedgerTransaction> transactions;
711 Result(String error) {
714 Result(List<LedgerAccount> accounts, List<LedgerTransaction> transactions) {
715 this.accounts = accounts;
716 this.transactions = transactions;