KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ebank > ejb > customer > CustomerControllerBean


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.customer;
30
31 import java.sql.*;
32 import javax.sql.*;
33 import java.util.*;
34 import javax.ejb.*;
35 import javax.naming.*;
36 import java.rmi.RemoteException JavaDoc;
37 import com.sun.ebank.ejb.exception.CustomerNotFoundException;
38 import com.sun.ebank.ejb.exception.InvalidParameterException;
39 import com.sun.ebank.util.Debug;
40 import com.sun.ebank.util.DBHelper;
41 import com.sun.ebank.util.CustomerDetails;
42 import com.sun.ebank.util.EJBGetter;
43 import com.sun.ebank.util.CodedNames;
44
45
46 public class CustomerControllerBean implements SessionBean {
47     private String JavaDoc customerId;
48     private CustomerHome customerHome;
49     private Customer customer;
50     private Connection con;
51
52     public CustomerControllerBean() {
53     }
54
55     // customer creation and removal methods
56
public String JavaDoc createCustomer(String JavaDoc lastName, String JavaDoc firstName,
57         String JavaDoc middleInitial, String JavaDoc street, String JavaDoc city, String JavaDoc state,
58         String JavaDoc zip, String JavaDoc phone, String JavaDoc email)
59         throws InvalidParameterException {
60         // makes a new customer and enters it into db,
61
// returns customerId
62
Debug.print("CustomerControllerBean createCustomer");
63
64         if (lastName == null) {
65             throw new InvalidParameterException("null lastName");
66         }
67
68         if (firstName == null) {
69             throw new InvalidParameterException("null firstName");
70         }
71
72         try {
73             makeConnection();
74             customerId = DBHelper.getNextCustomerId(con);
75             customer =
76                 customerHome.create(customerId, lastName, firstName,
77                     middleInitial, street, city, state, zip, phone, email);
78             releaseConnection();
79         } catch (Exception JavaDoc ex) {
80             releaseConnection();
81             throw new EJBException("createCustomer: " + ex.getMessage());
82         }
83
84         return customerId;
85     }
86      // createCustomer
87

88     public void removeCustomer(String JavaDoc customerId)
89         throws CustomerNotFoundException, InvalidParameterException {
90         // removes customer from db
91
Debug.print("CustomerControllerBean removeCustomer");
92
93         if (customerId == null) {
94             throw new InvalidParameterException("null customerId");
95         }
96
97         if (customerExists(customerId) == false) {
98             throw new CustomerNotFoundException(customerId);
99         }
100
101         try {
102             makeConnection();
103             deleteAllCustomerInXref(customerId);
104             customer.remove();
105             releaseConnection();
106         } catch (Exception JavaDoc ex) {
107             releaseConnection();
108             throw new EJBException("removeCustomer: " + ex.getMessage());
109         }
110     }
111      // removeCustomer
112

113     // getters
114
public ArrayList getCustomersOfAccount(String JavaDoc accountId)
115         throws CustomerNotFoundException, InvalidParameterException {
116         // returns an ArrayList of CustomerDetails
117
// that correspond to the accountId specified
118
Debug.print("CustomerControllerBean getCustomersOfAccount");
119
120         Collection customerIds;
121
122         if (accountId == null) {
123             throw new InvalidParameterException("null accountId");
124         }
125
126         try {
127             customerIds = customerHome.findByAccountId(accountId);
128
129             if (customerIds.isEmpty()) {
130                 throw new CustomerNotFoundException();
131             }
132         } catch (Exception JavaDoc ex) {
133             throw new CustomerNotFoundException();
134         }
135
136         ArrayList customerList = new ArrayList();
137
138         Iterator i = customerIds.iterator();
139
140         while (i.hasNext()) {
141             Customer customer = (Customer) i.next();
142             CustomerDetails customerDetail = customer.getDetails();
143             customerList.add(customerDetail);
144         }
145
146         return customerList;
147     }
148      // getCustomersOfAccount
149

150     public CustomerDetails getDetails(String JavaDoc customerId)
151         throws CustomerNotFoundException, InvalidParameterException {
152         // returns the CustomerDetails for the specified customer
153
Debug.print("CustomerControllerBean getDetails");
154
155         CustomerDetails result;
156
157         if (customerId == null) {
158             throw new InvalidParameterException("null customerId");
159         }
160
161         if (customerExists(customerId) == false) {
162             throw new CustomerNotFoundException(customerId);
163         }
164
165         result = customer.getDetails();
166
167         return result;
168     }
169      // getDetails
170

171     public ArrayList getCustomersOfLastName(String JavaDoc lastName)
172         throws InvalidParameterException {
173         // returns an ArrayList of CustomerDetails
174
// that correspond to the the lastName specified
175
// returns null if no customers are found
176
Debug.print("CustomerControllerBean getCustomersOfCustomer");
177
178         Collection customerIds;
179         ArrayList customerList = new ArrayList();
180
181         if (lastName == null) {
182             throw new InvalidParameterException("null lastName");
183         }
184
185         try {
186             customerIds = customerHome.findByLastName(lastName);
187         } catch (Exception JavaDoc ex) {
188             return customerList;
189         }
190
191         Iterator i = customerIds.iterator();
192
193         while (i.hasNext()) {
194             Customer customer = (Customer) i.next();
195             CustomerDetails customerDetail = customer.getDetails();
196             customerList.add(customerDetail);
197         }
198
199         return customerList;
200     }
201      // getCustomersOfLastName
202

203     // setters
204
public void setName(String JavaDoc lastName, String JavaDoc firstName,
205         String JavaDoc middleInitial, String JavaDoc customerId)
206         throws CustomerNotFoundException, InvalidParameterException {
207         Debug.print("CustomerControllerBean setName");
208
209         if (lastName == null) {
210             throw new InvalidParameterException("null lastName");
211         }
212
213         if (firstName == null) {
214             throw new InvalidParameterException("null firstName");
215         }
216
217         if (customerId == null) {
218             throw new InvalidParameterException("null customerId");
219         }
220
221         if (customerExists(customerId) == false) {
222             throw new CustomerNotFoundException(customerId);
223         }
224
225         try {
226             customer.setLastName(lastName);
227             customer.setFirstName(firstName);
228             customer.setMiddleInitial(middleInitial);
229         } catch (Exception JavaDoc ex) {
230             throw new EJBException(ex.getMessage());
231         }
232     }
233      // setName
234

235     public void setAddress(String JavaDoc street, String JavaDoc city, String JavaDoc state,
236         String JavaDoc zip, String JavaDoc phone, String JavaDoc email, String JavaDoc customerId)
237         throws CustomerNotFoundException, InvalidParameterException {
238         Debug.print("CustomerControllerBean setAddress");
239
240         if (street == null) {
241             throw new InvalidParameterException("null street");
242         }
243
244         if (city == null) {
245             throw new InvalidParameterException("null city");
246         }
247
248         if (state == null) {
249             throw new InvalidParameterException("null state");
250         }
251
252         if (customerId == null) {
253             throw new InvalidParameterException("null customerId");
254         }
255
256         if (customerExists(customerId) == false) {
257             throw new CustomerNotFoundException(customerId);
258         }
259
260         try {
261             customer.setStreet(street);
262             customer.setCity(city);
263             customer.setState(state);
264             customer.setZip(zip);
265             customer.setPhone(phone);
266             customer.setEmail(email);
267         } catch (Exception JavaDoc ex) {
268             throw new EJBException(ex.getMessage());
269         }
270     }
271      // setAddress
272

273     // ejb methods
274
public void ejbCreate() {
275         Debug.print("CustomerControllerBean ejbCreate");
276
277         try {
278             customerHome = EJBGetter.getCustomerHome();
279         } catch (Exception JavaDoc ex) {
280             Debug.print("CustomerControllerBean catch");
281             throw new EJBException("ejbCreate: " + ex.getMessage());
282         }
283
284         customer = null;
285         customerId = null;
286     }
287      // ejbCreate
288

289     public void ejbRemove() {
290     }
291
292     public void ejbActivate() {
293     }
294
295     public void ejbPassivate() {
296     }
297
298     public void setSessionContext(SessionContext sc) {
299     }
300
301     // private methods
302
private boolean customerExists(String JavaDoc customerId) {
303         // If a business method has been invoked with
304
// a different customerId, then update the
305
// customerId and customer variables.
306
// Return null if the customer is not found.
307
Debug.print("CustomerControllerBean customerExists");
308
309         if (customerId.equals(this.customerId) == false) {
310             try {
311                 customer = customerHome.findByPrimaryKey(customerId);
312                 this.customerId = customerId;
313             } catch (Exception JavaDoc ex) {
314                 return false;
315             }
316         }
317          // if
318

319         return true;
320     }
321      // customerExists
322

323     /*********************** Database Routines *************************/
324     private void makeConnection() {
325         Debug.print("CustomerControllerBean makeConnection");
326
327         try {
328             InitialContext ic = new InitialContext();
329             DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE);
330             con = ds.getConnection();
331         } catch (Exception JavaDoc ex) {
332             throw new EJBException("Unable to connect to database. " +
333                 ex.getMessage());
334         }
335     }
336      // makeConnection
337

338     private void releaseConnection() {
339         Debug.print("CustomerControllerBean releaseConnection");
340
341         try {
342             con.close();
343         } catch (SQLException ex) {
344             throw new EJBException("releaseConnection: " + ex.getMessage());
345         }
346     }
347      // releaseConnection
348

349     private void deleteAllCustomerInXref(String JavaDoc customerId)
350         throws SQLException {
351         Debug.print("CustomerControllerBean deleteAllCustomerInXref");
352
353         String JavaDoc deleteStatement =
354             "delete from customer_account_xref " + "where customer_id = ? ";
355         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
356
357         prepStmt.setString(1, customerId);
358         prepStmt.executeUpdate();
359         prepStmt.close();
360     }
361 }
362  // CustomerControllerBean
363
Popular Tags