]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
c5850512920312992243bff315ab379cef89e231
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
1 /*
2  * Copyright © 2019 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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.async;
19
20 import android.annotation.SuppressLint;
21 import android.content.SharedPreferences;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.os.AsyncTask;
24 import android.os.OperationCanceledException;
25 import android.util.Log;
26
27 import net.ktnx.mobileledger.R;
28 import net.ktnx.mobileledger.model.Data;
29 import net.ktnx.mobileledger.model.LedgerAccount;
30 import net.ktnx.mobileledger.model.LedgerTransaction;
31 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
32 import net.ktnx.mobileledger.ui.activity.MainActivity;
33 import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel;
34 import net.ktnx.mobileledger.utils.MLDB;
35 import net.ktnx.mobileledger.utils.NetworkUtil;
36
37 import java.io.BufferedReader;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.io.InputStreamReader;
42 import java.lang.ref.WeakReference;
43 import java.net.HttpURLConnection;
44 import java.net.MalformedURLException;
45 import java.net.URLDecoder;
46 import java.util.ArrayList;
47 import java.util.Date;
48 import java.util.regex.Matcher;
49 import java.util.regex.Pattern;
50
51
52 public class RetrieveTransactionsTask extends
53         AsyncTask<RetrieveTransactionsTask.Params, RetrieveTransactionsTask.Progress, Void> {
54     public static final int MATCHING_TRANSACTIONS_LIMIT = 50;
55     private static final Pattern transactionStartPattern = Pattern.compile("<tr class=\"title\" " +
56                                                                            "id=\"transaction-(\\d+)\"><td class=\"date\"[^\\\"]*>([\\d.-]+)</td>");
57     private static final Pattern transactionDescriptionPattern =
58             Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
59     private static final Pattern transactionDetailsPattern =
60             Pattern.compile("^\\s+" + "(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)(?:\\s+(\\S+)$)?");
61     private static final Pattern endPattern = Pattern.compile("\\bid=\"addmodal\"");
62     protected WeakReference<MainActivity> contextRef;
63     protected int error;
64     // %3A is '='
65     Pattern account_name_re = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
66     Pattern account_value_re = Pattern.compile(
67             "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
68     Pattern tr_end_re = Pattern.compile("</tr>");
69     Pattern descriptions_line_re = Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b");
70     Pattern description_items_re = Pattern.compile("\"value\":\"([^\"]+)\"");
71     private boolean success;
72     public RetrieveTransactionsTask(WeakReference<MainActivity> contextRef) {
73         this.contextRef = contextRef;
74     }
75     private static final void L(String msg) {
76         Log.d("transaction-parser", msg);
77     }
78     @Override
79     protected void onProgressUpdate(Progress... values) {
80         super.onProgressUpdate(values);
81         MainActivity context = getContext();
82         if (context == null) return;
83         context.onRetrieveProgress(values[0]);
84     }
85     @Override
86     protected void onPreExecute() {
87         super.onPreExecute();
88         MainActivity context = getContext();
89         if (context == null) return;
90         context.onRetrieveStart();
91     }
92     @Override
93     protected void onPostExecute(Void aVoid) {
94         super.onPostExecute(aVoid);
95         MainActivity context = getContext();
96         if (context == null) return;
97         context.onRetrieveDone(success);
98     }
99     @Override
100     protected void onCancelled() {
101         super.onCancelled();
102         MainActivity context = getContext();
103         if (context == null) return;
104         context.onRetrieveDone(false);
105     }
106     @SuppressLint("DefaultLocale")
107     @Override
108     protected Void doInBackground(Params... params) {
109         Progress progress = new Progress();
110         int maxTransactionId = Progress.INDETERMINATE;
111         success = false;
112         ArrayList<LedgerAccount> accountList = new ArrayList<>();
113         LedgerAccount lastAccount = null;
114         Data.backgroundTaskCount.incrementAndGet();
115         try {
116             HttpURLConnection http =
117                     NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
118             http.setAllowUserInteraction(false);
119             publishProgress(progress);
120             MainActivity ctx = getContext();
121             if (ctx == null) return null;
122             try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
123                 try (InputStream resp = http.getInputStream()) {
124                     if (http.getResponseCode() != 200) throw new IOException(
125                             String.format("HTTP error %d", http.getResponseCode()));
126                     db.beginTransaction();
127                     try {
128                         db.execSQL("UPDATE transactions set keep=0");
129                         db.execSQL("update account_values set keep=0;");
130                         db.execSQL("update accounts set keep=0;");
131
132                         ParserState state = ParserState.EXPECTING_ACCOUNT;
133                         String line;
134                         BufferedReader buf =
135                                 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
136
137                         int processedTransactionCount = 0;
138                         int transactionId = 0;
139                         int matchedTransactionsCount = 0;
140                         LedgerTransaction transaction = null;
141                         LINES:
142                         while ((line = buf.readLine()) != null) {
143                             throwIfCancelled();
144                             Matcher m;
145                             //L(String.format("State is %d", updating));
146                             switch (state) {
147                                 case EXPECTING_ACCOUNT:
148                                     if (line.equals("<h2>General Journal</h2>")) {
149                                         state = ParserState.EXPECTING_TRANSACTION;
150                                         L("→ expecting transaction");
151                                         Data.accounts.set(accountList);
152                                         continue;
153                                     }
154                                     m = account_name_re.matcher(line);
155                                     if (m.find()) {
156                                         String acct_encoded = m.group(1);
157                                         String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
158                                         acct_name = acct_name.replace("\"", "");
159                                         L(String.format("found account: %s", acct_name));
160
161                                         addAccount(db, acct_name);
162                                         lastAccount = new LedgerAccount(acct_name);
163                                         accountList.add(lastAccount);
164
165                                         state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
166                                         L("→ expecting account amount");
167                                     }
168                                     break;
169
170                                 case EXPECTING_ACCOUNT_AMOUNT:
171                                     m = account_value_re.matcher(line);
172                                     boolean match_found = false;
173                                     while (m.find()) {
174                                         throwIfCancelled();
175
176                                         match_found = true;
177                                         String value = m.group(1);
178                                         String currency = m.group(2);
179                                         if (currency == null) currency = "";
180                                         value = value.replace(',', '.');
181                                         L("curr=" + currency + ", value=" + value);
182                                         db.execSQL(
183                                                 "insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
184                                                 new Object[]{lastAccount.getName(),
185                                                              currency,
186                                                              Float.valueOf(value)
187                                                 });
188                                         lastAccount.addAmount(Float.parseFloat(value), currency);
189                                     }
190
191                                     if (match_found) {
192                                         state = ParserState.EXPECTING_ACCOUNT;
193                                         L("→ expecting account");
194                                     }
195
196                                     break;
197
198                                 case EXPECTING_TRANSACTION:
199                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
200                                     m = transactionStartPattern.matcher(line);
201                                     if (m.find()) {
202                                         transactionId = Integer.valueOf(m.group(1));
203                                         state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
204                                         L(String.format(
205                                                 "found transaction %d → expecting description",
206                                                 transactionId));
207                                         progress.setProgress(++processedTransactionCount);
208                                         if (maxTransactionId < transactionId)
209                                             maxTransactionId = transactionId;
210                                         if ((progress.getTotal() == Progress.INDETERMINATE) ||
211                                             (progress.getTotal() < transactionId))
212                                             progress.setTotal(transactionId);
213                                         publishProgress(progress);
214                                     }
215                                     m = endPattern.matcher(line);
216                                     if (m.find()) {
217                                         L("--- transaction value complete ---");
218                                         success = true;
219                                         break LINES;
220                                     }
221                                     break;
222
223                                 case EXPECTING_TRANSACTION_DESCRIPTION:
224                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
225                                     m = transactionDescriptionPattern.matcher(line);
226                                     if (m.find()) {
227                                         if (transactionId == 0)
228                                             throw new TransactionParserException(
229                                                     "Transaction Id is 0 while expecting " +
230                                                     "description");
231
232                                         transaction =
233                                                 new LedgerTransaction(transactionId, m.group(1),
234                                                         m.group(2));
235                                         state = ParserState.EXPECTING_TRANSACTION_DETAILS;
236                                         L(String.format("transaction %d created for %s (%s) →" +
237                                                         " expecting details", transactionId,
238                                                 m.group(1), m.group(2)));
239                                     }
240                                     break;
241
242                                 case EXPECTING_TRANSACTION_DETAILS:
243                                     if (line.isEmpty()) {
244                                         // transaction data collected
245                                         if (transaction.existsInDb(db)) {
246                                             db.execSQL("UPDATE transactions SET keep = 1 WHERE id" +
247                                                        "=?", new Integer[]{transaction.getId()});
248                                             matchedTransactionsCount++;
249
250                                             if (matchedTransactionsCount ==
251                                                 MATCHING_TRANSACTIONS_LIMIT)
252                                             {
253                                                 db.execSQL("UPDATE transactions SET keep=1 WHERE " +
254                                                            "id < ?",
255                                                         new Integer[]{transaction.getId()});
256                                                 success = true;
257                                                 progress.setTotal(progress.getProgress());
258                                                 publishProgress(progress);
259                                                 break LINES;
260                                             }
261                                         }
262                                         else {
263                                             db.execSQL("DELETE from transactions WHERE id=?",
264                                                     new Integer[]{transaction.getId()});
265                                             db.execSQL("DELETE from transaction_accounts WHERE " +
266                                                        "transaction_id=?",
267                                                     new Integer[]{transaction.getId()});
268                                             transaction.insertInto(db);
269                                             matchedTransactionsCount = 0;
270                                             progress.setTotal(maxTransactionId);
271                                         }
272
273                                         state = ParserState.EXPECTING_TRANSACTION;
274                                         L(String.format(
275                                                 "transaction %s saved → expecting transaction",
276                                                 transaction.getId()));
277
278 // sounds like a good idea, but transaction-1 may not be the first one chronologically
279 // for example, when you add the initial seeding transaction after entering some others
280 //                                            if (transactionId == 1) {
281 //                                                L("This was the initial transaction. Terminating " +
282 //                                                  "parser");
283 //                                                break LINES;
284 //                                            }
285                                     }
286                                     else {
287                                         m = transactionDetailsPattern.matcher(line);
288                                         if (m.find()) {
289                                             String acc_name = m.group(1);
290                                             String amount = m.group(2);
291                                             String currency = m.group(3);
292                                             amount = amount.replace(',', '.');
293                                             transaction.addAccount(
294                                                     new LedgerTransactionAccount(acc_name,
295                                                             Float.valueOf(amount), currency));
296                                             L(String.format("%s = %s", acc_name, amount));
297                                         }
298                                         else throw new IllegalStateException(
299                                                 String.format("Can't parse transaction %d details",
300                                                         transactionId));
301                                     }
302                                     break;
303                                 default:
304                                     throw new RuntimeException(
305                                             String.format("Unknown parser updating %s", state.name()));
306                             }
307                         }
308
309                         throwIfCancelled();
310
311                         db.execSQL("DELETE FROM transactions WHERE keep = 0");
312                         db.setTransactionSuccessful();
313                     }
314                     finally {
315                         db.endTransaction();
316                     }
317                 }
318             }
319
320             if (success && !isCancelled()) {
321                 Log.d("db", "Updating transaction value stamp");
322                 MLDB.set_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP, new Date().getTime());
323                 TransactionListViewModel.scheduleTransactionListReload(ctx);
324             }
325         }
326         catch (MalformedURLException e) {
327             error = R.string.err_bad_backend_url;
328             e.printStackTrace();
329         }
330         catch (FileNotFoundException e) {
331             error = R.string.err_bad_auth;
332             e.printStackTrace();
333         }
334         catch (IOException e) {
335             error = R.string.err_net_io_error;
336             e.printStackTrace();
337         }
338         finally {
339             Data.backgroundTaskCount.decrementAndGet();
340         }
341         return null;
342     }
343     private MainActivity getContext() {
344         return contextRef.get();
345     }
346     private void addAccount(SQLiteDatabase db, String name) {
347         do {
348             LedgerAccount acc = new LedgerAccount(name);
349             db.execSQL("update accounts set level = ?, keep = 1 where name = ?",
350                     new Object[]{acc.getLevel(), name});
351             db.execSQL("insert into accounts(name, name_upper, parent_name, level) select ?,?," +
352                        "?,? " + "where (select changes() = 0)",
353                     new Object[]{name, name.toUpperCase(), acc.getParentName(), acc.getLevel()});
354             name = acc.getParentName();
355         } while (name != null);
356     }
357     private void throwIfCancelled() {
358         if (isCancelled()) throw new OperationCanceledException(null);
359     }
360
361     private enum ParserState {
362         EXPECTING_ACCOUNT, EXPECTING_ACCOUNT_AMOUNT, EXPECTING_JOURNAL, EXPECTING_TRANSACTION,
363         EXPECTING_TRANSACTION_DESCRIPTION, EXPECTING_TRANSACTION_DETAILS
364     }
365
366     public static class Params {
367         private SharedPreferences backendPref;
368
369         public Params(SharedPreferences backendPref) {
370             this.backendPref = backendPref;
371         }
372         SharedPreferences getBackendPref() {
373             return backendPref;
374         }
375     }
376
377     public class Progress {
378         public static final int INDETERMINATE = -1;
379         private int progress;
380         private int total;
381         Progress() {
382             this(INDETERMINATE, INDETERMINATE);
383         }
384         Progress(int progress, int total) {
385             this.progress = progress;
386             this.total = total;
387         }
388         public int getProgress() {
389             return progress;
390         }
391         protected void setProgress(int progress) {
392             this.progress = progress;
393         }
394         public int getTotal() {
395             return total;
396         }
397         protected void setTotal(int total) {
398             this.total = total;
399         }
400     }
401
402     private class TransactionParserException extends IllegalStateException {
403         TransactionParserException(String message) {
404             super(message);
405         }
406     }
407 }