+ SQLiteDatabase db = App.getDatabase();
+ debug("db", String.format(Locale.ROOT, "removing profile %d from DB", id));
+ db.beginTransactionNonExclusive();
+ try {
+ Object[] id_param = new Object[]{id};
+ db.execSQL("delete from transactions where profile_id=?", id_param);
+ db.execSQL("delete from accounts where profile=?", id_param);
+ db.execSQL("delete from options where profile=?", id_param);
+ db.execSQL("delete from profiles where id=?", id_param);
+ db.setTransactionSuccessful();
+ }
+ finally {
+ db.endTransaction();
+ }
+ }
+ public LedgerTransaction loadTransaction(int transactionId) {
+ LedgerTransaction tr = new LedgerTransaction(transactionId, this.id);
+ tr.loadData(App.getDatabase());
+
+ return tr;
+ }
+ public int getThemeHue() {
+// debug("profile", String.format("Profile.getThemeHue() returning %d", themeHue));
+ return this.themeHue;
+ }
+ public void setThemeHue(Object o) {
+ setThemeId(Integer.parseInt(String.valueOf(o)));
+ }
+ public void setThemeId(int themeHue) {
+// debug("profile", String.format("Profile.setThemeHue(%d) called", themeHue));
+ this.themeHue = themeHue;
+ }
+ public int getNextTransactionsGeneration(SQLiteDatabase db) {
+ try (Cursor c = db.rawQuery(
+ "SELECT generation FROM transactions WHERE profile_id=? LIMIT 1",
+ new String[]{String.valueOf(id)}))
+ {
+ if (c.moveToFirst())
+ return c.getInt(0) + 1;
+ }
+ return 1;
+ }
+ private int getNextAccountsGeneration(SQLiteDatabase db) {
+ try (Cursor c = db.rawQuery("SELECT generation FROM accounts WHERE profile_id=? LIMIT 1",
+ new String[]{String.valueOf(id)})) {
+ if (c.moveToFirst())
+ return c.getInt(0) + 1;
+ }
+ return 1;
+ }
+ private void deleteNotPresentAccounts(SQLiteDatabase db, int generation) {
+ Logger.debug("db/benchmark", "Deleting obsolete accounts");
+ db.execSQL("DELETE FROM account_values WHERE (select a.profile_id from accounts a where a" +
+ ".id=account_values.account_id)=? AND generation <> ?",
+ new Object[]{id, generation});
+ db.execSQL("DELETE FROM accounts WHERE profile_id=? AND generation <> ?",
+ new Object[]{id, generation});
+ Logger.debug("db/benchmark", "Done deleting obsolete accounts");
+ }
+ private void deleteNotPresentTransactions(SQLiteDatabase db, int generation) {
+ Logger.debug("db/benchmark", "Deleting obsolete transactions");
+ db.execSQL(
+ "DELETE FROM transaction_accounts WHERE (select t.profile_id from transactions t " +
+ "where t.id=transaction_accounts.transaction_id)=? AND generation" + " <> ?",
+ new Object[]{id, generation});
+ db.execSQL("DELETE FROM transactions WHERE profile_id=? AND generation <> ?",
+ new Object[]{id, generation});
+ Logger.debug("db/benchmark", "Done deleting obsolete transactions");
+ }
+ public void wipeAllData() {
+ SQLiteDatabase db = App.getDatabase();
+ db.beginTransaction();
+ try {
+ String[] pUuid = new String[]{String.valueOf(id)};
+ db.execSQL("delete from options where profile=?", pUuid);
+ db.execSQL("delete from accounts where profile=?", pUuid);
+ db.execSQL("delete from account_values where profile=?", pUuid);
+ db.execSQL("delete from transactions where profile=?", pUuid);
+ db.execSQL("delete from transaction_accounts where profile=?", pUuid);
+ db.setTransactionSuccessful();
+ debug("wipe", String.format(Locale.ENGLISH, "Profile %s wiped out", pUuid[0]));
+ }
+ finally {
+ db.endTransaction();
+ }
+ }
+ public List<Currency> getCurrencies() {
+ SQLiteDatabase db = App.getDatabase();
+
+ ArrayList<Currency> result = new ArrayList<>();
+
+ try (Cursor c = db.rawQuery("SELECT c.id, c.name, c.position, c.has_gap FROM currencies c",
+ new String[]{}))
+ {
+ while (c.moveToNext()) {
+ Currency currency = new Currency(c.getInt(0), c.getString(1),
+ Currency.Position.valueOf(c.getString(2)), c.getInt(3) == 1);
+ result.add(currency);
+ }
+ }
+
+ return result;
+ }
+ Currency loadCurrencyByName(String name) {
+ SQLiteDatabase db = App.getDatabase();
+ Currency result = tryLoadCurrencyByName(db, name);
+ if (result == null)
+ throw new RuntimeException(String.format("Unable to load currency '%s'", name));
+ return result;
+ }
+ private Currency tryLoadCurrencyByName(SQLiteDatabase db, String name) {
+ try (Cursor cursor = db.rawQuery(
+ "SELECT c.id, c.name, c.position, c.has_gap FROM currencies c WHERE c.name=?",
+ new String[]{name}))
+ {
+ if (cursor.moveToFirst()) {
+ return new Currency(cursor.getInt(0), cursor.getString(1),
+ Currency.Position.valueOf(cursor.getString(2)), cursor.getInt(3) == 1);
+ }
+ return null;
+ }
+ }
+ public void storeAccountAndTransactionListAsync(List<LedgerAccount> accounts,
+ List<LedgerTransaction> transactions) {
+ if (accountAndTransactionListSaver != null)
+ accountAndTransactionListSaver.interrupt();
+
+ accountAndTransactionListSaver =
+ new AccountAndTransactionListSaver(this, accounts, transactions);
+ accountAndTransactionListSaver.start();
+ }
+ private Currency tryLoadCurrencyById(SQLiteDatabase db, int id) {
+ try (Cursor cursor = db.rawQuery(
+ "SELECT c.id, c.name, c.position, c.has_gap FROM currencies c WHERE c.id=?",
+ new String[]{String.valueOf(id)}))