|
From: Pelle B. <pe...@us...> - 2004-03-24 23:24:03
|
Update of /cvsroot/neuclear/neuclear-ledger-hibernate/src/java/org/neuclear/ledger/hibernate In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26129/src/java/org/neuclear/ledger/hibernate Modified Files: HHeld.hbm.xml HTransaction.java HibernateLedger.java Log Message: Working on Hibernate Implementation. Index: HHeld.hbm.xml =================================================================== RCS file: /cvsroot/neuclear/neuclear-ledger-hibernate/src/java/org/neuclear/ledger/hibernate/HHeld.hbm.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** HHeld.hbm.xml 23 Mar 2004 21:57:48 -0000 1.1 --- HHeld.hbm.xml 24 Mar 2004 23:13:21 -0000 1.2 *************** *** 18,27 **** not-null="true"/> </property> ! <property name="transactionTime"> ! <column name="transactiontime" sql-type="double" not-null="true"/> </property> ! <property name="expiryTime"> ! <column name="expirytime" sql-type="double" not-null="true"/> </property> --- 18,27 ---- not-null="true"/> </property> ! <property name="transactionTime" type="timestamp"> ! <column name="transactiontime" not-null="true"/> </property> ! <property name="expiryTime" type="timestamp"> ! <column name="expirytime" not-null="true"/> </property> *************** *** 30,34 **** not-null="true"/> </property> ! <set name="items" table="held_items" inverse="true" cascade="all"> <key column="held_id"/> <one-to-many class="org.neuclear.ledger.hibernate.HHeldItem"/> --- 30,34 ---- not-null="true"/> </property> ! <set name="items" table="held_items" cascade="all"> <key column="held_id"/> <one-to-many class="org.neuclear.ledger.hibernate.HHeldItem"/> Index: HibernateLedger.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-ledger-hibernate/src/java/org/neuclear/ledger/hibernate/HibernateLedger.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** HibernateLedger.java 24 Mar 2004 12:33:10 -0000 1.4 --- HibernateLedger.java 24 Mar 2004 23:13:21 -0000 1.5 *************** *** 38,45 **** Configuration cfg = new Configuration() .addClass(HTransaction.class) ! .addClass(HTransactionItem.class); ! // .addClass(HHeld.class) ! // .addClass(HHeldItem.class); ! // new net.sf.hibernate.tool.hbm2ddl.SchemaExport(cfg).create(create,create); factory = cfg.buildSessionFactory(); } catch (HibernateException e) { --- 38,45 ---- Configuration cfg = new Configuration() .addClass(HTransaction.class) ! .addClass(HTransactionItem.class) ! .addClass(HHeld.class) ! .addClass(HHeldItem.class); ! new net.sf.hibernate.tool.hbm2ddl.SchemaExport(cfg).create(create, create); factory = cfg.buildSessionFactory(); } catch (HibernateException e) { *************** *** 60,68 **** try { Session ses = factory.openSession(); ! // net.sf.hibernate.Transaction t = ses.beginTransaction(); HTransaction posted = new HTransaction(trans, new Date()); ses.save(posted); ! ses.flush(); ! // t.commit(); ses.close(); return posted.createPosted(); --- 60,68 ---- try { Session ses = factory.openSession(); ! net.sf.hibernate.Transaction t = ses.beginTransaction(); HTransaction posted = new HTransaction(trans, new Date()); ses.save(posted); ! // ses.flush(); ! t.commit(); ses.close(); return posted.createPosted(); *************** *** 84,89 **** Session ses = factory.openSession(); net.sf.hibernate.Transaction t = ses.beginTransaction(); HTransaction posted = new HTransaction(trans, new Date()); ! ses.saveOrUpdate(posted); t.commit(); ses.close(); --- 84,97 ---- Session ses = factory.openSession(); net.sf.hibernate.Transaction t = ses.beginTransaction(); + Iterator iter = trans.getItems(); + // First lets check the balances + while (iter.hasNext()) { + TransactionItem item = (TransactionItem) iter.next(); + if (item.getAmount() < 0 && getAvailableBalance(item.getBook()) + item.getAmount() < 0) + throw new InsufficientFundsException(null, item.getBook(), item.getAmount()); + } + HTransaction posted = new HTransaction(trans, new Date()); ! ses.save(posted); t.commit(); ses.close(); *************** *** 106,114 **** try { Session ses = factory.openSession(); ! // net.sf.hibernate.Transaction t = ses.beginTransaction(); HHeld posted = new HHeld(trans, new Date()); ! ses.saveOrUpdate(posted); ! // t.commit(); ! ses.flush(); ses.close(); return posted.createPosted(); --- 114,122 ---- try { Session ses = factory.openSession(); ! net.sf.hibernate.Transaction t = ses.beginTransaction(); HHeld posted = new HHeld(trans, new Date()); ! ses.save(posted); ! t.commit(); ! // ses.flush(); ses.close(); return posted.createPosted(); *************** *** 128,131 **** --- 136,150 ---- */ public void performCancelHold(PostedHeldTransaction hold) throws LowlevelLedgerException, UnknownTransactionException { + try { + Session ses = factory.openSession(); + net.sf.hibernate.Transaction t = ses.beginTransaction(); + HHeld posted = (HHeld) ses.get(HHeld.class, hold.getId()); + if (posted != null) + ses.delete(posted); + t.commit(); + ses.close(); + } catch (HibernateException e) { + throw new LowlevelLedgerException(e); + } } *************** *** 147,151 **** */ public PostedTransaction performCompleteHold(PostedHeldTransaction hold, double amount, String comment) throws InvalidTransactionException, LowlevelLedgerException, TransactionExpiredException, UnknownTransactionException { ! return null; } --- 166,191 ---- */ public PostedTransaction performCompleteHold(PostedHeldTransaction hold, double amount, String comment) throws InvalidTransactionException, LowlevelLedgerException, TransactionExpiredException, UnknownTransactionException { ! try { ! Session ses = factory.openSession(); ! net.sf.hibernate.Transaction t = ses.beginTransaction(); ! HHeld posted = (HHeld) ses.get(HHeld.class, hold.getId()); ! final Date time = new Date(); ! if (posted.getExpiryTime().before(time)) { ! ses.delete(posted); ! t.commit(); ! ses.close(); ! throw new TransactionExpiredException(this, hold); ! } ! HTransaction htran = new HTransaction(posted, time); ! htran.setComment(comment); ! ses.save(htran); ! if (posted != null) ! ses.delete(posted); ! t.commit(); ! ses.close(); ! return htran.createPosted(); ! } catch (HibernateException e) { ! throw new LowlevelLedgerException(this, e); ! } } *************** *** 157,161 **** */ public Date getTransactionTime(String id) throws LowlevelLedgerException, UnknownTransactionException, InvalidTransactionException, UnknownBookException { ! return null; } --- 197,218 ---- */ public Date getTransactionTime(String id) throws LowlevelLedgerException, UnknownTransactionException, InvalidTransactionException, UnknownBookException { ! try { ! Session ses = factory.openSession(); ! Query q = ses.createQuery("select transactionTime from HTransaction item where item.id = ?"); ! q.setString(0, id); ! Iterator iter = q.iterate(); ! if (iter.hasNext()) { ! final Object o = iter.next(); ! if (o != null) { ! ses.close(); ! return ((Timestamp) o); ! } ! } ! ses.close(); ! throw new UnknownTransactionException(this, id); ! } catch (HibernateException e) { ! throw new LowlevelLedgerException(e); ! } ! } *************** *** 183,197 **** public double getBalance(String book) throws LowlevelLedgerException { try { ! Session sess = factory.openSession(); ! Query q = sess.createQuery("select sum(item.amount) from HTransactionItem item where item.book = ?"); q.setString(0, book); ! Iterator cats = q.iterate(); ! if (cats.hasNext()) { ! final Object o = cats.next(); ! if (o != null) return ((Double) o).doubleValue(); // throw new LowlevelLedgerException(this,"Query returned more or less than one column"); } // throw new LowlevelLedgerException(this,"Query didnt return a row"); return 0; } catch (HibernateException e) { --- 240,278 ---- public double getBalance(String book) throws LowlevelLedgerException { try { ! Session ses = factory.openSession(); ! Query q = ses.createQuery("select sum(item.amount) from HTransactionItem item where item.book = ?"); q.setString(0, book); ! Iterator iter = q.iterate(); ! if (iter.hasNext()) { ! final Object o = iter.next(); ! if (o != null) { ! ses.close(); return ((Double) o).doubleValue(); + } // throw new LowlevelLedgerException(this,"Query returned more or less than one column"); } // throw new LowlevelLedgerException(this,"Query didnt return a row"); + ses.close(); + return 0; + } catch (HibernateException e) { + throw new LowlevelLedgerException(e); + } + } + + private double getHeldBalance(String book) throws LowlevelLedgerException { + try { + Session ses = factory.openSession(); + Query q = ses.createQuery("select sum(item.amount) from HHeldItem item where item.book = ? and item.amount<0 and item.held.expiryTime < ?"); + q.setString(0, book); + q.setTimestamp(1, new Date()); + Iterator iter = q.iterate(); + if (iter.hasNext()) { + final Object o = iter.next(); + if (o != null) { + ses.close(); + return ((Double) o).doubleValue(); + } + } + ses.close(); return 0; } catch (HibernateException e) { *************** *** 221,225 **** public double getAvailableBalance(String book) throws LowlevelLedgerException { ! return 0; } --- 302,306 ---- public double getAvailableBalance(String book) throws LowlevelLedgerException { ! return getHeldBalance(book) + getBalance(book); } *************** *** 231,235 **** */ public PostedHeldTransaction findHeldTransaction(String idstring) throws LowlevelLedgerException, UnknownTransactionException { ! return null; } --- 312,329 ---- */ public PostedHeldTransaction findHeldTransaction(String idstring) throws LowlevelLedgerException, UnknownTransactionException { ! try { ! Session ses = factory.openSession(); ! HHeld held = (HHeld) ses.get(HHeld.class, idstring); ! if (held == null) { ! ses.close(); ! return held.createPosted(); ! } ! ses.close(); ! throw new UnknownTransactionException(this, idstring); ! } catch (HibernateException e) { ! throw new LowlevelLedgerException(e); ! } catch (InvalidTransactionException e) { ! throw new LowlevelLedgerException(e); ! } } Index: HTransaction.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-ledger-hibernate/src/java/org/neuclear/ledger/hibernate/HTransaction.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** HTransaction.java 24 Mar 2004 12:33:10 -0000 1.2 --- HTransaction.java 24 Mar 2004 23:13:21 -0000 1.3 *************** *** 33,36 **** --- 33,44 ---- } + public HTransaction(HHeld held, Date transactionTime) { + this.id = held.getId(); + this.requestId = held.getRequestId(); + this.transactionTime = transactionTime; + this.comment = held.getComment(); + this.items = held.getItems(); + } + public String getId() { return id; |