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