- protected Void doInBackground(Params... params) {
- try {
- HttpURLConnection http =
- NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
- http.setAllowUserInteraction(false);
- publishProgress(0);
- Context 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 {
- String root = params[0].getAccountsRoot();
- if (root == null) db.execSQL("DELETE FROM transaction_history;");
+ protected void onProgressUpdate(Progress... values) {
+ super.onProgressUpdate(values);
+ MainActivity context = getContext();
+ if (context == null)
+ return;
+ context.onRetrieveProgress(values[0]);
+ }
+ @Override
+ protected void onPreExecute() {
+ super.onPreExecute();
+ MainActivity context = getContext();
+ if (context == null)
+ return;
+ context.onRetrieveStart();
+ }
+ @Override
+ protected void onPostExecute(String error) {
+ super.onPostExecute(error);
+ MainActivity context = getContext();
+ if (context == null)
+ return;
+ context.onRetrieveDone(error);
+ }
+ @Override
+ protected void onCancelled() {
+ super.onCancelled();
+ MainActivity context = getContext();
+ if (context == null)
+ return;
+ context.onRetrieveDone(null);
+ }
+ private String retrieveTransactionListLegacy() throws IOException, HTTPException {
+ Progress progress = new Progress();
+ int maxTransactionId = Progress.INDETERMINATE;
+ ArrayList<LedgerAccount> list = new ArrayList<>();
+ HashMap<String, LedgerAccount> map = new HashMap<>();
+ ArrayList<LedgerAccount> displayed = new ArrayList<>();
+ ArrayList<LedgerTransaction> transactions = new ArrayList<>();
+ LedgerAccount lastAccount = null;
+ ArrayList<LedgerAccount> syntheticAccounts = new ArrayList<>();
+
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
+ http.setAllowUserInteraction(false);
+ publishProgress(progress);
+ if (http.getResponseCode() != 200)
+ throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
+
+ try (InputStream resp = http.getInputStream()) {
+ if (http.getResponseCode() != 200)
+ throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
+
+ 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");
+ continue;
+ }
+ m = reAccountName.matcher(line);
+ if (m.find()) {
+ String acct_encoded = m.group(1);
+ String accName = URLDecoder.decode(acct_encoded, "UTF-8");
+ accName = accName.replace("\"", "");
+ L(String.format("found account: %s", accName));
+
+ lastAccount = map.get(accName);
+ if (lastAccount != null) {
+ L(String.format("ignoring duplicate account '%s'", accName));
+ continue;
+ }
+ String parentAccountName = LedgerAccount.extractParentName(accName);
+ LedgerAccount parentAccount;
+ if (parentAccountName != null) {
+ parentAccount = ensureAccountExists(parentAccountName, map,
+ syntheticAccounts);
+ }