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