+
+ if (match_found) {
+ syntheticAccounts.clear();
+ state = ParserState.EXPECTING_ACCOUNT;
+ L("→ expecting account");
+ }
+
+ break;
+
+ case EXPECTING_TRANSACTION:
+ if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
+ m = reTransactionStart.matcher(line);
+ if (m.find()) {
+ transactionId = Integer.valueOf(m.group(1));
+ state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
+ L(String.format(Locale.ENGLISH,
+ "found transaction %d → expecting description",
+ transactionId));
+ progress.setProgress(++processedTransactionCount);
+ if (maxTransactionId < transactionId)
+ maxTransactionId = transactionId;
+ if ((progress.getTotal() == Progress.INDETERMINATE) ||
+ (progress.getTotal() < transactionId))
+ progress.setTotal(transactionId);
+ publishProgress(progress);
+ }
+ m = reEnd.matcher(line);
+ if (m.find()) {
+ L("--- transaction value complete ---");
+ break LINES;
+ }
+ break;
+
+ case EXPECTING_TRANSACTION_DESCRIPTION:
+ if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
+ m = reTransactionDescription.matcher(line);
+ if (m.find()) {
+ if (transactionId == 0) throw new TransactionParserException(
+ "Transaction Id is 0 while expecting " + "description");
+
+ String date = m.group(1);
+ try {
+ int equalsIndex = date.indexOf('=');
+ if (equalsIndex >= 0) date = date.substring(equalsIndex + 1);
+ transaction =
+ new LedgerTransaction(transactionId, date, m.group(2));
+ }
+ catch (ParseException e) {
+ e.printStackTrace();
+ return String.format("Error parsing date '%s'", date);
+ }
+ state = ParserState.EXPECTING_TRANSACTION_DETAILS;
+ L(String.format(Locale.ENGLISH,
+ "transaction %d created for %s (%s) →" +
+ " expecting details", transactionId, date, m.group(2)));
+ }
+ break;
+
+ case EXPECTING_TRANSACTION_DETAILS:
+ if (line.isEmpty()) {
+ // transaction data collected
+ if (transaction.existsInDb(db)) {
+ profile.markTransactionAsPresent(db, transaction);
+ matchedTransactionsCount++;
+
+ if (matchedTransactionsCount == MATCHING_TRANSACTIONS_LIMIT) {
+ profile.markTransactionsBeforeTransactionAsPresent(db,
+ transaction);
+ progress.setTotal(progress.getProgress());
+ publishProgress(progress);
+ break LINES;
+ }
+ }
+ else {
+ profile.storeTransaction(db, transaction);
+ matchedTransactionsCount = 0;
+ progress.setTotal(maxTransactionId);
+ }
+
+ state = ParserState.EXPECTING_TRANSACTION;
+ L(String.format("transaction %s saved → expecting transaction",
+ transaction.getId()));
+ transaction.finishLoading();
+
+// sounds like a good idea, but transaction-1 may not be the first one chronologically
+// for example, when you add the initial seeding transaction after entering some others
+// if (transactionId == 1) {
+// L("This was the initial transaction. Terminating " +
+// "parser");
+// break LINES;
+// }
+ }
+ else {
+ m = reTransactionDetails.matcher(line);
+ if (m.find()) {
+ String acc_name = m.group(1);
+ String amount = m.group(2);
+ String currency = m.group(3);
+ if (currency == null) currency = "";
+ amount = amount.replace(',', '.');
+ transaction.addAccount(new LedgerTransactionAccount(acc_name,
+ Float.valueOf(amount), currency));
+ L(String.format(Locale.ENGLISH, "%d: %s = %s",
+ transaction.getId(), acc_name, amount));
+ }
+ else throw new IllegalStateException(
+ String.format("Can't parse transaction %d " + "details: %s",
+ transactionId, line));
+ }
+ break;
+ default:
+ throw new RuntimeException(
+ String.format("Unknown parser updating %s", state.name()));
+ }
+ }
+
+ throwIfCancelled();
+
+ profile.deleteNotPresentTransactions(db);
+ db.setTransactionSuccessful();
+
+ profile.setLastUpdateStamp();
+
+ return null;
+ }
+ finally {
+ db.endTransaction();
+ }
+ }
+ }
+ private void prepareDbForRetrieval(SQLiteDatabase db, MobileLedgerProfile profile) {
+ db.execSQL("UPDATE transactions set keep=0 where profile=?",
+ new String[]{profile.getUuid()});
+ db.execSQL("update account_values set keep=0 where profile=?;",
+ new String[]{profile.getUuid()});
+ db.execSQL("update accounts set keep=0 where profile=?;", new String[]{profile.getUuid()});
+ }
+ private boolean retrieveAccountList() throws IOException, HTTPException {
+ Progress progress = new Progress();
+
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
+ http.setAllowUserInteraction(false);
+ switch (http.getResponseCode()) {
+ case 200:
+ break;
+ case 404:
+ return false;
+ default:
+ throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
+ }
+ publishProgress(progress);
+ SQLiteDatabase db = App.getDatabase();
+ ArrayList<LedgerAccount> accountList = new ArrayList<>();
+ boolean listFilledOK = false;
+ try (InputStream resp = http.getInputStream()) {
+ if (http.getResponseCode() != 200)
+ throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
+
+ db.beginTransaction();
+ try {
+ profile.markAccountsAsNotPresent(db);
+
+ AccountListParser parser = new AccountListParser(resp);
+
+ LedgerAccount prevAccount = null;
+
+ while (true) {
+ throwIfCancelled();
+ ParsedLedgerAccount parsedAccount = parser.nextAccount();
+ if (parsedAccount == null) break;
+
+ LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname());
+ if (acc == null) acc = new LedgerAccount(parsedAccount.getAname());
+ else acc.removeAmounts();
+
+ profile.storeAccount(db, acc);
+ String lastCurrency = null;
+ float lastCurrencyAmount = 0;
+ for (ParsedBalance b : parsedAccount.getAibalance()) {
+ final String currency = b.getAcommodity();
+ final float amount = b.getAquantity().asFloat();
+ if (currency.equals(lastCurrency)) lastCurrencyAmount += amount;
+ else {
+ if (lastCurrency != null) {
+ profile.storeAccountValue(db, acc.getName(), lastCurrency,
+ lastCurrencyAmount);
+ acc.addAmount(lastCurrencyAmount, lastCurrency);
+ }
+ lastCurrency = currency;
+ lastCurrencyAmount = amount;