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.
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.
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/>.
18 package net.ktnx.mobileledger.async;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.os.AsyncTask;
25 import net.ktnx.mobileledger.R;
26 import net.ktnx.mobileledger.model.LedgerTransaction;
27 import net.ktnx.mobileledger.model.LedgerTransactionItem;
28 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
29 import net.ktnx.mobileledger.utils.NetworkUtil;
31 import java.io.BufferedReader;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.lang.ref.WeakReference;
37 import java.net.HttpURLConnection;
38 import java.net.MalformedURLException;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
43 class RetrieveTransactionsTask extends AsyncTask<RetrieveTransactionsTask.Params, Integer, Void> {
45 static final int DEFAULT_LIMIT = 100;
46 private SharedPreferences backendPref;
47 private String accountsRoot;
50 Params(SharedPreferences backendPref) {
51 this.backendPref = backendPref;
52 this.accountsRoot = null;
53 this.limit = DEFAULT_LIMIT;
55 Params(SharedPreferences backendPref, String accountsRoot) {
56 this(backendPref, accountsRoot, DEFAULT_LIMIT);
58 Params(SharedPreferences backendPref, String accountsRoot, int limit) {
59 this.backendPref = backendPref;
60 this.accountsRoot = accountsRoot;
63 String getAccountsRoot() {
66 SharedPreferences getBackendPref() {
73 private static final Pattern transactionStartPattern = Pattern.compile("<tr class=\"title\" "
74 + "id=\"transaction-(\\d+)\"><td class=\"date\"[^\\\"]*>([\\d.-]+)</td>");
75 private static final Pattern transactionDescriptionPattern =
76 Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
77 private static final Pattern transactionDetailsPattern =
78 Pattern.compile("^\\s+" + "(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)");
79 protected WeakReference<Context> contextRef;
82 protected Void doInBackground(Params... params) {
84 HttpURLConnection http =
85 NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
86 http.setAllowUserInteraction(false);
88 Context ctx = contextRef.get();
89 if (ctx == null) return null;
90 try (MobileLedgerDatabase dbh = new MobileLedgerDatabase(ctx)) {
91 try (SQLiteDatabase db = dbh.getWritableDatabase()) {
92 try (InputStream resp = http.getInputStream()) {
93 if (http.getResponseCode() != 200) throw new IOException(
94 String.format("HTTP error %d", http.getResponseCode()));
95 db.beginTransaction();
97 String root = params[0].getAccountsRoot();
98 if (root == null) db.execSQL("DELETE FROM transaction_history;");
100 StringBuilder sql = new StringBuilder();
101 sql.append("DELETE FROM transaction_history ");
103 "where id in (select transactions.id from transactions ");
104 sql.append("join transaction_accounts ");
106 "on transactions.id=transaction_accounts.transaction_id ");
107 sql.append("where transaction_accounts.account_name like ?||'%'");
108 db.execSQL(sql.toString(), new String[]{root});
111 int state = ParserState.EXPECTING_JOURNAL;
114 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
116 int transactionCount = 0;
117 String transactionId = null;
118 LedgerTransaction transaction = null;
119 while ((line = buf.readLine()) != null) {
121 case ParserState.EXPECTING_JOURNAL: {
122 if (line.equals("<h2>General Journal</h2>"))
123 state = ParserState.EXPECTING_TRANSACTION;
126 case ParserState.EXPECTING_TRANSACTION: {
127 Matcher m = transactionStartPattern.matcher(line);
129 transactionId = m.group(1);
130 state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
133 case ParserState.EXPECTING_TRANSACTION_DESCRIPTION: {
134 Matcher m = transactionDescriptionPattern.matcher(line);
136 if (transactionId == null)
137 throw new TransactionParserException(
138 "Transaction Id is null while expecting description");
141 new LedgerTransaction(transactionId, m.group(1),
143 state = ParserState.EXPECTING_TRANSACTION_DETAILS;
146 case ParserState.EXPECTING_TRANSACTION_DETAILS: {
147 if (transaction == null)
148 throw new TransactionParserException(
149 "Transaction is null while expecting details");
150 if (line.isEmpty()) {
151 // transaction data collected
152 transaction.insertInto(db);
154 state = ParserState.EXPECTING_TRANSACTION;
155 publishProgress(++transactionCount);
158 Matcher m = transactionDetailsPattern.matcher(line);
160 String acc_name = m.group(1);
161 String amount = m.group(2);
162 amount = amount.replace(',', '.');
163 transaction.add_item(
164 new LedgerTransactionItem(acc_name,
165 Float.valueOf(amount)));
167 else throw new IllegalStateException(String.format(
168 "Can't" + " parse transaction details"));
172 throw new RuntimeException(
173 String.format("Unknown " + "parser state %d",
177 db.setTransactionSuccessful();
186 catch (MalformedURLException e) {
187 error = R.string.err_bad_backend_url;
190 catch (FileNotFoundException e) {
191 error = R.string.err_bad_auth;
194 catch (IOException e) {
195 error = R.string.err_net_io_error;
200 WeakReference<Context> getContextRef() {
204 private class TransactionParserException extends IllegalStateException {
205 TransactionParserException(String message) {
210 private class ParserState {
211 static final int EXPECTING_JOURNAL = 0;
212 static final int EXPECTING_TRANSACTION = 1;
213 static final int EXPECTING_TRANSACTION_DESCRIPTION = 2;
214 static final int EXPECTING_TRANSACTION_DETAILS = 3;