]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
put all the annotated abstract DAO methods in the beginning
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TransactionDAO.java
index c03da12b1649befe77e421e5c7f9e8bb1153cd2c..31319f34b9f2be522273b3a5ade9a74f2ecdc3b9 100644 (file)
@@ -27,11 +27,15 @@ import androidx.room.OnConflictStrategy;
 import androidx.room.Query;
 import androidx.room.Update;
 
+import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.db.Transaction;
+import net.ktnx.mobileledger.db.TransactionAccount;
 import net.ktnx.mobileledger.db.TransactionWithAccounts;
+import net.ktnx.mobileledger.utils.Logger;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
 
 @Dao
 public abstract class TransactionDAO extends BaseDAO<Transaction> {
@@ -53,15 +57,11 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     public abstract void deleteSync(Transaction item);
 
     @Delete
-    public abstract void deleteSync(List<Transaction> items);
+    public abstract void deleteSync(Transaction... items);
 
-    @Query("SELECT * FROM transactions")
-    public abstract LiveData<List<Transaction>> getAll();
+    @Delete
+    public abstract void deleteSync(List<Transaction> items);
 
-    //    not useful for now
-//    @Transaction
-//    @Query("SELECT * FROM patterns")
-//    List<PatternWithAccounts> getPatternsWithAccounts();
     @Query("SELECT * FROM transactions WHERE id = :id")
     public abstract LiveData<Transaction> getById(long id);
 
@@ -87,17 +87,47 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     public abstract TransactionWithAccounts getFirstByDescriptionSync(@NonNull String description);
 
     @androidx.room.Transaction
-    @Query("SELECT * from transactions tr JOIN transaction_accounts t_a ON t_a.transaction_id = " +
-           "tr.id WHERE tr.description = :description AND t_a.account_name LIKE " +
-           "'%'||:accountTerm||'%' ORDER BY year desc, month desc, day desc LIMIT 1")
+    @Query("SELECT tr.id, tr.profile_id, tr.ledger_id, tr.description, tr.data_hash, tr.comment, " +
+           "tr.year, tr.month, tr.day, tr.generation from transactions tr JOIN " +
+           "transaction_accounts t_a ON t_a.transaction_id = tr.id WHERE tr.description = " +
+           ":description AND t_a.account_name LIKE '%'||:accountTerm||'%' ORDER BY year desc, " +
+           "month desc, day desc, tr.ledger_id desc LIMIT 1")
     public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
             @NonNull String description, @NonNull String accountTerm);
 
     @Query("SELECT * from transactions WHERE profile_id = :profileId")
-    public abstract List<Transaction> allForProfileSync(long profileId);
+    public abstract List<Transaction> getAllForProfileUnorderedSync(long profileId);
 
     @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
     protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
+
+    @androidx.room.Transaction
+    @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
+    public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
+
+    @androidx.room.Transaction
+    @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
+           " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
+           "transaction_accounts ta ON ta.transaction_id=tr.id WHERE ta.account_name LIKE " +
+           ":accountName||'%' AND ta.amount <> 0 AND tr.profile_id = :profileId ORDER BY tr.year " +
+           "desc, tr.month desc, tr.day desc, tr.ledger_id desc")
+    public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
+                                                                                 String accountName);
+
+    @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
+           ":currentGeneration")
+    public abstract int purgeOldTransactionsSync(long profileId, long currentGeneration);
+
+    @Query("DELETE FROM transaction_accounts WHERE EXISTS (SELECT 1 FROM transactions tr WHERE tr" +
+           ".id=transaction_accounts.transaction_id AND tr.profile_id=:profileId) AND generation " +
+           "<> :currentGeneration")
+    public abstract int purgeOldTransactionAccountsSync(long profileId, long currentGeneration);
+
+    @Query("DELETE FROM transactions WHERE profile_id = :profileId")
+    public abstract int deleteAllSync(long profileId);
+
+    @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId")
+    public abstract Transaction getByLedgerId(long profileId, long ledgerId);
     public long getGenerationSync(long profileId) {
         TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
 
@@ -105,9 +135,51 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
             return 0;
         return result.generation;
     }
-    @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
-           ":currentGeneration")
-    public abstract void purgeOldTransactionsSync(long profileId, long currentGeneration);
+    public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
+        long generation = getGenerationSync(profileId) + 1;
+
+        for (TransactionWithAccounts tr : list) {
+            tr.transaction.setGeneration(generation);
+            tr.transaction.setProfileId(profileId);
+
+            storeSync(tr);
+        }
+
+        Logger.debug("Transaction", "Purging old transactions");
+        int removed = purgeOldTransactionsSync(profileId, generation);
+        Logger.debug("Transaction", String.format(Locale.ROOT, "Purged %d transactions", removed));
+
+        removed = purgeOldTransactionAccountsSync(profileId, generation);
+        Logger.debug("Transaction",
+                String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
+    }
+    private void storeSync(TransactionWithAccounts rec) {
+        TransactionAccountDAO trAccDao = DB.get()
+                                           .getTransactionAccountDAO();
+
+        Transaction transaction = rec.transaction;
+        Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId());
+        if (existing != null) {
+            existing.copyDataFrom(transaction);
+            updateSync(existing);
+            transaction = existing;
+        }
+        else
+            transaction.setId(insertSync(transaction));
+
+        for (TransactionAccount trAcc : rec.accounts) {
+            trAcc.setTransactionId(transaction.getId());
+            trAcc.setGeneration(transaction.getGeneration());
+            TransactionAccount existingAcc =
+                    trAccDao.getByOrderNoSync(trAcc.getTransactionId(), trAcc.getOrderNo());
+            if (existingAcc != null) {
+                existingAcc.copyDataFrom(trAcc);
+                trAccDao.updateSync(existingAcc);
+            }
+            else
+                trAcc.setId(trAccDao.insertSync(trAcc));
+        }
+    }
     static class TransactionGenerationContainer {
         @ColumnInfo
         long generation;