KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > opc > orderfiller > OrderFillerBean


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

37
38 package com.sun.j2ee.blueprints.opc.orderfiller;
39
40 import java.text.*;
41 import java.io.*;
42 import java.util.*;
43 import javax.ejb.*;
44 import javax.jms.*;
45 import javax.xml.parsers.*;
46 import javax.xml.transform.*;
47 import javax.xml.transform.dom.*;
48 import javax.xml.transform.stream.*;
49 import org.w3c.dom.*;
50 import org.xml.sax.*;
51
52 import com.sun.j2ee.blueprints.opc.purchaseorder.*;
53 import com.sun.j2ee.blueprints.opc.JNDINames;
54 import com.sun.j2ee.blueprints.opc.utils.*;
55
56 /**
57  * This component splits a PO and sends string POs
58  * to the Web service broker queue
59  */

60 public class OrderFillerBean implements MessageDrivenBean, MessageListener {
61
62     private MessageDrivenContext context;
63     private PurchaseOrder po;
64  
65     public void setMessageDrivenContext(MessageDrivenContext context) {
66         this.context=context;
67     }
68       
69     public void ejbCreate() {}
70       
71     public void onMessage(Message message) {
72         try {
73             String JavaDoc docType = message.getStringProperty(JNDINames.DOC_TYPE);
74
75             //send the PO to the broker queue
76
if(message instanceof ObjectMessage){
77                 ObjectMessage objMsg = (ObjectMessage) message;
78                 po = (PurchaseOrder)objMsg.getObject();
79             }
80             if(po != null){
81                 sendPO(po);
82             }
83                
84         } catch (Exception JavaDoc exe) {
85             System.err.println(exe);
86             throw new EJBException(exe);
87         }
88     }
89     
90     public void ejbRemove(){}
91
92     private void sendPO(PurchaseOrder po) throws XMLException{
93   
94         //get the POs in xml String format
95
Lodging lodging = po.getLodging();
96         Transportation depFlight = po.getDepartureFlightInfo();
97         Transportation retFlight = po.getReturnFlightInfo();
98         Activity[] activities = po.getActivities();
99         if(lodging != null)
100             JMSUtils.sendMessage(JNDINames.WS_BROKER_MDB_QUEUE,
101                            JNDINames.DOC_TYPE,
102          JNDINames.LODGING_ORDER,
103          lodging.toXML(po.getPoId()));
104         if((depFlight != null) || (retFlight != null))
105             JMSUtils.sendMessage(JNDINames.WS_BROKER_MDB_QUEUE,
106          JNDINames.DOC_TYPE,
107          JNDINames.AIRLINE_ORDER,
108          getTransportationPO(depFlight,
109                                  retFlight, po.getPoId()));
110         if((po.getActivities().length)!= 0)
111             JMSUtils.sendMessage(JNDINames.WS_BROKER_MDB_QUEUE,
112          JNDINames.DOC_TYPE,
113          JNDINames.ACTIVITY_ORDER,
114          getActivityPO(activities, po.getPoId()));
115        
116     }
117
118   private String JavaDoc getActivityPO(Activity[] acts, String JavaDoc poId) throws XMLException{
119       
120       //get an activity PO combining all the activities
121
String JavaDoc actyPO = null;
122       try{
123           
124           //construct the DOM tree
125
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
126           docBuilderFactory.setNamespaceAware(true);
127           DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
128           Document doc = docBuilder.newDocument();
129           Element actsElem = doc.createElement("Activities");
130           doc.appendChild(actsElem);
131           Element elem = doc.createElement("OPCPoId");
132           elem.appendChild(doc.createTextNode(poId));
133           actsElem.appendChild(elem);
134
135           //add all the activities
136
for (int i = 0; i < acts.length; ++i){
137               Element actyElem = doc.createElement("Activity");
138               actsElem.appendChild(actyElem);
139               elem = doc.createElement("ActivityId");
140               elem.appendChild(doc.createTextNode(acts[i].getActivityId()));
141               actyElem.appendChild(elem);
142               elem = doc.createElement("StartDate");
143               elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(acts[i].getStartDate().getTime())));
144               actyElem.appendChild(elem);
145               elem = doc.createElement("EndDate");
146               elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(acts[i].getEndDate().getTime())));
147               actyElem.appendChild(elem);
148               elem = doc.createElement("HeadCount");
149               elem.appendChild(doc.createTextNode(Integer.toString(acts[i].getHeadCount())));
150               actyElem.appendChild(elem);
151           }
152                    
153           //process the source tree
154
ByteArrayOutputStream baStream = new ByteArrayOutputStream();
155           Result res = new StreamResult(baStream);
156           TransformerFactory transFactory = TransformerFactory.newInstance();
157           Transformer transformer = transFactory.newTransformer();
158           transformer.setOutputProperty(OutputKeys.METHOD, "xml");
159           transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
160       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
161       transformer.transform(new DOMSource(doc), res);
162           actyPO = baStream.toString("UTF-8");
163           
164       } catch(Exception JavaDoc exe){
165           throw new XMLException(exe);
166       }
167       return actyPO;
168   }
169
170   private String JavaDoc getTransportationPO(Transportation dep, Transportation ret, String JavaDoc poId) throws XMLException {
171       
172       //get a transportation PO
173
String JavaDoc transportPO = null;
174       try{
175           
176           //construct the DOM tree
177
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
178           docBuilderFactory.setNamespaceAware(true);
179           DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
180           Document doc = docBuilder.newDocument();
181           Element transportElem = doc.createElement("Transportation");
182           doc.appendChild(transportElem);
183           Element elem = doc.createElement("OPCPoId");
184           elem.appendChild(doc.createTextNode(poId));
185           transportElem.appendChild(elem);
186           elem = doc.createElement("DepartureTransportationId");
187           //This is a temporary fix. This is done since the schema for the
188
//supplier PO expects the DepartureTransportationId & DepartureDate
189
//to be always included. The schema has to be modified to properly
190
//validate POs without DepartureTransportationId & DepartureDate
191
if(dep == null){
192               elem.appendChild(doc.createTextNode("null"));
193           } else {
194               elem.appendChild(doc.createTextNode(dep.getTransportationId()));
195           }
196           transportElem.appendChild(elem);
197           elem = doc.createElement("DepartureDate");
198           if(dep == null){
199               elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(Calendar.getInstance().getTime())));
200           } else {
201               elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(dep.getDepartureDate().getTime())));
202           }
203           transportElem.appendChild(elem);
204           elem = doc.createElement("ReturnTransportationId");
205           //This is a temporary fix. This is done since the schema for the
206
//supplier PO expects the ReturnTransportationId & ReturnDate
207
//to be always included. The schema has to be modified to properly
208
//validate POs without ReturnTransportationId & ReturnDate
209
if(ret == null){
210               elem.appendChild(doc.createTextNode("null"));
211           } else {
212               elem.appendChild(doc.createTextNode(ret.getTransportationId()));
213           }
214           transportElem.appendChild(elem);
215           elem = doc.createElement("ReturnDate");
216           if(ret == null){
217               elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(Calendar.getInstance().getTime())));
218           } else {
219               elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(ret.getDepartureDate().getTime())));
220           }
221           transportElem.appendChild(elem);
222           String JavaDoc headCount = dep != null ? Integer.toString(dep.getHeadCount()): Integer.toString(ret.getHeadCount()) ;
223           elem = doc.createElement("HeadCount");
224           elem.appendChild(doc.createTextNode(headCount));
225           transportElem.appendChild(elem);
226          
227           //process the source tree
228
ByteArrayOutputStream baStream = new ByteArrayOutputStream();
229           Result res = new StreamResult(baStream);
230           TransformerFactory transFactory = TransformerFactory.newInstance();
231           Transformer transformer = transFactory.newTransformer();
232           transformer.setOutputProperty(OutputKeys.METHOD, "xml");
233           transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
234       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
235       transformer.transform(new DOMSource(doc), res);
236           transportPO = baStream.toString("UTF-8");
237           
238       } catch(Exception JavaDoc exe){
239           throw new XMLException(exe);
240       }
241       return transportPO;
242   }
243 }
244
245
Popular Tags