1 package org.neuclear.ledger.prevalent;
2
3 import org.neuclear.ledger.*;
4 import org.prevayler.PrevaylerFactory;
5 import org.prevayler.Prevayler;
6
7 import java.util.Date;
8 import java.io.IOException;
9 import java.io.Serializable;
10
11 /***
12 * Created by IntelliJ IDEA.
13 * User: pelleb
14 * Date: Mar 20, 2004
15 * Time: 1:03:54 PM
16 * To change this template use File | Settings | File Templates.
17 */
18 public class PrevalentLedger extends Ledger implements Serializable {
19 private final LedgerSystem system;
20
21 public PrevalentLedger(final String id, final String basedir) throws IOException, ClassNotFoundException {
22 super(id);
23 prevayler = PrevaylerFactory.createPrevayler(new LedgerSystem(id), basedir);
24 system=(LedgerSystem) prevayler.prevalentSystem();
25
26 }
27
28 /***
29 * The basic interface for creating Transactions in the database.
30 * The implementing class takes this transacion information and stores it with an automatically generated uniqueid.
31 * This id is returned as an identifier of the transaction.
32 *
33 * @param trans Transaction to perform
34 * @return Unique ID
35 */
36 public PostedTransaction performTransaction(UnPostedTransaction trans) throws UnBalancedTransactionException, LowlevelLedgerException, InvalidTransactionException {
37 try {
38 return (PostedTransaction) prevayler.execute(new PostTransaction(trans));
39 } catch (Exception e) {
40 if (e instanceof InvalidTransactionException)
41 throw (InvalidTransactionException)e;
42 if (e instanceof UnBalancedTransactionException)
43 throw (UnBalancedTransactionException)e;
44 if (e instanceof LowlevelLedgerException)
45 throw (LowlevelLedgerException)e;
46 throw new LowlevelLedgerException(e);
47 }
48 }
49
50 /***
51 * Similar to a transaction but guarantees that there wont be any negative balances left after the transaction.
52 *
53 * @param trans Transaction to perform
54 * @return The reference to the transaction
55 */
56 public PostedTransaction performVerifiedTransfer(UnPostedTransaction trans) throws UnBalancedTransactionException, LowlevelLedgerException, InvalidTransactionException {
57 try {
58 return (PostedTransaction) prevayler.execute(new PostVerifiedTransfer(trans));
59 } catch (Exception e) {
60 if (e instanceof InvalidTransactionException)
61 throw (InvalidTransactionException)e;
62 if (e instanceof UnBalancedTransactionException)
63 throw (UnBalancedTransactionException)e;
64 if (e instanceof LowlevelLedgerException)
65 throw (LowlevelLedgerException)e;
66 throw new LowlevelLedgerException(e);
67 }
68 }
69
70 /***
71 * The basic interface for creating Transactions in the database.
72 * The implementing class takes this transacion information and stores it with an automatically generated uniqueid.
73 * This id is returned as an identifier of the transaction.
74 *
75 * @param trans Transaction to perform
76 * @return Unique ID
77 */
78 public PostedHeldTransaction performHeldTransfer(UnPostedHeldTransaction trans) throws UnBalancedTransactionException, LowlevelLedgerException, InvalidTransactionException {
79 try {
80 return (PostedHeldTransaction) prevayler.execute(new PostHeldTransaction(trans));
81 } catch (Exception e) {
82 if (e instanceof InvalidTransactionException)
83 throw (InvalidTransactionException)e;
84 if (e instanceof UnBalancedTransactionException)
85 throw (UnBalancedTransactionException)e;
86 if (e instanceof LowlevelLedgerException)
87 throw (LowlevelLedgerException)e;
88 throw new LowlevelLedgerException(e);
89 }
90 }
91
92 /***
93 * Searches for a Transaction based on its Transaction ID
94 *
95 * @param id A valid ID
96 * @return The Transaction object
97 */
98 public Date getTransactionTime(String id) throws LowlevelLedgerException, UnknownTransactionException, InvalidTransactionException, UnknownBookException {
99 if (system.getTransactionTable().exists(id))
100 return new Date(system.getTransactionTable().getTransactionTime(id));
101 throw new UnknownTransactionException(this,id);
102 }
103
104 /***
105 * Calculate the true accounting balance at a given time. This does not take into account any held transactions, thus may not necessarily
106 * show the Available balance.<p>
107 * Example sql for implementors: <pre>
108 * select c.credit - d.debit from
109 * (
110 * select sum(amount) as credit
111 * from ledger
112 * where transactiondate <= sysdate and end_date is null and credit= 'neu://bob'
113 * ) c,
114 * (
115 * select sum(amount) as debit
116 * from ledger
117 * where transactiondate <= sysdate and end_date is null and debit= 'neu://bob'
118 * ) d
119 * <p/>
120 * </pre>
121 *
122 * @return the balance as a double
123 */
124
125 public double getBalance(String book) throws LowlevelLedgerException {
126 try {
127 return ((Double)prevayler.execute(new GetBalanceQuery(book))).doubleValue();
128 } catch (Exception e) {
129 throw new LowlevelLedgerException(e);
130 }
131 }
132
133 /***
134 * Calculate the available balance at a given time. This DOES take into account any held transactions.
135 * Example sql for implementors: <pre>
136 * select c.credit - d.debit from
137 * (
138 * select sum(amount) as credit
139 * from ledger
140 * where transactiondate <= sysdate and (end_date is null or end_date>= sysdate) and credit= 'neu://bob'
141 * ) c,
142 * (
143 * select sum(amount) as debit
144 * from ledger
145 * where transactiondate <= sysdate and end_date is null and debit= 'neu://bob'
146 * ) d
147 * <p/>
148 * </pre>
149 *
150 * @return the balance as a double
151 */
152
153 public double getAvailableBalance(String book) throws LowlevelLedgerException {
154 try {
155 return ((Double)prevayler.execute(new GetAvailableBalanceQuery(book))).doubleValue();
156 } catch (Exception e) {
157 throw new LowlevelLedgerException(e);
158 }
159 }
160
161 /***
162 * Searches for a Held Transaction based on its Transaction ID
163 *
164 * @param idstring A valid ID
165 * @return The Transaction object
166 */
167 public PostedHeldTransaction findHeldTransaction(String idstring) throws LowlevelLedgerException, UnknownTransactionException {
168 return system.getHoldTable().get(idstring); //To change body of implemented methods use File | Settings | File Templates.
169 }
170
171 /***
172 * Cancels a Held Transaction.
173 *
174 * @param hold
175 * @throws org.neuclear.ledger.LowlevelLedgerException
176 *
177 * @throws org.neuclear.ledger.UnknownTransactionException
178 *
179 */
180 public void performCancelHold(PostedHeldTransaction hold) throws LowlevelLedgerException, UnknownTransactionException {
181 try {
182 System.out.println("Perform Cancel");
183
184 prevayler.execute(new CancelHeldTransaction(hold));
185 } catch (Exception e) {
186 if (e instanceof UnknownTransactionException)
187 throw (UnknownTransactionException)e;
188 if (e instanceof LowlevelLedgerException)
189 throw (LowlevelLedgerException)e;
190 throw new LowlevelLedgerException(e);
191 }
192 }
193
194 public PostedTransaction performCompleteHold(PostedHeldTransaction hold, double amount, String comment) throws InvalidTransactionException, LowlevelLedgerException, TransactionExpiredException {
195 try {
196 return (PostedTransaction) prevayler.execute(new CompleteHeldTransaction(hold,amount,comment));
197 } catch (Exception e) {
198 if (e instanceof InvalidTransactionException)
199 throw (InvalidTransactionException)e;
200 if (e instanceof TransactionExpiredException)
201 throw (TransactionExpiredException)e;
202 if (e instanceof LowlevelLedgerException)
203 throw (LowlevelLedgerException)e;
204
205 throw new LowlevelLedgerException(e);
206 }
207 }
208
209 public void close() {
210 try {
211 prevayler.takeSnapshot();
212 prevayler.close();
213 } catch (IOException e) {
214 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
215 }
216 }
217
218 private Prevayler prevayler;
219 }
This page was automatically generated by Maven