remove context parameter from MLDB.getWritableDB
[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.util.Log;
25
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;
32
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;
44
45
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;
56     protected int error;
57     private boolean success;
58     public RetrieveTransactionsTask(WeakReference<TransactionListFragment> contextRef) {
59         this.contextRef = contextRef;
60     }
61     private static final void L(String msg) {
62 //        Log.d("transaction-parser", msg);
63     }
64     @Override
65     protected void onProgressUpdate(Progress... values) {
66         super.onProgressUpdate(values);
67         TransactionListFragment context = getContext();
68         if (context == null) return;
69         context.onRetrieveProgress(values[0]);
70     }
71     @Override
72     protected void onPreExecute() {
73         super.onPreExecute();
74         TransactionListFragment context = getContext();
75         if (context == null) return;
76         context.onRetrieveStart();
77     }
78     @Override
79     protected void onPostExecute(Void aVoid) {
80         super.onPostExecute(aVoid);
81         TransactionListFragment context = getContext();
82         if (context == null) return;
83         context.onRetrieveDone(success);
84     }
85     @Override
86     protected void onCancelled() {
87         super.onCancelled();
88         TransactionListFragment context = getContext();
89         if (context == null) return;
90         context.onRetrieveDone(false);
91     }
92     @SuppressLint("DefaultLocale")
93     @Override
94     protected Void doInBackground(Params... params) {
95         Progress progress = new Progress();
96         int maxTransactionId = Progress.INDETERMINATE;
97         success = false;
98         try {
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()) {
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();
110                     try {
111                         db.execSQL("UPDATE transactions set keep=0");
112
113                         int state = ParserState.EXPECTING_JOURNAL;
114                         String line;
115                         BufferedReader buf =
116                                 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
117
118                         int processedTransactionCount = 0;
119                         int transactionId = 0;
120                         int matchedTransactionsCount = 0;
121                         LedgerTransaction transaction = null;
122                         LINES:
123                         while ((line = buf.readLine()) != null) {
124                             if (isCancelled()) break;
125                             Matcher m;
126                             //L(String.format("State is %d", state));
127                             switch (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");
133                                     }
134                                     break;
135                                 case ParserState.EXPECTING_TRANSACTION:
136                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
137                                     m = transactionStartPattern.matcher(line);
138                                     if (m.find()) {
139                                         transactionId = Integer.valueOf(m.group(1));
140                                         state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
141                                         L(String.format(
142                                                 "found transaction %d → expecting " + "description",
143                                                 transactionId));
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);
151                                     }
152                                     m = endPattern.matcher(line);
153                                     if (m.find()) {
154                                         L("--- transaction list complete ---");
155                                         success = true;
156                                         break LINES;
157                                     }
158                                     break;
159                                 case ParserState.EXPECTING_TRANSACTION_DESCRIPTION:
160                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
161                                     m = transactionDescriptionPattern.matcher(line);
162                                     if (m.find()) {
163                                         if (transactionId == 0)
164                                             throw new TransactionParserException(
165                                                     "Transaction Id is 0 while expecting " +
166                                                     "description");
167
168                                         transaction =
169                                                 new LedgerTransaction(transactionId, m.group(1),
170                                                         m.group(2));
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)));
175                                     }
176                                     break;
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++;
184
185                                             if (matchedTransactionsCount == 100) {
186                                                 db.execSQL("UPDATE transactions SET keep=1 WHERE " +
187                                                            "id < ?",
188                                                         new Integer[]{transaction.getId()});
189                                                 success = true;
190                                                 progress.setTotal(progress.getProgress());
191                                                 publishProgress(progress);
192                                                 break LINES;
193                                             }
194                                         }
195                                         else {
196                                             db.execSQL("DELETE from transactions WHERE id=?",
197                                                     new Integer[]{transaction.getId()});
198                                             db.execSQL("DELETE from transaction_accounts WHERE " +
199                                                        "transaction_id=?",
200                                                     new Integer[]{transaction.getId()});
201                                             transaction.insertInto(db);
202                                             matchedTransactionsCount = 0;
203                                             progress.setTotal(maxTransactionId);
204                                         }
205
206                                         state = ParserState.EXPECTING_TRANSACTION;
207                                         L(String.format(
208                                                 "transaction %s saved → expecting " + "transaction",
209                                                 transaction.getId()));
210
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 " +
215 //                                                  "parser");
216 //                                                break LINES;
217 //                                            }
218                                     }
219                                     else {
220                                         m = transactionDetailsPattern.matcher(line);
221                                         if (m.find()) {
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));
230                                         }
231                                         else throw new IllegalStateException(
232                                                 String.format("Can't parse transaction %d details",
233                                                         transactionId));
234                                     }
235                                     break;
236                                 default:
237                                     throw new RuntimeException(
238                                             String.format("Unknown parser state %d", state));
239                             }
240                         }
241                         if (!isCancelled()) {
242                             db.execSQL("DELETE FROM transactions WHERE keep = 0");
243                             db.setTransactionSuccessful();
244                         }
245                     }
246                     finally {
247                         db.endTransaction();
248                     }
249                 }
250             }
251
252             if (success && !isCancelled()) {
253                 Log.d("db", "Updating transaction list stamp");
254                 MLDB.set_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP,
255                         new Date().getTime());
256                 ctx.model.reloadTransactions(ctx);
257             }
258         }
259         catch (MalformedURLException e) {
260             error = R.string.err_bad_backend_url;
261             e.printStackTrace();
262         }
263         catch (FileNotFoundException e) {
264             error = R.string.err_bad_auth;
265             e.printStackTrace();
266         }
267         catch (IOException e) {
268             error = R.string.err_net_io_error;
269             e.printStackTrace();
270         }
271         return null;
272     }
273     TransactionListFragment getContext() {
274         return contextRef.get();
275     }
276
277     public static class Params {
278         private SharedPreferences backendPref;
279
280         public Params(SharedPreferences backendPref) {
281             this.backendPref = backendPref;
282         }
283         SharedPreferences getBackendPref() {
284             return backendPref;
285         }
286     }
287
288     public class Progress {
289         public static final int INDETERMINATE = -1;
290         private int progress;
291         private int total;
292         Progress() {
293             this(INDETERMINATE, INDETERMINATE);
294         }
295         Progress(int progress, int total) {
296             this.progress = progress;
297             this.total = total;
298         }
299         public int getProgress() {
300             return progress;
301         }
302         protected void setProgress(int progress) {
303             this.progress = progress;
304         }
305         public int getTotal() {
306             return total;
307         }
308         protected void setTotal(int total) {
309             this.total = total;
310         }
311     }
312
313     private class TransactionParserException extends IllegalStateException {
314         TransactionParserException(String message) {
315             super(message);
316         }
317     }
318
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;
324     }
325 }