KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > airlinesupplier > powebservice > AirlineOrder


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.airlinesupplier.powebservice;
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 AirlineOrder implements Serializable {
51
52     protected String JavaDoc orderId;
53     protected String JavaDoc depFlightId;
54     protected Calendar depFlightDate;
55     protected String JavaDoc retFlightId;
56     protected Calendar retFlightDate;
57     protected int headCount;
58
59     // Constructor
60
public AirlineOrder() {}
61
62     public AirlineOrder(String JavaDoc orderId, String JavaDoc depFlightId,
63       Calendar depFlightDate, String JavaDoc retFlightId,
64       Calendar retFlightDate, int headCount) {
65         this.orderId = orderId;
66         this.depFlightId = depFlightId;
67         this.depFlightDate = depFlightDate;
68         this.retFlightId = retFlightId;
69         this.retFlightDate = retFlightDate;
70         this.headCount = headCount;
71     }
72
73     // getter methods
74
public String JavaDoc getOrderId() {
75         return orderId;
76     }
77
78     public String JavaDoc getDepFlightId() {
79         return depFlightId;
80     }
81
82     public Calendar getDepFlightDate() {
83         return depFlightDate;
84     }
85
86     public String JavaDoc getRetFlightId() {
87         return retFlightId;
88     }
89
90     public Calendar getRetFlightDate() {
91         return retFlightDate;
92     }
93
94     public int getHeadCount() {
95         return headCount;
96     }
97
98     // setter methods
99
public void setOrderId(String JavaDoc id) {
100         this.orderId = id;
101     }
102
103     public void setDepFlightId(String JavaDoc id) {
104         this.depFlightId = id;
105     }
106
107     public void setDepFlightDate(Calendar depFlightDate) {
108         this.depFlightDate = depFlightDate;
109     }
110
111     public void setRetFlightId(String JavaDoc id) {
112         this.retFlightId = id;
113     }
114
115     public void setRetFlightDate(Calendar retFlightDate) {
116         this.retFlightDate = retFlightDate;
117     }
118
119     public void setHeadCount(int headCount) {
120         this.headCount = headCount;
121     }
122
123     public static AirlineOrder fromXML(String JavaDoc flightPO)
124         throws InvalidOrderException{
125       
126         AirlineOrder flight = null;
127         try {
128           
129             InputSource source = new InputSource(new StringReader(flightPO));
130             DocumentBuilderFactory docBuilderFactory =
131     DocumentBuilderFactory.newInstance();
132             docBuilderFactory.setNamespaceAware(true);
133             DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
134             //parse the source doc and extract details
135
Document doc = docBuilder.parse(source);
136             Element elem = (Element)doc.getDocumentElement().getFirstChild().getNextSibling();
137             flight = new AirlineOrder();
138
139       // OrderId
140
flight.setOrderId(((Text)(elem.getFirstChild())).getData());
141
142       // DepFlightId
143
elem = getNextSibling(elem);
144             flight.setDepFlightId(((Text)(elem.getFirstChild())).getData());
145
146       // DepFlightDate
147
elem = getNextSibling(elem);
148             Date date = new SimpleDateFormat("MM-dd-yy").parse(((Text)(elem.getFirstChild())).getData());
149             Calendar cal = Calendar.getInstance();
150             cal.setTime(date);
151             flight.setDepFlightDate(cal);
152
153       // RetFlightId
154
elem = getNextSibling(elem);
155             flight.setRetFlightId(((Text)(elem.getFirstChild())).getData());
156
157       // RetFlightDate
158
elem = getNextSibling(elem);
159             date = new SimpleDateFormat("MM-dd-yy").parse(((Text)(elem.getFirstChild())).getData());
160             cal = Calendar.getInstance();
161             cal.setTime(date);
162             flight.setRetFlightDate(cal);
163
164       // HeadCount
165
elem = getNextSibling(elem);
166             flight.setHeadCount(Integer.parseInt(((Text)(elem.getFirstChild())).getData()));
167         } catch(Exception JavaDoc exe){
168             exe.printStackTrace(System.err);
169             throw new InvalidOrderException("PO for Airline not valid : " +
170                                             exe.getMessage());
171         }
172         return flight;
173     }
174     
175     public static Element getNextSibling(Element elem) {
176         for(Node sib=elem.getNextSibling(); sib!=null; sib=sib.getNextSibling()){
177             if(sib.getNodeType() == Node.ELEMENT_NODE){
178                 return (Element) sib;
179             }
180         }
181         return null;
182     }
183 }
184
185
Popular Tags