]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
progressive update of transaction list in DB, stop after 100 matches
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
1 /*
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.
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
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;
31
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;
42
43
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;
54     protected int error;
55     private boolean success;
56     public RetrieveTransactionsTask(WeakReference<TransactionListActivity> contextRef) {
57         this.contextRef = contextRef;
58     }
59     private static final void L(String msg) {
60 //        Log.d("transaction-parser", msg);
61     }
62     @Override
63     protected void onProgressUpdate(Progress... values) {
64         super.onProgressUpdate(values);
65         TransactionListActivity context = getContext();
66         if (context == null) return;
67         context.onRetrieveProgress(values[0]);
68     }
69     @Override
70     protected void onPreExecute() {
71         super.onPreExecute();
72         TransactionListActivity context = getContext();
73         if (context == null) return;
74         context.onRetrieveStart();
75     }
76     @Override
77     protected void onPostExecute(Void aVoid) {
78         super.onPostExecute(aVoid);
79         TransactionListActivity context = getContext();
80         if (context == null) return;
81         context.onRetrieveDone(success);
82     }
83     @SuppressLint("DefaultLocale")
84     @Override
85     protected Void doInBackground(Params... params) {
86         Progress progress = new Progress();
87         int maxTransactionId = Progress.INDETERMINATE;
88         success = false;
89         try {
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();
101                     try {
102                         db.execSQL("UPDATE transactions set keep=0");
103
104                         int state = ParserState.EXPECTING_JOURNAL;
105                         String line;
106                         BufferedReader buf =
107                                 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
108
109                         int processedTransactionCount = 0;
110                         int transactionId = 0;
111                         int matchedTransactionsCount = 0;
112                         LedgerTransaction transaction = null;
113                         LINES:
114                         while ((line = buf.readLine()) != null) {
115                             if (isCancelled()) break;
116                             Matcher m;
117                             //L(String.format("State is %d", state));
118                             switch (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");
124                                     }
125                                     break;
126                                 case ParserState.EXPECTING_TRANSACTION:
127                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
128                                     m = transactionStartPattern.matcher(line);
129                                     if (m.find()) {
130                                         transactionId = Integer.valueOf(m.group(1));
131                                         state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
132                                         L(String.format(
133                                                 "found transaction %d → expecting " + "description",
134                                                 transactionId));
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);
142                                     }
143                                     m = endPattern.matcher(line);
144                                     if (m.find()) {
145                                         L("--- transaction list complete ---");
146                                         success = true;
147                                         break LINES;
148                                     }
149                                     break;
150                                 case ParserState.EXPECTING_TRANSACTION_DESCRIPTION:
151                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
152                                     m = transactionDescriptionPattern.matcher(line);
153                                     if (m.find()) {
154                                         if (transactionId == 0)
155                                             throw new TransactionParserException(
156                                                     "Transaction Id is 0 while expecting " +
157                                                     "description");
158
159                                         transaction =
160                                                 new LedgerTransaction(transactionId, m.group(1),
161                                                         m.group(2));
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)));
166                                     }
167                                     break;
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++;
175
176                                             if (matchedTransactionsCount == 100) {
177                                                 db.execSQL("UPDATE transactions SET keep=1 WHERE " +
178                                                            "id < ?",
179                                                         new Integer[]{transaction.getId()});
180                                                 progress.setTotal(progress.getProgress());
181                                                 publishProgress(progress);
182                                                 break LINES;
183                                             }
184                                         }
185                                         else {
186                                             db.execSQL("DELETE from transactions WHERE id=?",
187                                                     new Integer[]{transaction.getId()});
188                                             db.execSQL("DELETE from transaction_accounts WHERE " +
189                                                        "transaction_id=?",
190                                                     new Integer[]{transaction.getId()});
191                                             transaction.insertInto(db);
192                                             matchedTransactionsCount = 0;
193                                             progress.setTotal(maxTransactionId);
194                                         }
195
196                                         state = ParserState.EXPECTING_TRANSACTION;
197                                         L(String.format(
198                                                 "transaction %s saved → expecting " + "transaction",
199                                                 transaction.getId()));
200
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 " +
205 //                                                  "parser");
206 //                                                break LINES;
207 //                                            }
208                                     }
209                                     else {
210                                         m = transactionDetailsPattern.matcher(line);
211                                         if (m.find()) {
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));
218                                         }
219                                         else throw new IllegalStateException(
220                                                 String.format("Can't parse transaction details"));
221                                     }
222                                     break;
223                                 default:
224                                     throw new RuntimeException(
225                                             String.format("Unknown " + "parser state %d", state));
226                             }
227                         }
228                         if (!isCancelled()) {
229                             db.execSQL("DELETE FROM transactions WHERE keep = 0");
230                             db.setTransactionSuccessful();
231                         }
232                     }
233                     finally {
234                         db.endTransaction();
235                     }
236                 }
237             }
238
239             if (success && !isCancelled()) ctx.model.reloadTransactions(ctx);
240         }
241         catch (MalformedURLException e) {
242             error = R.string.err_bad_backend_url;
243             e.printStackTrace();
244         }
245         catch (FileNotFoundException e) {
246             error = R.string.err_bad_auth;
247             e.printStackTrace();
248         }
249         catch (IOException e) {
250             error = R.string.err_net_io_error;
251             e.printStackTrace();
252         }
253         return null;
254     }
255     TransactionListActivity getContext() {
256         return contextRef.get();
257     }
258
259     public static class Params {
260         static final int DEFAULT_LIMIT = 100;
261         private SharedPreferences backendPref;
262         private String accountsRoot;
263         private int limit;
264
265         public Params(SharedPreferences backendPref) {
266             this.backendPref = backendPref;
267             this.accountsRoot = null;
268             this.limit = DEFAULT_LIMIT;
269         }
270         Params(SharedPreferences backendPref, String accountsRoot) {
271             this(backendPref, accountsRoot, DEFAULT_LIMIT);
272         }
273         Params(SharedPreferences backendPref, String accountsRoot, int limit) {
274             this.backendPref = backendPref;
275             this.accountsRoot = accountsRoot;
276             this.limit = limit;
277         }
278         String getAccountsRoot() {
279             return accountsRoot;
280         }
281         SharedPreferences getBackendPref() {
282             return backendPref;
283         }
284         int getLimit() {
285             return limit;
286         }
287     }
288
289     public class Progress {
290         public static final int INDETERMINATE = -1;
291         private int progress;
292         private int total;
293         Progress() {
294             this(INDETERMINATE, INDETERMINATE);
295         }
296         Progress(int progress, int total) {
297             this.progress = progress;
298             this.total = total;
299         }
300         public int getProgress() {
301             return progress;
302         }
303         protected void setProgress(int progress) {
304             this.progress = progress;
305         }
306         public int getTotal() {
307             return total;
308         }
309         protected void setTotal(int total) {
310             this.total = total;
311         }
312     }
313
314     private class TransactionParserException extends IllegalStateException {
315         TransactionParserException(String message) {
316             super(message);
317         }
318     }
319
320     private class ParserState {
321         static final int EXPECTING_JOURNAL = 0;
322         static final int EXPECTING_TRANSACTION = 1;
323         static final int EXPECTING_TRANSACTION_DESCRIPTION = 2;
324         static final int EXPECTING_TRANSACTION_DETAILS = 3;
325     }
326 }