]> git.ktnx.net Git - mobile-ledger.git/commitdiff
store transaction description history upon database refresh
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 8 Dec 2018 15:07:07 +0000 (15:07 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 8 Dec 2018 15:07:07 +0000 (15:07 +0000)
app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java
app/src/main/res/raw/sql_2.sql [new file with mode: 0644]

index 60583f9b13ec7afadd3e96689ad216ce18955594..33f1e9c901cd926d88063c5adb58c204988cced6 100644 (file)
@@ -53,13 +53,14 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, In
                         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\\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");
@@ -76,26 +77,45 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, In
 
                             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 or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
-                                        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, keep) values(?, 1);", new Object[]{description});
+                                }
                             }
                         }
 
                         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 {
diff --git a/app/src/main/res/raw/sql_2.sql b/app/src/main/res/raw/sql_2.sql
new file mode 100644 (file)
index 0000000..bd27382
--- /dev/null
@@ -0,0 +1 @@
+create table description_history(description varchar not null primary key, keep boolean);
\ No newline at end of file