X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FRetrieveTransactionsTask.java;h=db8f8de76a9b7b4d328b4f82b48e636bcebefa60;hb=d58f8f4a9edd8b96005d1900c51b589471424165;hp=6afe539b2ba242ac5e405e5a2624bb3dc530e678;hpb=460876f2ca83b72ae815b0d781de2153132c9658;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index 6afe539b..db8f8de7 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2020 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -63,12 +63,14 @@ public class RetrieveTransactionsTask extends AsyncTask { private static final int MATCHING_TRANSACTIONS_LIMIT = 150; private static final Pattern reComment = Pattern.compile("^\\s*;"); - private static final Pattern reTransactionStart = Pattern.compile("([\\d.-]+)"); + private static final Pattern reTransactionStart = Pattern.compile( + "([\\d.-]+)"); private static final Pattern reTransactionDescription = Pattern.compile(" 0)) { + if ((currencyPost != null) && (currencyPost.length() > 0)) + return null; + currency = currencyPre; + } + else if ((currencyPost != null) && (currencyPost.length() > 0)) { + currency = currencyPost; + } + + amount = amount.replace(',', '.'); + + return new LedgerTransactionAccount(acc_name, Float.valueOf(amount), currency, null); + } + else { + return null; + } + } @Override protected void onProgressUpdate(Progress... values) { super.onProgressUpdate(values); MainActivity context = getContext(); - if (context == null) return; + if (context == null) + return; context.onRetrieveProgress(values[0]); } @Override protected void onPreExecute() { super.onPreExecute(); MainActivity context = getContext(); - if (context == null) return; + if (context == null) + return; context.onRetrieveStart(); } @Override protected void onPostExecute(String error) { super.onPostExecute(error); MainActivity context = getContext(); - if (context == null) return; + if (context == null) + return; context.onRetrieveDone(error); } @Override protected void onCancelled() { super.onCancelled(); MainActivity context = getContext(); - if (context == null) return; + if (context == null) + return; context.onRetrieveDone(null); } private String retrieveTransactionListLegacy() @@ -186,12 +219,15 @@ public class RetrieveTransactionsTask prevAccount = lastAccount; lastAccount = profile.tryLoadAccount(db, acct_name); - if (lastAccount == null) lastAccount = new LedgerAccount(acct_name); - else lastAccount.removeAmounts(); + if (lastAccount == null) + lastAccount = new LedgerAccount(profile, acct_name); + else + lastAccount.removeAmounts(); profile.storeAccount(db, lastAccount); - if (prevAccount != null) prevAccount - .setHasSubAccounts(prevAccount.isParentOf(lastAccount)); + if (prevAccount != null) + prevAccount.setHasSubAccounts( + prevAccount.isParentOf(lastAccount)); // make sure the parent account(s) are present, // synthesising them if necessary // this happens when the (missing-in-HTML) parent account has @@ -201,22 +237,23 @@ public class RetrieveTransactionsTask if (parentName != null) { Stack toAppend = new Stack<>(); while (parentName != null) { - if (accountNames.containsKey(parentName)) break; + if (accountNames.containsKey(parentName)) + break; toAppend.push(parentName); - parentName = new LedgerAccount(parentName).getParentName(); + parentName = new LedgerAccount(profile, parentName).getParentName(); } syntheticAccounts.clear(); while (!toAppend.isEmpty()) { String aName = toAppend.pop(); LedgerAccount acc = profile.tryLoadAccount(db, aName); if (acc == null) { - acc = new LedgerAccount(aName); - acc.setHiddenByStar(lastAccount.isHiddenByStar()); + acc = new LedgerAccount(profile, aName); acc.setExpanded(!lastAccount.hasSubAccounts() || lastAccount.isExpanded()); } acc.setHasSubAccounts(true); - acc.removeAmounts(); // filled below when amounts are parsed + acc.removeAmounts(); // filled below when amounts are + // parsed if (acc.isVisible(accountList)) accountList.add(acc); L(String.format("gap-filling with %s", aName)); @@ -244,7 +281,8 @@ public class RetrieveTransactionsTask match_found = true; String value = m.group(1); String currency = m.group(2); - if (currency == null) currency = ""; + if (currency == null) + currency = ""; { Matcher tmpM = reDecimalComma.matcher(value); @@ -280,7 +318,8 @@ public class RetrieveTransactionsTask break; case EXPECTING_TRANSACTION: - if (!line.isEmpty() && (line.charAt(0) == ' ')) continue; + if (!line.isEmpty() && (line.charAt(0) == ' ')) + continue; m = reTransactionStart.matcher(line); if (m.find()) { transactionId = Integer.valueOf(m.group(1)); @@ -304,16 +343,19 @@ public class RetrieveTransactionsTask break; case EXPECTING_TRANSACTION_DESCRIPTION: - if (!line.isEmpty() && (line.charAt(0) == ' ')) continue; + if (!line.isEmpty() && (line.charAt(0) == ' ')) + continue; m = reTransactionDescription.matcher(line); if (m.find()) { - if (transactionId == 0) throw new TransactionParserException( - "Transaction Id is 0 while expecting " + "description"); + if (transactionId == 0) + throw new TransactionParserException( + "Transaction Id is 0 while expecting " + "description"); String date = m.group(1); try { int equalsIndex = date.indexOf('='); - if (equalsIndex >= 0) date = date.substring(equalsIndex + 1); + if (equalsIndex >= 0) + date = date.substring(equalsIndex + 1); transaction = new LedgerTransaction(transactionId, date, m.group(2)); } @@ -357,28 +399,24 @@ public class RetrieveTransactionsTask // sounds like a good idea, but transaction-1 may not be the first one chronologically // for example, when you add the initial seeding transaction after entering some others // if (transactionId == 1) { -// L("This was the initial transaction. Terminating " + +// L("This was the initial transaction. +// Terminating " + // "parser"); // break LINES; // } } else { - m = reTransactionDetails.matcher(line); - if (m.find()) { - String postingStatus = m.group(1); - String acc_name = m.group(2); - String amount = m.group(3); - String currency = m.group(4); - if (currency == null) currency = ""; - amount = amount.replace(',', '.'); - transaction.addAccount(new LedgerTransactionAccount(acc_name, - Float.valueOf(amount), currency, null)); + LedgerTransactionAccount lta = parseTransactionAccountLine(line); + if (lta != null) { + transaction.addAccount(lta); L(String.format(Locale.ENGLISH, "%d: %s = %s", - transaction.getId(), acc_name, amount)); + transaction.getId(), lta.getAccountName(), + lta.getAmount())); } - else throw new IllegalStateException( - String.format("Can't parse transaction %d " + "details: %s", - transactionId, line)); + else + throw new IllegalStateException( + String.format("Can't parse transaction %d details: %s", + transactionId, line)); } break; default: @@ -440,19 +478,24 @@ public class RetrieveTransactionsTask while (true) { throwIfCancelled(); ParsedLedgerAccount parsedAccount = parser.nextAccount(); - if (parsedAccount == null) break; + if (parsedAccount == null) + break; LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname()); - if (acc == null) acc = new LedgerAccount(parsedAccount.getAname()); - else acc.removeAmounts(); + if (acc == null) + acc = new LedgerAccount(profile, parsedAccount.getAname()); + else + acc.removeAmounts(); profile.storeAccount(db, acc); String lastCurrency = null; float lastCurrencyAmount = 0; for (ParsedBalance b : parsedAccount.getAibalance()) { final String currency = b.getAcommodity(); - final float amount = b.getAquantity().asFloat(); - if (currency.equals(lastCurrency)) lastCurrencyAmount += amount; + final float amount = b.getAquantity() + .asFloat(); + if (currency.equals(lastCurrency)) + lastCurrencyAmount += amount; else { if (lastCurrency != null) { profile.storeAccountValue(db, acc.getName(), lastCurrency, @@ -469,11 +512,12 @@ public class RetrieveTransactionsTask acc.addAmount(lastCurrencyAmount, lastCurrency); } - if (acc.isVisible(accountList)) accountList.add(acc); + if (acc.isVisible(accountList)) + accountList.add(acc); if (prevAccount != null) { - prevAccount.setHasSubAccounts( - acc.getName().startsWith(prevAccount.getName() + ":")); + prevAccount.setHasSubAccounts(acc.getName() + .startsWith(prevAccount.getName() + ":")); } prevAccount = acc; @@ -491,7 +535,8 @@ public class RetrieveTransactionsTask } // should not be set in the DB transaction, because of a possible deadlock // with the main and DbOpQueueRunner threads - if (listFilledOK) Data.accounts.setList(accountList); + if (listFilledOK) + Data.accounts.setList(accountList); return true; } @@ -532,11 +577,14 @@ public class RetrieveTransactionsTask throwIfCancelled(); ParsedLedgerTransaction parsedTransaction = parser.nextTransaction(); throwIfCancelled(); - if (parsedTransaction == null) break; + if (parsedTransaction == null) + break; LedgerTransaction transaction = parsedTransaction.asLedgerTransaction(); - if (transaction.getId() > lastTransactionId) orderAccumulator++; - else orderAccumulator--; + if (transaction.getId() > lastTransactionId) + orderAccumulator++; + else + orderAccumulator--; lastTransactionId = transaction.getId(); if (transactionOrder == DetectedTransactionOrder.UNKNOWN) { if (orderAccumulator > 30) { @@ -549,8 +597,8 @@ public class RetrieveTransactionsTask else if (orderAccumulator < -30) { transactionOrder = DetectedTransactionOrder.REVERSE_CHRONOLOGICAL; debug("rtt", String.format(Locale.ENGLISH, - "Detected reverse chronological order after %d transactions (factor %d)", - processedTransactionCount, orderAccumulator)); + "Detected reverse chronological order after %d transactions " + + "(factor %d)", processedTransactionCount, orderAccumulator)); } } @@ -636,7 +684,8 @@ public class RetrieveTransactionsTask return contextRef.get(); } private void throwIfCancelled() { - if (isCancelled()) throw new OperationCanceledException(null); + if (isCancelled()) + throw new OperationCanceledException(null); } enum DetectedTransactionOrder {UNKNOWN, REVERSE_CHRONOLOGICAL, FILE}