2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
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.os.AsyncTask;
22 import android.os.OperationCanceledException;
24 import androidx.annotation.NonNull;
25 import androidx.room.Transaction;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
30 import net.ktnx.mobileledger.dao.AccountDAO;
31 import net.ktnx.mobileledger.dao.AccountValueDAO;
32 import net.ktnx.mobileledger.dao.TransactionAccountDAO;
33 import net.ktnx.mobileledger.dao.TransactionDAO;
34 import net.ktnx.mobileledger.db.Account;
35 import net.ktnx.mobileledger.db.AccountWithAmounts;
36 import net.ktnx.mobileledger.db.DB;
37 import net.ktnx.mobileledger.db.Option;
38 import net.ktnx.mobileledger.db.Profile;
39 import net.ktnx.mobileledger.db.TransactionAccount;
40 import net.ktnx.mobileledger.db.TransactionWithAccounts;
41 import net.ktnx.mobileledger.err.HTTPException;
42 import net.ktnx.mobileledger.json.API;
43 import net.ktnx.mobileledger.json.AccountListParser;
44 import net.ktnx.mobileledger.json.ApiNotSupportedException;
45 import net.ktnx.mobileledger.json.TransactionListParser;
46 import net.ktnx.mobileledger.model.Data;
47 import net.ktnx.mobileledger.model.LedgerAccount;
48 import net.ktnx.mobileledger.model.LedgerTransaction;
49 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
50 import net.ktnx.mobileledger.ui.MainModel;
51 import net.ktnx.mobileledger.utils.Logger;
52 import net.ktnx.mobileledger.utils.NetworkUtil;
54 import java.io.BufferedReader;
55 import java.io.IOException;
56 import java.io.InputStream;
57 import java.io.InputStreamReader;
58 import java.net.HttpURLConnection;
59 import java.net.MalformedURLException;
60 import java.net.URLDecoder;
61 import java.nio.charset.StandardCharsets;
62 import java.text.ParseException;
63 import java.util.ArrayList;
64 import java.util.Collections;
65 import java.util.Date;
66 import java.util.HashMap;
67 import java.util.List;
68 import java.util.Locale;
69 import java.util.Objects;
70 import java.util.regex.Matcher;
71 import java.util.regex.Pattern;
74 public class RetrieveTransactionsTask extends
75 AsyncTask<Void, RetrieveTransactionsTask.Progress, RetrieveTransactionsTask.Result> {
76 private static final int MATCHING_TRANSACTIONS_LIMIT = 150;
77 private static final Pattern reComment = Pattern.compile("^\\s*;");
78 private static final Pattern reTransactionStart = Pattern.compile(
79 "<tr class=\"title\" " + "id=\"transaction-(\\d+)" + "\"><td class=\"date" +
80 "\"[^\"]*>([\\d.-]+)</td>");
81 private static final Pattern reTransactionDescription =
82 Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
83 private static final Pattern reTransactionDetails = Pattern.compile(
84 "^\\s+" + "([!*]\\s+)?" + "(\\S[\\S\\s]+\\S)\\s\\s+" + "(?:([^\\d\\s+\\-]+)\\s*)?" +
85 "([-+]?\\d[\\d,.]*)" + "(?:\\s*([^\\d\\s+\\-]+)\\s*$)?");
86 private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
87 private static final Pattern reDecimalPoint = Pattern.compile("\\.\\d\\d?$");
88 private static final Pattern reDecimalComma = Pattern.compile(",\\d\\d?$");
90 private final Pattern reAccountName =
91 Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
92 private final Pattern reAccountValue = Pattern.compile(
93 "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
94 private final MainModel mainModel;
95 private final Profile profile;
96 private int expectedPostingsCount = -1;
97 public RetrieveTransactionsTask(@NonNull MainModel mainModel, @NonNull Profile profile) {
98 this.mainModel = mainModel;
99 this.profile = profile;
101 private static void L(String msg) {
102 //debug("transaction-parser", msg);
104 static LedgerTransactionAccount parseTransactionAccountLine(String line) {
105 Matcher m = reTransactionDetails.matcher(line);
107 String postingStatus = m.group(1);
108 String acc_name = m.group(2);
109 String currencyPre = m.group(3);
110 String amount = Objects.requireNonNull(m.group(4));
111 String currencyPost = m.group(5);
113 String currency = null;
114 if ((currencyPre != null) && (currencyPre.length() > 0)) {
115 if ((currencyPost != null) && (currencyPost.length() > 0))
117 currency = currencyPre;
119 else if ((currencyPost != null) && (currencyPost.length() > 0)) {
120 currency = currencyPost;
123 amount = amount.replace(',', '.');
125 return new LedgerTransactionAccount(acc_name, Float.parseFloat(amount), currency, null);
132 protected void onProgressUpdate(Progress... values) {
133 super.onProgressUpdate(values);
134 Data.backgroundTaskProgress.postValue(values[0]);
137 protected void onPostExecute(Result result) {
138 super.onPostExecute(result);
139 Progress progress = new Progress();
140 progress.setState(ProgressState.FINISHED);
141 progress.setError(result.error);
142 onProgressUpdate(progress);
145 protected void onCancelled() {
147 Progress progress = new Progress();
148 progress.setState(ProgressState.FINISHED);
149 onProgressUpdate(progress);
151 private void retrieveTransactionListLegacy(List<LedgerAccount> accounts,
152 List<LedgerTransaction> transactions)
153 throws IOException, HTTPException {
154 Progress progress = Progress.indeterminate();
155 progress.setState(ProgressState.RUNNING);
156 progress.setTotal(expectedPostingsCount);
157 int maxTransactionId = -1;
158 HashMap<String, LedgerAccount> map = new HashMap<>();
159 LedgerAccount lastAccount = null;
160 ArrayList<LedgerAccount> syntheticAccounts = new ArrayList<>();
162 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
163 http.setAllowUserInteraction(false);
164 publishProgress(progress);
165 if (http.getResponseCode() != 200)
166 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
168 try (InputStream resp = http.getInputStream()) {
169 if (http.getResponseCode() != 200)
170 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
172 int matchedTransactionsCount = 0;
174 ParserState state = ParserState.EXPECTING_ACCOUNT;
177 new BufferedReader(new InputStreamReader(resp, StandardCharsets.UTF_8));
179 int processedTransactionCount = 0;
180 int transactionId = 0;
181 LedgerTransaction transaction = null;
183 while ((line = buf.readLine()) != null) {
186 m = reComment.matcher(line);
188 // TODO: comments are ignored for now
189 // Log.v("transaction-parser", "Ignoring comment");
192 //L(String.format("State is %d", updating));
194 case EXPECTING_ACCOUNT:
195 if (line.equals("<h2>General Journal</h2>")) {
196 state = ParserState.EXPECTING_TRANSACTION;
197 L("→ expecting transaction");
200 m = reAccountName.matcher(line);
202 String acct_encoded = m.group(1);
203 String accName = URLDecoder.decode(acct_encoded, "UTF-8");
204 accName = accName.replace("\"", "");
205 L(String.format("found account: %s", accName));
207 lastAccount = map.get(accName);
208 if (lastAccount != null) {
209 L(String.format("ignoring duplicate account '%s'", accName));
212 String parentAccountName = LedgerAccount.extractParentName(accName);
213 LedgerAccount parentAccount;
214 if (parentAccountName != null) {
215 parentAccount = ensureAccountExists(parentAccountName, map,
219 parentAccount = null;
221 lastAccount = new LedgerAccount(accName, parentAccount);
223 accounts.add(lastAccount);
224 map.put(accName, lastAccount);
226 state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
227 L("→ expecting account amount");
231 case EXPECTING_ACCOUNT_AMOUNT:
232 m = reAccountValue.matcher(line);
233 boolean match_found = false;
238 String value = Objects.requireNonNull(m.group(1));
239 String currency = m.group(2);
240 if (currency == null)
244 Matcher tmpM = reDecimalComma.matcher(value);
246 value = value.replace(".", "");
247 value = value.replace(',', '.');
250 tmpM = reDecimalPoint.matcher(value);
252 value = value.replace(",", "");
253 value = value.replace(" ", "");
256 L("curr=" + currency + ", value=" + value);
257 final float val = Float.parseFloat(value);
258 lastAccount.addAmount(val, currency);
259 for (LedgerAccount syn : syntheticAccounts) {
260 L(String.format(Locale.ENGLISH, "propagating %s %1.2f to %s",
261 currency, val, syn.getName()));
262 syn.addAmount(val, currency);
267 syntheticAccounts.clear();
268 state = ParserState.EXPECTING_ACCOUNT;
269 L("→ expecting account");
274 case EXPECTING_TRANSACTION:
275 if (!line.isEmpty() && (line.charAt(0) == ' '))
277 m = reTransactionStart.matcher(line);
279 transactionId = Integer.parseInt(Objects.requireNonNull(m.group(1)));
280 state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
281 L(String.format(Locale.ENGLISH,
282 "found transaction %d → expecting description", transactionId));
283 progress.setProgress(++processedTransactionCount);
284 if (maxTransactionId < transactionId)
285 maxTransactionId = transactionId;
286 if ((progress.isIndeterminate()) ||
287 (progress.getTotal() < transactionId))
288 progress.setTotal(transactionId);
289 publishProgress(progress);
291 m = reEnd.matcher(line);
293 L("--- transaction value complete ---");
298 case EXPECTING_TRANSACTION_DESCRIPTION:
299 if (!line.isEmpty() && (line.charAt(0) == ' '))
301 m = reTransactionDescription.matcher(line);
303 if (transactionId == 0)
304 throw new TransactionParserException(
305 "Transaction Id is 0 while expecting description");
307 String date = Objects.requireNonNull(m.group(1));
309 int equalsIndex = date.indexOf('=');
310 if (equalsIndex >= 0)
311 date = date.substring(equalsIndex + 1);
313 new LedgerTransaction(transactionId, date, m.group(2));
315 catch (ParseException e) {
316 throw new TransactionParserException(
317 String.format("Error parsing date '%s'", date));
319 state = ParserState.EXPECTING_TRANSACTION_DETAILS;
320 L(String.format(Locale.ENGLISH,
321 "transaction %d created for %s (%s) →" + " expecting details",
322 transactionId, date, m.group(2)));
326 case EXPECTING_TRANSACTION_DETAILS:
327 if (line.isEmpty()) {
328 // transaction data collected
330 transaction.finishLoading();
331 transactions.add(transaction);
333 state = ParserState.EXPECTING_TRANSACTION;
334 L(String.format("transaction %s parsed → expecting transaction",
335 transaction.getLedgerId()));
337 // sounds like a good idea, but transaction-1 may not be the first one chronologically
338 // for example, when you add the initial seeding transaction after entering some others
339 // if (transactionId == 1) {
340 // L("This was the initial transaction.
347 LedgerTransactionAccount lta = parseTransactionAccountLine(line);
349 transaction.addAccount(lta);
350 L(String.format(Locale.ENGLISH, "%d: %s = %s",
351 transaction.getLedgerId(), lta.getAccountName(),
355 throw new IllegalStateException(
356 String.format("Can't parse transaction %d details: %s",
357 transactionId, line));
361 throw new RuntimeException(
362 String.format("Unknown parser updating %s", state.name()));
370 public LedgerAccount ensureAccountExists(String accountName, HashMap<String, LedgerAccount> map,
371 ArrayList<LedgerAccount> createdAccounts) {
372 LedgerAccount acc = map.get(accountName);
377 String parentName = LedgerAccount.extractParentName(accountName);
378 LedgerAccount parentAccount;
379 if (parentName != null) {
380 parentAccount = ensureAccountExists(parentName, map, createdAccounts);
383 parentAccount = null;
386 acc = new LedgerAccount(accountName, parentAccount);
387 createdAccounts.add(acc);
390 public void addNumberOfPostings(int number) {
391 expectedPostingsCount += number;
393 private List<LedgerAccount> retrieveAccountList()
394 throws IOException, HTTPException, ApiNotSupportedException {
395 final API apiVersion = API.valueOf(profile.getApiVersion());
396 if (apiVersion.equals(API.auto)) {
397 return retrieveAccountListAnyVersion();
399 else if (apiVersion.equals(API.html)) {
401 "Declining using JSON API for /accounts with configured legacy API version");
405 return retrieveAccountListForVersion(apiVersion);
408 private List<LedgerAccount> retrieveAccountListAnyVersion()
409 throws ApiNotSupportedException, IOException, HTTPException {
410 for (API ver : API.allVersions) {
412 return retrieveAccountListForVersion(ver);
414 catch (JsonParseException | RuntimeJsonMappingException e) {
416 String.format(Locale.US, "Error during account list retrieval using API %s",
417 ver.getDescription()), e);
422 throw new ApiNotSupportedException();
424 private List<LedgerAccount> retrieveAccountListForVersion(API version)
425 throws IOException, HTTPException {
426 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
427 http.setAllowUserInteraction(false);
428 switch (http.getResponseCode()) {
434 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
436 publishProgress(Progress.indeterminate());
437 ArrayList<LedgerAccount> list = new ArrayList<>();
438 HashMap<String, LedgerAccount> map = new HashMap<>();
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);
460 private List<LedgerTransaction> retrieveTransactionList()
461 throws ParseException, HTTPException, IOException, ApiNotSupportedException {
462 final API apiVersion = API.valueOf(profile.getApiVersion());
463 if (apiVersion.equals(API.auto)) {
464 return retrieveTransactionListAnyVersion();
466 else if (apiVersion.equals(API.html)) {
468 "Declining using JSON API for /accounts with configured legacy API version");
472 return retrieveTransactionListForVersion(apiVersion);
476 private List<LedgerTransaction> retrieveTransactionListAnyVersion()
477 throws ApiNotSupportedException {
478 for (API ver : API.allVersions) {
480 return retrieveTransactionListForVersion(ver);
482 catch (Exception e) {
484 String.format(Locale.US, "Error during account list retrieval using API %s",
485 ver.getDescription()));
490 throw new ApiNotSupportedException();
492 private List<LedgerTransaction> retrieveTransactionListForVersion(API apiVersion)
493 throws IOException, ParseException, HTTPException {
494 Progress progress = new Progress();
495 progress.setTotal(expectedPostingsCount);
497 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
498 http.setAllowUserInteraction(false);
499 publishProgress(progress);
500 switch (http.getResponseCode()) {
506 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
508 ArrayList<LedgerTransaction> trList = new ArrayList<>();
509 try (InputStream resp = http.getInputStream()) {
512 TransactionListParser parser = TransactionListParser.forApiVersion(apiVersion, resp);
514 int processedPostings = 0;
518 LedgerTransaction transaction = parser.nextTransaction();
520 if (transaction == null)
523 trList.add(transaction);
525 progress.setProgress(processedPostings += transaction.getAccounts()
527 // Logger.debug("trParser",
528 // String.format(Locale.US, "Parsed transaction %d - %s", transaction
530 // transaction.getDescription()));
531 // for (LedgerTransactionAccount acc : transaction.getAccounts()) {
532 // Logger.debug("trParser",
533 // String.format(Locale.US, " %s", acc.getAccountName()));
535 publishProgress(progress);
541 // json interface returns transactions if file order and the rest of the machinery
542 // expects them in reverse chronological order
543 Collections.sort(trList, (o1, o2) -> {
544 int res = o2.getDate()
545 .compareTo(o1.getDate());
548 return Long.compare(o2.getLedgerId(), o1.getLedgerId());
553 @SuppressLint("DefaultLocale")
555 protected Result doInBackground(Void... params) {
556 Data.backgroundTaskStarted();
557 List<LedgerAccount> accounts;
558 List<LedgerTransaction> transactions;
560 accounts = retrieveAccountList();
561 // accounts is null in API-version auto-detection and means
562 // requesting 'html' API version via the JSON classes
563 // this can't work, and the null results in the legacy code below
565 if (accounts == null)
568 transactions = retrieveTransactionList();
570 if (accounts == null || transactions == null) {
571 accounts = new ArrayList<>();
572 transactions = new ArrayList<>();
573 retrieveTransactionListLegacy(accounts, transactions);
576 storeAccountsAndTransactions(accounts, transactions);
578 mainModel.updateDisplayedTransactionsFromWeb(transactions);
580 return new Result(accounts, transactions);
582 catch (MalformedURLException e) {
584 return new Result("Invalid server URL");
586 catch (HTTPException e) {
589 String.format("HTTP error %d: %s", e.getResponseCode(), e.getMessage()));
591 catch (IOException e) {
593 return new Result(e.getLocalizedMessage());
595 catch (RuntimeJsonMappingException e) {
597 return new Result(Result.ERR_JSON_PARSER_ERROR);
599 catch (ParseException e) {
601 return new Result("Network error");
603 catch (OperationCanceledException e) {
605 return new Result("Operation cancelled");
607 catch (ApiNotSupportedException e) {
609 return new Result("Server version not supported");
612 Data.backgroundTaskFinished();
616 private void storeAccountsAndTransactions(List<LedgerAccount> accounts,
617 List<LedgerTransaction> transactions) {
618 AccountDAO accDao = DB.get()
620 TransactionDAO trDao = DB.get()
621 .getTransactionDAO();
622 TransactionAccountDAO trAccDao = DB.get()
623 .getTransactionAccountDAO();
624 AccountValueDAO valDao = DB.get()
625 .getAccountValueDAO();
627 final List<AccountWithAmounts> list = new ArrayList<>();
628 for (LedgerAccount acc : accounts) {
629 final AccountWithAmounts a = acc.toDBOWithAmounts();
630 Account existing = accDao.getByNameSync(profile.getId(), acc.getName());
631 if (existing != null) {
632 a.account.setExpanded(existing.isExpanded());
633 a.account.setAmountsExpanded(existing.isAmountsExpanded());
635 existing.getId()); // not strictly needed, but since we have it anyway...
640 accDao.storeAccountsSync(list, profile.getId());
642 long trGen = trDao.getGenerationSync(profile.getId());
643 for (LedgerTransaction tr : transactions) {
644 TransactionWithAccounts tran = tr.toDBO();
645 tran.transaction.setGeneration(trGen);
646 tran.transaction.setProfileId(profile.getId());
648 tran.transaction.setId(trDao.insertSync(tran.transaction));
650 for (TransactionAccount trAcc : tran.accounts) {
651 trAcc.setGeneration(trGen);
652 trAcc.setTransactionId(tran.transaction.getId());
653 trAcc.setId(trAccDao.insertSync(trAcc));
657 trDao.purgeOldTransactionsSync(profile.getId(), trGen);
661 .insertSync(new Option(profile.getId(), Option.OPT_LAST_SCRAPE,
662 String.valueOf((new Date()).getTime())));
664 public void throwIfCancelled() {
666 throw new OperationCanceledException(null);
668 private enum ParserState {
669 EXPECTING_ACCOUNT, EXPECTING_ACCOUNT_AMOUNT, EXPECTING_TRANSACTION,
670 EXPECTING_TRANSACTION_DESCRIPTION, EXPECTING_TRANSACTION_DETAILS
673 public enum ProgressState {STARTING, RUNNING, FINISHED}
675 public static class Progress {
676 private int progress;
678 private ProgressState state = ProgressState.RUNNING;
679 private String error = null;
680 private boolean indeterminate;
682 indeterminate = true;
684 Progress(int progress, int total) {
685 this.indeterminate = false;
686 this.progress = progress;
689 public static Progress indeterminate() {
690 return new Progress();
692 public static Progress finished(String error) {
693 Progress p = new Progress();
694 p.setState(ProgressState.FINISHED);
698 public int getProgress() {
699 ensureState(ProgressState.RUNNING);
702 protected void setProgress(int progress) {
703 this.progress = progress;
704 this.state = ProgressState.RUNNING;
706 public int getTotal() {
707 ensureState(ProgressState.RUNNING);
710 protected void setTotal(int total) {
712 state = ProgressState.RUNNING;
713 indeterminate = total == -1;
715 private void ensureState(ProgressState wanted) {
717 throw new IllegalStateException(
718 String.format("Bad state: %s, expected %s", state, wanted));
720 public ProgressState getState() {
723 public void setState(ProgressState state) {
726 public String getError() {
727 ensureState(ProgressState.FINISHED);
730 public void setError(String error) {
732 state = ProgressState.FINISHED;
734 public boolean isIndeterminate() {
735 return indeterminate;
737 public void setIndeterminate(boolean indeterminate) {
738 this.indeterminate = indeterminate;
742 private static class TransactionParserException extends IllegalStateException {
743 TransactionParserException(String message) {
748 public static class Result {
749 public static String ERR_JSON_PARSER_ERROR = "err_json_parser";
751 public List<LedgerAccount> accounts;
752 public List<LedgerTransaction> transactions;
753 Result(String error) {
756 Result(List<LedgerAccount> accounts, List<LedgerTransaction> transactions) {
757 this.accounts = accounts;
758 this.transactions = transactions;