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 request;
029:
030: import dataregistry.LineItemLocal;
031: import dataregistry.LineItemLocalHome;
032: import dataregistry.OrderLocal;
033: import dataregistry.OrderLocalHome;
034: import dataregistry.PartLocal;
035: import dataregistry.PartLocalHome;
036: import dataregistry.PartPK;
037: import dataregistry.VendorKey;
038: import dataregistry.VendorLocal;
039: import dataregistry.VendorLocalHome;
040: import dataregistry.VendorPartLocal;
041: import dataregistry.VendorPartLocalHome;
042: import java.math.BigDecimal;
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Iterator;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.naming.Context;
049: import javax.naming.InitialContext;
050: import javax.naming.NamingException;
051: import javax.ejb.*;
052:
053: /**
054: * This is the bean class for the RequestBean enterprise bean.
055: */
056: public class RequestBean implements SessionBean,
057: request.RequestRemoteBusiness {
058: private SessionContext context;
059: private LineItemLocalHome lineItemHome = null;
060: private OrderLocalHome orderHome = null;
061: private PartLocalHome partHome = null;
062: private VendorLocalHome vendorHome = null;
063: private VendorPartLocalHome vendorPartHome = null;
064:
065: // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
066: // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
067: // TODO Add business methods or web service operations
068: /**
069: * @see SessionBean#setSessionContext(SessionContext)
070: */
071: public void setSessionContext(SessionContext aContext) {
072: context = aContext;
073: }
074:
075: /**
076: * @see SessionBean#ejbActivate()
077: */
078: public void ejbActivate() {
079:
080: }
081:
082: /**
083: * @see SessionBean#ejbPassivate()
084: */
085: public void ejbPassivate() {
086:
087: }
088:
089: /**
090: * @see SessionBean#ejbRemove()
091: */
092: public void ejbRemove() {
093:
094: }
095:
096: // </editor-fold>
097:
098: /**
099: * See section 7.10.3 of the EJB 2.0 specification
100: * See section 7.11.3 of the EJB 2.1 specification
101: */
102: public void ejbCreate() {
103: try {
104: lineItemHome = lookupLineitem();
105: orderHome = lookupOrder();
106: partHome = lookupPart();
107: vendorHome = lookupVendor();
108: vendorPartHome = lookupVendorPart();
109: } catch (Exception e) {
110: throw new EJBException(e.getMessage());
111: }
112: }
113:
114: public void createPart(PartRequest partRequest) {
115: try {
116: PartLocal part = partHome.create(partRequest.partNumber,
117: partRequest.revision, partRequest.description,
118: partRequest.revisionDate,
119: partRequest.specification, partRequest.drawing);
120: } catch (Exception e) {
121: throw new EJBException(e.getMessage());
122: }
123: }
124:
125: public void addPartToBillOfMaterial(BomRequest bomRequest) {
126: try {
127: PartPK bomkey = new PartPK();
128: bomkey.partNumber = bomRequest.bomPartNumber;
129: bomkey.revision = new BigDecimal(bomRequest.bomRevision);
130:
131: PartLocal bom = partHome.findByPrimaryKey(bomkey);
132:
133: PartPK pkey = new PartPK();
134: pkey.partNumber = bomRequest.partNumber;
135: pkey.revision = new BigDecimal(bomRequest.revision);
136:
137: PartLocal part = partHome.findByPrimaryKey(pkey);
138: part.setBomPart(bom);
139: } catch (Exception e) {
140: throw new EJBException(e.getMessage());
141: }
142:
143: }
144:
145: public void createVendor(VendorRequest vendorRequest) {
146: try {
147: VendorLocal vendor = vendorHome.create(
148: vendorRequest.vendorId, vendorRequest.name,
149: vendorRequest.address, vendorRequest.contact,
150: vendorRequest.phone);
151: } catch (Exception e) {
152: throw new EJBException(e.getMessage());
153: }
154:
155: }
156:
157: public void createVendorPart(VendorPartRequest vendorPartRequest) {
158: try {
159: PartPK pkey = new PartPK();
160: pkey.partNumber = vendorPartRequest.partNumber;
161: pkey.revision = new BigDecimal(vendorPartRequest.revision);
162:
163: PartLocal part = partHome.findByPrimaryKey(pkey);
164: VendorPartLocal vendorPart = vendorPartHome.create(
165: vendorPartRequest.description,
166: vendorPartRequest.price, part);
167:
168: VendorKey vkey = new VendorKey();
169: vkey.vendorId = vendorPartRequest.vendorId;
170:
171: VendorLocal vendor = vendorHome.findByPrimaryKey(vkey);
172: vendorPart.setVendor(vendor);
173: } catch (Exception e) {
174: throw new EJBException(e.getMessage());
175: }
176: }
177:
178: public void createOrder(OrderRequest orderRequest) {
179: try {
180: OrderLocal order = orderHome.create(orderRequest.orderId,
181: String.valueOf(orderRequest.status),
182: new BigDecimal(orderRequest.discount),
183: orderRequest.shipmentInfo);
184: } catch (Exception e) {
185: throw new EJBException(e.getMessage());
186: }
187:
188: }
189:
190: public void addLineItem(LineItemRequest lineItemRequest) {
191: try {
192: OrderLocal order = orderHome
193: .findByPrimaryKey(lineItemRequest.orderId);
194:
195: PartPK pkey = new PartPK();
196: pkey.partNumber = lineItemRequest.partNumber;
197: pkey.revision = new BigDecimal(lineItemRequest.revision);
198:
199: PartLocal part = partHome.findByPrimaryKey(pkey);
200:
201: LineItemLocal lineItem = lineItemHome.create(order,
202: new BigDecimal(lineItemRequest.quantity), part
203: .getVendorPartBean());
204: } catch (Exception e) {
205: throw new EJBException(e.getMessage());
206: }
207: }
208:
209: public double getBillOfMaterialPrice(BomRequest bomRequest) {
210: double price = 0.0;
211:
212: try {
213: PartPK bomkey = new PartPK();
214: bomkey.partNumber = bomRequest.bomPartNumber;
215: bomkey.revision = new BigDecimal(bomRequest.bomRevision);
216:
217: PartLocal bom = partHome.findByPrimaryKey(bomkey);
218: Collection parts = bom.getPartBean1();
219:
220: for (Iterator iterator = parts.iterator(); iterator
221: .hasNext();) {
222: PartLocal part = (PartLocal) iterator.next();
223: VendorPartLocal vendorPart = part.getVendorPartBean();
224: price += vendorPart.getPrice().doubleValue();
225: }
226: } catch (Exception e) {
227: throw new EJBException(e.getMessage());
228: }
229:
230: return price;
231: }
232:
233: public double getOrderPrice(Integer orderId) {
234: double price = 0.0;
235: try {
236: OrderLocal order = orderHome.findByPrimaryKey(orderId);
237: price = order.calculateAmmount();
238: } catch (Exception e) {
239: throw new EJBException(e.getMessage());
240: }
241: return price;
242: }
243:
244: public void adjustOrderDiscount(int adjustment) {
245: orderHome.adjustDiscount(adjustment);
246: }
247:
248: public Double getAvgPrice() {
249: Double avgPrice = new Double(0);
250: try {
251: avgPrice = vendorPartHome.getAvgPrice();
252: } catch (FinderException ex) {
253: Logger.getLogger(getClass().getName()).log(Level.SEVERE,
254: ex.getMessage());
255: throw new EJBException(ex.getMessage());
256: }
257: return avgPrice;
258: }
259:
260: public Double getTotalPricePerVendor(VendorRequest vendorRequest) {
261: return vendorPartHome
262: .getTotalPricePerVendor(vendorRequest.vendorId);
263: }
264:
265: public Collection locateVendorsByPartialName(String name) {
266: Collection names = new ArrayList();
267:
268: try {
269: Collection vendors = vendorHome.findByPartialName(name);
270: for (Iterator iterator = vendors.iterator(); iterator
271: .hasNext();) {
272: VendorLocal vendor = (VendorLocal) iterator.next();
273: names.add(vendor.getName());
274: }
275: } catch (FinderException e) {
276: }
277:
278: return names;
279: }
280:
281: public int countAllItems() {
282: int count = 0;
283: try {
284: count = lineItemHome.findAll().size();
285: } catch (Exception e) {
286: throw new EJBException(e.getMessage());
287: }
288: return count;
289: }
290:
291: public void removeOrder(Integer orderId) {
292: try {
293: orderHome.remove(orderId);
294: } catch (Exception e) {
295: throw new EJBException(e.getMessage());
296: }
297: }
298:
299: public String reportVendorsByOrder(Integer orderId) {
300: StringBuffer report = new StringBuffer();
301: try {
302: Collection vendors = vendorHome.findByOrder(orderId);
303:
304: for (Iterator iterator = vendors.iterator(); iterator
305: .hasNext();) {
306: VendorLocal vendor = (VendorLocal) iterator.next();
307: report.append(vendor.getVendorId()).append(' ').append(
308: vendor.getName()).append(' ').append(
309: vendor.getContact()).append('\n');
310: }
311: } catch (FinderException e) {
312: throw new EJBException(e.getMessage());
313: }
314: return report.toString();
315:
316: }
317:
318: private LineItemLocalHome lookupLineitem() {
319: try {
320: Context c = new InitialContext();
321: LineItemLocalHome rv = (LineItemLocalHome) c
322: .lookup("java:comp/env/ejb/Lineitem");
323: return rv;
324: } catch (NamingException ne) {
325: Logger.getLogger(getClass().getName()).log(
326: java.util.logging.Level.SEVERE, "exception caught",
327: ne);
328: throw new RuntimeException(ne);
329: }
330: }
331:
332: private dataregistry.OrderLocalHome lookupOrder() {
333: try {
334: Context c = new InitialContext();
335: OrderLocalHome rv = (OrderLocalHome) c
336: .lookup("java:comp/env/ejb/Order");
337: return rv;
338: } catch (NamingException ne) {
339: Logger.getLogger(getClass().getName()).log(
340: java.util.logging.Level.SEVERE, "exception caught",
341: ne);
342: throw new RuntimeException(ne);
343: }
344: }
345:
346: private PartLocalHome lookupPart() {
347: try {
348: Context c = new InitialContext();
349: PartLocalHome rv = (PartLocalHome) c
350: .lookup("java:comp/env/ejb/Part");
351: return rv;
352: } catch (NamingException ne) {
353: Logger.getLogger(getClass().getName()).log(
354: java.util.logging.Level.SEVERE, "exception caught",
355: ne);
356: throw new RuntimeException(ne);
357: }
358: }
359:
360: private VendorLocalHome lookupVendor() {
361: try {
362: Context c = new InitialContext();
363: VendorLocalHome rv = (VendorLocalHome) c
364: .lookup("java:comp/env/ejb/Vendor");
365: return rv;
366: } catch (NamingException ne) {
367: Logger.getLogger(getClass().getName()).log(
368: java.util.logging.Level.SEVERE, "exception caught",
369: ne);
370: throw new RuntimeException(ne);
371: }
372: }
373:
374: private VendorPartLocalHome lookupVendorPart() {
375: try {
376: Context c = new InitialContext();
377: VendorPartLocalHome rv = (VendorPartLocalHome) c
378: .lookup("java:comp/env/ejb/VendorPart");
379: return rv;
380: } catch (NamingException ne) {
381: Logger.getLogger(getClass().getName()).log(
382: java.util.logging.Level.SEVERE, "exception caught",
383: ne);
384: throw new RuntimeException(ne);
385: }
386: }
387:
388: }
|