/** * Insert an entry into the table * * @param ledgerEntry Ledger entry */ public void insert(LedgerEntry ledgerEntry) { try (Connection con = db.getConnection()) { ledgerEntry.save(con); } catch (SQLException e) { throw new RuntimeException(e.toString(), e); } }
/** * Log an event in the account_ledger table * * @param ledgerEntry Ledger entry */ static void logEntry(LedgerEntry ledgerEntry) { // // Must be in a database transaction // if (!Db.db.isInTransaction()) { throw new IllegalStateException("Not in transaction"); } // // Combine multiple ledger entries // int index = pendingEntries.indexOf(ledgerEntry); if (index >= 0) { LedgerEntry existingEntry = pendingEntries.remove(index); ledgerEntry.updateChange(existingEntry.getChange()); long adjustedBalance = existingEntry.getBalance() - existingEntry.getChange(); for (; index < pendingEntries.size(); index++) { existingEntry = pendingEntries.get(index); if (existingEntry.getAccountId() == ledgerEntry.getAccountId() && existingEntry.getHolding() == ledgerEntry.getHolding() && ((existingEntry.getHoldingId() == null && ledgerEntry.getHoldingId() == null) || (existingEntry.getHoldingId() != null && existingEntry.getHoldingId().equals(ledgerEntry.getHoldingId())))) { adjustedBalance += existingEntry.getChange(); existingEntry.setBalance(adjustedBalance); } } } pendingEntries.add(ledgerEntry); }