KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salesrep > CustomerBean


1 /*
2  * Copyright (c) 2005 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) 2005 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 package salesrep;
29
30 import java.sql.*;
31 import javax.sql.*;
32 import java.util.*;
33 import javax.ejb.*;
34 import javax.naming.*;
35
36
37 public class CustomerBean implements EntityBean, CustomerRemoteBusiness {
38     private static final String JavaDoc dbName = "java:comp/env/jdbc/SalesDB";
39     private String JavaDoc customerId;
40     private String JavaDoc salesRepId;
41     private String JavaDoc name;
42     private Connection con;
43     private EntityContext context;
44
45     public String JavaDoc getSalesRepId() {
46         return salesRepId;
47     }
48
49     public String JavaDoc getName() {
50         System.out.println("entering getName()");
51
52         return name;
53     }
54
55     public void setSalesRepId(String JavaDoc salesRepId) {
56         this.salesRepId = salesRepId;
57     }
58
59     public void setName(String JavaDoc name) {
60         this.name = name;
61     }
62
63     public String JavaDoc ejbCreate(String JavaDoc customerId, String JavaDoc salesRepId, String JavaDoc name)
64         throws CreateException {
65         System.out.println("in ejbCreate");
66
67         try {
68             insertCustomer(customerId, salesRepId, name);
69         } catch (Exception JavaDoc ex) {
70             throw new EJBException("ejbCreate: " + ex.getMessage());
71         }
72
73         this.customerId = customerId;
74         this.salesRepId = salesRepId;
75         this.name = name;
76
77         System.out.println("about to leave ejbCreate");
78
79         return customerId;
80     }
81
82     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc primaryKey)
83         throws FinderException {
84         boolean result;
85
86         try {
87             result = selectByPrimaryKey(primaryKey);
88         } catch (Exception JavaDoc ex) {
89             throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
90         }
91
92         if (result) {
93             return primaryKey;
94         } else {
95             throw new ObjectNotFoundException("Row for id " + primaryKey +
96                 " not found.");
97         }
98     }
99
100     public Collection ejbFindBySalesRep(String JavaDoc salesRepId)
101         throws FinderException {
102         Collection result;
103
104         try {
105             result = selectBySalesRep(salesRepId);
106         } catch (Exception JavaDoc ex) {
107             throw new EJBException("ejbFindBySalesRep: " + ex.getMessage());
108         }
109
110         return result;
111     }
112
113     public void ejbRemove() {
114         try {
115             deleteCustomer(customerId);
116         } catch (Exception JavaDoc ex) {
117             throw new EJBException("ejbRemove: " + ex.getMessage());
118         }
119     }
120
121     public void setEntityContext(EntityContext context) {
122         this.context = context;
123     }
124
125     public void unsetEntityContext() {
126     }
127
128     public void ejbActivate() {
129         customerId = (String JavaDoc) context.getPrimaryKey();
130     }
131
132     public void ejbPassivate() {
133         customerId = null;
134     }
135
136     public void ejbLoad() {
137         System.out.println("in ejbLoad");
138
139         try {
140             loadCustomer();
141         } catch (Exception JavaDoc ex) {
142             throw new EJBException("ejbLoad: " + ex.getMessage());
143         }
144
145         System.out.println("leaving ejbLoad");
146     }
147
148     public void ejbStore() {
149         System.out.println("in ejbStore");
150
151         try {
152             storeCustomer();
153         } catch (Exception JavaDoc ex) {
154             throw new EJBException("ejbStore: " + ex.getMessage());
155         }
156
157         System.out.println("leaving ejbStore");
158     }
159
160     public void ejbPostCreate(String JavaDoc customerId, String JavaDoc salesRepId, String JavaDoc name) {
161     }
162
163     /*********************** Database Routines *************************/
164     private void makeConnection() {
165         try {
166             InitialContext ic = new InitialContext();
167             DataSource ds = (DataSource) ic.lookup(dbName);
168
169             con = ds.getConnection();
170         } catch (Exception JavaDoc ex) {
171             throw new EJBException("Unable to connect to database. " +
172                 ex.getMessage());
173         }
174     }
175
176     private void releaseConnection() {
177         try {
178             con.close();
179         } catch (SQLException ex) {
180             throw new EJBException("releaseConnection: " + ex.getMessage());
181         }
182     }
183
184     private void insertCustomer(String JavaDoc customerId, String JavaDoc salesRepId,
185         String JavaDoc name) throws SQLException {
186         makeConnection();
187
188         String JavaDoc insertStatement = "insert into customer values ( ? , ? , ? )";
189         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
190
191         prepStmt.setString(1, customerId);
192         prepStmt.setString(2, salesRepId);
193         prepStmt.setString(3, name);
194
195         prepStmt.executeUpdate();
196         prepStmt.close();
197         releaseConnection();
198     }
199
200     private boolean selectByPrimaryKey(String JavaDoc primaryKey)
201         throws SQLException {
202         makeConnection();
203
204         String JavaDoc selectStatement =
205             "select customerid " + "from customer where customerid = ? ";
206         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
207
208         prepStmt.setString(1, primaryKey);
209
210         ResultSet rs = prepStmt.executeQuery();
211         boolean result = rs.next();
212
213         prepStmt.close();
214         releaseConnection();
215
216         return result;
217     }
218
219     private Collection selectBySalesRep(String JavaDoc salesRepId)
220         throws SQLException {
221         makeConnection();
222
223         String JavaDoc selectStatement =
224             "select customerid " + "from customer where salesrepid = ? ";
225         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
226
227         prepStmt.setString(1, salesRepId);
228
229         ResultSet rs = prepStmt.executeQuery();
230         ArrayList a = new ArrayList();
231
232         while (rs.next()) {
233             String JavaDoc id = rs.getString(1);
234
235             a.add(id);
236         }
237
238         prepStmt.close();
239         releaseConnection();
240
241         return a;
242     }
243
244     private void deleteCustomer(String JavaDoc customerId) throws SQLException {
245         makeConnection();
246
247         String JavaDoc deleteStatement =
248             "delete from customer " + "where customerid = ?";
249         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
250
251         prepStmt.setString(1, customerId);
252         prepStmt.executeUpdate();
253         prepStmt.close();
254         releaseConnection();
255     }
256
257     private void loadCustomer() throws SQLException {
258         makeConnection();
259
260         String JavaDoc selectStatement =
261             "select customerid, salesRepid, name " +
262             "from customer where customerid = ? ";
263         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
264
265         prepStmt.setString(1, customerId);
266
267         ResultSet rs = prepStmt.executeQuery();
268
269         if (rs.next()) {
270             customerId = rs.getString(1);
271             salesRepId = rs.getString(2);
272             name = rs.getString(3);
273             prepStmt.close();
274         } else {
275             prepStmt.close();
276             throw new NoSuchEntityException("Row for customerId " + customerId +
277                 " not found in database.");
278         }
279
280         releaseConnection();
281     }
282
283     private void storeCustomer() throws SQLException {
284         makeConnection();
285         System.out.println("entering storeCustomer");
286
287         String JavaDoc updateStatement =
288             "update customer " + "set salesRepid = ? , name = ? " +
289             "where customerid = ?";
290         PreparedStatement prepStmt = con.prepareStatement(updateStatement);
291
292         prepStmt.setString(1, salesRepId);
293         prepStmt.setString(2, name);
294         prepStmt.setString(3, customerId);
295
296         int rowCount = prepStmt.executeUpdate();
297
298         prepStmt.close();
299
300         if (rowCount == 0) {
301             throw new EJBException("Storing row for customerId " + customerId +
302                 " failed.");
303         }
304
305         releaseConnection();
306         System.out.println("leaving storeCustomer");
307     }
308 }
309  // CustomerBean
Popular Tags