KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ebank > ejb > tx > TxControllerBean


1 /*
2  * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2004 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28
29 package com.sun.ebank.ejb.tx;
30
31 import java.sql.*;
32 import javax.sql.*;
33 import java.util.*;
34 import java.math.*;
35 import javax.ejb.*;
36 import javax.naming.*;
37 import java.rmi.RemoteException JavaDoc;
38 import com.sun.ebank.ejb.account.AccountHome;
39 import com.sun.ebank.ejb.account.Account;
40 import com.sun.ebank.ejb.exception.TxNotFoundException;
41 import com.sun.ebank.ejb.exception.AccountNotFoundException;
42 import com.sun.ebank.ejb.exception.InsufficientFundsException;
43 import com.sun.ebank.ejb.exception.InsufficientCreditException;
44 import com.sun.ebank.ejb.exception.IllegalAccountTypeException;
45 import com.sun.ebank.ejb.exception.InvalidParameterException;
46 import com.sun.ebank.util.Debug;
47 import com.sun.ebank.util.TxDetails;
48 import com.sun.ebank.util.DBHelper;
49 import com.sun.ebank.util.EJBGetter;
50 import com.sun.ebank.util.CodedNames;
51 import com.sun.ebank.util.DomainUtil;
52
53
54 public class TxControllerBean implements SessionBean {
55     private TxHome txHome;
56     private AccountHome accountHome;
57     private Connection con;
58     private SessionContext context;
59     private BigDecimal bigZero = new BigDecimal("0.00");
60
61     public TxControllerBean() {
62     }
63
64     public ArrayList getTxsOfAccount(java.util.Date JavaDoc startDate,
65         java.util.Date JavaDoc endDate, String JavaDoc accountId)
66         throws InvalidParameterException {
67         Debug.print("TxControllerBean getTxsOfAccount");
68
69         Collection txIds;
70         ArrayList txList = new ArrayList();
71
72         if (startDate == null) {
73             throw new InvalidParameterException("null startDate");
74         }
75
76         if (endDate == null) {
77             throw new InvalidParameterException("null endDate");
78         }
79
80         if (accountId == null) {
81             throw new InvalidParameterException("null accountId");
82         }
83
84         try {
85             txIds = txHome.findByAccountId(startDate, endDate, accountId);
86         } catch (Exception JavaDoc ex) {
87             return txList;
88         }
89
90         Iterator i = txIds.iterator();
91
92         while (i.hasNext()) {
93             Tx tx = (Tx) i.next();
94             TxDetails txDetail = tx.getDetails();
95             txList.add(txDetail);
96         }
97
98         return txList;
99     }
100      // getTxsOfAccount
101

102     public TxDetails getDetails(String JavaDoc txId)
103         throws TxNotFoundException, InvalidParameterException {
104         Debug.print("TxControllerBean getDetails");
105
106         if (txId == null) {
107             throw new InvalidParameterException("null txId");
108         }
109
110         try {
111             Tx tx = txHome.findByPrimaryKey(txId);
112
113             return tx.getDetails();
114         } catch (Exception JavaDoc ex) {
115             throw new TxNotFoundException(txId);
116         }
117     }
118      // getDetails
119

120     public void withdraw(BigDecimal amount, String JavaDoc description, String JavaDoc accountId)
121         throws InvalidParameterException, AccountNotFoundException,
122             IllegalAccountTypeException, InsufficientFundsException {
123         Debug.print("TxControllerBean withdraw");
124
125         Account account = checkAccountArgs(amount, description, accountId);
126
127         String JavaDoc type = account.getType();
128
129         if (DomainUtil.isCreditAccount(type)) {
130             throw new IllegalAccountTypeException(type);
131         }
132
133         BigDecimal newBalance = account.getBalance()
134                                        .subtract(amount);
135
136         if (newBalance.compareTo(bigZero) == -1) {
137             throw new InsufficientFundsException();
138         }
139
140         executeTx(amount.negate(), description, accountId, newBalance, account);
141     }
142      // withdraw
143

144     public void deposit(BigDecimal amount, String JavaDoc description, String JavaDoc accountId)
145         throws InvalidParameterException, AccountNotFoundException,
146             IllegalAccountTypeException {
147         Debug.print("TxControllerBean deposit");
148
149         Account account = checkAccountArgs(amount, description, accountId);
150
151         String JavaDoc type = account.getType();
152
153         if (DomainUtil.isCreditAccount(type)) {
154             throw new IllegalAccountTypeException(type);
155         }
156
157         BigDecimal newBalance = account.getBalance()
158                                        .add(amount);
159         executeTx(amount, description, accountId, newBalance, account);
160     }
161      // deposit
162

163     public void makeCharge(BigDecimal amount, String JavaDoc description,
164         String JavaDoc accountId)
165         throws InvalidParameterException, AccountNotFoundException,
166             IllegalAccountTypeException, InsufficientCreditException {
167         Debug.print("TxControllerBean charge");
168
169         Account account = checkAccountArgs(amount, description, accountId);
170
171         String JavaDoc type = account.getType();
172
173         if (DomainUtil.isCreditAccount(type) == false) {
174             throw new IllegalAccountTypeException(type);
175         }
176
177         BigDecimal newBalance = account.getBalance()
178                                        .add(amount);
179
180         if (newBalance.compareTo(account.getCreditLine()) == 1) {
181             throw new InsufficientCreditException();
182         }
183
184         executeTx(amount, description, accountId, newBalance, account);
185     }
186      // charge
187

188     public void makePayment(BigDecimal amount, String JavaDoc description,
189         String JavaDoc accountId)
190         throws InvalidParameterException, AccountNotFoundException,
191             IllegalAccountTypeException {
192         Debug.print("TxControllerBean makePayment");
193
194         Account account = checkAccountArgs(amount, description, accountId);
195
196         String JavaDoc type = account.getType();
197
198         if (DomainUtil.isCreditAccount(type) == false) {
199             throw new IllegalAccountTypeException(type);
200         }
201
202         BigDecimal newBalance = account.getBalance()
203                                        .subtract(amount);
204         executeTx(amount.negate(), description, accountId, newBalance, account);
205     }
206      // makePayment
207

208     public void transferFunds(BigDecimal amount, String JavaDoc description,
209         String JavaDoc fromAccountId, String JavaDoc toAccountId)
210         throws InvalidParameterException, AccountNotFoundException,
211             InsufficientFundsException, InsufficientCreditException {
212         Account fromAccount =
213             checkAccountArgs(amount, description, fromAccountId);
214         Account toAccount = checkAccountArgs(amount, description, toAccountId);
215
216         String JavaDoc fromType = fromAccount.getType();
217         BigDecimal fromBalance = fromAccount.getBalance();
218
219         if (DomainUtil.isCreditAccount(fromType)) {
220             BigDecimal fromNewBalance = fromBalance.add(amount);
221
222             if (fromNewBalance.compareTo(fromAccount.getCreditLine()) == 1) {
223                 throw new InsufficientCreditException();
224             }
225
226             executeTx(amount, description, fromAccountId, fromNewBalance,
227                 fromAccount);
228         } else {
229             BigDecimal fromNewBalance = fromBalance.subtract(amount);
230
231             if (fromNewBalance.compareTo(bigZero) == -1) {
232                 throw new InsufficientFundsException();
233             }
234
235             executeTx(amount.negate(), description, fromAccountId,
236                 fromNewBalance, fromAccount);
237         }
238
239         String JavaDoc toType = toAccount.getType();
240         BigDecimal toBalance = toAccount.getBalance();
241
242         if (DomainUtil.isCreditAccount(toType)) {
243             BigDecimal toNewBalance = toBalance.subtract(amount);
244             executeTx(amount.negate(), description, toAccountId, toNewBalance,
245                 toAccount);
246         } else {
247             BigDecimal toNewBalance = toBalance.add(amount);
248             executeTx(amount, description, toAccountId, toNewBalance, toAccount);
249         }
250     }
251      // transferFunds
252

253     // private methods
254
private void executeTx(BigDecimal amount, String JavaDoc description,
255         String JavaDoc accountId, BigDecimal newBalance, Account account) {
256         Debug.print("TxControllerBean executeTx");
257
258         try {
259             makeConnection();
260
261             String JavaDoc txId = DBHelper.getNextTxId(con);
262             account.setBalance(newBalance);
263
264             Tx tx =
265                 txHome.create(txId, accountId, new java.util.Date JavaDoc(), amount,
266                     newBalance, description);
267         } catch (Exception JavaDoc ex) {
268             throw new EJBException("executeTx: " + ex.getMessage());
269         } finally {
270             releaseConnection();
271         }
272     }
273      // executeTx
274

275     private Account checkAccountArgs(BigDecimal amount, String JavaDoc description,
276         String JavaDoc accountId)
277         throws InvalidParameterException, AccountNotFoundException {
278         Account account = null;
279
280         if (description == null) {
281             throw new InvalidParameterException("null description");
282         }
283
284         if (accountId == null) {
285             throw new InvalidParameterException("null accountId");
286         }
287
288         if (amount.compareTo(bigZero) != 1) {
289             throw new InvalidParameterException("amount <= 0");
290         }
291
292         try {
293             account = accountHome.findByPrimaryKey(accountId);
294         } catch (Exception JavaDoc ex) {
295             throw new AccountNotFoundException(accountId);
296         }
297
298         return account;
299     }
300      // checkAccountArgs
301

302     // ejb methods
303
public void ejbCreate() {
304         Debug.print("TxControllerBean ejbCreate");
305
306         try {
307             txHome = EJBGetter.getTxHome();
308             accountHome = EJBGetter.getAccountHome();
309         } catch (Exception JavaDoc ex) {
310             throw new EJBException("ejbCreate: " + ex.getMessage());
311         }
312     }
313      // ejbCreate
314

315     public void setSessionContext(SessionContext context) {
316         this.context = context;
317     }
318
319     public void ejbRemove() {
320     }
321
322     public void ejbActivate() {
323     }
324
325     public void ejbPassivate() {
326     }
327
328     /*********************** Database Routines *************************/
329     private void makeConnection() {
330         Debug.print("TxControllerBean makeConnection");
331
332         try {
333             InitialContext ic = new InitialContext();
334             DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE);
335             con = ds.getConnection();
336         } catch (Exception JavaDoc ex) {
337             throw new EJBException("Unable to connect to database. " +
338                 ex.getMessage());
339         }
340     }
341      // makeConnection
342

343     private void releaseConnection() {
344         Debug.print("TxControllerBean releaseConnection");
345
346         try {
347             con.close();
348         } catch (SQLException ex) {
349             throw new EJBException("releaseConnection: " + ex.getMessage());
350         }
351     }
352      // releaseConnection
353
}
354  // TxControllerBean
355
Popular Tags