KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > order > OrderBean


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 order;
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 OrderBean implements EntityBean, OrderRemoteBusiness {
38     private static final String JavaDoc dbName = "jdbc/OrderDB";
39     private String JavaDoc orderId;
40     private ArrayList lineItems;
41     private String JavaDoc customerId;
42     private double totalPrice;
43     private String JavaDoc status;
44     private Connection con;
45     private EntityContext context;
46
47     public ArrayList getLineItems() {
48         return lineItems;
49     }
50
51     public String JavaDoc getCustomerId() {
52         return customerId;
53     }
54
55     public double getTotalPrice() {
56         return totalPrice;
57     }
58
59     public String JavaDoc getStatus() {
60         return status;
61     }
62
63     public String JavaDoc ejbCreate(String JavaDoc orderId, String JavaDoc customerId, String JavaDoc status,
64         double totalPrice, ArrayList lineItems) throws CreateException {
65         try {
66             insertOrder(orderId, customerId, status, totalPrice);
67
68             for (int i = 0; i < lineItems.size(); i++) {
69                 LineItem item = (LineItem) lineItems.get(i);
70
71                 insertItem(item);
72             }
73         } catch (Exception JavaDoc ex) {
74             throw new EJBException("ejbCreate: " + ex.getMessage());
75         }
76
77         this.orderId = orderId;
78         this.customerId = customerId;
79         this.status = status;
80         this.totalPrice = totalPrice;
81         this.lineItems = lineItems;
82
83         return orderId;
84     }
85
86     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc primaryKey)
87         throws FinderException {
88         boolean result;
89
90         try {
91             result = selectByPrimaryKey(primaryKey);
92         } catch (Exception JavaDoc ex) {
93             throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
94         }
95
96         if (result) {
97             return primaryKey;
98         } else {
99             throw new ObjectNotFoundException("Row for id " + primaryKey +
100                 " not found.");
101         }
102     }
103
104     public Collection ejbFindByProductId(String JavaDoc productId)
105         throws FinderException {
106         Collection result;
107
108         try {
109             result = selectByProductId(productId);
110         } catch (Exception JavaDoc ex) {
111             throw new EJBException("ejbFindByProductId " + ex.getMessage());
112         }
113
114         return result;
115     }
116
117     public void ejbRemove() {
118         try {
119             deleteOrder(orderId);
120             deleteItems(orderId);
121         } catch (Exception JavaDoc ex) {
122             throw new EJBException("ejbRemove: " + ex.getMessage());
123         }
124     }
125
126     public void setEntityContext(EntityContext context) {
127         this.context = context;
128         lineItems = new ArrayList();
129     }
130
131     public void unsetEntityContext() {
132         lineItems = null;
133     }
134
135     public void ejbActivate() {
136         orderId = (String JavaDoc) context.getPrimaryKey();
137     }
138
139     public void ejbPassivate() {
140         orderId = null;
141     }
142
143     public void ejbLoad() {
144         try {
145             loadOrder();
146             loadItems();
147         } catch (Exception JavaDoc ex) {
148             throw new EJBException("ejbLoad: " + ex.getMessage());
149         }
150     }
151
152     public void ejbStore() {
153         try {
154             storeOrder();
155
156             for (int i = 0; i < lineItems.size(); i++) {
157                 LineItem item = (LineItem) lineItems.get(i);
158
159                 storeItem(item);
160             }
161         } catch (Exception JavaDoc ex) {
162             throw new EJBException("ejbStore: " + ex.getMessage());
163         }
164     }
165
166     public void ejbPostCreate(String JavaDoc orderId, String JavaDoc customerId, String JavaDoc status,
167         double totalPrice, ArrayList lineItems) {
168     }
169
170     /*********************** Database Routines *************************/
171     private void makeConnection() {
172         try {
173             InitialContext ic = new InitialContext();
174             DataSource ds = (DataSource) ic.lookup(dbName);
175
176             con = ds.getConnection();
177         } catch (Exception JavaDoc ex) {
178             throw new EJBException("Unable to connect to database. " +
179                 ex.getMessage());
180         }
181     }
182
183     private void releaseConnection() {
184         try {
185             con.close();
186         } catch (SQLException ex) {
187             throw new EJBException("releaseConnection: " + ex.getMessage());
188         }
189     }
190
191     private void insertOrder(String JavaDoc orderId, String JavaDoc customerId, String JavaDoc status,
192         double totalPrice) throws SQLException {
193         makeConnection();
194
195         String JavaDoc insertStatement = "insert into orders values ( ? , ? , ? , ? )";
196         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
197
198         prepStmt.setString(1, orderId);
199         prepStmt.setString(2, customerId);
200         prepStmt.setDouble(3, totalPrice);
201         prepStmt.setString(4, status);
202
203         prepStmt.executeUpdate();
204         prepStmt.close();
205         releaseConnection();
206     }
207
208     private void insertItem(LineItem lineItem) throws SQLException {
209         makeConnection();
210
211         String JavaDoc insertStatement =
212             "insert into lineitems values ( ? , ? , ? , ? , ? )";
213         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
214
215         prepStmt.setInt(1, lineItem.getItemNo());
216         prepStmt.setString(2, lineItem.getOrderId());
217         prepStmt.setString(3, lineItem.getProductId());
218         prepStmt.setDouble(4, lineItem.getUnitPrice());
219         prepStmt.setInt(5, lineItem.getQuantity());
220
221         prepStmt.executeUpdate();
222         prepStmt.close();
223         releaseConnection();
224     }
225
226     private boolean selectByPrimaryKey(String JavaDoc primaryKey)
227         throws SQLException {
228         makeConnection();
229
230         String JavaDoc selectStatement =
231             "select orderid " + "from orders where orderid = ? ";
232         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
233
234         prepStmt.setString(1, primaryKey);
235
236         ResultSet rs = prepStmt.executeQuery();
237         boolean result = rs.next();
238
239         prepStmt.close();
240         releaseConnection();
241
242         return result;
243     }
244
245     private Collection selectByProductId(String JavaDoc productId)
246         throws SQLException {
247         makeConnection();
248
249         String JavaDoc selectStatement =
250             "select distinct orderid " + "from lineitems where productid = ? ";
251         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
252
253         prepStmt.setString(1, productId);
254
255         ResultSet rs = prepStmt.executeQuery();
256         ArrayList a = new ArrayList();
257
258         while (rs.next()) {
259             String JavaDoc id = rs.getString(1);
260
261             a.add(id);
262         }
263
264         prepStmt.close();
265         releaseConnection();
266
267         return a;
268     }
269
270     private void deleteItems(String JavaDoc orderId) throws SQLException {
271         makeConnection();
272
273         String JavaDoc deleteStatement =
274             "delete from lineitems " + "where orderid = ?";
275         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
276
277         prepStmt.setString(1, orderId);
278         prepStmt.executeUpdate();
279         prepStmt.close();
280         releaseConnection();
281     }
282
283     private void deleteOrder(String JavaDoc orderId) throws SQLException {
284         makeConnection();
285
286         String JavaDoc deleteStatement = "delete from orders " + "where orderid = ?";
287         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
288
289         prepStmt.setString(1, orderId);
290         prepStmt.executeUpdate();
291         prepStmt.close();
292         releaseConnection();
293     }
294
295     private void loadOrder() throws SQLException {
296         makeConnection();
297
298         String JavaDoc selectStatement =
299             "select customerid, totalprice, status " +
300             "from orders where orderid = ? ";
301         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
302
303         prepStmt.setString(1, orderId);
304
305         ResultSet rs = prepStmt.executeQuery();
306
307         if (rs.next()) {
308             customerId = rs.getString(1);
309             totalPrice = rs.getDouble(2);
310             status = rs.getString(3);
311             prepStmt.close();
312         } else {
313             prepStmt.close();
314             throw new NoSuchEntityException("Row for orderId " + orderId +
315                 " not found in database.");
316         }
317
318         releaseConnection();
319     }
320
321     private void loadItems() throws SQLException {
322         makeConnection();
323
324         String JavaDoc selectStatement =
325             "select itemno, productid, unitprice, quantity " +
326             "from lineitems where orderid = ? " + "order by itemno";
327         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
328
329         prepStmt.setString(1, orderId);
330
331         ResultSet rs = prepStmt.executeQuery();
332
333         lineItems.clear();
334
335         int count = 0;
336
337         while (rs.next()) {
338             int itemNo = rs.getInt(1);
339             String JavaDoc productId = rs.getString(2);
340             double unitPrice = rs.getDouble(3);
341             int quantity = rs.getInt(4);
342
343             lineItems.add(new LineItem(productId, quantity, unitPrice, itemNo,
344                     orderId));
345             count++;
346         }
347
348         prepStmt.close();
349
350         if (count == 0) {
351             throw new NoSuchEntityException("No items for orderId " + orderId +
352                 " found in database.");
353         }
354
355         releaseConnection();
356     }
357
358     private void storeOrder() throws SQLException {
359         makeConnection();
360
361         String JavaDoc updateStatement =
362             "update orders set customerid = ? ," +
363             "totalprice = ? , status = ? " + "where orderid = ?";
364         PreparedStatement prepStmt = con.prepareStatement(updateStatement);
365
366         prepStmt.setString(1, customerId);
367         prepStmt.setDouble(2, totalPrice);
368         prepStmt.setString(3, status);
369         prepStmt.setString(4, orderId);
370
371         int rowCount = prepStmt.executeUpdate();
372
373         prepStmt.close();
374
375         if (rowCount == 0) {
376             throw new EJBException("Storing row for orderId " + orderId +
377                 " failed.");
378         }
379
380         releaseConnection();
381     }
382
383     private void storeItem(LineItem item) throws SQLException {
384         makeConnection();
385
386         String JavaDoc updateStatement =
387             "update lineitems set productid = ? ," +
388             "unitprice = ? , quantity = ? " +
389             "where orderid = ? and itemno = ?";
390         PreparedStatement prepStmt = con.prepareStatement(updateStatement);
391
392         prepStmt.setString(1, item.getProductId());
393         prepStmt.setDouble(2, item.getUnitPrice());
394         prepStmt.setInt(3, item.getQuantity());
395         prepStmt.setString(4, orderId);
396         prepStmt.setInt(5, item.getItemNo());
397
398         int rowCount = prepStmt.executeUpdate();
399
400         prepStmt.close();
401
402         if (rowCount == 0) {
403             throw new EJBException("Storing itemNo " + item.getItemNo() +
404                 "for orderId " + orderId + " failed.");
405         }
406
407         releaseConnection();
408     }
409 }
410  // OrderBean
Popular Tags