2 * Copyright © 2019 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 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.activity.MainActivity;
40 import net.ktnx.mobileledger.utils.NetworkUtil;
42 import java.io.BufferedReader;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.InputStreamReader;
46 import java.lang.ref.WeakReference;
47 import java.net.HttpURLConnection;
48 import java.net.MalformedURLException;
49 import java.net.URLDecoder;
50 import java.nio.charset.StandardCharsets;
51 import java.text.ParseException;
52 import java.util.ArrayList;
53 import java.util.HashMap;
54 import java.util.Locale;
55 import java.util.Stack;
56 import java.util.regex.Matcher;
57 import java.util.regex.Pattern;
59 import static net.ktnx.mobileledger.utils.Logger.debug;
62 public class RetrieveTransactionsTask
63 extends AsyncTask<Void, RetrieveTransactionsTask.Progress, String> {
64 private static final int MATCHING_TRANSACTIONS_LIMIT = 150;
65 private static final Pattern reComment = Pattern.compile("^\\s*;");
66 private static final Pattern reTransactionStart = Pattern.compile("<tr class=\"title\" " +
67 "id=\"transaction-(\\d+)\"><td class=\"date\"[^\"]*>([\\d.-]+)</td>");
68 private static final Pattern reTransactionDescription =
69 Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
70 private static final Pattern reTransactionDetails =
71 Pattern.compile("^\\s+(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)(?:\\s+(\\S+)$)?");
72 private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
73 private static final Pattern reDecimalPoint = Pattern.compile("\\.\\d\\d?$");
74 private static final Pattern reDecimalComma = Pattern.compile(",\\d\\d?$");
75 private WeakReference<MainActivity> contextRef;
77 private Pattern reAccountName = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
78 private Pattern reAccountValue = Pattern.compile(
79 "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
80 private MobileLedgerProfile profile;
81 public RetrieveTransactionsTask(WeakReference<MainActivity> contextRef,
82 @NonNull MobileLedgerProfile profile) {
83 this.contextRef = contextRef;
84 this.profile = profile;
86 private static void L(String msg) {
87 //debug("transaction-parser", msg);
90 protected void onProgressUpdate(Progress... values) {
91 super.onProgressUpdate(values);
92 MainActivity context = getContext();
93 if (context == null) return;
94 context.onRetrieveProgress(values[0]);
97 protected void onPreExecute() {
99 MainActivity context = getContext();
100 if (context == null) return;
101 context.onRetrieveStart();
104 protected void onPostExecute(String error) {
105 super.onPostExecute(error);
106 MainActivity context = getContext();
107 if (context == null) return;
108 context.onRetrieveDone(error);
111 protected void onCancelled() {
113 MainActivity context = getContext();
114 if (context == null) return;
115 context.onRetrieveDone(null);
117 private String retrieveTransactionListLegacy()
118 throws IOException, ParseException, HTTPException {
119 Progress progress = new Progress();
120 int maxTransactionId = Progress.INDETERMINATE;
121 ArrayList<LedgerAccount> accountList = new ArrayList<>();
122 HashMap<String, Void> accountNames = new HashMap<>();
123 HashMap<String, LedgerAccount> syntheticAccounts = new HashMap<>();
124 LedgerAccount lastAccount = null, prevAccount = null;
126 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
127 http.setAllowUserInteraction(false);
128 publishProgress(progress);
129 switch (http.getResponseCode()) {
133 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
136 SQLiteDatabase db = App.getDatabase();
137 try (InputStream resp = http.getInputStream()) {
138 if (http.getResponseCode() != 200)
139 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
140 db.beginTransaction();
142 prepareDbForRetrieval(db, profile);
144 int matchedTransactionsCount = 0;
147 ParserState state = ParserState.EXPECTING_ACCOUNT;
150 new BufferedReader(new InputStreamReader(resp, StandardCharsets.UTF_8));
152 int processedTransactionCount = 0;
153 int transactionId = 0;
154 LedgerTransaction transaction = null;
156 while ((line = buf.readLine()) != null) {
159 m = reComment.matcher(line);
161 // TODO: comments are ignored for now
162 // Log.v("transaction-parser", "Ignoring comment");
165 //L(String.format("State is %d", updating));
167 case EXPECTING_ACCOUNT:
168 if (line.equals("<h2>General Journal</h2>")) {
169 state = ParserState.EXPECTING_TRANSACTION;
170 L("→ expecting transaction");
171 // commit the current transaction and start a new one
172 // the account list in the UI should reflect the (committed)
173 // state of the database
174 db.setTransactionSuccessful();
176 Data.accounts.setList(accountList);
177 db.beginTransaction();
180 m = reAccountName.matcher(line);
182 String acct_encoded = m.group(1);
183 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
184 acct_name = acct_name.replace("\"", "");
185 L(String.format("found account: %s", acct_name));
187 prevAccount = lastAccount;
188 lastAccount = profile.tryLoadAccount(db, acct_name);
189 if (lastAccount == null) lastAccount = new LedgerAccount(acct_name);
190 else lastAccount.removeAmounts();
191 profile.storeAccount(db, lastAccount);
193 if (prevAccount != null) prevAccount
194 .setHasSubAccounts(prevAccount.isParentOf(lastAccount));
195 // make sure the parent account(s) are present,
196 // synthesising them if necessary
197 // this happens when the (missing-in-HTML) parent account has
198 // only one child so we create a synthetic parent account record,
199 // copying the amounts when child's amounts are parsed
200 String parentName = lastAccount.getParentName();
201 if (parentName != null) {
202 Stack<String> toAppend = new Stack<>();
203 while (parentName != null) {
204 if (accountNames.containsKey(parentName)) break;
205 toAppend.push(parentName);
206 parentName = new LedgerAccount(parentName).getParentName();
208 syntheticAccounts.clear();
209 while (!toAppend.isEmpty()) {
210 String aName = toAppend.pop();
211 LedgerAccount acc = profile.tryLoadAccount(db, aName);
213 acc = new LedgerAccount(aName);
214 acc.setHiddenByStar(lastAccount.isHiddenByStar());
215 acc.setExpanded(!lastAccount.hasSubAccounts() ||
216 lastAccount.isExpanded());
218 acc.setHasSubAccounts(true);
219 acc.removeAmounts(); // filled below when amounts are parsed
220 if (acc.isVisible(accountList))
221 accountList.add(acc);
222 L(String.format("gap-filling with %s", aName));
223 accountNames.put(aName, null);
224 profile.storeAccount(db, acc);
225 syntheticAccounts.put(aName, acc);
229 if (lastAccount.isVisible(accountList))
230 accountList.add(lastAccount);
231 accountNames.put(acct_name, null);
233 state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
234 L("→ expecting account amount");
238 case EXPECTING_ACCOUNT_AMOUNT:
239 m = reAccountValue.matcher(line);
240 boolean match_found = false;
245 String value = m.group(1);
246 String currency = m.group(2);
247 if (currency == null) currency = "";
250 Matcher tmpM = reDecimalComma.matcher(value);
252 value = value.replace(".", "");
253 value = value.replace(',', '.');
256 tmpM = reDecimalPoint.matcher(value);
258 value = value.replace(",", "");
259 value = value.replace(" ", "");
262 L("curr=" + currency + ", value=" + value);
263 final float val = Float.parseFloat(value);
264 profile.storeAccountValue(db, lastAccount.getName(), currency, val);
265 lastAccount.addAmount(val, currency);
266 for (LedgerAccount syn : syntheticAccounts.values()) {
267 L(String.format(Locale.ENGLISH, "propagating %s %1.2f to %s",
268 currency, val, syn.getName()));
269 syn.addAmount(val, currency);
270 profile.storeAccountValue(db, syn.getName(), currency, val);
275 syntheticAccounts.clear();
276 state = ParserState.EXPECTING_ACCOUNT;
277 L("→ expecting account");
282 case EXPECTING_TRANSACTION:
283 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
284 m = reTransactionStart.matcher(line);
286 transactionId = Integer.valueOf(m.group(1));
287 state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
288 L(String.format(Locale.ENGLISH,
289 "found transaction %d → expecting description",
291 progress.setProgress(++processedTransactionCount);
292 if (maxTransactionId < transactionId)
293 maxTransactionId = transactionId;
294 if ((progress.getTotal() == Progress.INDETERMINATE) ||
295 (progress.getTotal() < transactionId))
296 progress.setTotal(transactionId);
297 publishProgress(progress);
299 m = reEnd.matcher(line);
301 L("--- transaction value complete ---");
306 case EXPECTING_TRANSACTION_DESCRIPTION:
307 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
308 m = reTransactionDescription.matcher(line);
310 if (transactionId == 0) throw new TransactionParserException(
311 "Transaction Id is 0 while expecting " + "description");
313 String date = m.group(1);
315 int equalsIndex = date.indexOf('=');
316 if (equalsIndex >= 0) date = date.substring(equalsIndex + 1);
318 new LedgerTransaction(transactionId, date, m.group(2));
320 catch (ParseException e) {
322 return String.format("Error parsing date '%s'", date);
324 state = ParserState.EXPECTING_TRANSACTION_DETAILS;
325 L(String.format(Locale.ENGLISH,
326 "transaction %d created for %s (%s) →" +
327 " expecting details", transactionId, date, m.group(2)));
331 case EXPECTING_TRANSACTION_DETAILS:
332 if (line.isEmpty()) {
333 // transaction data collected
334 if (transaction.existsInDb(db)) {
335 profile.markTransactionAsPresent(db, transaction);
336 matchedTransactionsCount++;
338 if (matchedTransactionsCount == MATCHING_TRANSACTIONS_LIMIT) {
339 profile.markTransactionsBeforeTransactionAsPresent(db,
341 progress.setTotal(progress.getProgress());
342 publishProgress(progress);
347 profile.storeTransaction(db, transaction);
348 matchedTransactionsCount = 0;
349 progress.setTotal(maxTransactionId);
352 state = ParserState.EXPECTING_TRANSACTION;
353 L(String.format("transaction %s saved → expecting transaction",
354 transaction.getId()));
355 transaction.finishLoading();
357 // sounds like a good idea, but transaction-1 may not be the first one chronologically
358 // for example, when you add the initial seeding transaction after entering some others
359 // if (transactionId == 1) {
360 // L("This was the initial transaction. Terminating " +
366 m = reTransactionDetails.matcher(line);
368 String acc_name = m.group(1);
369 String amount = m.group(2);
370 String currency = m.group(3);
371 if (currency == null) currency = "";
372 amount = amount.replace(',', '.');
373 transaction.addAccount(new LedgerTransactionAccount(acc_name,
374 Float.valueOf(amount), currency, null));
375 L(String.format(Locale.ENGLISH, "%d: %s = %s",
376 transaction.getId(), acc_name, amount));
378 else throw new IllegalStateException(
379 String.format("Can't parse transaction %d " + "details: %s",
380 transactionId, line));
384 throw new RuntimeException(
385 String.format("Unknown parser updating %s", state.name()));
391 profile.deleteNotPresentTransactions(db);
392 db.setTransactionSuccessful();
394 profile.setLastUpdateStamp();
403 private void prepareDbForRetrieval(SQLiteDatabase db, MobileLedgerProfile profile) {
404 db.execSQL("UPDATE transactions set keep=0 where profile=?",
405 new String[]{profile.getUuid()});
406 db.execSQL("update account_values set keep=0 where profile=?;",
407 new String[]{profile.getUuid()});
408 db.execSQL("update accounts set keep=0 where profile=?;", new String[]{profile.getUuid()});
410 private boolean retrieveAccountList() throws IOException, HTTPException {
411 Progress progress = new Progress();
413 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
414 http.setAllowUserInteraction(false);
415 switch (http.getResponseCode()) {
421 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
423 publishProgress(progress);
424 SQLiteDatabase db = App.getDatabase();
425 ArrayList<LedgerAccount> accountList = new ArrayList<>();
426 boolean listFilledOK = false;
427 try (InputStream resp = http.getInputStream()) {
428 if (http.getResponseCode() != 200)
429 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
431 db.beginTransaction();
433 profile.markAccountsAsNotPresent(db);
435 AccountListParser parser = new AccountListParser(resp);
437 LedgerAccount prevAccount = null;
441 ParsedLedgerAccount parsedAccount = parser.nextAccount();
442 if (parsedAccount == null) break;
444 LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname());
445 if (acc == null) acc = new LedgerAccount(parsedAccount.getAname());
446 else acc.removeAmounts();
448 profile.storeAccount(db, acc);
449 String lastCurrency = null;
450 float lastCurrencyAmount = 0;
451 for (ParsedBalance b : parsedAccount.getAibalance()) {
452 final String currency = b.getAcommodity();
453 final float amount = b.getAquantity().asFloat();
454 if (currency.equals(lastCurrency)) lastCurrencyAmount += amount;
456 if (lastCurrency != null) {
457 profile.storeAccountValue(db, acc.getName(), lastCurrency,
459 acc.addAmount(lastCurrencyAmount, lastCurrency);
461 lastCurrency = currency;
462 lastCurrencyAmount = amount;
465 if (lastCurrency != null) {
466 profile.storeAccountValue(db, acc.getName(), lastCurrency,
468 acc.addAmount(lastCurrencyAmount, lastCurrency);
471 if (acc.isVisible(accountList)) accountList.add(acc);
473 if (prevAccount != null) {
474 prevAccount.setHasSubAccounts(
475 acc.getName().startsWith(prevAccount.getName() + ":"));
482 profile.deleteNotPresentAccounts(db);
484 db.setTransactionSuccessful();
491 // should not be set in the DB transaction, because of a possible deadlock
492 // with the main and DbOpQueueRunner threads
493 if (listFilledOK) Data.accounts.setList(accountList);
497 private boolean retrieveTransactionList() throws IOException, ParseException, HTTPException {
498 Progress progress = new Progress();
499 int maxTransactionId = Progress.INDETERMINATE;
501 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
502 http.setAllowUserInteraction(false);
503 publishProgress(progress);
504 switch (http.getResponseCode()) {
510 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
512 SQLiteDatabase db = App.getDatabase();
513 try (InputStream resp = http.getInputStream()) {
514 if (http.getResponseCode() != 200)
515 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
517 db.beginTransaction();
519 profile.markTransactionsAsNotPresent(db);
521 int matchedTransactionsCount = 0;
522 TransactionListParser parser = new TransactionListParser(resp);
524 int processedTransactionCount = 0;
526 DetectedTransactionOrder transactionOrder = DetectedTransactionOrder.UNKNOWN;
527 int orderAccumulator = 0;
528 int lastTransactionId = 0;
532 ParsedLedgerTransaction parsedTransaction = parser.nextTransaction();
534 if (parsedTransaction == null) break;
536 LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
537 if (transaction.getId() > lastTransactionId) orderAccumulator++;
538 else orderAccumulator--;
539 lastTransactionId = transaction.getId();
540 if (transactionOrder == DetectedTransactionOrder.UNKNOWN) {
541 if (orderAccumulator > 30) {
542 transactionOrder = DetectedTransactionOrder.FILE;
543 debug("rtt", String.format(Locale.ENGLISH,
544 "Detected native file order after %d transactions (factor %d)",
545 processedTransactionCount, orderAccumulator));
546 progress.setTotal(Data.transactions.size());
548 else if (orderAccumulator < -30) {
549 transactionOrder = DetectedTransactionOrder.REVERSE_CHRONOLOGICAL;
550 debug("rtt", String.format(Locale.ENGLISH,
551 "Detected reverse chronological order after %d transactions (factor %d)",
552 processedTransactionCount, orderAccumulator));
556 if (transaction.existsInDb(db)) {
557 profile.markTransactionAsPresent(db, transaction);
558 matchedTransactionsCount++;
560 if ((transactionOrder == DetectedTransactionOrder.REVERSE_CHRONOLOGICAL) &&
561 (matchedTransactionsCount == MATCHING_TRANSACTIONS_LIMIT))
563 profile.markTransactionsBeforeTransactionAsPresent(db, transaction);
564 progress.setTotal(progress.getProgress());
565 publishProgress(progress);
566 db.setTransactionSuccessful();
567 profile.setLastUpdateStamp();
572 profile.storeTransaction(db, transaction);
573 matchedTransactionsCount = 0;
574 progress.setTotal(maxTransactionId);
578 if ((transactionOrder != DetectedTransactionOrder.UNKNOWN) &&
579 ((progress.getTotal() == Progress.INDETERMINATE) ||
580 (progress.getTotal() < transaction.getId())))
581 progress.setTotal(transaction.getId());
583 progress.setProgress(++processedTransactionCount);
584 publishProgress(progress);
588 profile.deleteNotPresentTransactions(db);
590 db.setTransactionSuccessful();
591 profile.setLastUpdateStamp();
601 @SuppressLint("DefaultLocale")
603 protected String doInBackground(Void... params) {
604 Data.backgroundTaskStarted();
606 if (!retrieveAccountList() || !retrieveTransactionList())
607 return retrieveTransactionListLegacy();
610 catch (MalformedURLException e) {
612 return "Invalid server URL";
614 catch (HTTPException e) {
616 return String.format("HTTP error %d: %s", e.getResponseCode(), e.getResponseMessage());
618 catch (IOException e) {
620 return e.getLocalizedMessage();
622 catch (ParseException e) {
624 return "Network error";
626 catch (OperationCanceledException e) {
628 return "Operation cancelled";
631 Data.backgroundTaskFinished();
634 private MainActivity getContext() {
635 return contextRef.get();
637 private void throwIfCancelled() {
638 if (isCancelled()) throw new OperationCanceledException(null);
640 enum DetectedTransactionOrder {UNKNOWN, REVERSE_CHRONOLOGICAL, FILE}
642 private enum ParserState {
643 EXPECTING_ACCOUNT, EXPECTING_ACCOUNT_AMOUNT, EXPECTING_JOURNAL, EXPECTING_TRANSACTION,
644 EXPECTING_TRANSACTION_DESCRIPTION, EXPECTING_TRANSACTION_DETAILS
647 public class Progress {
648 public static final int INDETERMINATE = -1;
649 private int progress;
652 this(INDETERMINATE, INDETERMINATE);
654 Progress(int progress, int total) {
655 this.progress = progress;
658 public int getProgress() {
661 protected void setProgress(int progress) {
662 this.progress = progress;
664 public int getTotal() {
667 protected void setTotal(int total) {
672 private class TransactionParserException extends IllegalStateException {
673 TransactionParserException(String message) {