KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > opc > purchaseorder > Transportation


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.purchaseorder;
39
40 import java.util.*;
41 import java.text.*;
42 import java.io.*;
43 import javax.xml.parsers.*;
44 import org.w3c.dom.*;
45 import org.xml.sax.*;
46 import javax.xml.transform.*;
47 import javax.xml.transform.dom.*;
48 import javax.xml.transform.stream.*;
49
50 public class Transportation implements Serializable{
51
52   protected String JavaDoc transportationId;
53   protected String JavaDoc carrier;
54   protected String JavaDoc origin;
55   protected String JavaDoc destination;
56   protected Calendar departureDate;
57   protected String JavaDoc departureTime;
58   protected float price;
59   protected String JavaDoc travelClass;
60   protected int headCount;
61
62   // Constructor
63
public Transportation() {}
64
65   public Transportation(String JavaDoc transportationId, String JavaDoc carrier, String JavaDoc origin,
66                         String JavaDoc destination, Calendar departureDate,
67                         String JavaDoc departureTime, float price, String JavaDoc travelClass, int headCount) {
68     this.transportationId = transportationId;
69     this.carrier = carrier;
70     this.origin = origin;
71     this.destination = destination;
72     this.departureDate = departureDate;
73     this.price = price;
74     this.travelClass = travelClass;
75     this.departureTime = departureTime;
76     this.headCount = headCount;
77   }
78
79   // getter methods
80
public String JavaDoc getTransportationId() {
81     return transportationId;
82   }
83
84   public String JavaDoc getCarrier() {
85     return carrier;
86   }
87
88   public String JavaDoc getOrigin() {
89     return origin;
90   }
91
92   public String JavaDoc getDestination() {
93     return destination;
94   }
95
96   public Calendar getDepartureDate() {
97     return departureDate;
98   }
99
100   public String JavaDoc getDepartureTime() {
101     return departureTime;
102   }
103
104   public float getPrice() {
105     return price;
106   }
107
108   public String JavaDoc getTravelClass() {
109     return travelClass;
110   }
111   public int getHeadCount() {
112     return headCount;
113   }
114
115   // setter methods
116
public void setTransportationId(String JavaDoc transportationId) {
117     this.transportationId = transportationId;
118   }
119
120   public void setCarrier(String JavaDoc carrier) {
121     this.carrier = carrier;
122   }
123
124   public void setOrigin(String JavaDoc origin) {
125     this.origin = origin;
126   }
127
128   public void setDestination(String JavaDoc destination) {
129     this.destination = destination;
130   }
131
132   public void setDepartureDate(Calendar departureDate) {
133     this.departureDate = departureDate;
134   }
135
136   public void setDepartureTime(String JavaDoc departureTime) {
137     this.departureTime = departureTime;
138   }
139
140   public void setPrice(float price) {
141     this.price = price;
142   }
143
144   public void setTravelClass(String JavaDoc travelClass) {
145     this.travelClass = travelClass;
146   }
147
148   public void setHeadCount(int headCount) {
149     this.headCount = headCount;
150   }
151
152   //XML serialization methods
153
public String JavaDoc toXML(String JavaDoc poId) throws XMLException{
154           
155       String JavaDoc transportPO = null;
156       try{
157           
158           //construct the DOM tree
159
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
160           docBuilderFactory.setNamespaceAware(true);
161           DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
162           Document doc = docBuilder.newDocument();
163           Element transportElem = doc.createElement("Transportation");
164           doc.appendChild(transportElem);
165           Element elem = doc.createElement("OPCPoId");
166           elem.appendChild(doc.createTextNode(poId));
167           transportElem.appendChild(elem);
168           elem = doc.createElement("TransportationId");
169           elem.appendChild(doc.createTextNode(transportationId));
170           transportElem.appendChild(elem);
171           elem = doc.createElement("DepartureDate");
172           elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(departureDate.getTime())));
173           transportElem.appendChild(elem);
174           elem = doc.createElement("HeadCount");
175           elem.appendChild(doc.createTextNode(Integer.toString(headCount)));
176           transportElem.appendChild(elem);
177          
178           //process the source tree
179
ByteArrayOutputStream baStream = new ByteArrayOutputStream();
180           Result res = new StreamResult(baStream);
181           TransformerFactory transFactory = TransformerFactory.newInstance();
182           Transformer transformer = transFactory.newTransformer();
183           transformer.setOutputProperty(OutputKeys.METHOD, "xml");
184           transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
185       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
186       transformer.transform(new DOMSource(doc), res);
187           transportPO = baStream.toString("UTF-8");
188           
189       } catch(Exception JavaDoc exe){
190           throw new XMLException(exe);
191       }
192       return transportPO;
193   }
194 }
195
Popular Tags