|
From: Pelle B. <pe...@us...> - 2004-03-29 17:07:55
|
Update of /cvsroot/neuclear/neuclear-ledger/src/java/org/neuclear/ledger/simple In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30575/src/java/org/neuclear/ledger/simple Modified Files: SimpleLedger.java Log Message: AbstractLedgerBrowserTest has been extended to test date ranges SimpleLedger now passes all tests. HibernateLedger passes at times, which is mysterious. More research needed. Index: SimpleLedger.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-ledger/src/java/org/neuclear/ledger/simple/SimpleLedger.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** SimpleLedger.java 26 Mar 2004 23:36:34 -0000 1.6 --- SimpleLedger.java 29 Mar 2004 16:56:26 -0000 1.7 *************** *** 4,7 **** --- 4,12 ---- * $Id$ * $Log$ + * Revision 1.7 2004/03/29 16:56:26 pelle + * AbstractLedgerBrowserTest has been extended to test date ranges + * SimpleLedger now passes all tests. + * HibernateLedger passes at times, which is mysterious. More research needed. + * * Revision 1.6 2004/03/26 23:36:34 pelle * The simple browse(book) now works on hibernate, I have implemented the other two, which currently don not constrain the query correctly. *************** *** 89,95 **** import org.neuclear.ledger.browser.LedgerBrowser; ! import java.util.Date; ! import java.util.HashMap; ! import java.util.Iterator; /** --- 94,98 ---- import org.neuclear.ledger.browser.LedgerBrowser; ! import java.util.*; /** *************** *** 105,108 **** --- 108,112 ---- held = new HashMap(); balances = new HashMap(); + books = new HashMap(); } *************** *** 121,129 **** throw new UnBalancedTransactionException(this, trans); final PostedTransaction posted = new PostedTransaction(trans, new Date()); ! ledger.put(id, posted); updateBalances(posted); return posted; } private void updateBalances(final PostedTransaction trans) { synchronized (balances) { --- 125,149 ---- throw new UnBalancedTransactionException(this, trans); final PostedTransaction posted = new PostedTransaction(trans, new Date()); ! return post(posted); ! } ! ! private PostedTransaction post(final PostedTransaction posted) { ! ledger.put(posted.getId(), posted); ! postToBook(posted); updateBalances(posted); return posted; } + private void postToBook(final PostedTransaction posted) { + Iterator iter = posted.getItems(); + while (iter.hasNext()) { + TransactionItem item = (TransactionItem) iter.next(); + if (!books.containsKey(item.getBook())) + books.put(item.getBook(), new LinkedList()); + List bookentries = (List) books.get(item.getBook()); + bookentries.add(posted); + } + } + private void updateBalances(final PostedTransaction trans) { synchronized (balances) { *************** *** 172,175 **** --- 192,196 ---- final PostedHeldTransaction posted = new PostedHeldTransaction(trans, new Date()); held.put(posted.getId(), posted); + postToBook(posted); return posted; } *************** *** 211,217 **** held.remove(hold.getId()); PostedTransaction posted = new PostedTransaction(hold, new Date(), amount, comment); ! ledger.put(posted.getId(), posted); ! updateBalances(posted); ! return posted; } --- 232,236 ---- held.remove(hold.getId()); PostedTransaction posted = new PostedTransaction(hold, new Date(), amount, comment); ! return post(posted); } *************** *** 324,332 **** public BookBrowser browseFrom(String book, Date from) throws LowlevelLedgerException { ! return null; } public BookBrowser browseRange(String book, Date from, Date until) throws LowlevelLedgerException { ! return null; } --- 343,351 ---- public BookBrowser browseFrom(String book, Date from) throws LowlevelLedgerException { ! return new SimpleBookBrowser(book, from); } public BookBrowser browseRange(String book, Date from, Date until) throws LowlevelLedgerException { ! return new SimpleBookBrowser(book, from, until); } *************** *** 335,344 **** private final String id; private final HashMap balances; private class SimpleBookBrowser extends BookBrowser { ! public SimpleBookBrowser(String book) { super(book); ! iter = ledger.keySet().iterator(); ! System.out.println("ledger contains: " + ledger.size()); } --- 354,391 ---- private final String id; private final HashMap balances; + private final HashMap books; private class SimpleBookBrowser extends BookBrowser { ! public SimpleBookBrowser(final String book) { ! this(book, null, null); ! } ! ! public SimpleBookBrowser(final String book, final Date from) { ! this(book, from, null); ! } ! ! public SimpleBookBrowser(final String book, final Date from, final Date to) { super(book); ! this.from = from; ! this.to = to; ! if (books.containsKey(book)) { ! iter = ((List) books.get(book)).iterator(); ! // System.out.println("book contains: " + ((List)books.get(book)).size()); ! } else { ! iter = new Iterator() { ! public void remove() { ! ! } ! ! public boolean hasNext() { ! return false; ! } ! ! public Object next() { ! return null; ! } ! ! }; ! } } *************** *** 346,355 **** if (!iter.hasNext()) return false; ! // PostedTransaction tran=(PostedTransaction) ledger.get((iter.next())); ! iter.next(); return true; } private final Iterator iter; } } --- 393,433 ---- if (!iter.hasNext()) return false; ! PostedTransaction tran = (PostedTransaction) iter.next(); ! if (!isValid(tran)) ! return next(); ! ! setRow(tran); return true; } + private boolean isValid(final PostedTransaction posted) { + if (from == null) + return true; + if (posted.getTransactionTime().after(from)) { + return (to == null) || posted.getTransactionTime().before(to); + } + return false; + } + + private void setRow(PostedTransaction tran) { + Iterator iter = tran.getItems(); + TransactionItem item = null; + String counterparty = null; + while (iter.hasNext()) { + TransactionItem party = (TransactionItem) iter.next(); + if (!party.getBook().equals(getBook())) { + counterparty = party.getBook(); + } else { + item = party; + } + } + + setRow(tran.getId(), tran.getRequestId(), counterparty, tran.getComment(), tran.getTransactionTime(), item.getAmount(), null, null, null); + } + private final Iterator iter; + private final Date from; + private final Date to; + // private int i=0; } } |