package net.ktnx.mobileledger.dao;
+import android.os.AsyncTask;
+
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.room.ColumnInfo;
import androidx.room.Query;
import androidx.room.Update;
+import net.ktnx.mobileledger.db.Account;
+import net.ktnx.mobileledger.db.AccountValue;
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.model.LedgerAccount;
import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
import java.util.ArrayList;
import java.util.List;
protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
@androidx.room.Transaction
- @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
+ @Query("SELECT * FROM transactions WHERE profile_id = :profileId ORDER BY year " +
+ " desc, month desc, day desc, ledger_id desc")
public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
+ @androidx.room.Transaction
+ @Query("SELECT * FROM transactions WHERE profile_id = :profileId ORDER BY year " +
+ " desc, month desc, day desc, ledger_id desc")
+ public abstract LiveData<List<TransactionWithAccounts>> getAllWithAccounts(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 " +
public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
String accountName);
+ @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 LiveData<List<TransactionWithAccounts>> getAllWithAccountsFiltered(
+ long profileId, String accountName);
+
@Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
":currentGeneration")
public abstract int purgeOldTransactionsSync(long profileId, long currentGeneration);
@Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId")
public abstract Transaction getByLedgerId(long profileId, long ledgerId);
+
+ @Query("UPDATE transactions SET generation = :newGeneration WHERE id = :transactionId")
+ public abstract int updateGeneration(long transactionId, long newGeneration);
+
+ @Query("UPDATE transaction_accounts SET generation = :newGeneration WHERE transaction_id = " +
+ ":transactionId")
+ public abstract int updateAccountsGeneration(long transactionId, long newGeneration);
+
+ @Query("SELECT max(ledger_id) as ledger_id FROM transactions WHERE profile_id = :profileId")
+ public abstract LedgerIdContainer getMaxLedgerIdPOJOSync(long profileId);
+ @androidx.room.Transaction
+ public void updateGenerationWithAccounts(long transactionId, long newGeneration) {
+ updateGeneration(transactionId, newGeneration);
+ updateAccountsGeneration(transactionId, newGeneration);
+ }
public long getGenerationSync(long profileId) {
TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
return 0;
return result.generation;
}
+ public long getMaxLedgerIdSync(long profileId) {
+ LedgerIdContainer result = getMaxLedgerIdPOJOSync(profileId);
+
+ if (result == null)
+ return 0;
+ return result.ledgerId;
+ }
+ @androidx.room.Transaction
public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
long generation = getGenerationSync(profileId) + 1;
Logger.debug("Transaction",
String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
}
- private void storeSync(TransactionWithAccounts rec) {
+ @androidx.room.Transaction
+ void storeSync(TransactionWithAccounts rec) {
TransactionAccountDAO trAccDao = DB.get()
.getTransactionAccountDAO();
Transaction transaction = rec.transaction;
Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId());
if (existing != null) {
+ if (Misc.equalStrings(transaction.getDataHash(), existing.getDataHash())) {
+ updateGenerationWithAccounts(existing.getId(), rec.transaction.getGeneration());
+ return;
+ }
+
existing.copyDataFrom(transaction);
updateSync(existing);
+
transaction = existing;
}
else
trAcc.setId(trAccDao.insertSync(trAcc));
}
}
+ public void storeLast(TransactionWithAccounts rec) {
+ AsyncTask.execute(() -> appendSync(rec));
+ }
+ @androidx.room.Transaction
+ public void appendSync(TransactionWithAccounts rec) {
+ TransactionAccountDAO trAccDao = DB.get()
+ .getTransactionAccountDAO();
+ AccountDAO accDao = DB.get()
+ .getAccountDAO();
+ AccountValueDAO accValDao = DB.get()
+ .getAccountValueDAO();
+
+ Transaction transaction = rec.transaction;
+ final long profileId = transaction.getProfileId();
+ transaction.setGeneration(getGenerationSync(profileId));
+ transaction.setLedgerId(getMaxLedgerIdSync(profileId) + 1);
+ transaction.setId(insertSync(transaction));
+
+ for (TransactionAccount trAcc : rec.accounts) {
+ trAcc.setTransactionId(transaction.getId());
+ trAcc.setGeneration(transaction.getGeneration());
+ trAcc.setId(trAccDao.insertSync(trAcc));
+
+ Account acc = accDao.getByNameSync(profileId, trAcc.getAccountName());
+ if (acc == null) {
+ acc = new Account();
+ acc.setProfileId(profileId);
+ acc.setName(trAcc.getAccountName());
+ acc.setNameUpper(acc.getName()
+ .toUpperCase());
+ acc.setParentName(LedgerAccount.extractParentName(acc.getName()));
+ acc.setLevel(LedgerAccount.determineLevel(acc.getName()));
+ acc.setGeneration(trAcc.getGeneration());
+
+ acc.setId(accDao.insertSync(acc));
+ }
+
+ AccountValue accVal = accValDao.getByCurrencySync(acc.getId(), trAcc.getCurrency());
+ if (accVal == null) {
+ accVal = new AccountValue();
+ accVal.setAccountId(acc.getId());
+ accVal.setGeneration(trAcc.getGeneration());
+ accVal.setCurrency(trAcc.getCurrency());
+ accVal.setValue(trAcc.getAmount());
+ accVal.setId(accValDao.insertSync(accVal));
+ }
+ else {
+ accVal.setValue(accVal.getValue() + trAcc.getAmount());
+ accValDao.updateSync(accVal);
+ }
+ }
+ }
static class TransactionGenerationContainer {
@ColumnInfo
long generation;
}
}
+ static class LedgerIdContainer {
+ @ColumnInfo(name = "ledger_id")
+ long ledgerId;
+ public LedgerIdContainer(long ledgerId) {
+ this.ledgerId = ledgerId;
+ }
+ }
+
static public class DescriptionContainer {
@ColumnInfo
public String description;