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;
24 import android.util.Log;
26 import net.ktnx.mobileledger.err.HTTPException;
27 import net.ktnx.mobileledger.json.AccountListParser;
28 import net.ktnx.mobileledger.json.ParsedBalance;
29 import net.ktnx.mobileledger.json.ParsedLedgerAccount;
30 import net.ktnx.mobileledger.json.ParsedLedgerTransaction;
31 import net.ktnx.mobileledger.json.TransactionListParser;
32 import net.ktnx.mobileledger.model.Data;
33 import net.ktnx.mobileledger.model.LedgerAccount;
34 import net.ktnx.mobileledger.model.LedgerTransaction;
35 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
36 import net.ktnx.mobileledger.model.MobileLedgerProfile;
37 import net.ktnx.mobileledger.ui.activity.MainActivity;
38 import net.ktnx.mobileledger.utils.MLDB;
39 import net.ktnx.mobileledger.utils.NetworkUtil;
41 import java.io.BufferedReader;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.InputStreamReader;
45 import java.lang.ref.WeakReference;
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.HashMap;
53 import java.util.Stack;
54 import java.util.regex.Matcher;
55 import java.util.regex.Pattern;
58 public class RetrieveTransactionsTask
59 extends AsyncTask<Void, RetrieveTransactionsTask.Progress, String> {
60 private static final int MATCHING_TRANSACTIONS_LIMIT = 50;
61 private static final Pattern reComment = Pattern.compile("^\\s*;");
62 private static final Pattern reTransactionStart = Pattern.compile("<tr class=\"title\" " +
63 "id=\"transaction-(\\d+)\"><td class=\"date\"[^\"]*>([\\d.-]+)</td>");
64 private static final Pattern reTransactionDescription =
65 Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
66 private static final Pattern reTransactionDetails =
67 Pattern.compile("^\\s+(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)(?:\\s+(\\S+)$)?");
68 private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
69 private WeakReference<MainActivity> contextRef;
72 private Pattern reAccountName = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
73 private Pattern reAccountValue = Pattern.compile(
74 "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
75 public RetrieveTransactionsTask(WeakReference<MainActivity> contextRef) {
76 this.contextRef = contextRef;
78 private static void L(String msg) {
79 //Log.d("transaction-parser", msg);
82 protected void onProgressUpdate(Progress... values) {
83 super.onProgressUpdate(values);
84 MainActivity context = getContext();
85 if (context == null) return;
86 context.onRetrieveProgress(values[0]);
89 protected void onPreExecute() {
91 MainActivity context = getContext();
92 if (context == null) return;
93 context.onRetrieveStart();
96 protected void onPostExecute(String error) {
97 super.onPostExecute(error);
98 MainActivity context = getContext();
99 if (context == null) return;
100 context.onRetrieveDone(error);
103 protected void onCancelled() {
105 MainActivity context = getContext();
106 if (context == null) return;
107 context.onRetrieveDone(null);
109 private String retrieveTransactionListLegacy(MobileLedgerProfile profile)
110 throws IOException, ParseException, HTTPException {
111 Progress progress = new Progress();
112 int maxTransactionId = Progress.INDETERMINATE;
113 ArrayList<LedgerAccount> accountList = new ArrayList<>();
114 HashMap<String, Void> accountNames = new HashMap<>();
115 LedgerAccount lastAccount = null, prevAccount = null;
116 boolean onlyStarred = Data.optShowOnlyStarred.get();
118 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
119 http.setAllowUserInteraction(false);
120 publishProgress(progress);
121 switch (http.getResponseCode()) {
125 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
127 try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
128 try (InputStream resp = http.getInputStream()) {
129 if (http.getResponseCode() != 200)
130 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
131 db.beginTransaction();
133 prepareDbForRetrieval(db, profile);
135 int matchedTransactionsCount = 0;
138 ParserState state = ParserState.EXPECTING_ACCOUNT;
141 new BufferedReader(new InputStreamReader(resp, StandardCharsets.UTF_8));
143 int processedTransactionCount = 0;
144 int transactionId = 0;
145 LedgerTransaction transaction = null;
147 while ((line = buf.readLine()) != null) {
150 m = reComment.matcher(line);
152 // TODO: comments are ignored for now
153 Log.v("transaction-parser", "Ignoring comment");
156 //L(String.format("State is %d", updating));
158 case EXPECTING_ACCOUNT:
159 if (line.equals("<h2>General Journal</h2>")) {
160 state = ParserState.EXPECTING_TRANSACTION;
161 L("→ expecting transaction");
162 Data.accounts.set(accountList);
165 m = reAccountName.matcher(line);
167 String acct_encoded = m.group(1);
168 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
169 acct_name = acct_name.replace("\"", "");
170 L(String.format("found account: %s", acct_name));
172 prevAccount = lastAccount;
173 lastAccount = profile.tryLoadAccount(db, acct_name);
174 if (lastAccount == null)
175 lastAccount = new LedgerAccount(acct_name);
176 else lastAccount.removeAmounts();
177 profile.storeAccount(db, lastAccount);
179 if (prevAccount != null) prevAccount
180 .setHasSubAccounts(prevAccount.isParentOf(lastAccount));
181 // make sure the parent account(s) are present,
182 // synthesising them if necessary
183 String parentName = lastAccount.getParentName();
184 if (parentName != null) {
185 Stack<String> toAppend = new Stack<>();
186 while (parentName != null) {
187 if (accountNames.containsKey(parentName)) break;
188 toAppend.push(parentName);
190 new LedgerAccount(parentName).getParentName();
192 while (!toAppend.isEmpty()) {
193 String aName = toAppend.pop();
194 LedgerAccount acc = new LedgerAccount(aName);
195 acc.setHiddenByStar(lastAccount.isHiddenByStar());
196 acc.setHasSubAccounts(true);
197 if ((!onlyStarred || !acc.isHiddenByStar()) &&
198 acc.isVisible(accountList)) accountList.add(acc);
199 L(String.format("gap-filling with %s", aName));
200 accountNames.put(aName, null);
201 profile.storeAccount(db, acc);
205 if ((!onlyStarred || !lastAccount.isHiddenByStar()) &&
206 lastAccount.isVisible(accountList))
207 accountList.add(lastAccount);
208 accountNames.put(acct_name, null);
210 state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
211 L("→ expecting account amount");
215 case EXPECTING_ACCOUNT_AMOUNT:
216 m = reAccountValue.matcher(line);
217 boolean match_found = false;
222 String value = m.group(1);
223 String currency = m.group(2);
224 if (currency == null) currency = "";
225 value = value.replace(',', '.');
226 L("curr=" + currency + ", value=" + value);
227 profile.storeAccountValue(db, lastAccount.getName(), currency,
228 Float.valueOf(value));
229 lastAccount.addAmount(Float.parseFloat(value), currency);
233 state = ParserState.EXPECTING_ACCOUNT;
234 L("→ expecting account");
239 case EXPECTING_TRANSACTION:
240 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
241 m = reTransactionStart.matcher(line);
243 transactionId = Integer.valueOf(m.group(1));
244 state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
245 L(String.format("found transaction %d → expecting description",
247 progress.setProgress(++processedTransactionCount);
248 if (maxTransactionId < transactionId)
249 maxTransactionId = transactionId;
250 if ((progress.getTotal() == Progress.INDETERMINATE) ||
251 (progress.getTotal() < transactionId))
252 progress.setTotal(transactionId);
253 publishProgress(progress);
255 m = reEnd.matcher(line);
257 L("--- transaction value complete ---");
262 case EXPECTING_TRANSACTION_DESCRIPTION:
263 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
264 m = reTransactionDescription.matcher(line);
266 if (transactionId == 0) throw new TransactionParserException(
267 "Transaction Id is 0 while expecting " + "description");
269 String date = m.group(1);
271 int equalsIndex = date.indexOf('=');
272 if (equalsIndex >= 0)
273 date = date.substring(equalsIndex + 1);
274 transaction = new LedgerTransaction(transactionId, date,
277 catch (ParseException e) {
279 return String.format("Error parsing date '%s'", date);
281 state = ParserState.EXPECTING_TRANSACTION_DETAILS;
282 L(String.format("transaction %d created for %s (%s) →" +
283 " expecting details", transactionId, date,
288 case EXPECTING_TRANSACTION_DETAILS:
289 if (line.isEmpty()) {
290 // transaction data collected
291 if (transaction.existsInDb(db)) {
292 profile.markTransactionAsPresent(db, transaction);
293 matchedTransactionsCount++;
295 if (matchedTransactionsCount ==
296 MATCHING_TRANSACTIONS_LIMIT)
298 profile.markTransactionsBeforeTransactionAsPresent(db,
300 progress.setTotal(progress.getProgress());
301 publishProgress(progress);
306 profile.storeTransaction(db, transaction);
307 matchedTransactionsCount = 0;
308 progress.setTotal(maxTransactionId);
311 state = ParserState.EXPECTING_TRANSACTION;
312 L(String.format("transaction %s saved → expecting transaction",
313 transaction.getId()));
314 transaction.finishLoading();
316 // sounds like a good idea, but transaction-1 may not be the first one chronologically
317 // for example, when you add the initial seeding transaction after entering some others
318 // if (transactionId == 1) {
319 // L("This was the initial transaction. Terminating " +
325 m = reTransactionDetails.matcher(line);
327 String acc_name = m.group(1);
328 String amount = m.group(2);
329 String currency = m.group(3);
330 if (currency == null) currency = "";
331 amount = amount.replace(',', '.');
332 transaction.addAccount(
333 new LedgerTransactionAccount(acc_name,
334 Float.valueOf(amount), currency));
335 L(String.format("%d: %s = %s", transaction.getId(),
338 else throw new IllegalStateException(String.format(
339 "Can't parse transaction %d " + "details: %s",
340 transactionId, line));
344 throw new RuntimeException(
345 String.format("Unknown parser updating %s", state.name()));
351 profile.deleteNotPresentTransactions(db);
352 db.setTransactionSuccessful();
354 profile.setLastUpdateStamp();
364 private void prepareDbForRetrieval(SQLiteDatabase db, MobileLedgerProfile profile) {
365 db.execSQL("UPDATE transactions set keep=0 where profile=?",
366 new String[]{profile.getUuid()});
367 db.execSQL("update account_values set keep=0 where profile=?;",
368 new String[]{profile.getUuid()});
369 db.execSQL("update accounts set keep=0 where profile=?;", new String[]{profile.getUuid()});
371 private boolean retrieveAccountList(MobileLedgerProfile profile)
372 throws IOException, HTTPException {
373 Progress progress = new Progress();
375 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
376 http.setAllowUserInteraction(false);
377 switch (http.getResponseCode()) {
383 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
385 publishProgress(progress);
386 try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
387 try (InputStream resp = http.getInputStream()) {
388 if (http.getResponseCode() != 200)
389 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
391 db.beginTransaction();
393 profile.markAccountsAsNotPresent(db);
395 AccountListParser parser = new AccountListParser(resp);
396 ArrayList<LedgerAccount> accountList = new ArrayList<>();
398 LedgerAccount prevAccount = null;
402 ParsedLedgerAccount parsedAccount = parser.nextAccount();
403 if (parsedAccount == null) break;
405 LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname());
406 if (acc == null) acc = new LedgerAccount(parsedAccount.getAname());
407 else acc.removeAmounts();
409 profile.storeAccount(db, acc);
410 for (ParsedBalance b : parsedAccount.getAebalance()) {
411 profile.storeAccountValue(db, acc.getName(), b.getAcommodity(),
412 b.getAquantity().asFloat());
415 if (acc.isVisible(accountList)) accountList.add(acc);
417 if (prevAccount != null) {
418 prevAccount.setHasSubAccounts(
419 acc.getName().startsWith(prevAccount.getName() + ":"));
426 profile.deleteNotPresentAccounts(db);
428 db.setTransactionSuccessful();
429 Data.accounts.set(accountList);
439 private boolean retrieveTransactionList(MobileLedgerProfile profile)
440 throws IOException, ParseException, HTTPException {
441 Progress progress = new Progress();
442 int maxTransactionId = Progress.INDETERMINATE;
444 HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
445 http.setAllowUserInteraction(false);
446 publishProgress(progress);
447 switch (http.getResponseCode()) {
453 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
455 try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
456 try (InputStream resp = http.getInputStream()) {
457 if (http.getResponseCode() != 200)
458 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
460 db.beginTransaction();
462 profile.markTransactionsAsNotPresent(db);
464 int matchedTransactionsCount = 0;
465 TransactionListParser parser = new TransactionListParser(resp);
467 int processedTransactionCount = 0;
471 ParsedLedgerTransaction parsedTransaction = parser.nextTransaction();
473 if (parsedTransaction == null) break;
474 LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
475 if (transaction.existsInDb(db)) {
476 profile.markTransactionAsPresent(db, transaction);
477 matchedTransactionsCount++;
479 if (matchedTransactionsCount == MATCHING_TRANSACTIONS_LIMIT) {
480 profile.markTransactionsBeforeTransactionAsPresent(db, transaction);
481 progress.setTotal(progress.getProgress());
482 publishProgress(progress);
483 db.setTransactionSuccessful();
484 profile.setLastUpdateStamp();
489 profile.storeTransaction(db, transaction);
490 matchedTransactionsCount = 0;
491 progress.setTotal(maxTransactionId);
494 if ((progress.getTotal() == Progress.INDETERMINATE) ||
495 (progress.getTotal() < transaction.getId()))
496 progress.setTotal(transaction.getId());
498 progress.setProgress(++processedTransactionCount);
499 publishProgress(progress);
503 profile.deleteNotPresentTransactions(db);
505 db.setTransactionSuccessful();
506 profile.setLastUpdateStamp();
516 @SuppressLint("DefaultLocale")
518 protected String doInBackground(Void... params) {
519 MobileLedgerProfile profile = Data.profile.get();
520 Data.backgroundTaskCount.incrementAndGet();
522 if (!retrieveAccountList(profile) || !retrieveTransactionList(profile))
523 return retrieveTransactionListLegacy(profile);
526 catch (MalformedURLException e) {
528 return "Invalid server URL";
530 catch (HTTPException e) {
532 return String.format("HTTP error %d: %s", e.getResponseCode(), e.getResponseMessage());
534 catch (IOException e) {
536 return e.getLocalizedMessage();
538 catch (ParseException e) {
540 return "Network error";
542 catch (OperationCanceledException e) {
544 return "Operation cancelled";
547 Data.backgroundTaskCount.decrementAndGet();
550 private MainActivity getContext() {
551 return contextRef.get();
553 private void throwIfCancelled() {
554 if (isCancelled()) throw new OperationCanceledException(null);
557 private enum ParserState {
558 EXPECTING_ACCOUNT, EXPECTING_ACCOUNT_AMOUNT, EXPECTING_JOURNAL, EXPECTING_TRANSACTION,
559 EXPECTING_TRANSACTION_DESCRIPTION, EXPECTING_TRANSACTION_DETAILS
562 public class Progress {
563 public static final int INDETERMINATE = -1;
564 private int progress;
567 this(INDETERMINATE, INDETERMINATE);
569 Progress(int progress, int total) {
570 this.progress = progress;
573 public int getProgress() {
576 protected void setProgress(int progress) {
577 this.progress = progress;
579 public int getTotal() {
582 protected void setTotal(int total) {
587 private class TransactionParserException extends IllegalStateException {
588 TransactionParserException(String message) {