KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.rmi.RemoteException JavaDoc;
38 import com.sun.ebank.ejb.customer.CustomerHome;
39 import com.sun.ebank.ejb.customer.Customer;
40 import com.sun.ebank.ejb.exception.MissingPrimaryKeyException;
41 import com.sun.ebank.ejb.exception.AccountNotFoundException;
42 import com.sun.ebank.ejb.exception.CustomerInAccountException;
43 import com.sun.ebank.ejb.exception.IllegalAccountTypeException;
44 import com.sun.ebank.ejb.exception.CustomerRequiredException;
45 import com.sun.ebank.ejb.exception.CustomerNotInAccountException;
46 import com.sun.ebank.ejb.exception.CustomerNotFoundException;
47 import com.sun.ebank.ejb.exception.InvalidParameterException;
48 import com.sun.ebank.util.Debug;
49 import com.sun.ebank.util.DomainUtil;
50 import com.sun.ebank.util.DBHelper;
51 import com.sun.ebank.util.AccountDetails;
52 import com.sun.ebank.util.EJBGetter;
53 import com.sun.ebank.util.CodedNames;
54
55
56 public class AccountControllerBean implements SessionBean {
57     private String JavaDoc accountId;
58     private AccountHome accountHome;
59     private Account account;
60     private Connection con;
61
62     public AccountControllerBean() {
63     }
64
65     // account creation and removal methods
66
public String JavaDoc createAccount(String JavaDoc customerId, String JavaDoc type,
67         String JavaDoc description, BigDecimal balance, BigDecimal creditLine,
68         BigDecimal beginBalance, java.util.Date JavaDoc beginBalanceTimeStamp)
69         throws IllegalAccountTypeException, CustomerNotFoundException,
70             InvalidParameterException {
71         // makes a new account and enters it into db,
72
// customer for customerId must exist 1st,
73
// returns accountId
74
Debug.print("AccountControllerBean createAccount");
75
76         if (customerId == null) {
77             throw new InvalidParameterException("null customerId");
78         }
79
80         if (type == null) {
81             throw new InvalidParameterException("null type");
82         }
83
84         if (description == null) {
85             throw new InvalidParameterException("null description");
86         }
87
88         if (beginBalanceTimeStamp == null) {
89             throw new InvalidParameterException("null beginBalanceTimeStamp");
90         }
91
92         DomainUtil.checkAccountType(type);
93
94         if (customerExists(customerId) == false) {
95             throw new CustomerNotFoundException(customerId);
96         }
97
98         ArrayList customerIds = new ArrayList();
99         customerIds.add(customerId);
100
101         try {
102             makeConnection();
103             accountId = DBHelper.getNextAccountId(con);
104             account =
105                 accountHome.create(accountId, type, description, balance,
106                     creditLine, beginBalance, beginBalanceTimeStamp, customerIds);
107             insertXref(customerId, accountId);
108             releaseConnection();
109         } catch (Exception JavaDoc ex) {
110             releaseConnection();
111             throw new EJBException("createAccount: " + ex.getMessage());
112         }
113
114         return accountId;
115     }
116      // createAccount
117

118     public void removeAccount(String JavaDoc accountId)
119         throws AccountNotFoundException, InvalidParameterException {
120         // removes account from db
121
Debug.print("AccountControllerBean removeAccount");
122
123         if (accountId == null) {
124             throw new InvalidParameterException("null accountId");
125         }
126
127         if (accountExists(accountId) == false) {
128             throw new AccountNotFoundException(accountId);
129         }
130
131         try {
132             makeConnection();
133             deleteAllAccountInXref(accountId);
134             releaseConnection();
135             account.remove();
136         } catch (Exception JavaDoc ex) {
137             releaseConnection();
138             throw new EJBException("removeAccount: " + ex.getMessage());
139         }
140     }
141      // removeAccount
142

143     // customer-account relationship methods
144
public void addCustomerToAccount(String JavaDoc customerId, String JavaDoc accountId)
145         throws AccountNotFoundException, CustomerNotFoundException,
146             CustomerInAccountException, InvalidParameterException {
147         // adds another customer to the account
148
Debug.print("AccountControllerBean addCustomerToAccount");
149
150         if (customerId == null) {
151             throw new InvalidParameterException("null customerId");
152         }
153
154         if (accountId == null) {
155             throw new InvalidParameterException("null accountId");
156         }
157
158         ArrayList customerIds;
159         CustomerHome customerHome;
160
161         if (customerExists(customerId) == false) {
162             throw new CustomerNotFoundException(customerId);
163         }
164
165         if (accountExists(accountId) == false) {
166             throw new AccountNotFoundException(accountId);
167         }
168
169         try {
170             makeConnection();
171             customerIds = getCustomerIds(accountId);
172         } catch (Exception JavaDoc ex) {
173             releaseConnection();
174             throw new EJBException("addCustomerToAccount: " + ex.getMessage());
175         }
176
177         if (customerIds.contains(customerId)) {
178             releaseConnection();
179             throw new CustomerInAccountException("customer " + customerId +
180                 " already assigned to account " + accountId);
181         }
182
183         try {
184             insertXref(customerId, accountId);
185             releaseConnection();
186         } catch (Exception JavaDoc ex) {
187             releaseConnection();
188             throw new EJBException("addCustomerToAccount: " + ex.getMessage());
189         }
190     }
191      // addCustomerToAccount
192

193     public void removeCustomerFromAccount(String JavaDoc customerId, String JavaDoc accountId)
194         throws AccountNotFoundException, CustomerRequiredException,
195             CustomerNotInAccountException, InvalidParameterException {
196         // removes a customer from this account, but
197
// the customer is not removed from the db
198
Debug.print("AccountControllerBean removeCustomerFromAccount");
199
200         ArrayList customerIds;
201
202         if (customerId == null) {
203             throw new InvalidParameterException("null customerId");
204         }
205
206         if (accountId == null) {
207             throw new InvalidParameterException("null accountId");
208         }
209
210         if (accountExists(accountId) == false) {
211             throw new AccountNotFoundException(accountId);
212         }
213
214         try {
215             makeConnection();
216             customerIds = getCustomerIds(accountId);
217         } catch (Exception JavaDoc ex) {
218             releaseConnection();
219             throw new EJBException("removeCustomerFromAccount: " +
220                 ex.getMessage());
221         }
222
223         if (customerIds.size() == 1) {
224             releaseConnection();
225             throw new CustomerRequiredException();
226         }
227
228         if (customerIds.contains(customerId) == false) {
229             releaseConnection();
230             throw new CustomerNotInAccountException("customer " + customerId +
231                 " not assigned to account " + accountId);
232         }
233
234         try {
235             deleteOneCustomerInXref(customerId, accountId);
236             releaseConnection();
237         } catch (Exception JavaDoc ex) {
238             releaseConnection();
239             throw new EJBException("removeCustomerFromAccount: " +
240                 ex.getMessage());
241         }
242     }
243      // removeCustomerFromAccount
244

245     // getters
246
public ArrayList getAccountsOfCustomer(String JavaDoc customerId)
247         throws AccountNotFoundException, InvalidParameterException {
248         // returns an ArrayList of AccountDetails
249
// that correspond to the accounts for the specified
250
// customer
251
Debug.print("AccountControllerBean getAccountsOfCustomer");
252
253         Collection accountIds;
254
255         if (customerId == null) {
256             throw new InvalidParameterException("null customerId");
257         }
258
259         try {
260             accountIds = accountHome.findByCustomerId(customerId);
261
262             if (accountIds.isEmpty()) {
263                 throw new AccountNotFoundException();
264             }
265         } catch (Exception JavaDoc ex) {
266             throw new AccountNotFoundException();
267         }
268
269         ArrayList accountList = new ArrayList();
270
271         Iterator i = accountIds.iterator();
272
273         while (i.hasNext()) {
274             Account account = (Account) i.next();
275             AccountDetails accountDetails = account.getDetails();
276             accountList.add(accountDetails);
277         }
278
279         return accountList;
280     }
281      // getAccountsOfCustomer
282

283     public AccountDetails getDetails(String JavaDoc accountId)
284         throws AccountNotFoundException, InvalidParameterException {
285         // returns the AccountDetails for the specified account
286
Debug.print("AccountControllerBean getDetails");
287
288         AccountDetails result;
289
290         if (accountId == null) {
291             throw new InvalidParameterException("null accountId");
292         }
293
294         if (accountExists(accountId) == false) {
295             throw new AccountNotFoundException(accountId);
296         }
297
298         result = account.getDetails();
299         Debug.print(result.getAccountId());
300
301         return result;
302     }
303      // getDetails
304

305     // setters
306
public void setType(String JavaDoc type, String JavaDoc accountId)
307         throws AccountNotFoundException, IllegalAccountTypeException,
308             InvalidParameterException {
309         Debug.print("AccountControllerBean setType");
310
311         if (type == null) {
312             throw new InvalidParameterException("null type");
313         }
314
315         if (accountId == null) {
316             throw new InvalidParameterException("null accountId");
317         }
318
319         DomainUtil.checkAccountType(type);
320
321         if (accountExists(accountId) == false) {
322             throw new AccountNotFoundException(accountId);
323         }
324
325         account.setType(type);
326     }
327      // setType
328

329     public void setDescription(String JavaDoc description, String JavaDoc accountId)
330         throws AccountNotFoundException, InvalidParameterException {
331         Debug.print("AccountControllerBean setDescription");
332
333         if (description == null) {
334             throw new InvalidParameterException("null description");
335         }
336
337         if (accountId == null) {
338             throw new InvalidParameterException("null accountId");
339         }
340
341         if (accountExists(accountId) == false) {
342             throw new AccountNotFoundException(accountId);
343         }
344     }
345      // setDescription
346

347     public void setBalance(BigDecimal balance, String JavaDoc accountId)
348         throws AccountNotFoundException, InvalidParameterException {
349         Debug.print("AccountControllerBean setBalance");
350
351         if (accountId == null) {
352             throw new InvalidParameterException("null accountId");
353         }
354
355         if (accountExists(accountId) == false) {
356             throw new AccountNotFoundException(accountId);
357         }
358
359         account.setBalance(balance);
360     }
361      // setBalance
362

363     public void setCreditLine(BigDecimal creditLine, String JavaDoc accountId)
364         throws EJBException, AccountNotFoundException,
365             InvalidParameterException {
366         Debug.print("AccountControllerBean setCreditLine");
367
368         if (accountId == null) {
369             throw new InvalidParameterException("null accountId");
370         }
371
372         if (accountExists(accountId) == false) {
373             throw new AccountNotFoundException(accountId);
374         }
375
376         account.setCreditLine(creditLine);
377     }
378      // setCreditLine
379

380     public void setBeginBalance(BigDecimal beginBalance, String JavaDoc accountId)
381         throws AccountNotFoundException, InvalidParameterException {
382         Debug.print("AccountControllerBean setBeginBalance");
383
384         if (accountId == null) {
385             throw new InvalidParameterException("null accountId");
386         }
387
388         if (accountExists(accountId) == false) {
389             throw new AccountNotFoundException(accountId);
390         }
391
392         account.setBeginBalance(beginBalance);
393     }
394      // setBeginBalance
395

396     public void setBeginBalanceTimeStamp(java.util.Date JavaDoc beginBalanceTimeStamp,
397         String JavaDoc accountId)
398         throws AccountNotFoundException, InvalidParameterException {
399         Debug.print("AccountControllerBean setBeginBalanceTimeStamp");
400
401         if (beginBalanceTimeStamp == null) {
402             throw new InvalidParameterException("null beginBalanceTimeStamp");
403         }
404
405         if (accountId == null) {
406             throw new InvalidParameterException("null accountId");
407         }
408
409         if (accountExists(accountId) == false) {
410             throw new AccountNotFoundException(accountId);
411         }
412
413         account.setBeginBalanceTimeStamp(beginBalanceTimeStamp);
414     }
415      // setBeginBalanceTimeStamp
416

417     // ejb methods
418
public void ejbCreate() {
419         Debug.print("AccountControllerBean ejbCreate");
420
421         try {
422             accountHome = EJBGetter.getAccountHome();
423         } catch (Exception JavaDoc ex) {
424             throw new EJBException("ejbCreate: " + ex.getMessage());
425         }
426
427         account = null;
428         accountId = null;
429     }
430      // ejbCreate
431

432     public void ejbRemove() {
433     }
434
435     public void ejbActivate() {
436     }
437
438     public void ejbPassivate() {
439     }
440
441     public void setSessionContext(SessionContext sc) {
442     }
443
444     // private methods
445
private boolean accountExists(String JavaDoc accountId) {
446         // If a business method has been invoked with
447
// a different accountId, then find the new
448
// accountId and update the accountId and account
449
// variables. Return false if the account
450
// cannot be found.
451
Debug.print("AccountControllerBean accountExists");
452
453         if (accountId.equals(this.accountId) == false) {
454             try {
455                 account = accountHome.findByPrimaryKey(accountId);
456                 this.accountId = accountId;
457             } catch (Exception JavaDoc ex) {
458                 return false;
459             }
460         }
461          // if
462

463         return true;
464     }
465      // accountExists
466

467     private boolean customerExists(String JavaDoc customerId) {
468         Debug.print("AccountControllerBean customerExists");
469
470         CustomerHome customerHome;
471
472         try {
473             customerHome = EJBGetter.getCustomerHome();
474         } catch (Exception JavaDoc ex) {
475             throw new EJBException("customerExists: " + ex.getMessage());
476         }
477
478         try {
479             customerHome.findByPrimaryKey(customerId);
480
481             return true;
482         } catch (Exception JavaDoc ex) {
483             return false;
484         }
485     }
486      // customerExists
487

488     /*********************** Database Routines *************************/
489     private void makeConnection() {
490         Debug.print("AccountControllerBean makeConnection");
491
492         try {
493             InitialContext ic = new InitialContext();
494             DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE);
495             con = ds.getConnection();
496         } catch (Exception JavaDoc ex) {
497             throw new EJBException("Unable to connect to database. " +
498                 ex.getMessage());
499         }
500     }
501      // makeConnection
502

503     private void releaseConnection() {
504         Debug.print("AccountControllerBean releaseConnection");
505
506         try {
507             con.close();
508         } catch (SQLException ex) {
509             throw new EJBException("releaseConnection: " + ex.getMessage());
510         }
511     }
512      // releaseConnection
513

514     private void insertXref(String JavaDoc customerId, String JavaDoc accountId)
515         throws SQLException {
516         Debug.print("AccountControllerBean insertXref");
517
518         String JavaDoc insertStatement =
519             "insert into customer_account_xref " + "values ( ? , ? )";
520         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
521
522         prepStmt.setString(1, customerId);
523         prepStmt.setString(2, accountId);
524         prepStmt.executeUpdate();
525         prepStmt.close();
526     }
527
528     private void deleteAllAccountInXref(String JavaDoc accountId)
529         throws SQLException {
530         Debug.print("AccountControllerBean deleteAllAccountInXref");
531
532         String JavaDoc deleteStatement =
533             "delete from customer_account_xref " + "where account_id = ? ";
534         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
535
536         prepStmt.setString(1, accountId);
537         prepStmt.executeUpdate();
538         prepStmt.close();
539     }
540
541     private ArrayList getCustomerIds(String JavaDoc accountId)
542         throws SQLException {
543         Debug.print("AccountControllerBean getCustomerIds");
544
545         String JavaDoc selectStatement =
546             "select customer_id " + "from customer_account_xref " +
547             "where account_id = ? ";
548         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
549
550         prepStmt.setString(1, accountId);
551
552         ResultSet rs = prepStmt.executeQuery();
553         ArrayList a = new ArrayList();
554
555         while (rs.next()) {
556             a.add(rs.getString(1));
557         }
558
559         prepStmt.close();
560
561         return a;
562     }
563      // getCustomerIds
564

565     private void deleteOneCustomerInXref(String JavaDoc customerId, String JavaDoc accountId)
566         throws SQLException {
567         Debug.print("AccountControllerBean deleteOneCustomerInXref");
568
569         String JavaDoc deleteStatement =
570             "delete from customer_account_xref " +
571             "where account_id = ? and customer_id = ? ";
572         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
573
574         prepStmt.setString(1, accountId);
575         prepStmt.setString(2, customerId);
576         prepStmt.executeUpdate();
577         prepStmt.close();
578     }
579 }
580  // AccountControllerBean
581
Popular Tags