- 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;");
- else {
- StringBuilder sql = new StringBuilder();
- sql.append("DELETE FROM transaction_history ");
- sql.append(
- "where id in (select transactions.id from transactions ");
- sql.append("join transaction_accounts ");
- sql.append(
- "on transactions.id=transaction_accounts.transaction_id ");
- sql.append("where transaction_accounts.account_name like ?||'%'");
- db.execSQL(sql.toString(), new String[]{root});
- }
-
- int state = ParserState.EXPECTING_JOURNAL;
- String line;
- BufferedReader buf =
- new BufferedReader(new InputStreamReader(resp, "UTF-8"));
-
- int transactionCount = 0;
- String transactionId = null;
- LedgerTransaction transaction = null;
- while ((line = buf.readLine()) != null) {
- switch (state) {
- case ParserState.EXPECTING_JOURNAL: {
- if (line.equals("<h2>General Journal</h2>"))
- state = ParserState.EXPECTING_TRANSACTION;
- continue;
- }
- case ParserState.EXPECTING_TRANSACTION: {
- Matcher m = transactionStartPattern.matcher(line);
- if (m.find()) {
- transactionId = m.group(1);
- state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
+ 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(MobileLedgerProfile profile)
+ throws IOException, ParseException, HTTPException {
+ Progress progress = new Progress();
+ int maxTransactionId = Progress.INDETERMINATE;
+ ArrayList<LedgerAccount> accountList = new ArrayList<>();
+ HashMap<String, Void> accountNames = new HashMap<>();
+ LedgerAccount lastAccount = null, prevAccount = null;
+ boolean onlyStarred = Data.optShowOnlyStarred.get();
+
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
+ http.setAllowUserInteraction(false);
+ publishProgress(progress);
+ switch (http.getResponseCode()) {
+ case 200:
+ break;
+ default:
+ throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
+ }
+ try (SQLiteDatabase db = MLDB.getDatabase()) {
+ try (InputStream resp = http.getInputStream()) {
+ if (http.getResponseCode() != 200)
+ throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
+ db.beginTransaction();
+ try {
+ prepareDbForRetrieval(db, profile);
+
+ 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");
+ // commit the current transaction and start a new one
+ // the account list in the UI should reflect the (committed)
+ // state of the database
+ db.setTransactionSuccessful();
+ db.endTransaction();
+ Data.accounts.setList(accountList);
+ db.beginTransaction();
+ continue;
+ }
+ m = reAccountName.matcher(line);
+ if (m.find()) {
+ String acct_encoded = m.group(1);
+ String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
+ acct_name = acct_name.replace("\"", "");
+ L(String.format("found account: %s", acct_name));
+
+ prevAccount = lastAccount;
+ lastAccount = profile.tryLoadAccount(db, acct_name);
+ if (lastAccount == null)
+ lastAccount = new LedgerAccount(acct_name);
+ else lastAccount.removeAmounts();
+ profile.storeAccount(db, lastAccount);
+
+ if (prevAccount != null) prevAccount
+ .setHasSubAccounts(prevAccount.isParentOf(lastAccount));
+ // make sure the parent account(s) are present,
+ // synthesising them if necessary
+ String parentName = lastAccount.getParentName();
+ if (parentName != null) {
+ Stack<String> toAppend = new Stack<>();
+ while (parentName != null) {
+ if (accountNames.containsKey(parentName)) break;
+ toAppend.push(parentName);
+ parentName =
+ new LedgerAccount(parentName).getParentName();