]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
remove the distinction between "readable" and "writable" database connections
[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 MoLe.
4  * MoLe 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  * MoLe 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 MoLe. 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.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23 import android.os.OperationCanceledException;
24 import android.util.Log;
25
26 import net.ktnx.mobileledger.err.HTTPException;
27 import net.ktnx.mobileledger.json.AccountListParser;
28 import net.ktnx.mobileledger.json.ParsedBalance;
29 import net.ktnx.mobileledger.json.ParsedLedgerAccount;
30 import net.ktnx.mobileledger.json.ParsedLedgerTransaction;
31 import net.ktnx.mobileledger.json.TransactionListParser;
32 import net.ktnx.mobileledger.model.Data;
33 import net.ktnx.mobileledger.model.LedgerAccount;
34 import net.ktnx.mobileledger.model.LedgerTransaction;
35 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
36 import net.ktnx.mobileledger.model.MobileLedgerProfile;
37 import net.ktnx.mobileledger.ui.activity.MainActivity;
38 import net.ktnx.mobileledger.utils.MLDB;
39 import net.ktnx.mobileledger.utils.NetworkUtil;
40
41 import java.io.BufferedReader;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.InputStreamReader;
45 import java.lang.ref.WeakReference;
46 import java.net.HttpURLConnection;
47 import java.net.MalformedURLException;
48 import java.net.URLDecoder;
49 import java.nio.charset.StandardCharsets;
50 import java.text.ParseException;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.Stack;
54 import java.util.regex.Matcher;
55 import java.util.regex.Pattern;
56
57
58 public class RetrieveTransactionsTask
59         extends AsyncTask<Void, RetrieveTransactionsTask.Progress, String> {
60     private static final int MATCHING_TRANSACTIONS_LIMIT = 50;
61     private static final Pattern reComment = Pattern.compile("^\\s*;");
62     private static final Pattern reTransactionStart = Pattern.compile("<tr class=\"title\" " +
63                                                                       "id=\"transaction-(\\d+)\"><td class=\"date\"[^\"]*>([\\d.-]+)</td>");
64     private static final Pattern reTransactionDescription =
65             Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
66     private static final Pattern reTransactionDetails =
67             Pattern.compile("^\\s+(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)(?:\\s+(\\S+)$)?");
68     private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
69     private WeakReference<MainActivity> contextRef;
70     private int error;
71     // %3A is '='
72     private Pattern reAccountName = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
73     private Pattern reAccountValue = Pattern.compile(
74             "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
75     public RetrieveTransactionsTask(WeakReference<MainActivity> contextRef) {
76         this.contextRef = contextRef;
77     }
78     private static void L(String msg) {
79         //Log.d("transaction-parser", msg);
80     }
81     @Override
82     protected void onProgressUpdate(Progress... values) {
83         super.onProgressUpdate(values);
84         MainActivity context = getContext();
85         if (context == null) return;
86         context.onRetrieveProgress(values[0]);
87     }
88     @Override
89     protected void onPreExecute() {
90         super.onPreExecute();
91         MainActivity context = getContext();
92         if (context == null) return;
93         context.onRetrieveStart();
94     }
95     @Override
96     protected void onPostExecute(String error) {
97         super.onPostExecute(error);
98         MainActivity context = getContext();
99         if (context == null) return;
100         context.onRetrieveDone(error);
101     }
102     @Override
103     protected void onCancelled() {
104         super.onCancelled();
105         MainActivity context = getContext();
106         if (context == null) return;
107         context.onRetrieveDone(null);
108     }
109     private String retrieveTransactionListLegacy(MobileLedgerProfile profile)
110             throws IOException, ParseException, HTTPException {
111         Progress progress = new Progress();
112         int maxTransactionId = Progress.INDETERMINATE;
113         ArrayList<LedgerAccount> accountList = new ArrayList<>();
114         HashMap<String, Void> accountNames = new HashMap<>();
115         LedgerAccount lastAccount = null, prevAccount = null;
116         boolean onlyStarred = Data.optShowOnlyStarred.get();
117
118         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
119         http.setAllowUserInteraction(false);
120         publishProgress(progress);
121         switch (http.getResponseCode()) {
122             case 200:
123                 break;
124             default:
125                 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
126         }
127         try (SQLiteDatabase db = MLDB.getDatabase()) {
128             try (InputStream resp = http.getInputStream()) {
129                 if (http.getResponseCode() != 200)
130                     throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
131                 db.beginTransaction();
132                 try {
133                     prepareDbForRetrieval(db, profile);
134
135                     int matchedTransactionsCount = 0;
136
137
138                     ParserState state = ParserState.EXPECTING_ACCOUNT;
139                     String line;
140                     BufferedReader buf =
141                             new BufferedReader(new InputStreamReader(resp, StandardCharsets.UTF_8));
142
143                     int processedTransactionCount = 0;
144                     int transactionId = 0;
145                     LedgerTransaction transaction = null;
146                     LINES:
147                     while ((line = buf.readLine()) != null) {
148                         throwIfCancelled();
149                         Matcher m;
150                         m = reComment.matcher(line);
151                         if (m.find()) {
152                             // TODO: comments are ignored for now
153                             Log.v("transaction-parser", "Ignoring comment");
154                             continue;
155                         }
156                         //L(String.format("State is %d", updating));
157                         switch (state) {
158                             case EXPECTING_ACCOUNT:
159                                 if (line.equals("<h2>General Journal</h2>")) {
160                                     state = ParserState.EXPECTING_TRANSACTION;
161                                     L("→ expecting transaction");
162                                     Data.accounts.set(accountList);
163                                     continue;
164                                 }
165                                 m = reAccountName.matcher(line);
166                                 if (m.find()) {
167                                     String acct_encoded = m.group(1);
168                                     String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
169                                     acct_name = acct_name.replace("\"", "");
170                                     L(String.format("found account: %s", acct_name));
171
172                                     prevAccount = lastAccount;
173                                     lastAccount = profile.tryLoadAccount(db, acct_name);
174                                     if (lastAccount == null)
175                                         lastAccount = new LedgerAccount(acct_name);
176                                     else lastAccount.removeAmounts();
177                                     profile.storeAccount(db, lastAccount);
178
179                                     if (prevAccount != null) prevAccount
180                                             .setHasSubAccounts(prevAccount.isParentOf(lastAccount));
181                                     // make sure the parent account(s) are present,
182                                     // synthesising them if necessary
183                                     String parentName = lastAccount.getParentName();
184                                     if (parentName != null) {
185                                         Stack<String> toAppend = new Stack<>();
186                                         while (parentName != null) {
187                                             if (accountNames.containsKey(parentName)) break;
188                                             toAppend.push(parentName);
189                                             parentName =
190                                                     new LedgerAccount(parentName).getParentName();
191                                         }
192                                         while (!toAppend.isEmpty()) {
193                                             String aName = toAppend.pop();
194                                             LedgerAccount acc = new LedgerAccount(aName);
195                                             acc.setHiddenByStar(lastAccount.isHiddenByStar());
196                                             acc.setHasSubAccounts(true);
197                                             if ((!onlyStarred || !acc.isHiddenByStar()) &&
198                                                 acc.isVisible(accountList)) accountList.add(acc);
199                                             L(String.format("gap-filling with %s", aName));
200                                             accountNames.put(aName, null);
201                                             profile.storeAccount(db, acc);
202                                         }
203                                     }
204
205                                     if ((!onlyStarred || !lastAccount.isHiddenByStar()) &&
206                                         lastAccount.isVisible(accountList))
207                                         accountList.add(lastAccount);
208                                     accountNames.put(acct_name, null);
209
210                                     state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
211                                     L("→ expecting account amount");
212                                 }
213                                 break;
214
215                             case EXPECTING_ACCOUNT_AMOUNT:
216                                 m = reAccountValue.matcher(line);
217                                 boolean match_found = false;
218                                 while (m.find()) {
219                                     throwIfCancelled();
220
221                                     match_found = true;
222                                     String value = m.group(1);
223                                     String currency = m.group(2);
224                                     if (currency == null) currency = "";
225                                     value = value.replace(',', '.');
226                                     L("curr=" + currency + ", value=" + value);
227                                     profile.storeAccountValue(db, lastAccount.getName(), currency,
228                                             Float.valueOf(value));
229                                     lastAccount.addAmount(Float.parseFloat(value), currency);
230                                 }
231
232                                 if (match_found) {
233                                     state = ParserState.EXPECTING_ACCOUNT;
234                                     L("→ expecting account");
235                                 }
236
237                                 break;
238
239                             case EXPECTING_TRANSACTION:
240                                 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
241                                 m = reTransactionStart.matcher(line);
242                                 if (m.find()) {
243                                     transactionId = Integer.valueOf(m.group(1));
244                                     state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
245                                     L(String.format("found transaction %d → expecting description",
246                                             transactionId));
247                                     progress.setProgress(++processedTransactionCount);
248                                     if (maxTransactionId < transactionId)
249                                         maxTransactionId = transactionId;
250                                     if ((progress.getTotal() == Progress.INDETERMINATE) ||
251                                         (progress.getTotal() < transactionId))
252                                         progress.setTotal(transactionId);
253                                     publishProgress(progress);
254                                 }
255                                 m = reEnd.matcher(line);
256                                 if (m.find()) {
257                                     L("--- transaction value complete ---");
258                                     break LINES;
259                                 }
260                                 break;
261
262                             case EXPECTING_TRANSACTION_DESCRIPTION:
263                                 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
264                                 m = reTransactionDescription.matcher(line);
265                                 if (m.find()) {
266                                     if (transactionId == 0) throw new TransactionParserException(
267                                             "Transaction Id is 0 while expecting " + "description");
268
269                                     String date = m.group(1);
270                                     try {
271                                         int equalsIndex = date.indexOf('=');
272                                         if (equalsIndex >= 0)
273                                             date = date.substring(equalsIndex + 1);
274                                         transaction = new LedgerTransaction(transactionId, date,
275                                                 m.group(2));
276                                     }
277                                     catch (ParseException e) {
278                                         e.printStackTrace();
279                                         return String.format("Error parsing date '%s'", date);
280                                     }
281                                     state = ParserState.EXPECTING_TRANSACTION_DETAILS;
282                                     L(String.format("transaction %d created for %s (%s) →" +
283                                                     " expecting details", transactionId, date,
284                                             m.group(2)));
285                                 }
286                                 break;
287
288                             case EXPECTING_TRANSACTION_DETAILS:
289                                 if (line.isEmpty()) {
290                                     // transaction data collected
291                                     if (transaction.existsInDb(db)) {
292                                         profile.markTransactionAsPresent(db, transaction);
293                                         matchedTransactionsCount++;
294
295                                         if (matchedTransactionsCount ==
296                                             MATCHING_TRANSACTIONS_LIMIT)
297                                         {
298                                             profile.markTransactionsBeforeTransactionAsPresent(db,
299                                                     transaction);
300                                             progress.setTotal(progress.getProgress());
301                                             publishProgress(progress);
302                                             break LINES;
303                                         }
304                                     }
305                                     else {
306                                         profile.storeTransaction(db, transaction);
307                                         matchedTransactionsCount = 0;
308                                         progress.setTotal(maxTransactionId);
309                                     }
310
311                                     state = ParserState.EXPECTING_TRANSACTION;
312                                     L(String.format("transaction %s saved → expecting transaction",
313                                             transaction.getId()));
314                                     transaction.finishLoading();
315
316 // sounds like a good idea, but transaction-1 may not be the first one chronologically
317 // for example, when you add the initial seeding transaction after entering some others
318 //                                            if (transactionId == 1) {
319 //                                                L("This was the initial transaction. Terminating " +
320 //                                                  "parser");
321 //                                                break LINES;
322 //                                            }
323                                 }
324                                 else {
325                                     m = reTransactionDetails.matcher(line);
326                                     if (m.find()) {
327                                         String acc_name = m.group(1);
328                                         String amount = m.group(2);
329                                         String currency = m.group(3);
330                                         if (currency == null) currency = "";
331                                         amount = amount.replace(',', '.');
332                                         transaction.addAccount(
333                                                 new LedgerTransactionAccount(acc_name,
334                                                         Float.valueOf(amount), currency));
335                                         L(String.format("%d: %s = %s", transaction.getId(),
336                                                 acc_name, amount));
337                                     }
338                                     else throw new IllegalStateException(String.format(
339                                             "Can't parse transaction %d " + "details: %s",
340                                             transactionId, line));
341                                 }
342                                 break;
343                             default:
344                                 throw new RuntimeException(
345                                         String.format("Unknown parser updating %s", state.name()));
346                         }
347                     }
348
349                     throwIfCancelled();
350
351                     profile.deleteNotPresentTransactions(db);
352                     db.setTransactionSuccessful();
353
354                     profile.setLastUpdateStamp();
355
356                     return null;
357                 }
358                 finally {
359                     db.endTransaction();
360                 }
361             }
362         }
363     }
364     private void prepareDbForRetrieval(SQLiteDatabase db, MobileLedgerProfile profile) {
365         db.execSQL("UPDATE transactions set keep=0 where profile=?",
366                 new String[]{profile.getUuid()});
367         db.execSQL("update account_values set keep=0 where profile=?;",
368                 new String[]{profile.getUuid()});
369         db.execSQL("update accounts set keep=0 where profile=?;", new String[]{profile.getUuid()});
370     }
371     private boolean retrieveAccountList(MobileLedgerProfile profile)
372             throws IOException, HTTPException {
373         Progress progress = new Progress();
374
375         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
376         http.setAllowUserInteraction(false);
377         switch (http.getResponseCode()) {
378             case 200:
379                 break;
380             case 404:
381                 return false;
382             default:
383                 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
384         }
385         publishProgress(progress);
386         SQLiteDatabase db = MLDB.getDatabase();
387         try (InputStream resp = http.getInputStream()) {
388             if (http.getResponseCode() != 200)
389                 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
390
391             db.beginTransaction();
392             try {
393                 profile.markAccountsAsNotPresent(db);
394
395                 AccountListParser parser = new AccountListParser(resp);
396                 ArrayList<LedgerAccount> accountList = new ArrayList<>();
397
398                 LedgerAccount prevAccount = null;
399
400                 while (true) {
401                     throwIfCancelled();
402                     ParsedLedgerAccount parsedAccount = parser.nextAccount();
403                     if (parsedAccount == null) break;
404
405                     LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname());
406                     if (acc == null) acc = new LedgerAccount(parsedAccount.getAname());
407                     else acc.removeAmounts();
408
409                     profile.storeAccount(db, acc);
410                     for (ParsedBalance b : parsedAccount.getAebalance()) {
411                         profile.storeAccountValue(db, acc.getName(), b.getAcommodity(),
412                                 b.getAquantity().asFloat());
413                     }
414
415                     if (acc.isVisible(accountList)) accountList.add(acc);
416
417                     if (prevAccount != null) {
418                         prevAccount.setHasSubAccounts(
419                                 acc.getName().startsWith(prevAccount.getName() + ":"));
420                     }
421
422                     prevAccount = acc;
423                 }
424                 throwIfCancelled();
425
426                 profile.deleteNotPresentAccounts(db);
427                 throwIfCancelled();
428                 db.setTransactionSuccessful();
429                 Data.accounts.set(accountList);
430             }
431             finally {
432                 db.endTransaction();
433             }
434         }
435
436         return true;
437     }
438     private boolean retrieveTransactionList(MobileLedgerProfile profile)
439             throws IOException, ParseException, HTTPException {
440         Progress progress = new Progress();
441         int maxTransactionId = Progress.INDETERMINATE;
442
443         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
444         http.setAllowUserInteraction(false);
445         publishProgress(progress);
446         switch (http.getResponseCode()) {
447             case 200:
448                 break;
449             case 404:
450                 return false;
451             default:
452                 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
453         }
454         try (SQLiteDatabase db = MLDB.getDatabase()) {
455             try (InputStream resp = http.getInputStream()) {
456                 if (http.getResponseCode() != 200)
457                     throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
458                 throwIfCancelled();
459                 db.beginTransaction();
460                 try {
461                     profile.markTransactionsAsNotPresent(db);
462
463                     int matchedTransactionsCount = 0;
464                     TransactionListParser parser = new TransactionListParser(resp);
465
466                     int processedTransactionCount = 0;
467
468                     while (true) {
469                         throwIfCancelled();
470                         ParsedLedgerTransaction parsedTransaction = parser.nextTransaction();
471                         throwIfCancelled();
472                         if (parsedTransaction == null) break;
473                         LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
474                         if (transaction.existsInDb(db)) {
475                             profile.markTransactionAsPresent(db, transaction);
476                             matchedTransactionsCount++;
477
478                             if (matchedTransactionsCount == MATCHING_TRANSACTIONS_LIMIT) {
479                                 profile.markTransactionsBeforeTransactionAsPresent(db, transaction);
480                                 progress.setTotal(progress.getProgress());
481                                 publishProgress(progress);
482                                 db.setTransactionSuccessful();
483                                 profile.setLastUpdateStamp();
484                                 return true;
485                             }
486                         }
487                         else {
488                             profile.storeTransaction(db, transaction);
489                             matchedTransactionsCount = 0;
490                             progress.setTotal(maxTransactionId);
491                         }
492
493                         if ((progress.getTotal() == Progress.INDETERMINATE) ||
494                             (progress.getTotal() < transaction.getId()))
495                             progress.setTotal(transaction.getId());
496
497                         progress.setProgress(++processedTransactionCount);
498                         publishProgress(progress);
499                     }
500
501                     throwIfCancelled();
502                     profile.deleteNotPresentTransactions(db);
503                     throwIfCancelled();
504                     db.setTransactionSuccessful();
505                     profile.setLastUpdateStamp();
506                 }
507                 finally {
508                     db.endTransaction();
509                 }
510             }
511         }
512
513         return true;
514     }
515     @SuppressLint("DefaultLocale")
516     @Override
517     protected String doInBackground(Void... params) {
518         MobileLedgerProfile profile = Data.profile.get();
519         Data.backgroundTaskCount.incrementAndGet();
520         try {
521             if (!retrieveAccountList(profile) || !retrieveTransactionList(profile))
522                 return retrieveTransactionListLegacy(profile);
523             return null;
524         }
525         catch (MalformedURLException e) {
526             e.printStackTrace();
527             return "Invalid server URL";
528         }
529         catch (HTTPException e) {
530             e.printStackTrace();
531             return String.format("HTTP error %d: %s", e.getResponseCode(), e.getResponseMessage());
532         }
533         catch (IOException e) {
534             e.printStackTrace();
535             return e.getLocalizedMessage();
536         }
537         catch (ParseException e) {
538             e.printStackTrace();
539             return "Network error";
540         }
541         catch (OperationCanceledException e) {
542             e.printStackTrace();
543             return "Operation cancelled";
544         }
545         finally {
546             Data.backgroundTaskCount.decrementAndGet();
547         }
548     }
549     private MainActivity getContext() {
550         return contextRef.get();
551     }
552     private void throwIfCancelled() {
553         if (isCancelled()) throw new OperationCanceledException(null);
554     }
555
556     private enum ParserState {
557         EXPECTING_ACCOUNT, EXPECTING_ACCOUNT_AMOUNT, EXPECTING_JOURNAL, EXPECTING_TRANSACTION,
558         EXPECTING_TRANSACTION_DESCRIPTION, EXPECTING_TRANSACTION_DETAILS
559     }
560
561     public class Progress {
562         public static final int INDETERMINATE = -1;
563         private int progress;
564         private int total;
565         Progress() {
566             this(INDETERMINATE, INDETERMINATE);
567         }
568         Progress(int progress, int total) {
569             this.progress = progress;
570             this.total = total;
571         }
572         public int getProgress() {
573             return progress;
574         }
575         protected void setProgress(int progress) {
576             this.progress = progress;
577         }
578         public int getTotal() {
579             return total;
580         }
581         protected void setTotal(int total) {
582             this.total = total;
583         }
584     }
585
586     private class TransactionParserException extends IllegalStateException {
587         TransactionParserException(String message) {
588             super(message);
589         }
590     }
591 }