+ 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;
+ }
+ }
+ if (lastCurrency != null) {
+ profile.storeAccountValue(db, acc.getName(), lastCurrency,
+ lastCurrencyAmount);
+ acc.addAmount(lastCurrencyAmount, lastCurrency);
+ }
+
+ if (acc.isVisible(accountList)) accountList.add(acc);
+
+ if (prevAccount != null) {
+ prevAccount.setHasSubAccounts(
+ acc.getName().startsWith(prevAccount.getName() + ":"));
+ }
+
+ prevAccount = acc;
+ }
+ throwIfCancelled();
+
+ profile.deleteNotPresentAccounts(db);
+ throwIfCancelled();
+ db.setTransactionSuccessful();
+ listFilledOK = true;
+ }
+ finally {
+ db.endTransaction();
+ }
+ }
+ // should not be set in the DB transaction, because of a possible deadlock
+ // with the main and DbOpQueueRunner threads
+ if (listFilledOK) Data.accounts.setList(accountList);
+
+ return true;
+ }
+ private boolean retrieveTransactionList() throws IOException, ParseException, HTTPException {
+ Progress progress = new Progress();
+ int maxTransactionId = Progress.INDETERMINATE;
+
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
+ http.setAllowUserInteraction(false);
+ publishProgress(progress);
+ switch (http.getResponseCode()) {
+ case 200:
+ break;
+ case 404:
+ return false;
+ 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()));
+ throwIfCancelled();
+ db.beginTransaction();
+ try {
+ profile.markTransactionsAsNotPresent(db);
+
+ int matchedTransactionsCount = 0;
+ TransactionListParser parser = new TransactionListParser(resp);
+
+ int processedTransactionCount = 0;
+
+ DetectedTransactionOrder transactionOrder = DetectedTransactionOrder.UNKNOWN;
+ int orderAccumulator = 0;
+ int lastTransactionId = 0;
+
+ while (true) {
+ throwIfCancelled();
+ ParsedLedgerTransaction parsedTransaction = parser.nextTransaction();
+ throwIfCancelled();
+ if (parsedTransaction == null) break;
+
+ LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
+ if (transaction.getId() > lastTransactionId) orderAccumulator++;
+ else orderAccumulator--;
+ lastTransactionId = transaction.getId();
+ if (transactionOrder == DetectedTransactionOrder.UNKNOWN) {
+ if (orderAccumulator > 30) {
+ transactionOrder = DetectedTransactionOrder.FILE;
+ debug("rtt", String.format(Locale.ENGLISH,
+ "Detected native file order after %d transactions (factor %d)",
+ processedTransactionCount, orderAccumulator));
+ progress.setTotal(Data.transactions.size());
+ }
+ else if (orderAccumulator < -30) {
+ transactionOrder = DetectedTransactionOrder.REVERSE_CHRONOLOGICAL;
+ debug("rtt", String.format(Locale.ENGLISH,
+ "Detected reverse chronological order after %d transactions (factor %d)",
+ processedTransactionCount, orderAccumulator));