2 * Copyright © 2018 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger 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 * Mobile-Ledger 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 Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.async;
20 import android.annotation.SuppressLint;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.database.sqlite.SQLiteDatabase;
24 import android.os.AsyncTask;
25 import android.util.Log;
27 import net.ktnx.mobileledger.R;
28 import net.ktnx.mobileledger.TransactionListActivity;
29 import net.ktnx.mobileledger.model.LedgerTransaction;
30 import net.ktnx.mobileledger.model.LedgerTransactionItem;
31 import net.ktnx.mobileledger.utils.MLDB;
32 import net.ktnx.mobileledger.utils.NetworkUtil;
34 import java.io.BufferedReader;
35 import java.io.FileNotFoundException;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.lang.ref.WeakReference;
40 import java.net.HttpURLConnection;
41 import java.net.MalformedURLException;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
46 public class RetrieveTransactionsTask extends
47 AsyncTask<RetrieveTransactionsTask.Params, RetrieveTransactionsTask.Progress, Void> {
48 private static final Pattern transactionStartPattern = Pattern.compile("<tr class=\"title\" " +
49 "id=\"transaction-(\\d+)\"><td class=\"date\"[^\\\"]*>([\\d.-]+)</td>");
50 private static final Pattern transactionDescriptionPattern =
51 Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
52 private static final Pattern transactionDetailsPattern =
53 Pattern.compile("^\\s+" + "(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)");
54 private static final Pattern endPattern = Pattern.compile("\\bid=\"addmodal\"");
55 protected WeakReference<TransactionListActivity> contextRef;
57 private boolean success;
58 public RetrieveTransactionsTask(WeakReference<TransactionListActivity> contextRef) {
59 this.contextRef = contextRef;
61 private static final void L(String msg) {
62 Log.d("transaction-parser", msg);
65 protected void onProgressUpdate(Progress... values) {
66 super.onProgressUpdate(values);
67 TransactionListActivity context = getContext();
68 if (context == null) return;
69 context.onRetrieveProgress(values[0]);
72 protected void onPreExecute() {
74 TransactionListActivity context = getContext();
75 if (context == null) return;
76 context.onRetrieveStart();
79 protected void onPostExecute(Void aVoid) {
80 super.onPostExecute(aVoid);
81 TransactionListActivity context = getContext();
82 if (context == null) return;
83 context.onRetrieveDone(success);
85 @SuppressLint("DefaultLocale")
87 protected Void doInBackground(Params... params) {
88 Progress progress = new Progress();
91 HttpURLConnection http =
92 NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
93 http.setAllowUserInteraction(false);
94 publishProgress(progress);
95 Context ctx = contextRef.get();
96 if (ctx == null) return null;
97 try (SQLiteDatabase db = MLDB.getWritableDatabase(ctx)) {
98 try (InputStream resp = http.getInputStream()) {
99 if (http.getResponseCode() != 200) throw new IOException(
100 String.format("HTTP error %d", http.getResponseCode()));
101 db.beginTransaction();
103 db.execSQL("DELETE FROM transactions;");
104 db.execSQL("DELETE FROM transaction_accounts");
106 int state = ParserState.EXPECTING_JOURNAL;
109 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
111 int transactionCount = 0;
112 int transactionId = 0;
113 LedgerTransaction transaction = null;
115 while ((line = buf.readLine()) != null) {
116 if (isCancelled()) break;
117 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
119 //L(String.format("State is %d", state));
121 case ParserState.EXPECTING_JOURNAL:
122 if (line.equals("<h2>General Journal</h2>")) {
123 state = ParserState.EXPECTING_TRANSACTION;
124 L("→ expecting transaction");
127 case ParserState.EXPECTING_TRANSACTION:
128 m = transactionStartPattern.matcher(line);
130 transactionId = Integer.valueOf(m.group(1));
131 state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
132 L(String.format("found transaction %d → expecting " +
133 "description", transactionId));
134 progress.setProgress(++transactionCount);
135 if (progress.getTotal() == Progress.INDETERMINATE)
136 progress.setTotal(transactionId);
137 publishProgress(progress);
139 m = endPattern.matcher(line);
141 L("--- transaction list complete ---");
146 case ParserState.EXPECTING_TRANSACTION_DESCRIPTION:
147 m = transactionDescriptionPattern.matcher(line);
149 if (transactionId == 0)
150 throw new TransactionParserException(
151 "Transaction Id is 0 while expecting " +
155 new LedgerTransaction(transactionId, m.group(1),
157 state = ParserState.EXPECTING_TRANSACTION_DETAILS;
158 L(String.format("transaction %d created for %s (%s) →" +
159 " expecting details", transactionId,
160 m.group(1), m.group(2)));
163 case ParserState.EXPECTING_TRANSACTION_DETAILS:
164 if (line.isEmpty()) {
165 // transaction data collected
166 transaction.insertInto(db);
168 state = ParserState.EXPECTING_TRANSACTION;
169 L(String.format("transaction %s saved → expecting " +
170 "transaction", transaction.getId()));
172 // sounds like a good idea, but transaction-1 may not be the first one chronologically
173 // for example, when you add the initial seeding transaction after entering some others
174 // if (transactionId == 1) {
175 // L("This was the initial transaction. Terminating " +
181 m = transactionDetailsPattern.matcher(line);
183 String acc_name = m.group(1);
184 String amount = m.group(2);
185 amount = amount.replace(',', '.');
186 transaction.add_item(
187 new LedgerTransactionItem(acc_name,
188 Float.valueOf(amount)));
189 L(String.format("%s = %s", acc_name, amount));
191 else throw new IllegalStateException(String.format(
192 "Can't parse transaction details"));
196 throw new RuntimeException(
197 String.format("Unknown " + "parser state %d",
201 if (!isCancelled()) db.setTransactionSuccessful();
210 catch (MalformedURLException e) {
211 error = R.string.err_bad_backend_url;
214 catch (FileNotFoundException e) {
215 error = R.string.err_bad_auth;
218 catch (IOException e) {
219 error = R.string.err_net_io_error;
224 TransactionListActivity getContext() {
225 return contextRef.get();
228 public static class Params {
229 static final int DEFAULT_LIMIT = 100;
230 private SharedPreferences backendPref;
231 private String accountsRoot;
234 public Params(SharedPreferences backendPref) {
235 this.backendPref = backendPref;
236 this.accountsRoot = null;
237 this.limit = DEFAULT_LIMIT;
239 Params(SharedPreferences backendPref, String accountsRoot) {
240 this(backendPref, accountsRoot, DEFAULT_LIMIT);
242 Params(SharedPreferences backendPref, String accountsRoot, int limit) {
243 this.backendPref = backendPref;
244 this.accountsRoot = accountsRoot;
247 String getAccountsRoot() {
250 SharedPreferences getBackendPref() {
258 public class Progress {
259 public static final int INDETERMINATE = -1;
260 private int progress;
263 this(INDETERMINATE, INDETERMINATE);
265 Progress(int progress, int total) {
266 this.progress = progress;
269 public int getProgress() {
272 protected void setProgress(int progress) {
273 this.progress = progress;
275 public int getTotal() {
278 protected void setTotal(int total) {
283 private class TransactionParserException extends IllegalStateException {
284 TransactionParserException(String message) {
289 private class ParserState {
290 static final int EXPECTING_JOURNAL = 0;
291 static final int EXPECTING_TRANSACTION = 1;
292 static final int EXPECTING_TRANSACTION_DESCRIPTION = 2;
293 static final int EXPECTING_TRANSACTION_DETAILS = 3;