+ 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(MobileLedgerProfile profile) throws IOException {
+ Progress progress = new Progress();
+
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
+ http.setAllowUserInteraction(false);
+ publishProgress(progress);
+ 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 {
+ profile.markAccountsAsNotPresent(db);
+
+ AccountListParser parser = new AccountListParser(resp);
+ ArrayList<LedgerAccount> accountList = new ArrayList<>();
+
+ while (true) {
+ throwIfCancelled();
+ ParsedLedgerAccount parsedAccount = parser.nextAccount();
+ if (parsedAccount == null) break;
+
+ LedgerAccount acc = new LedgerAccount(parsedAccount.getAname());
+ profile.storeAccount(acc);
+ for (ParsedBalance b : parsedAccount.getAebalance()) {
+ profile.storeAccountValue(acc.getName(), b.getAcommodity(),
+ b.getAquantity().asFloat());
+ }
+
+ accountList.add(acc);
+ }
+ throwIfCancelled();
+
+ profile.deleteNotPresentAccounts(db);
+ throwIfCancelled();
+ db.setTransactionSuccessful();
+ Data.accounts.set(accountList);
+ }
+ finally {
+ db.endTransaction();
+ }
+ }
+ }
+
+ return true;
+ }
+ private boolean retrieveTransactionList(MobileLedgerProfile profile)
+ throws IOException, ParseException {
+ Progress progress = new Progress();
+ int maxTransactionId = Progress.INDETERMINATE;
+
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
+ http.setAllowUserInteraction(false);
+ publishProgress(progress);
+ try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
+ 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;
+
+ while (true) {
+ throwIfCancelled();
+ ParsedLedgerTransaction parsedTransaction = parser.nextTransaction();
+ throwIfCancelled();
+ if (parsedTransaction == null) break;
+ LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
+ if (transaction.existsInDb(db)) {
+ profile.markTransactionAsPresent(db, transaction);
+ matchedTransactionsCount++;
+
+ if (matchedTransactionsCount == MATCHING_TRANSACTIONS_LIMIT) {
+ profile.markTransactionsBeforeTransactionAsPresent(db, transaction);
+ progress.setTotal(progress.getProgress());
+ publishProgress(progress);
+ db.setTransactionSuccessful();
+ profile.setLastUpdateStamp();
+ return true;