remove context parameter from MLDB.getWritableDB
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveAccountsTask.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.content.SharedPreferences;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.util.Log;
23
24 import net.ktnx.mobileledger.R;
25 import net.ktnx.mobileledger.model.LedgerAccount;
26 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
27 import net.ktnx.mobileledger.utils.MLDB;
28 import net.ktnx.mobileledger.utils.NetworkUtil;
29
30 import java.io.BufferedReader;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.lang.ref.WeakReference;
36 import java.net.HttpURLConnection;
37 import java.net.MalformedURLException;
38 import java.net.URLDecoder;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41
42 public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
43     int error;
44     WeakReference<AccountSummaryFragment> mContext;
45     private SharedPreferences pref;
46
47     public RetrieveAccountsTask(WeakReference<AccountSummaryFragment> context) {
48         mContext = context;
49         error = 0;
50     }
51
52     public void setPref(SharedPreferences pref) {
53         this.pref = pref;
54     }
55
56     protected Void doInBackground(Void... params) {
57         try {
58             HttpURLConnection http = NetworkUtil.prepare_connection(pref, "add");
59             publishProgress(0);
60             try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
61                 try (InputStream resp = http.getInputStream()) {
62                     Log.d("update_accounts", String.valueOf(http.getResponseCode()));
63                     if (http.getResponseCode() != 200) {
64                         throw new IOException(
65                                 String.format("HTTP error: %d %s", http.getResponseCode(),
66                                         http.getResponseMessage()));
67                     }
68                     else {
69                         if (db.inTransaction()) throw new AssertionError();
70
71                         db.beginTransaction();
72
73                         try {
74                             db.execSQL("update account_values set keep=0;");
75                             db.execSQL("update accounts set keep=0;");
76
77                             String line;
78                             String last_account_name = null;
79                             BufferedReader buf =
80                                     new BufferedReader(new InputStreamReader(resp, "UTF-8"));
81                             // %3A is '='
82                             Pattern account_name_re =
83                                     Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
84                             Pattern value_re = Pattern.compile(
85                                     "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
86                             Pattern tr_re = Pattern.compile("</tr>");
87                             Pattern descriptions_line_re =
88                                     Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b");
89                             Pattern description_items_re =
90                                     Pattern.compile("\"value\":\"([^\"]+)\"");
91                             int count = 0;
92                             while ((line = buf.readLine()) != null) {
93                                 Matcher m = account_name_re.matcher(line);
94                                 if (m.find()) {
95                                     String acct_encoded = m.group(1);
96                                     String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
97                                     acct_name = acct_name.replace("\"", "");
98                                     Log.d("account-parser", acct_name);
99
100                                     addAccount(db, acct_name);
101                                     publishProgress(++count);
102
103                                     last_account_name = acct_name;
104
105                                     continue;
106                                 }
107
108                                 Matcher tr_m = tr_re.matcher(line);
109                                 if (tr_m.find()) {
110                                     Log.d("account-parser", "<tr> - another account expected");
111                                     last_account_name = null;
112                                     continue;
113                                 }
114
115                                 if (last_account_name != null) {
116                                     m = value_re.matcher(line);
117                                     boolean match_found = false;
118                                     while (m.find()) {
119                                         match_found = true;
120                                         String value = m.group(1);
121                                         String currency = m.group(2);
122                                         if (currency == null) currency = "";
123                                         value = value.replace(',', '.');
124                                         Log.d("db", "curr=" + currency + ", value=" + value);
125                                         db.execSQL(
126                                                 "insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
127                                                 new Object[]{last_account_name, currency,
128                                                              Float.valueOf(value)
129                                                 });
130                                     }
131
132                                     if (match_found) continue;
133                                 }
134
135                                 m = descriptions_line_re.matcher(line);
136                                 if (m.find()) {
137                                     db.execSQL("update description_history set keep=0;");
138                                     m = description_items_re.matcher(line);
139                                     while (m.find()) {
140                                         String description = m.group(1);
141                                         if (description.isEmpty()) continue;
142
143                                         Log.d("db", String.format("Stored description: %s",
144                                                 description));
145                                         db.execSQL("insert or replace into description_history" +
146                                                    "(description, description_upper, keep) " +
147                                                    "values(?, ?, 1);",
148                                                 new Object[]{description, description.toUpperCase()
149                                                 });
150                                     }
151                                 }
152                             }
153
154                             db.execSQL("delete from account_values where keep=0;");
155                             db.execSQL("delete from accounts where keep=0;");
156 //                        db.execSQL("delete from description_history where keep=0;");
157                             db.setTransactionSuccessful();
158                         }
159                         finally {
160                             db.endTransaction();
161                         }
162
163                     }
164                 }
165             }
166         }
167         catch (MalformedURLException e) {
168             error = R.string.err_bad_backend_url;
169             e.printStackTrace();
170         }
171         catch (FileNotFoundException e) {
172             error = R.string.err_bad_auth;
173             e.printStackTrace();
174         }
175         catch (IOException e) {
176             error = R.string.err_net_io_error;
177             e.printStackTrace();
178         }
179         catch (Exception e) {
180             error = R.string.err_net_error;
181             e.printStackTrace();
182         }
183
184         return null;
185     }
186
187     private void addAccount(SQLiteDatabase db, String name) {
188         do {
189             LedgerAccount acc = new LedgerAccount(name);
190             db.execSQL("update accounts set level = ?, keep = 1 where name = ?",
191                     new Object[]{acc.getLevel(), name});
192             db.execSQL("insert into accounts(name, name_upper, parent_name, level) select ?,?," +
193                        "?,? " + "where (select changes() = 0)",
194                     new Object[]{name, name.toUpperCase(), acc.getParentName(), acc.getLevel()});
195             name = acc.getParentName();
196         } while (name != null);
197     }
198     @Override
199     protected void onPostExecute(Void result) {
200         AccountSummaryFragment ctx = mContext.get();
201         if (ctx == null) return;
202         ctx.onAccountRefreshDone(this.error);
203     }
204
205 }