X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=sidebyside;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FRetrieveTransactionsTask.java;h=e63f30d2d026cc30988b1f191f0a38657f67472e;hb=1857937cbdb55199cb9391a7b42766d6dcba80c8;hp=1ef1ccade00e4d40cf93f266b6c3a1205b9e36a1;hpb=9afaa87d47292f5351f8ae447a9d3ae557147472;p=mobile-ledger.git
diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
index 1ef1ccad..e63f30d2 100644
--- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
+++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
@@ -18,7 +18,6 @@
package net.ktnx.mobileledger.async;
import android.annotation.SuppressLint;
-import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
@@ -28,7 +27,7 @@ import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.TransactionListActivity;
import net.ktnx.mobileledger.model.LedgerTransaction;
import net.ktnx.mobileledger.model.LedgerTransactionItem;
-import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
+import net.ktnx.mobileledger.utils.MLDB;
import net.ktnx.mobileledger.utils.NetworkUtil;
import java.io.BufferedReader;
@@ -92,82 +91,86 @@ public class RetrieveTransactionsTask extends
NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
http.setAllowUserInteraction(false);
publishProgress(progress);
- Context ctx = contextRef.get();
+ TransactionListActivity ctx = contextRef.get();
if (ctx == null) return null;
- try (MobileLedgerDatabase dbh = new MobileLedgerDatabase(ctx)) {
- try (SQLiteDatabase db = dbh.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("DELETE FROM transactions;");
- db.execSQL("DELETE FROM transaction_accounts");
+ try (SQLiteDatabase db = MLDB.getWritableDatabase(ctx)) {
+ try (InputStream resp = http.getInputStream()) {
+ if (http.getResponseCode() != 200) throw new IOException(
+ String.format("HTTP error %d", http.getResponseCode()));
+ db.beginTransaction();
+ try {
+ db.execSQL("DELETE FROM transactions;");
+ db.execSQL("DELETE FROM transaction_accounts");
- int state = ParserState.EXPECTING_JOURNAL;
- String line;
- BufferedReader buf =
- new BufferedReader(new InputStreamReader(resp, "UTF-8"));
+ int state = ParserState.EXPECTING_JOURNAL;
+ String line;
+ BufferedReader buf =
+ new BufferedReader(new InputStreamReader(resp, "UTF-8"));
- int transactionCount = 0;
- int transactionId = 0;
- LedgerTransaction transaction = null;
- LINES:
- while ((line = buf.readLine()) != null) {
- if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
- Matcher m;
- L(String.format("State is %d", state));
- switch (state) {
- case ParserState.EXPECTING_JOURNAL:
- if (line.equals("
General Journal
")) {
- state = ParserState.EXPECTING_TRANSACTION;
- L("â expecting transaction");
- }
- break;
- case ParserState.EXPECTING_TRANSACTION:
- m = transactionStartPattern.matcher(line);
- if (m.find()) {
- transactionId = Integer.valueOf(m.group(1));
- state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
- L(String.format("found transaction %d â expecting " +
- "description", transactionId));
- progress.setProgress(++transactionCount);
- if (progress.getTotal() == Progress.INDETERMINATE)
- progress.setTotal(transactionId);
- publishProgress(progress);
- }
- m = endPattern.matcher(line);
- if (m.find()) {
- L("--- transaction list complete ---");
- success = true;
- break LINES;
- }
- break;
- case ParserState.EXPECTING_TRANSACTION_DESCRIPTION:
- m = transactionDescriptionPattern.matcher(line);
- if (m.find()) {
- if (transactionId == 0)
- throw new TransactionParserException(
- "Transaction Id is 0 while expecting " +
- "description");
+ int transactionCount = 0;
+ int transactionId = 0;
+ LedgerTransaction transaction = null;
+ LINES:
+ while ((line = buf.readLine()) != null) {
+ if (isCancelled()) break;
+ Matcher m;
+ //L(String.format("State is %d", state));
+ switch (state) {
+ case ParserState.EXPECTING_JOURNAL:
+ if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
+ if (line.equals("General Journal
")) {
+ state = ParserState.EXPECTING_TRANSACTION;
+ L("â expecting transaction");
+ }
+ break;
+ case ParserState.EXPECTING_TRANSACTION:
+ if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
+ m = transactionStartPattern.matcher(line);
+ if (m.find()) {
+ transactionId = Integer.valueOf(m.group(1));
+ state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
+ L(String.format(
+ "found transaction %d â expecting " + "description",
+ transactionId));
+ progress.setProgress(++transactionCount);
+ if (progress.getTotal() == Progress.INDETERMINATE)
+ progress.setTotal(transactionId);
+ publishProgress(progress);
+ }
+ m = endPattern.matcher(line);
+ if (m.find()) {
+ L("--- transaction list complete ---");
+ success = true;
+ break LINES;
+ }
+ break;
+ case ParserState.EXPECTING_TRANSACTION_DESCRIPTION:
+ if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
+ m = transactionDescriptionPattern.matcher(line);
+ if (m.find()) {
+ if (transactionId == 0)
+ throw new TransactionParserException(
+ "Transaction Id is 0 while expecting " +
+ "description");
- transaction =
- new LedgerTransaction(transactionId, m.group(1),
- m.group(2));
- state = ParserState.EXPECTING_TRANSACTION_DETAILS;
- L(String.format("transaction %d created for %s (%s) â" +
- " expecting details", transactionId,
- m.group(1), m.group(2)));
- }
- break;
- case ParserState.EXPECTING_TRANSACTION_DETAILS:
- if (line.isEmpty()) {
- // transaction data collected
- transaction.insertInto(db);
+ transaction =
+ new LedgerTransaction(transactionId, m.group(1),
+ m.group(2));
+ state = ParserState.EXPECTING_TRANSACTION_DETAILS;
+ L(String.format("transaction %d created for %s (%s) â" +
+ " expecting details", transactionId,
+ m.group(1), m.group(2)));
+ }
+ break;
+ case ParserState.EXPECTING_TRANSACTION_DETAILS:
+ if (line.isEmpty()) {
+ // transaction data collected
+ transaction.insertInto(db);
- state = ParserState.EXPECTING_TRANSACTION;
- L(String.format("transaction %s saved â expecting " +
- "transaction", transaction.getId()));
+ state = ParserState.EXPECTING_TRANSACTION;
+ L(String.format(
+ "transaction %s saved â expecting " + "transaction",
+ transaction.getId()));
// 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
@@ -176,36 +179,35 @@ public class RetrieveTransactionsTask extends
// "parser");
// break LINES;
// }
+ }
+ else {
+ m = transactionDetailsPattern.matcher(line);
+ if (m.find()) {
+ String acc_name = m.group(1);
+ String amount = m.group(2);
+ amount = amount.replace(',', '.');
+ transaction.add_item(new LedgerTransactionItem(acc_name,
+ Float.valueOf(amount)));
+ L(String.format("%s = %s", acc_name, amount));
}
- else {
- m = transactionDetailsPattern.matcher(line);
- if (m.find()) {
- String acc_name = m.group(1);
- String amount = m.group(2);
- amount = amount.replace(',', '.');
- transaction.add_item(
- new LedgerTransactionItem(acc_name,
- Float.valueOf(amount)));
- L(String.format("%s = %s", acc_name, amount));
- }
- else throw new IllegalStateException(String.format(
- "Can't parse transaction details"));
- }
- break;
- default:
- throw new RuntimeException(
- String.format("Unknown " + "parser state %d",
- state));
- }
+ else throw new IllegalStateException(
+ String.format("Can't parse transaction details"));
+ }
+ break;
+ default:
+ throw new RuntimeException(
+ String.format("Unknown " + "parser state %d", state));
}
- db.setTransactionSuccessful();
- }
- finally {
- db.endTransaction();
}
+ if (!isCancelled()) db.setTransactionSuccessful();
+ }
+ finally {
+ db.endTransaction();
}
}
}
+
+ if (success && !isCancelled()) ctx.model.reloadTransactions(ctx);
}
catch (MalformedURLException e) {
error = R.string.err_bad_backend_url;