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.SharedPreferences;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.os.AsyncTask;
25 import net.ktnx.mobileledger.R;
26 import net.ktnx.mobileledger.TransactionListActivity;
27 import net.ktnx.mobileledger.model.LedgerTransaction;
28 import net.ktnx.mobileledger.model.LedgerTransactionItem;
29 import net.ktnx.mobileledger.utils.MLDB;
30 import net.ktnx.mobileledger.utils.NetworkUtil;
32 import java.io.BufferedReader;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.InputStreamReader;
37 import java.lang.ref.WeakReference;
38 import java.net.HttpURLConnection;
39 import java.net.MalformedURLException;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
44 public class RetrieveTransactionsTask extends
45 AsyncTask<RetrieveTransactionsTask.Params, RetrieveTransactionsTask.Progress, Void> {
46 private static final Pattern transactionStartPattern = Pattern.compile("<tr class=\"title\" " +
47 "id=\"transaction-(\\d+)\"><td class=\"date\"[^\\\"]*>([\\d.-]+)</td>");
48 private static final Pattern transactionDescriptionPattern =
49 Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
50 private static final Pattern transactionDetailsPattern =
51 Pattern.compile("^\\s+" + "(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)");
52 private static final Pattern endPattern = Pattern.compile("\\bid=\"addmodal\"");
53 protected WeakReference<TransactionListActivity> contextRef;
55 private boolean success;
56 public RetrieveTransactionsTask(WeakReference<TransactionListActivity> contextRef) {
57 this.contextRef = contextRef;
59 private static final void L(String msg) {
60 // Log.d("transaction-parser", msg);
63 protected void onProgressUpdate(Progress... values) {
64 super.onProgressUpdate(values);
65 TransactionListActivity context = getContext();
66 if (context == null) return;
67 context.onRetrieveProgress(values[0]);
70 protected void onPreExecute() {
72 TransactionListActivity context = getContext();
73 if (context == null) return;
74 context.onRetrieveStart();
77 protected void onPostExecute(Void aVoid) {
78 super.onPostExecute(aVoid);
79 TransactionListActivity context = getContext();
80 if (context == null) return;
81 context.onRetrieveDone(success);
83 @SuppressLint("DefaultLocale")
85 protected Void doInBackground(Params... params) {
86 Progress progress = new Progress();
87 int maxTransactionId = Progress.INDETERMINATE;
90 HttpURLConnection http =
91 NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
92 http.setAllowUserInteraction(false);
93 publishProgress(progress);
94 TransactionListActivity ctx = contextRef.get();
95 if (ctx == null) return null;
96 try (SQLiteDatabase db = MLDB.getWritableDatabase(ctx)) {
97 try (InputStream resp = http.getInputStream()) {
98 if (http.getResponseCode() != 200) throw new IOException(
99 String.format("HTTP error %d", http.getResponseCode()));
100 db.beginTransaction();
102 db.execSQL("UPDATE transactions set keep=0");
104 int state = ParserState.EXPECTING_JOURNAL;
107 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
109 int processedTransactionCount = 0;
110 int transactionId = 0;
111 int matchedTransactionsCount = 0;
112 LedgerTransaction transaction = null;
114 while ((line = buf.readLine()) != null) {
115 if (isCancelled()) break;
117 //L(String.format("State is %d", state));
119 case ParserState.EXPECTING_JOURNAL:
120 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
121 if (line.equals("<h2>General Journal</h2>")) {
122 state = ParserState.EXPECTING_TRANSACTION;
123 L("→ expecting transaction");
126 case ParserState.EXPECTING_TRANSACTION:
127 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
128 m = transactionStartPattern.matcher(line);
130 transactionId = Integer.valueOf(m.group(1));
131 state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
133 "found transaction %d → expecting " + "description",
135 progress.setProgress(++processedTransactionCount);
136 if (maxTransactionId < transactionId)
137 maxTransactionId = transactionId;
138 if ((progress.getTotal() == Progress.INDETERMINATE) ||
139 (progress.getTotal() < transactionId))
140 progress.setTotal(transactionId);
141 publishProgress(progress);
143 m = endPattern.matcher(line);
145 L("--- transaction list complete ---");
150 case ParserState.EXPECTING_TRANSACTION_DESCRIPTION:
151 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
152 m = transactionDescriptionPattern.matcher(line);
154 if (transactionId == 0)
155 throw new TransactionParserException(
156 "Transaction Id is 0 while expecting " +
160 new LedgerTransaction(transactionId, m.group(1),
162 state = ParserState.EXPECTING_TRANSACTION_DETAILS;
163 L(String.format("transaction %d created for %s (%s) →" +
164 " expecting details", transactionId,
165 m.group(1), m.group(2)));
168 case ParserState.EXPECTING_TRANSACTION_DETAILS:
169 if (line.isEmpty()) {
170 // transaction data collected
171 if (transaction.existsInDb(db)) {
172 db.execSQL("UPDATE transactions SET keep = 1 WHERE id" +
173 "=?", new Integer[]{transaction.getId()});
174 matchedTransactionsCount++;
176 if (matchedTransactionsCount == 100) {
177 db.execSQL("UPDATE transactions SET keep=1 WHERE " +
179 new Integer[]{transaction.getId()});
180 progress.setTotal(progress.getProgress());
181 publishProgress(progress);
186 db.execSQL("DELETE from transactions WHERE id=?",
187 new Integer[]{transaction.getId()});
188 db.execSQL("DELETE from transaction_accounts WHERE " +
190 new Integer[]{transaction.getId()});
191 transaction.insertInto(db);
192 matchedTransactionsCount = 0;
193 progress.setTotal(maxTransactionId);
196 state = ParserState.EXPECTING_TRANSACTION;
198 "transaction %s saved → expecting " + "transaction",
199 transaction.getId()));
201 // sounds like a good idea, but transaction-1 may not be the first one chronologically
202 // for example, when you add the initial seeding transaction after entering some others
203 // if (transactionId == 1) {
204 // L("This was the initial transaction. Terminating " +
210 m = transactionDetailsPattern.matcher(line);
212 String acc_name = m.group(1);
213 String amount = m.group(2);
214 amount = amount.replace(',', '.');
215 transaction.add_item(new LedgerTransactionItem(acc_name,
216 Float.valueOf(amount)));
217 L(String.format("%s = %s", acc_name, amount));
219 else throw new IllegalStateException(
220 String.format("Can't parse transaction details"));
224 throw new RuntimeException(
225 String.format("Unknown parser state %d", state));
228 if (!isCancelled()) {
229 db.execSQL("DELETE FROM transactions WHERE keep = 0");
230 db.setTransactionSuccessful();
239 if (success && !isCancelled()) ctx.model.reloadTransactions(ctx);
241 catch (MalformedURLException e) {
242 error = R.string.err_bad_backend_url;
245 catch (FileNotFoundException e) {
246 error = R.string.err_bad_auth;
249 catch (IOException e) {
250 error = R.string.err_net_io_error;
255 TransactionListActivity getContext() {
256 return contextRef.get();
259 public static class Params {
260 private SharedPreferences backendPref;
262 public Params(SharedPreferences backendPref) {
263 this.backendPref = backendPref;
265 SharedPreferences getBackendPref() {
270 public class Progress {
271 public static final int INDETERMINATE = -1;
272 private int progress;
275 this(INDETERMINATE, INDETERMINATE);
277 Progress(int progress, int total) {
278 this.progress = progress;
281 public int getProgress() {
284 protected void setProgress(int progress) {
285 this.progress = progress;
287 public int getTotal() {
290 protected void setTotal(int total) {
295 private class TransactionParserException extends IllegalStateException {
296 TransactionParserException(String message) {
301 private class ParserState {
302 static final int EXPECTING_JOURNAL = 0;
303 static final int EXPECTING_TRANSACTION = 1;
304 static final int EXPECTING_TRANSACTION_DESCRIPTION = 2;
305 static final int EXPECTING_TRANSACTION_DETAILS = 3;