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;
24 import android.util.Log;
26 import net.ktnx.mobileledger.R;
27 import net.ktnx.mobileledger.model.LedgerTransaction;
28 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
29 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
30 import net.ktnx.mobileledger.utils.MLDB;
31 import net.ktnx.mobileledger.utils.NetworkUtil;
33 import java.io.BufferedReader;
34 import java.io.FileNotFoundException;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.lang.ref.WeakReference;
39 import java.net.HttpURLConnection;
40 import java.net.MalformedURLException;
41 import java.util.Date;
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,.]*)(?:\\s+(\\S+)$)?");
54 private static final Pattern endPattern = Pattern.compile("\\bid=\"addmodal\"");
55 protected WeakReference<TransactionListFragment> contextRef;
57 private boolean success;
58 public RetrieveTransactionsTask(WeakReference<TransactionListFragment> 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 TransactionListFragment context = getContext();
68 if (context == null) return;
69 context.onRetrieveProgress(values[0]);
72 protected void onPreExecute() {
74 TransactionListFragment context = getContext();
75 if (context == null) return;
76 context.onRetrieveStart();
79 protected void onPostExecute(Void aVoid) {
80 super.onPostExecute(aVoid);
81 TransactionListFragment context = getContext();
82 if (context == null) return;
83 context.onRetrieveDone(success);
86 protected void onCancelled() {
88 TransactionListFragment context = getContext();
89 if (context == null) return;
90 context.onRetrieveDone(false);
92 @SuppressLint("DefaultLocale")
94 protected Void doInBackground(Params... params) {
95 Progress progress = new Progress();
96 int maxTransactionId = Progress.INDETERMINATE;
99 HttpURLConnection http =
100 NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
101 http.setAllowUserInteraction(false);
102 publishProgress(progress);
103 TransactionListFragment ctx = getContext();
104 if (ctx == null) return null;
105 try (SQLiteDatabase db = MLDB.getWritableDatabase(ctx.getActivity())) {
106 try (InputStream resp = http.getInputStream()) {
107 if (http.getResponseCode() != 200) throw new IOException(
108 String.format("HTTP error %d", http.getResponseCode()));
109 db.beginTransaction();
111 db.execSQL("UPDATE transactions set keep=0");
113 int state = ParserState.EXPECTING_JOURNAL;
116 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
118 int processedTransactionCount = 0;
119 int transactionId = 0;
120 int matchedTransactionsCount = 0;
121 LedgerTransaction transaction = null;
123 while ((line = buf.readLine()) != null) {
124 if (isCancelled()) break;
126 //L(String.format("State is %d", state));
128 case ParserState.EXPECTING_JOURNAL:
129 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
130 if (line.equals("<h2>General Journal</h2>")) {
131 state = ParserState.EXPECTING_TRANSACTION;
132 L("→ expecting transaction");
135 case ParserState.EXPECTING_TRANSACTION:
136 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
137 m = transactionStartPattern.matcher(line);
139 transactionId = Integer.valueOf(m.group(1));
140 state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
142 "found transaction %d → expecting " + "description",
144 progress.setProgress(++processedTransactionCount);
145 if (maxTransactionId < transactionId)
146 maxTransactionId = transactionId;
147 if ((progress.getTotal() == Progress.INDETERMINATE) ||
148 (progress.getTotal() < transactionId))
149 progress.setTotal(transactionId);
150 publishProgress(progress);
152 m = endPattern.matcher(line);
154 L("--- transaction list complete ---");
159 case ParserState.EXPECTING_TRANSACTION_DESCRIPTION:
160 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
161 m = transactionDescriptionPattern.matcher(line);
163 if (transactionId == 0)
164 throw new TransactionParserException(
165 "Transaction Id is 0 while expecting " +
169 new LedgerTransaction(transactionId, m.group(1),
171 state = ParserState.EXPECTING_TRANSACTION_DETAILS;
172 L(String.format("transaction %d created for %s (%s) →" +
173 " expecting details", transactionId,
174 m.group(1), m.group(2)));
177 case ParserState.EXPECTING_TRANSACTION_DETAILS:
178 if (line.isEmpty()) {
179 // transaction data collected
180 if (transaction.existsInDb(db)) {
181 db.execSQL("UPDATE transactions SET keep = 1 WHERE id" +
182 "=?", new Integer[]{transaction.getId()});
183 matchedTransactionsCount++;
185 if (matchedTransactionsCount == 100) {
186 db.execSQL("UPDATE transactions SET keep=1 WHERE " +
188 new Integer[]{transaction.getId()});
190 progress.setTotal(progress.getProgress());
191 publishProgress(progress);
196 db.execSQL("DELETE from transactions WHERE id=?",
197 new Integer[]{transaction.getId()});
198 db.execSQL("DELETE from transaction_accounts WHERE " +
200 new Integer[]{transaction.getId()});
201 transaction.insertInto(db);
202 matchedTransactionsCount = 0;
203 progress.setTotal(maxTransactionId);
206 state = ParserState.EXPECTING_TRANSACTION;
208 "transaction %s saved → expecting " + "transaction",
209 transaction.getId()));
211 // sounds like a good idea, but transaction-1 may not be the first one chronologically
212 // for example, when you add the initial seeding transaction after entering some others
213 // if (transactionId == 1) {
214 // L("This was the initial transaction. Terminating " +
220 m = transactionDetailsPattern.matcher(line);
222 String acc_name = m.group(1);
223 String amount = m.group(2);
224 String currency = m.group(3);
225 amount = amount.replace(',', '.');
226 transaction.addAccount(
227 new LedgerTransactionAccount(acc_name,
228 Float.valueOf(amount), currency));
229 L(String.format("%s = %s", acc_name, amount));
231 else throw new IllegalStateException(
232 String.format("Can't parse transaction %d details",
237 throw new RuntimeException(
238 String.format("Unknown parser state %d", state));
241 if (!isCancelled()) {
242 db.execSQL("DELETE FROM transactions WHERE keep = 0");
243 db.setTransactionSuccessful();
252 if (success && !isCancelled()) {
253 Log.d("db", "Updating transaction list stamp");
254 MLDB.set_option_value(ctx.getActivity(), MLDB.OPT_TRANSACTION_LIST_STAMP,
255 new Date().getTime());
256 ctx.model.reloadTransactions(ctx);
259 catch (MalformedURLException e) {
260 error = R.string.err_bad_backend_url;
263 catch (FileNotFoundException e) {
264 error = R.string.err_bad_auth;
267 catch (IOException e) {
268 error = R.string.err_net_io_error;
273 TransactionListFragment getContext() {
274 return contextRef.get();
277 public static class Params {
278 private SharedPreferences backendPref;
280 public Params(SharedPreferences backendPref) {
281 this.backendPref = backendPref;
283 SharedPreferences getBackendPref() {
288 public class Progress {
289 public static final int INDETERMINATE = -1;
290 private int progress;
293 this(INDETERMINATE, INDETERMINATE);
295 Progress(int progress, int total) {
296 this.progress = progress;
299 public int getProgress() {
302 protected void setProgress(int progress) {
303 this.progress = progress;
305 public int getTotal() {
308 protected void setTotal(int total) {
313 private class TransactionParserException extends IllegalStateException {
314 TransactionParserException(String message) {
319 private class ParserState {
320 static final int EXPECTING_JOURNAL = 0;
321 static final int EXPECTING_TRANSACTION = 1;
322 static final int EXPECTING_TRANSACTION_DESCRIPTION = 2;
323 static final int EXPECTING_TRANSACTION_DETAILS = 3;