1 package org.neuclear.ledger.prevalent;
2
3 import org.prevayler.TransactionWithQuery;
4 import org.neuclear.ledger.*;
5
6 import java.util.Date;
7 import java.util.Iterator;
8
9 /***
10 * Created by IntelliJ IDEA.
11 * User: pelleb
12 * Date: Mar 20, 2004
13 * Time: 1:31:57 PM
14 * To change this template use File | Settings | File Templates.
15 */
16 public class PostVerifiedTransfer implements TransactionWithQuery{
17
18 final UnPostedTransaction tran;
19
20
21 PostVerifiedTransfer(UnPostedTransaction tran) {
22 this.tran = tran;
23 }
24
25 /***
26 * Performs the necessary modifications on the given prevalentSystem and also returns an Object or throws an Exception.
27 * This method is called by Prevayler.execute(TransactionWithQuery) to execute this TransactionWithQuery on the given Prevalent System. See org.prevayler.demos for usage examples.
28 *
29 * @param prevalentSystem The system on which this TransactionWithQuery will execute.
30 * @param executionTime The time at which this TransactionWithQuery is being executed. Every Transaction executes completely within a single moment in time. Logically, a Prevalent System's time does not pass during the execution of a Transaction.
31 */
32 public Object executeAndQuery(Object prevalentSystem, Date executionTime) throws Exception {
33 LedgerSystem system=(LedgerSystem) prevalentSystem;
34 TransactionTable table=system.getTransactionTable();
35 if (table.exists(tran.getId()))
36 throw new TransactionExistsException(null,tran.getId());
37 if (table.exists(tran.getRequestId()))
38 throw new TransactionExistsException(null,tran.getRequestId());
39
40 Iterator iter=tran.getItems();
41 // First lets check the balances
42 while (iter.hasNext()) {
43 TransactionItem item = (TransactionItem) iter.next();
44 if (system.getAvailableBalance(item.getBook())+item.getAmount()<0)
45 throw new InsufficientFundsException(null,item.getBook(),item.getAmount());
46 }
47 // We're ok so lets register them.
48 table.register(tran.getId(),executionTime);
49 table.register(tran.getRequestId(),executionTime);
50
51 // Now modify the balances
52 iter=tran.getItems();
53 while (iter.hasNext()) {
54 TransactionItem item = (TransactionItem) iter.next();
55 system.getBalanceTable().add(item.getBook(),item.getAmount());
56 }
57
58 return new PostedTransaction(tran,executionTime);
59 }
60 }
This page was automatically generated by Maven