X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fdao%2FTransactionDAO.java;h=882e73a5f1514b389c612d63b8249e4f354ef804;hb=178110fe9e95cff2c1eb91b257f3caf5e0017d5b;hp=03f43533fa05641d8505f233323614f95613fd6f;hpb=3a3e22991c6e4666eb5c800bfd49ee7c271b5a32;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java index 03f43533..882e73a5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java @@ -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 { @@ -80,15 +84,16 @@ public abstract class TransactionDAO extends BaseDAO { 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 ORDER BY " + - "year desc, month desc, day desc, id desc") - public abstract List allForProfileSync(long profileId); + @Query("SELECT * from transactions WHERE profile_id = :profileId") + public abstract List getAllForProfileUnorderedSync(long profileId); @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1") protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId); @@ -102,7 +107,7 @@ public abstract class TransactionDAO extends BaseDAO { " 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.id desc") + "desc, tr.month desc, tr.day desc, tr.ledger_id desc") public abstract List getAllWithAccountsFilteredSync(long profileId, String accountName); @@ -115,7 +120,60 @@ public abstract class TransactionDAO extends BaseDAO { } @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " + ":currentGeneration") - public abstract void purgeOldTransactionsSync(long profileId, long 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); + public void storeTransactionsSync(List 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(trAcc); + trAcc = existingAcc; + } + else + trAcc.setId(trAccDao.insertSync(trAcc)); + } + } + @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId") + public abstract Transaction getByLedgerId(long profileId, long ledgerId); static class TransactionGenerationContainer { @ColumnInfo long generation;