001: /*
002: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
003: * Government Rights - Commercial software. Government users are subject
004: * to the Sun Microsystems, Inc. standard license agreement and
005: * applicable provisions of the FAR and its supplements. Use is subject
006: * to license terms.
007: *
008: * This distribution may include materials developed by third parties.
009: * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
010: * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
011: * other countries.
012: *
013: * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
014: *
015: * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
016: * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
017: * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
018: * en vigueur de la FAR (Federal Acquisition Regulations) et des
019: * supplements a celles-ci. Distribue par des licences qui en
020: * restreignent l'utilisation.
021: *
022: * Cette distribution peut comprendre des composants developpes par des
023: * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
024: * sont des marques de fabrique ou des marques deposees de Sun
025: * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
026: */
027:
028: package order;
029:
030: import java.sql.*;
031: import javax.sql.*;
032: import java.util.*;
033: import javax.ejb.*;
034: import javax.naming.*;
035:
036: public class OrderBean implements EntityBean, OrderRemoteBusiness {
037: private static final String dbName = "jdbc/OrderDB";
038: private String orderId;
039: private ArrayList lineItems;
040: private String customerId;
041: private double totalPrice;
042: private String status;
043: private Connection con;
044: private EntityContext context;
045:
046: public ArrayList getLineItems() {
047: return lineItems;
048: }
049:
050: public String getCustomerId() {
051: return customerId;
052: }
053:
054: public double getTotalPrice() {
055: return totalPrice;
056: }
057:
058: public String getStatus() {
059: return status;
060: }
061:
062: public String ejbCreate(String orderId, String customerId,
063: String status, double totalPrice, ArrayList lineItems)
064: throws CreateException {
065: try {
066: insertOrder(orderId, customerId, status, totalPrice);
067:
068: for (int i = 0; i < lineItems.size(); i++) {
069: LineItem item = (LineItem) lineItems.get(i);
070:
071: insertItem(item);
072: }
073: } catch (Exception ex) {
074: throw new EJBException("ejbCreate: " + ex.getMessage());
075: }
076:
077: this .orderId = orderId;
078: this .customerId = customerId;
079: this .status = status;
080: this .totalPrice = totalPrice;
081: this .lineItems = lineItems;
082:
083: return orderId;
084: }
085:
086: public String ejbFindByPrimaryKey(String primaryKey)
087: throws FinderException {
088: boolean result;
089:
090: try {
091: result = selectByPrimaryKey(primaryKey);
092: } catch (Exception ex) {
093: throw new EJBException("ejbFindByPrimaryKey: "
094: + ex.getMessage());
095: }
096:
097: if (result) {
098: return primaryKey;
099: } else {
100: throw new ObjectNotFoundException("Row for id "
101: + primaryKey + " not found.");
102: }
103: }
104:
105: public Collection ejbFindByProductId(String productId)
106: throws FinderException {
107: Collection result;
108:
109: try {
110: result = selectByProductId(productId);
111: } catch (Exception ex) {
112: throw new EJBException("ejbFindByProductId "
113: + ex.getMessage());
114: }
115:
116: return result;
117: }
118:
119: public void ejbRemove() {
120: try {
121: deleteOrder(orderId);
122: deleteItems(orderId);
123: } catch (Exception ex) {
124: throw new EJBException("ejbRemove: " + ex.getMessage());
125: }
126: }
127:
128: public void setEntityContext(EntityContext context) {
129: this .context = context;
130: lineItems = new ArrayList();
131: }
132:
133: public void unsetEntityContext() {
134: lineItems = null;
135: }
136:
137: public void ejbActivate() {
138: orderId = (String) context.getPrimaryKey();
139: }
140:
141: public void ejbPassivate() {
142: orderId = null;
143: }
144:
145: public void ejbLoad() {
146: try {
147: loadOrder();
148: loadItems();
149: } catch (Exception ex) {
150: throw new EJBException("ejbLoad: " + ex.getMessage());
151: }
152: }
153:
154: public void ejbStore() {
155: try {
156: storeOrder();
157:
158: for (int i = 0; i < lineItems.size(); i++) {
159: LineItem item = (LineItem) lineItems.get(i);
160:
161: storeItem(item);
162: }
163: } catch (Exception ex) {
164: throw new EJBException("ejbStore: " + ex.getMessage());
165: }
166: }
167:
168: public void ejbPostCreate(String orderId, String customerId,
169: String status, double totalPrice, ArrayList lineItems) {
170: }
171:
172: /*********************** Database Routines *************************/
173: private void makeConnection() {
174: try {
175: InitialContext ic = new InitialContext();
176: DataSource ds = (DataSource) ic.lookup(dbName);
177:
178: con = ds.getConnection();
179: } catch (Exception ex) {
180: throw new EJBException("Unable to connect to database. "
181: + ex.getMessage());
182: }
183: }
184:
185: private void releaseConnection() {
186: try {
187: con.close();
188: } catch (SQLException ex) {
189: throw new EJBException("releaseConnection: "
190: + ex.getMessage());
191: }
192: }
193:
194: private void insertOrder(String orderId, String customerId,
195: String status, double totalPrice) throws SQLException {
196: makeConnection();
197:
198: String insertStatement = "insert into orders values ( ? , ? , ? , ? )";
199: PreparedStatement prepStmt = con
200: .prepareStatement(insertStatement);
201:
202: prepStmt.setString(1, orderId);
203: prepStmt.setString(2, customerId);
204: prepStmt.setDouble(3, totalPrice);
205: prepStmt.setString(4, status);
206:
207: prepStmt.executeUpdate();
208: prepStmt.close();
209: releaseConnection();
210: }
211:
212: private void insertItem(LineItem lineItem) throws SQLException {
213: makeConnection();
214:
215: String insertStatement = "insert into lineitems values ( ? , ? , ? , ? , ? )";
216: PreparedStatement prepStmt = con
217: .prepareStatement(insertStatement);
218:
219: prepStmt.setInt(1, lineItem.getItemNo());
220: prepStmt.setString(2, lineItem.getOrderId());
221: prepStmt.setString(3, lineItem.getProductId());
222: prepStmt.setDouble(4, lineItem.getUnitPrice());
223: prepStmt.setInt(5, lineItem.getQuantity());
224:
225: prepStmt.executeUpdate();
226: prepStmt.close();
227: releaseConnection();
228: }
229:
230: private boolean selectByPrimaryKey(String primaryKey)
231: throws SQLException {
232: makeConnection();
233:
234: String selectStatement = "select orderid "
235: + "from orders where orderid = ? ";
236: PreparedStatement prepStmt = con
237: .prepareStatement(selectStatement);
238:
239: prepStmt.setString(1, primaryKey);
240:
241: ResultSet rs = prepStmt.executeQuery();
242: boolean result = rs.next();
243:
244: prepStmt.close();
245: releaseConnection();
246:
247: return result;
248: }
249:
250: private Collection selectByProductId(String productId)
251: throws SQLException {
252: makeConnection();
253:
254: String selectStatement = "select distinct orderid "
255: + "from lineitems where productid = ? ";
256: PreparedStatement prepStmt = con
257: .prepareStatement(selectStatement);
258:
259: prepStmt.setString(1, productId);
260:
261: ResultSet rs = prepStmt.executeQuery();
262: ArrayList a = new ArrayList();
263:
264: while (rs.next()) {
265: String id = rs.getString(1);
266:
267: a.add(id);
268: }
269:
270: prepStmt.close();
271: releaseConnection();
272:
273: return a;
274: }
275:
276: private void deleteItems(String orderId) throws SQLException {
277: makeConnection();
278:
279: String deleteStatement = "delete from lineitems "
280: + "where orderid = ?";
281: PreparedStatement prepStmt = con
282: .prepareStatement(deleteStatement);
283:
284: prepStmt.setString(1, orderId);
285: prepStmt.executeUpdate();
286: prepStmt.close();
287: releaseConnection();
288: }
289:
290: private void deleteOrder(String orderId) throws SQLException {
291: makeConnection();
292:
293: String deleteStatement = "delete from orders "
294: + "where orderid = ?";
295: PreparedStatement prepStmt = con
296: .prepareStatement(deleteStatement);
297:
298: prepStmt.setString(1, orderId);
299: prepStmt.executeUpdate();
300: prepStmt.close();
301: releaseConnection();
302: }
303:
304: private void loadOrder() throws SQLException {
305: makeConnection();
306:
307: String selectStatement = "select customerid, totalprice, status "
308: + "from orders where orderid = ? ";
309: PreparedStatement prepStmt = con
310: .prepareStatement(selectStatement);
311:
312: prepStmt.setString(1, orderId);
313:
314: ResultSet rs = prepStmt.executeQuery();
315:
316: if (rs.next()) {
317: customerId = rs.getString(1);
318: totalPrice = rs.getDouble(2);
319: status = rs.getString(3);
320: prepStmt.close();
321: } else {
322: prepStmt.close();
323: throw new NoSuchEntityException("Row for orderId "
324: + orderId + " not found in database.");
325: }
326:
327: releaseConnection();
328: }
329:
330: private void loadItems() throws SQLException {
331: makeConnection();
332:
333: String selectStatement = "select itemno, productid, unitprice, quantity "
334: + "from lineitems where orderid = ? "
335: + "order by itemno";
336: PreparedStatement prepStmt = con
337: .prepareStatement(selectStatement);
338:
339: prepStmt.setString(1, orderId);
340:
341: ResultSet rs = prepStmt.executeQuery();
342:
343: lineItems.clear();
344:
345: int count = 0;
346:
347: while (rs.next()) {
348: int itemNo = rs.getInt(1);
349: String productId = rs.getString(2);
350: double unitPrice = rs.getDouble(3);
351: int quantity = rs.getInt(4);
352:
353: lineItems.add(new LineItem(productId, quantity, unitPrice,
354: itemNo, orderId));
355: count++;
356: }
357:
358: prepStmt.close();
359:
360: if (count == 0) {
361: throw new NoSuchEntityException("No items for orderId "
362: + orderId + " found in database.");
363: }
364:
365: releaseConnection();
366: }
367:
368: private void storeOrder() throws SQLException {
369: makeConnection();
370:
371: String updateStatement = "update orders set customerid = ? ,"
372: + "totalprice = ? , status = ? " + "where orderid = ?";
373: PreparedStatement prepStmt = con
374: .prepareStatement(updateStatement);
375:
376: prepStmt.setString(1, customerId);
377: prepStmt.setDouble(2, totalPrice);
378: prepStmt.setString(3, status);
379: prepStmt.setString(4, orderId);
380:
381: int rowCount = prepStmt.executeUpdate();
382:
383: prepStmt.close();
384:
385: if (rowCount == 0) {
386: throw new EJBException("Storing row for orderId " + orderId
387: + " failed.");
388: }
389:
390: releaseConnection();
391: }
392:
393: private void storeItem(LineItem item) throws SQLException {
394: makeConnection();
395:
396: String updateStatement = "update lineitems set productid = ? ,"
397: + "unitprice = ? , quantity = ? "
398: + "where orderid = ? and itemno = ?";
399: PreparedStatement prepStmt = con
400: .prepareStatement(updateStatement);
401:
402: prepStmt.setString(1, item.getProductId());
403: prepStmt.setDouble(2, item.getUnitPrice());
404: prepStmt.setInt(3, item.getQuantity());
405: prepStmt.setString(4, orderId);
406: prepStmt.setInt(5, item.getItemNo());
407:
408: int rowCount = prepStmt.executeUpdate();
409:
410: prepStmt.close();
411:
412: if (rowCount == 0) {
413: throw new EJBException("Storing itemNo " + item.getItemNo()
414: + "for orderId " + orderId + " failed.");
415: }
416:
417: releaseConnection();
418: }
419: }
420: // OrderBean
|