]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
transactions may come in random order from the backend
[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 import android.util.Log;
25
26 import net.ktnx.mobileledger.R;
27 import net.ktnx.mobileledger.TransactionListActivity;
28 import net.ktnx.mobileledger.model.LedgerTransaction;
29 import net.ktnx.mobileledger.model.LedgerTransactionItem;
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.regex.Matcher;
42 import java.util.regex.Pattern;
43
44
45 public class RetrieveTransactionsTask extends
46         AsyncTask<RetrieveTransactionsTask.Params, RetrieveTransactionsTask.Progress, Void> {
47     private static final Pattern transactionStartPattern = Pattern.compile("<tr class=\"title\" " +
48                                                                            "id=\"transaction-(\\d+)\"><td class=\"date\"[^\\\"]*>([\\d.-]+)</td>");
49     private static final Pattern transactionDescriptionPattern =
50             Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
51     private static final Pattern transactionDetailsPattern =
52             Pattern.compile("^\\s+" + "(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)");
53     private static final Pattern endPattern = Pattern.compile("\\bid=\"addmodal\"");
54     protected WeakReference<TransactionListActivity> contextRef;
55     protected int error;
56     private boolean success;
57     public RetrieveTransactionsTask(WeakReference<TransactionListActivity> contextRef) {
58         this.contextRef = contextRef;
59     }
60     private static final void L(String msg) {
61         Log.d("transaction-parser", msg);
62     }
63     @Override
64     protected void onProgressUpdate(Progress... values) {
65         super.onProgressUpdate(values);
66         TransactionListActivity context = getContext();
67         if (context == null) return;
68         context.onRetrieveProgress(values[0]);
69     }
70     @Override
71     protected void onPreExecute() {
72         super.onPreExecute();
73         TransactionListActivity context = getContext();
74         if (context == null) return;
75         context.onRetrieveStart();
76     }
77     @Override
78     protected void onPostExecute(Void aVoid) {
79         super.onPostExecute(aVoid);
80         TransactionListActivity context = getContext();
81         if (context == null) return;
82         context.onRetrieveDone(success);
83     }
84     @SuppressLint("DefaultLocale")
85     @Override
86     protected Void doInBackground(Params... params) {
87         Progress progress = new Progress();
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("DELETE FROM transactions;");
103                         db.execSQL("DELETE FROM transaction_accounts");
104
105                         int state = ParserState.EXPECTING_JOURNAL;
106                         String line;
107                         BufferedReader buf =
108                                 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
109
110                         int transactionCount = 0;
111                         int transactionId = 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(++transactionCount);
136                                         if ((progress.getTotal() == Progress.INDETERMINATE) ||
137                                             (progress.getTotal() < transactionId))
138                                             progress.setTotal(transactionId);
139                                         publishProgress(progress);
140                                     }
141                                     m = endPattern.matcher(line);
142                                     if (m.find()) {
143                                         L("--- transaction list complete ---");
144                                         success = true;
145                                         break LINES;
146                                     }
147                                     break;
148                                 case ParserState.EXPECTING_TRANSACTION_DESCRIPTION:
149                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
150                                     m = transactionDescriptionPattern.matcher(line);
151                                     if (m.find()) {
152                                         if (transactionId == 0)
153                                             throw new TransactionParserException(
154                                                     "Transaction Id is 0 while expecting " +
155                                                     "description");
156
157                                         transaction =
158                                                 new LedgerTransaction(transactionId, m.group(1),
159                                                         m.group(2));
160                                         state = ParserState.EXPECTING_TRANSACTION_DETAILS;
161                                         L(String.format("transaction %d created for %s (%s) →" +
162                                                         " expecting details", transactionId,
163                                                 m.group(1), m.group(2)));
164                                     }
165                                     break;
166                                 case ParserState.EXPECTING_TRANSACTION_DETAILS:
167                                     if (line.isEmpty()) {
168                                         // transaction data collected
169                                         transaction.insertInto(db);
170
171                                         state = ParserState.EXPECTING_TRANSACTION;
172                                         L(String.format(
173                                                 "transaction %s saved → expecting " + "transaction",
174                                                 transaction.getId()));
175
176 // sounds like a good idea, but transaction-1 may not be the first one chronologically
177 // for example, when you add the initial seeding transaction after entering some others
178 //                                            if (transactionId == 1) {
179 //                                                L("This was the initial transaction. Terminating " +
180 //                                                  "parser");
181 //                                                break LINES;
182 //                                            }
183                                     }
184                                     else {
185                                         m = transactionDetailsPattern.matcher(line);
186                                         if (m.find()) {
187                                             String acc_name = m.group(1);
188                                             String amount = m.group(2);
189                                             amount = amount.replace(',', '.');
190                                             transaction.add_item(new LedgerTransactionItem(acc_name,
191                                                     Float.valueOf(amount)));
192                                             L(String.format("%s = %s", acc_name, amount));
193                                         }
194                                         else throw new IllegalStateException(
195                                                 String.format("Can't parse transaction details"));
196                                     }
197                                     break;
198                                 default:
199                                     throw new RuntimeException(
200                                             String.format("Unknown " + "parser state %d", state));
201                             }
202                         }
203                         if (!isCancelled()) db.setTransactionSuccessful();
204                     }
205                     finally {
206                         db.endTransaction();
207                     }
208                 }
209             }
210
211             if (success && !isCancelled()) ctx.model.reloadTransactions(ctx);
212         }
213         catch (MalformedURLException e) {
214             error = R.string.err_bad_backend_url;
215             e.printStackTrace();
216         }
217         catch (FileNotFoundException e) {
218             error = R.string.err_bad_auth;
219             e.printStackTrace();
220         }
221         catch (IOException e) {
222             error = R.string.err_net_io_error;
223             e.printStackTrace();
224         }
225         return null;
226     }
227     TransactionListActivity getContext() {
228         return contextRef.get();
229     }
230
231     public static class Params {
232         static final int DEFAULT_LIMIT = 100;
233         private SharedPreferences backendPref;
234         private String accountsRoot;
235         private int limit;
236
237         public Params(SharedPreferences backendPref) {
238             this.backendPref = backendPref;
239             this.accountsRoot = null;
240             this.limit = DEFAULT_LIMIT;
241         }
242         Params(SharedPreferences backendPref, String accountsRoot) {
243             this(backendPref, accountsRoot, DEFAULT_LIMIT);
244         }
245         Params(SharedPreferences backendPref, String accountsRoot, int limit) {
246             this.backendPref = backendPref;
247             this.accountsRoot = accountsRoot;
248             this.limit = limit;
249         }
250         String getAccountsRoot() {
251             return accountsRoot;
252         }
253         SharedPreferences getBackendPref() {
254             return backendPref;
255         }
256         int getLimit() {
257             return limit;
258         }
259     }
260
261     public class Progress {
262         public static final int INDETERMINATE = -1;
263         private int progress;
264         private int total;
265         Progress() {
266             this(INDETERMINATE, INDETERMINATE);
267         }
268         Progress(int progress, int total) {
269             this.progress = progress;
270             this.total = total;
271         }
272         public int getProgress() {
273             return progress;
274         }
275         protected void setProgress(int progress) {
276             this.progress = progress;
277         }
278         public int getTotal() {
279             return total;
280         }
281         protected void setTotal(int total) {
282             this.total = total;
283         }
284     }
285
286     private class TransactionParserException extends IllegalStateException {
287         TransactionParserException(String message) {
288             super(message);
289         }
290     }
291
292     private class ParserState {
293         static final int EXPECTING_JOURNAL = 0;
294         static final int EXPECTING_TRANSACTION = 1;
295         static final int EXPECTING_TRANSACTION_DESCRIPTION = 2;
296         static final int EXPECTING_TRANSACTION_DETAILS = 3;
297     }
298 }