X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fdao%2FTransactionDAO.java;h=31319f34b9f2be522273b3a5ade9a74f2ecdc3b9;hb=d3a26723c8dd18dc6a27084c227e652d6cb78424;hp=c03da12b1649befe77e421e5c7f9e8bb1153cd2c;hpb=5df10dc0b58df4d4be4e9ab34f1e0f477ca46766;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 c03da12b..31319f34 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 { @@ -53,15 +57,11 @@ public abstract class TransactionDAO extends BaseDAO { public abstract void deleteSync(Transaction item); @Delete - public abstract void deleteSync(List items); + public abstract void deleteSync(Transaction... items); - @Query("SELECT * FROM transactions") - public abstract LiveData> getAll(); + @Delete + public abstract void deleteSync(List items); - // not useful for now -// @Transaction -// @Query("SELECT * FROM patterns") -// List getPatternsWithAccounts(); @Query("SELECT * FROM transactions WHERE id = :id") public abstract LiveData getById(long id); @@ -87,17 +87,47 @@ 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") - public abstract List allForProfileSync(long profileId); + public abstract List 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 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 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 { 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 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;