- LedgerAccount lastAccount = null;
- Data.backgroundTaskCount.incrementAndGet();
- try {
- HttpURLConnection http =
- NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
- http.setAllowUserInteraction(false);
- publishProgress(progress);
- MainActivity ctx = getContext();
- if (ctx == null) return null;
- try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
- try (InputStream resp = http.getInputStream()) {
- if (http.getResponseCode() != 200) throw new IOException(
- String.format("HTTP error %d", http.getResponseCode()));
- db.beginTransaction();
- try {
- db.execSQL("UPDATE transactions set keep=0");
- db.execSQL("update account_values set keep=0;");
- db.execSQL("update accounts set keep=0;");
-
- ParserState state = ParserState.EXPECTING_ACCOUNT;
- String line;
- BufferedReader buf =
- new BufferedReader(new InputStreamReader(resp, "UTF-8"));
-
- int processedTransactionCount = 0;
- int transactionId = 0;
- int matchedTransactionsCount = 0;
- LedgerTransaction transaction = null;
- LINES:
- while ((line = buf.readLine()) != null) {
- throwIfCancelled();
- Matcher m;
- //L(String.format("State is %d", updating));
- switch (state) {
- case EXPECTING_ACCOUNT:
- if (line.equals("<h2>General Journal</h2>")) {
- state = ParserState.EXPECTING_TRANSACTION;
- L("→ expecting transaction");
- Data.accounts.set(accountList);
- continue;
+ HashMap<String, Void> accountNames = new HashMap<>();
+ HashMap<String, LedgerAccount> syntheticAccounts = new HashMap<>();
+ LedgerAccount lastAccount = null, prevAccount = null;
+ boolean onlyStarred = Data.optShowOnlyStarred.get();
+
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
+ http.setAllowUserInteraction(false);
+ publishProgress(progress);
+ switch (http.getResponseCode()) {
+ case 200:
+ break;
+ default:
+ throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
+ }
+
+ SQLiteDatabase db = App.getDatabase();
+ try (InputStream resp = http.getInputStream()) {
+ if (http.getResponseCode() != 200)
+ throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
+ db.beginTransaction();
+ try {
+ prepareDbForRetrieval(db, profile);
+
+ int matchedTransactionsCount = 0;
+
+
+ ParserState state = ParserState.EXPECTING_ACCOUNT;
+ String line;
+ BufferedReader buf =
+ new BufferedReader(new InputStreamReader(resp, StandardCharsets.UTF_8));
+
+ int processedTransactionCount = 0;
+ int transactionId = 0;
+ LedgerTransaction transaction = null;
+ LINES:
+ while ((line = buf.readLine()) != null) {
+ throwIfCancelled();
+ Matcher m;
+ m = reComment.matcher(line);
+ if (m.find()) {
+ // TODO: comments are ignored for now
+// Log.v("transaction-parser", "Ignoring comment");
+ continue;
+ }
+ //L(String.format("State is %d", updating));
+ switch (state) {
+ case EXPECTING_ACCOUNT:
+ if (line.equals("<h2>General Journal</h2>")) {
+ state = ParserState.EXPECTING_TRANSACTION;
+ L("→ expecting transaction");
+ // commit the current transaction and start a new one
+ // the account list in the UI should reflect the (committed)
+ // state of the database
+ db.setTransactionSuccessful();
+ db.endTransaction();
+ Data.accounts.setList(accountList);
+ db.beginTransaction();
+ continue;
+ }
+ m = reAccountName.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("\"", "");
+ L(String.format("found account: %s", acct_name));
+
+ prevAccount = lastAccount;
+ lastAccount = profile.tryLoadAccount(db, acct_name);
+ if (lastAccount == null) lastAccount = new LedgerAccount(acct_name);
+ else lastAccount.removeAmounts();
+ profile.storeAccount(db, lastAccount);
+
+ if (prevAccount != null) prevAccount
+ .setHasSubAccounts(prevAccount.isParentOf(lastAccount));
+ // make sure the parent account(s) are present,
+ // synthesising them if necessary
+ // this happens when the (missing-in-HTML) parent account has
+ // only one child so we create a synthetic parent account record,
+ // copying the amounts when child's amounts are parsed
+ String parentName = lastAccount.getParentName();
+ if (parentName != null) {
+ Stack<String> toAppend = new Stack<>();
+ while (parentName != null) {
+ if (accountNames.containsKey(parentName)) break;
+ toAppend.push(parentName);
+ parentName = new LedgerAccount(parentName).getParentName();