+ public boolean existsInDb(SQLiteDatabase db) {
+ fillDataHash();
+ try (Cursor c = db
+ .rawQuery("SELECT 1 from transactions where data_hash = ?", new String[]{dataHash}))
+ {
+ boolean result = c.moveToFirst();
+ debug("db", String.format("Transaction %d (%s) %s", id, dataHash,
+ result ? "already present" : "not present"));
+ return result;
+ }
+ }
+ public void loadData(SQLiteDatabase db) {
+ if (dataLoaded) return;
+
+ try (Cursor cTr = db
+ .rawQuery("SELECT date, description from transactions WHERE profile=? AND id=?",
+ new String[]{profile, String.valueOf(id)}))
+ {
+ if (cTr.moveToFirst()) {
+ String dateString = cTr.getString(0);
+ try {
+ date = Globals.parseLedgerDate(dateString);
+ }
+ catch (ParseException e) {
+ e.printStackTrace();
+ throw new RuntimeException(
+ String.format("Error parsing date '%s' from " + "transacion %d",
+ dateString, id));
+ }
+ description = cTr.getString(1);
+
+ try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
+ "transaction_accounts WHERE " +
+ "profile=? AND transaction_id = ?",
+ new String[]{profile, String.valueOf(id)}))
+ {
+ while (cAcc.moveToNext()) {
+// debug("transactions",
+// String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
+// cAcc.getFloat(1), cAcc.getString(2)));
+ addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
+ cAcc.getString(2)));
+ }
+
+ finishLoading();
+ }
+ }
+ }
+
+ }
+ public String getDataHash() {
+ return dataHash;
+ }
+ public void finishLoading() {
+ dataLoaded = true;
+ }
+ public ParsedLedgerTransaction toParsedLedgerTransaction() {
+ ParsedLedgerTransaction result = new ParsedLedgerTransaction();
+ result.setTcomment("");
+ result.setTprecedingcomment("");
+
+ ArrayList<ParsedPosting> postings = new ArrayList<>();
+ for (LedgerTransactionAccount acc : accounts) {
+ if (!acc.getAccountName().isEmpty()) postings.add(acc.asParsedPosting());
+ }
+
+ result.setTpostings(postings);
+ result.setTdate(Globals.formatIsoDate(date));
+ result.setTdate2(null);
+ result.setTindex(1);
+ result.setTdescription(description);
+ return result;
+ }