]> git.ktnx.net Git - mobile-ledger.git/blobdiff - 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
index f1511f8051dc4fb4ee71eba7f98f49d65f0d295a..16919b336b62fac88f73489492924c0178c35dfd 100644 (file)
@@ -1,10 +1,10 @@
 package net.ktnx.mobileledger;
 
 import android.content.SharedPreferences;
-import android.database.sqlite.SQLiteDatabase;
 import android.util.Log;
 
 import java.io.BufferedReader;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -14,19 +14,22 @@ import java.net.URLDecoder;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-abstract public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDatabase, Integer, Void> {
+import static net.ktnx.mobileledger.MobileLedgerDB.db;
+
+abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
     int error;
 
     private SharedPreferences pref;
-    public void setPref(SharedPreferences pref) {
-        this.pref = pref;
-    }
-    public RetrieveAccountsTask() {
+
+    RetrieveAccountsTask() {
         error = 0;
     }
 
-    protected Void doInBackground(SQLiteDatabase... sqLiteDatabases) {
-        final SQLiteDatabase db = sqLiteDatabases[0];
+    void setPref(SharedPreferences pref) {
+        this.pref = pref;
+    }
+
+    protected Void doInBackground(Void... params) {
         try {
             HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
             http.setAllowUserInteraction(false);
@@ -36,7 +39,7 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDa
             try {
                 Log.d("update_accounts", String.valueOf(http.getResponseCode()));
                 if (http.getResponseCode() != 200) {
-                    throw new IOException(String.valueOf(R.string.err_http_error));
+                    throw new IOException(String.format("HTTP error: %d %s", http.getResponseCode(), http.getResponseMessage()));
                 }
                 else {
                     if (db.inTransaction()) throw new AssertionError();
@@ -51,20 +54,23 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDa
                         String last_account_name = null;
                         BufferedReader buf = new BufferedReader(new InputStreamReader(resp, "UTF-8"));
                         // %3A is '='
-                        Pattern re = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
+                        Pattern account_name_re = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
                         Pattern value_re = Pattern.compile("<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
                         Pattern tr_re = Pattern.compile("</tr>");
+                        Pattern descriptions_line_re = Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b");
+                        Pattern description_items_re = Pattern.compile("\"value\":\"([^\"]+)\"");
                         int count = 0;
                         while ((line = buf.readLine()) != null) {
-
-                            Matcher m = re.matcher(line);
+                            Matcher m = account_name_re.matcher(line);
                             if (m.find()) {
                                 String acct_encoded = m.group(1);
                                 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
                                 acct_name = acct_name.replace("\"", "");
                                 Log.d("account-parser", acct_name);
 
-                                db.execSQL("insert into accounts(name) values(?)", new Object[]{acct_name} );
+                                db.execSQL("insert or replace into accounts(name, name_upper, " +
+                                                "keep) values(?, ?, 1)",
+                                        new Object[]{acct_name, acct_name.toUpperCase()});
                                 publishProgress(++count);
 
                                 last_account_name = acct_name;
@@ -74,24 +80,48 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDa
 
                             Matcher tr_m = tr_re.matcher(line);
                             if (tr_m.find()) {
+                                Log.d("account-parser", "<tr> - another account expected");
                                 last_account_name = null;
                                 continue;
                             }
 
-                            if (last_account_name == null) continue;
-
-                            m = value_re.matcher(line);
-                            while (m.find()) {
-                                String value = m.group(1);
-                                String currency = m.group(2);
-                                if(currency == null) currency="";
-                                value = value.replace(',', '.');
-                                Log.d("db", "curr="+currency+", value="+value);
-                                db.execSQL("insert into account_values(account, currency, value) values(?, ?, ?);",
-                                        new Object[]{last_account_name, currency, Float.valueOf(value)});
+                            if (last_account_name != null) {
+                                m = value_re.matcher(line);
+                                boolean match_found = false;
+                                while (m.find()) {
+                                    match_found = true;
+                                    String value = m.group(1);
+                                    String currency = m.group(2);
+                                    if (currency == null) currency = "";
+                                    value = value.replace(',', '.');
+                                    Log.d("db", "curr=" + currency + ", value=" + value);
+                                    db.execSQL("insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
+                                            new Object[]{last_account_name, currency, Float.valueOf(value)});
+                                }
+
+                                if (match_found) continue;
+                            }
+
+                            m = descriptions_line_re.matcher(line);
+                            if (m.find()) {
+                                db.execSQL("update description_history set keep=0;");
+                                m = description_items_re.matcher(line);
+                                while(m.find()) {
+                                    String description = m.group(1);
+                                    if (description.isEmpty()) continue;
+
+                                    Log.d("db", String.format("Stored description: %s", description));
+                                    db.execSQL("insert or replace into description_history" +
+                                                    "(description, description_upper, keep) " +
+                                                    "values(?, ?, 1);",
+                                            new Object[]{description, description.toUpperCase()});
+                                }
                             }
                         }
 
+                        db.execSQL("delete from account_values where keep=0;");
+                        db.execSQL("delete from accounts where keep=0;");
+//                        db.execSQL("delete from description_history where keep=0;");
                         db.setTransactionSuccessful();
                     }
                     finally {
@@ -106,7 +136,12 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDa
         } catch (MalformedURLException e) {
             error = R.string.err_bad_backend_url;
             e.printStackTrace();
-        } catch (IOException e) {
+        }
+        catch (FileNotFoundException e) {
+            error = R.string.err_bad_auth;
+            e.printStackTrace();
+        }
+        catch (IOException e) {
             error = R.string.err_net_io_error;
             e.printStackTrace();
         }