]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
adopt Room for displaying account lists
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / MobileLedgerProfile.java
index 15be74b6be1c572ba9c4e65d4a94e84dab2bfcbd..eef71a32b1a11490a513fdcf0156bb44b078edbe 100644 (file)
@@ -22,15 +22,21 @@ import android.content.Intent;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.util.SparseArray;
 
 import androidx.annotation.Nullable;
+import androidx.room.Transaction;
 
 import net.ktnx.mobileledger.App;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.DbOpQueue;
+import net.ktnx.mobileledger.dao.AccountDAO;
+import net.ktnx.mobileledger.dao.OptionDAO;
+import net.ktnx.mobileledger.dao.TransactionDAO;
+import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.json.API;
 import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
@@ -449,7 +455,7 @@ public final class MobileLedgerProfile {
                     });
             db.execSQL("INSERT INTO transaction_accounts(transaction_id, " +
                        "order_no, account_name, amount, currency, comment, generation) " +
-                       "select ?, ?, ?, ?, ?, ?, ?, ? WHERE (select changes() = 0)",
+                       "select ?, ?, ?, ?, ?, ?, ? WHERE (select changes() = 0)",
                     new Object[]{tr.getId(), accountOrderNo, item.getAccountName(),
                                  item.getAmount(), Misc.nullIsEmpty(item.getCurrency()),
                                  item.getComment(), generation
@@ -547,60 +553,59 @@ public final class MobileLedgerProfile {
         this.themeHue = themeHue;
     }
     public int getNextTransactionsGeneration(SQLiteDatabase db) {
-        int generation = 1;
         try (Cursor c = db.rawQuery(
                 "SELECT generation FROM transactions WHERE profile_id=? LIMIT 1",
                 new String[]{String.valueOf(id)}))
         {
-            if (c.moveToFirst()) {
-                generation = c.getInt(0) + 1;
-            }
+            if (c.moveToFirst())
+                return c.getInt(0) + 1;
         }
-        return generation;
+        return 1;
     }
     private int getNextAccountsGeneration(SQLiteDatabase db) {
-        int generation = 1;
         try (Cursor c = db.rawQuery("SELECT generation FROM accounts WHERE profile_id=? LIMIT 1",
                 new String[]{String.valueOf(id)}))
         {
-            if (c.moveToFirst()) {
-                generation = c.getInt(0) + 1;
-            }
+            if (c.moveToFirst())
+                return c.getInt(0) + 1;
         }
-        return generation;
+        return 1;
     }
     private void deleteNotPresentAccounts(SQLiteDatabase db, int generation) {
         Logger.debug("db/benchmark", "Deleting obsolete accounts");
-        db.execSQL("DELETE FROM account_values WHERE profile=? AND generation <> ?",
+        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=? AND 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 profile=? AND generation <> ?",
+        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=? AND generation <> ?",
+        db.execSQL("DELETE FROM transactions WHERE profile_id=? AND generation <> ?",
                 new Object[]{id, generation});
         Logger.debug("db/benchmark", "Done deleting obsolete transactions");
     }
+    @Transaction
+    public void wipeAllDataSync() {
+        OptionDAO optDao = DB.get()
+                             .getOptionDAO();
+        optDao.deleteSync(optDao.allForProfileSync(id));
+
+        AccountDAO accDao = DB.get()
+                              .getAccountDAO();
+        accDao.deleteSync(accDao.allForProfileSync(id));
+
+        TransactionDAO trnDao = DB.get()
+                                  .getTransactionDAO();
+        trnDao.deleteSync(trnDao.allForProfileSync(id));
+    }
     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();
-        }
+        AsyncTask.execute(this::wipeAllDataSync);
     }
     public List<Currency> getCurrencies() {
         SQLiteDatabase db = App.getDatabase();