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