KickJava   Java API By Example, From Geeks To Geeks.

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


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

264     private void releaseConnection() {
265         Debug.print("CustomerBean releaseConnection");
266
267         try {
268             con.close();
269         } catch (SQLException ex) {
270             throw new EJBException("releaseConnection: " + ex.getMessage());
271         }
272     }
273      // releaseConnection
274

275     private void insertRow() throws SQLException {
276         Debug.print("CustomerBean insertRow");
277
278         makeConnection();
279
280         String JavaDoc insertStatement =
281             "insert into customer values ( ? , ? , ? , ? , ? , " +
282             " ? , ? , ? , ? , ? )";
283         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
284
285         prepStmt.setString(1, customerId);
286         prepStmt.setString(2, lastName);
287         prepStmt.setString(3, firstName);
288         prepStmt.setString(4, middleInitial);
289         prepStmt.setString(5, street);
290         prepStmt.setString(6, city);
291         prepStmt.setString(7, state);
292         prepStmt.setString(8, zip);
293         prepStmt.setString(9, phone);
294         prepStmt.setString(10, email);
295
296         prepStmt.executeUpdate();
297         prepStmt.close();
298         releaseConnection();
299     }
300
301     private void deleteRow(String JavaDoc id) throws SQLException {
302         Debug.print("CustomerBean deleteRow");
303
304         makeConnection();
305
306         String JavaDoc deleteStatement = "delete from customer where customer_id = ? ";
307         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
308
309         prepStmt.setString(1, id);
310         prepStmt.executeUpdate();
311         prepStmt.close();
312         releaseConnection();
313     }
314
315     private boolean selectByPrimaryKey(String JavaDoc primaryKey)
316         throws SQLException {
317         Debug.print("CustomerBean selectByPrimaryKey");
318
319         makeConnection();
320
321         String JavaDoc selectStatement =
322             "select customer_id " + "from customer where customer_id = ? ";
323         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
324         prepStmt.setString(1, primaryKey);
325
326         ResultSet rs = prepStmt.executeQuery();
327         boolean result = rs.next();
328         prepStmt.close();
329         releaseConnection();
330
331         return result;
332     }
333
334     private Collection selectByAccountId(String JavaDoc accountId)
335         throws SQLException {
336         Debug.print("CustomerBean selectByAccountId");
337
338         makeConnection();
339
340         String JavaDoc selectStatement =
341             "select customer_id " + "from customer_account_xref " +
342             "where account_id = ? ";
343         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
344
345         prepStmt.setString(1, accountId);
346
347         ResultSet rs = prepStmt.executeQuery();
348         ArrayList a = new ArrayList();
349
350         while (rs.next()) {
351             a.add(rs.getString(1));
352         }
353
354         prepStmt.close();
355         releaseConnection();
356
357         return a;
358     }
359
360     private Collection selectByLastName(String JavaDoc lastName)
361         throws SQLException {
362         Debug.print("CustomerBean selectByLastName");
363
364         makeConnection();
365
366         String JavaDoc selectStatement =
367             "select customer_id " + "from customer " + "where last_name = ? ";
368         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
369
370         prepStmt.setString(1, lastName);
371
372         ResultSet rs = prepStmt.executeQuery();
373         ArrayList a = new ArrayList();
374
375         while (rs.next()) {
376             a.add(rs.getString(1));
377         }
378
379         prepStmt.close();
380         releaseConnection();
381
382         return a;
383     }
384
385     private void loadCustomer() throws SQLException {
386         Debug.print("CustomerBean loadCustomer");
387
388         makeConnection();
389
390         String JavaDoc selectStatement =
391             "select last_name, first_name, middle_initial, " +
392             "street, city, state, zip, phone, email " +
393             "from customer where customer_id = ? ";
394         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
395
396         prepStmt.setString(1, customerId);
397
398         ResultSet rs = prepStmt.executeQuery();
399
400         if (rs.next()) {
401             lastName = rs.getString(1);
402             firstName = rs.getString(2);
403             middleInitial = rs.getString(3);
404             street = rs.getString(4);
405             city = rs.getString(5);
406             state = rs.getString(6);
407             zip = rs.getString(7);
408             phone = rs.getString(8);
409             email = rs.getString(9);
410             prepStmt.close();
411             releaseConnection();
412         } else {
413             prepStmt.close();
414             releaseConnection();
415             throw new NoSuchEntityException("Row for id " + customerId +
416                 " not found in database.");
417         }
418     }
419
420     private void storeCustomer() throws SQLException {
421         Debug.print("CustomerBean storeCustomer");
422
423         makeConnection();
424
425         String JavaDoc updateStatement =
426             "update customer " + "set last_name = ? , first_name = ? , " +
427             "middle_initial = ? , street = ? , city = ? , " +
428             "state = ? , zip = ? , phone = ? , email = ? " +
429             "where customer_id = ? ";
430         PreparedStatement prepStmt = con.prepareStatement(updateStatement);
431
432         prepStmt.setString(1, lastName);
433         prepStmt.setString(2, firstName);
434         prepStmt.setString(3, middleInitial);
435         prepStmt.setString(4, street);
436         prepStmt.setString(5, city);
437         prepStmt.setString(6, state);
438         prepStmt.setString(7, zip);
439         prepStmt.setString(8, phone);
440         prepStmt.setString(9, email);
441         prepStmt.setString(10, customerId);
442
443         int rowCount = prepStmt.executeUpdate();
444         prepStmt.close();
445         releaseConnection();
446
447         if (rowCount == 0) {
448             throw new EJBException("Storing row for id " + customerId +
449                 " failed.");
450         }
451     }
452 }
453  // CustomerBean
454
Popular Tags