KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > lodgingsupplier > powebservice > LodgingOrder


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 package com.sun.j2ee.blueprints.lodgingsupplier.powebservice;
38
39 import java.util.*;
40 import java.text.*;
41 import java.io.*;
42 import javax.xml.parsers.*;
43 import org.w3c.dom.*;
44 import org.xml.sax.*;
45 import javax.xml.transform.*;
46 import javax.xml.transform.dom.*;
47 import javax.xml.transform.stream.*;
48
49 public class LodgingOrder implements Serializable {
50
51     protected String JavaDoc orderId;
52     protected String JavaDoc lodgingId;
53     protected Calendar startDate;
54     protected Calendar endDate;
55     protected int headCount;
56
57     // Constructor
58
public LodgingOrder() {}
59
60     public LodgingOrder(String JavaDoc orderId, String JavaDoc lodgingId, Calendar startDate,
61                          Calendar endDate, int headCount) {
62         this.orderId = orderId;
63         this.lodgingId = lodgingId;
64         this.startDate = startDate;
65         this.endDate = endDate;
66         this.headCount = headCount;
67     }
68
69     // getter methods
70
public String JavaDoc getOrderId() {
71         return orderId;
72     }
73
74     public String JavaDoc getLodgingId() {
75         return lodgingId;
76     }
77
78     public Calendar getStartDate() {
79         return startDate;
80     }
81
82     public Calendar getEndDate() {
83         return endDate;
84     }
85
86     public int getHeadCount() {
87         return headCount;
88     }
89
90     // setter methods
91
public void setOrderId(String JavaDoc id) {
92         this.orderId = id;
93     }
94
95     public void setLodgingId(String JavaDoc id) {
96         this.lodgingId = id;
97     }
98
99     public void setStartDate(Calendar startDate) {
100         this.startDate = startDate;
101     }
102
103     public void setEndDate(Calendar endDate) {
104         this.endDate = endDate;
105     }
106
107     public void setHeadCount(int headCount) {
108         this.headCount = headCount;
109     }
110
111     public static LodgingOrder fromXML(String JavaDoc lodgyPO)
112         throws InvalidOrderException{
113       
114         LodgingOrder lodgy = null;
115         try {
116           
117             InputSource source = new InputSource(new StringReader(lodgyPO));
118             DocumentBuilderFactory docBuilderFactory =
119     DocumentBuilderFactory.newInstance();
120             docBuilderFactory.setNamespaceAware(true);
121             DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
122             //parse the source doc and extract details
123
Document doc = docBuilder.parse(source);
124             Element elem = (Element)doc.getDocumentElement().getFirstChild().getNextSibling();
125             lodgy = new LodgingOrder();
126             lodgy.setOrderId(((Text)(elem.getFirstChild())).getData());
127             elem = getNextSibling(elem);
128             lodgy.setLodgingId(((Text)(elem.getFirstChild())).getData());
129             elem = getNextSibling(elem);
130             Date date = new SimpleDateFormat("MM-dd-yy").parse(((Text)(elem.getFirstChild())).getData());
131             Calendar cal = Calendar.getInstance();
132             cal.setTime(date);
133             lodgy.setStartDate(cal);
134             elem = getNextSibling(elem);
135             date = new SimpleDateFormat("MM-dd-yy").parse(((Text)(elem.getFirstChild())).getData());
136             cal = Calendar.getInstance();
137             cal.setTime(date);
138             lodgy.setEndDate(cal);
139             elem = getNextSibling(elem);
140             lodgy.setHeadCount(Integer.parseInt(((Text)(elem.getFirstChild())).getData()));
141         } catch(Exception JavaDoc exe){
142             exe.printStackTrace(System.err);
143             throw new InvalidOrderException("PO for Lodging not valid : " +
144                                             exe.getMessage());
145         }
146         return lodgy;
147     }
148     
149     public static Element getNextSibling(Element elem) {
150         for(Node sib=elem.getNextSibling(); sib!=null; sib=sib.getNextSibling()){
151             if(sib.getNodeType() == Node.ELEMENT_NODE){
152                 return (Element) sib;
153             }
154         }
155         return null;
156     }
157 }
158
159
Popular Tags