KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ebank > ejb > account > AccountBean


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.account;
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 com.sun.ebank.ejb.exception.MissingPrimaryKeyException;
38 import com.sun.ebank.util.Debug;
39 import com.sun.ebank.util.CodedNames;
40 import com.sun.ebank.util.DBHelper;
41 import com.sun.ebank.util.AccountDetails;
42
43
44 public class AccountBean implements EntityBean {
45     private String JavaDoc accountId;
46     private String JavaDoc type;
47     private String JavaDoc description;
48     private BigDecimal balance;
49     private BigDecimal creditLine;
50     private BigDecimal beginBalance;
51     private java.util.Date JavaDoc beginBalanceTimeStamp;
52     private ArrayList customerIds;
53     private EntityContext context;
54     private Connection con;
55
56     // business methods
57
public AccountDetails getDetails() {
58         Debug.print("AccountBean getDetails");
59
60         try {
61             loadCustomerIds();
62         } catch (Exception JavaDoc ex) {
63             throw new EJBException("loadCustomerIds: " + ex.getMessage());
64         }
65
66         Debug.print(accountId);
67         Debug.print(description);
68
69         return new AccountDetails(accountId, type, description, balance,
70             creditLine, beginBalance, beginBalanceTimeStamp, customerIds);
71     }
72
73     public BigDecimal getBalance() {
74         Debug.print("AccountBean getBalance");
75
76         return balance;
77     }
78
79     public String JavaDoc getType() {
80         Debug.print("AccountBean getType");
81
82         return type;
83     }
84
85     public BigDecimal getCreditLine() {
86         Debug.print("AccountBean getCreditLine");
87
88         return creditLine;
89     }
90
91     public void setType(String JavaDoc type) {
92         Debug.print("AccountBean setType");
93         this.type = type;
94     }
95
96     public void setDescription(String JavaDoc description) {
97         Debug.print("AccountBean setDescription");
98         this.description = description;
99     }
100
101     public void setBalance(BigDecimal balance) {
102         Debug.print("AccountBean setBalance");
103         this.balance = balance;
104     }
105
106     public void setCreditLine(BigDecimal creditLine) {
107         Debug.print("AccountBean setCreditLine");
108         this.creditLine = creditLine;
109     }
110
111     public void setBeginBalance(BigDecimal beginBalance) {
112         Debug.print("AccountBean setBeginBalance");
113         this.beginBalance = beginBalance;
114     }
115
116     public void setBeginBalanceTimeStamp(java.util.Date JavaDoc beginBalanceTimeStamp) {
117         Debug.print("AccountBean setBeginBalanceTimeStamp");
118         this.beginBalanceTimeStamp = beginBalanceTimeStamp;
119     }
120
121     // ejb methods
122
public String JavaDoc ejbCreate(String JavaDoc accountId, String JavaDoc type, String JavaDoc description,
123         BigDecimal balance, BigDecimal creditLine, BigDecimal beginBalance,
124         java.util.Date JavaDoc beginBalanceTimeStamp, ArrayList customerIds)
125         throws CreateException, MissingPrimaryKeyException {
126         Debug.print("AccountBean ejbCreate");
127
128         if ((accountId == null) || (accountId.trim()
129                                                  .length() == 0)) {
130             throw new MissingPrimaryKeyException(
131                 "ejbCreate: accountId arg is null or empty");
132         }
133
134         this.accountId = accountId;
135         this.type = type;
136         this.description = description;
137         this.balance = balance;
138         this.creditLine = creditLine;
139         this.beginBalance = beginBalance;
140         this.beginBalanceTimeStamp = beginBalanceTimeStamp;
141         this.customerIds = customerIds;
142
143         try {
144             insertRow();
145         } catch (Exception JavaDoc ex) {
146             throw new EJBException("ejbCreate: " + ex.getMessage());
147         }
148
149         return accountId;
150     }
151
152     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc primaryKey)
153         throws FinderException {
154         Debug.print("AccountBean ejbFindByPrimaryKey");
155
156         boolean result;
157
158         try {
159             result = selectByPrimaryKey(primaryKey);
160         } catch (Exception JavaDoc ex) {
161             throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
162         }
163
164         if (result) {
165             return primaryKey;
166         } else {
167             throw new ObjectNotFoundException("Row for id " + primaryKey +
168                 " not found.");
169         }
170     }
171
172     public Collection ejbFindByCustomerId(String JavaDoc customerId)
173         throws FinderException {
174         Debug.print("AccountBean ejbFindByCustomerId");
175
176         Collection result;
177
178         try {
179             result = selectByCustomerId(customerId);
180         } catch (Exception JavaDoc ex) {
181             throw new EJBException("ejbFindByCustomerId " + ex.getMessage());
182         }
183
184         return result;
185     }
186
187     public void ejbRemove() {
188         Debug.print("AccountBean ejbRemove");
189
190         try {
191             deleteRow(accountId);
192         } catch (Exception JavaDoc ex) {
193             throw new EJBException("ejbRemove: " + ex.getMessage());
194         }
195     }
196
197     public void setEntityContext(EntityContext context) {
198         Debug.print("AccountBean setEntityContext");
199         this.context = context;
200         customerIds = new ArrayList();
201     }
202
203     public void unsetEntityContext() {
204         Debug.print("AccountBean unsetEntityContext");
205     }
206
207     public void ejbLoad() {
208         Debug.print("AccountBean ejbLoad");
209
210         try {
211             loadAccount();
212         } catch (Exception JavaDoc ex) {
213             throw new EJBException("ejbLoad: " + ex.getMessage());
214         }
215     }
216
217     public void ejbStore() {
218         Debug.print("AccountBean ejbStore");
219
220         try {
221             storeAccount();
222         } catch (Exception JavaDoc ex) {
223             throw new EJBException("ejbStore: " + ex.getMessage());
224         }
225     }
226
227     public void ejbActivate() {
228         Debug.print("AccountBean ejbActivate");
229         accountId = (String JavaDoc) context.getPrimaryKey();
230     }
231
232     public void ejbPassivate() {
233         Debug.print("AccountBean ejbPassivate");
234         accountId = null;
235     }
236
237     public void ejbPostCreate(String JavaDoc accountId, String JavaDoc type,
238         String JavaDoc description, BigDecimal balance, BigDecimal creditLine,
239         BigDecimal beginBalance, java.util.Date JavaDoc beginBalanceTimeStamp,
240         ArrayList customerIds) {
241     }
242
243     /*********************** Database Routines *************************/
244     private void makeConnection() {
245         Debug.print("AccountBean makeConnection");
246
247         try {
248             InitialContext ic = new InitialContext();
249             DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE);
250             con = ds.getConnection();
251         } catch (Exception JavaDoc ex) {
252             throw new EJBException("Unable to connect to database. " +
253                 ex.getMessage());
254         }
255     }
256      // makeConnection
257

258     private void releaseConnection() {
259         Debug.print("AccountBean releaseConnection");
260
261         try {
262             con.close();
263         } catch (SQLException ex) {
264             throw new EJBException("releaseConnection: " + ex.getMessage());
265         }
266     }
267      // releaseConnection
268

269     private void insertRow() throws SQLException {
270         Debug.print("AccountBean insertRow");
271
272         makeConnection();
273
274         String JavaDoc insertStatement =
275             "insert into account values ( ? , ? , ? , ? , ? , ? , ? )";
276         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
277
278         prepStmt.setString(1, accountId);
279         prepStmt.setString(2, type);
280         prepStmt.setString(3, description);
281         prepStmt.setBigDecimal(4, balance);
282         prepStmt.setBigDecimal(5, creditLine);
283         prepStmt.setBigDecimal(6, beginBalance);
284         prepStmt.setDate(7, DBHelper.toSQLDate(beginBalanceTimeStamp));
285
286         prepStmt.executeUpdate();
287         prepStmt.close();
288         releaseConnection();
289     }
290
291     private void deleteRow(String JavaDoc id) throws SQLException {
292         Debug.print("AccountBean deleteRow");
293
294         makeConnection();
295
296         String JavaDoc deleteStatement = "delete from account where account_id = ? ";
297         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
298
299         prepStmt.setString(1, id);
300         prepStmt.executeUpdate();
301         prepStmt.close();
302         releaseConnection();
303     }
304
305     private boolean selectByPrimaryKey(String JavaDoc primaryKey)
306         throws SQLException {
307         Debug.print("AccountBean selectByPrimaryKey");
308
309         makeConnection();
310
311         String JavaDoc selectStatement =
312             "select account_id " + "from account where account_id = ? ";
313         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
314         prepStmt.setString(1, primaryKey);
315
316         ResultSet rs = prepStmt.executeQuery();
317         boolean result = rs.next();
318         prepStmt.close();
319         releaseConnection();
320
321         return result;
322     }
323
324     private Collection selectByCustomerId(String JavaDoc customerId)
325         throws SQLException {
326         Debug.print("AccountBean selectByCustomerId");
327
328         makeConnection();
329
330         String JavaDoc selectStatement =
331             "select account_id " + "from customer_account_xref " +
332             "where customer_id = ? ";
333         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
334
335         prepStmt.setString(1, customerId);
336
337         ResultSet rs = prepStmt.executeQuery();
338         ArrayList a = new ArrayList();
339
340         while (rs.next()) {
341             a.add(rs.getString(1));
342         }
343
344         prepStmt.close();
345         releaseConnection();
346
347         return a;
348     }
349
350     private void loadAccount() throws SQLException {
351         Debug.print("AccountBean loadAccount");
352
353         makeConnection();
354
355         String JavaDoc selectStatement =
356             "select type, description, balance, credit_line, " +
357             "begin_balance, begin_balance_time_stamp " +
358             "from account where account_id = ? ";
359         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
360
361         prepStmt.setString(1, accountId);
362
363         ResultSet rs = prepStmt.executeQuery();
364
365         if (rs.next()) {
366             type = rs.getString(1);
367             description = rs.getString(2);
368             balance = rs.getBigDecimal(3);
369             creditLine = rs.getBigDecimal(4);
370             beginBalance = rs.getBigDecimal(5);
371             beginBalanceTimeStamp = rs.getDate(6);
372             prepStmt.close();
373             releaseConnection();
374         } else {
375             prepStmt.close();
376             releaseConnection();
377             throw new NoSuchEntityException("Row for id " + accountId +
378                 " not found in database.");
379         }
380     }
381
382     private void loadCustomerIds() throws SQLException {
383         Debug.print("AccountBean loadCustomerIds");
384
385         makeConnection();
386
387         String JavaDoc selectStatement =
388             "select customer_id " +
389             "from customer_account_xref where account_id = ? ";
390         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
391
392         prepStmt.setString(1, accountId);
393
394         ResultSet rs = prepStmt.executeQuery();
395         customerIds.clear();
396
397         while (rs.next()) {
398             customerIds.add(rs.getString(1));
399         }
400
401         prepStmt.close();
402         releaseConnection();
403     }
404
405     private void storeAccount() throws SQLException {
406         Debug.print("AccountBean storeAccount");
407
408         makeConnection();
409
410         String JavaDoc updateStatement =
411             "update account set type = ? , description = ? , " +
412             "balance = ? , credit_line = ? , " +
413             "begin_balance = ? , begin_balance_time_stamp = ? " +
414             "where account_id = ?";
415         PreparedStatement prepStmt = con.prepareStatement(updateStatement);
416
417         prepStmt.setString(1, type);
418         prepStmt.setString(2, description);
419         prepStmt.setBigDecimal(3, balance);
420         prepStmt.setBigDecimal(4, creditLine);
421         prepStmt.setBigDecimal(5, beginBalance);
422         prepStmt.setDate(6, DBHelper.toSQLDate(beginBalanceTimeStamp));
423         prepStmt.setString(7, accountId);
424
425         int rowCount = prepStmt.executeUpdate();
426         prepStmt.close();
427         releaseConnection();
428
429         if (rowCount == 0) {
430             throw new EJBException("Storing row for id " + accountId +
431                 " failed.");
432         }
433     }
434 }
435  // AccountBean
436
Popular Tags