KickJava   Java API By Example, From Geeks To Geeks.

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


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 Lodging implements Serializable{
51
52   protected String JavaDoc lodgingId;
53   protected String JavaDoc name;
54   protected float pricePerNight;
55   protected String JavaDoc location;
56   protected Calendar startDate;
57   protected Calendar endDate;
58   protected int noNights;
59   protected int noRooms;
60
61   // Constructor
62
public Lodging() {}
63
64   public Lodging(String JavaDoc lodgingId, String JavaDoc name, float pricePerNight,
65                  String JavaDoc location, Calendar startDate, Calendar endDate, int noNights,
66                  int noRooms) {
67       this.lodgingId = lodgingId;
68       this.name = name;
69       this.pricePerNight = pricePerNight;
70       this.location = location;
71       this.startDate = startDate;
72       this.endDate = endDate;
73       this.noNights = noNights;
74       this.noRooms = noRooms;
75
76   }
77
78   // getter methods
79
public String JavaDoc getLodgingId() {
80       return lodgingId;
81   }
82
83   public String JavaDoc getName() {
84       return name;
85   }
86
87   public float getPricePerNight() {
88       return pricePerNight;
89   }
90
91   public String JavaDoc getLocation() {
92       return location;
93   }
94
95   public Calendar getStartDate() {
96       return startDate;
97   }
98
99   public Calendar getEndDate() {
100       return endDate;
101   }
102
103   public int getNoNights() {
104       return noNights;
105   }
106
107   public int getNoRooms() {
108       return noRooms;
109   }
110
111   // setter methods
112
public void setLodgingId(String JavaDoc lodgingId) {
113       this.lodgingId = lodgingId;
114   }
115
116   public void setName(String JavaDoc name) {
117       this.name = name;
118   }
119
120   public void setPricePerNight(float pricePerNight) {
121       this.pricePerNight = pricePerNight;
122   }
123
124   public void setLocation(String JavaDoc location) {
125       this.location = location;
126   }
127
128   public void setStartDate(Calendar startDate) {
129       this.startDate = startDate;
130   }
131
132   public void setEndDate(Calendar endDate) {
133       this.endDate = endDate;
134   }
135
136   public void setNoNights(int noNights) {
137       this.noNights = noNights;
138   }
139
140   public void setNoRooms(int noRooms) {
141       this.noRooms = noRooms;
142   }
143   
144   //XML serialization methods
145
public String JavaDoc toXML(String JavaDoc poId) throws XMLException{
146           
147       String JavaDoc lodgingPO = null;
148       try{
149           
150           //construct the DOM tree
151
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
152           docBuilderFactory.setNamespaceAware(true);
153           DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
154           Document doc = docBuilder.newDocument();
155           Element lodgingElem = doc.createElement("Lodging");
156           doc.appendChild(lodgingElem);
157           Element elem = doc.createElement("OPCPoId");
158           elem.appendChild(doc.createTextNode(poId));
159           lodgingElem.appendChild(elem);
160           elem = doc.createElement("LodgingId");
161           elem.appendChild(doc.createTextNode(lodgingId));
162           lodgingElem.appendChild(elem);
163           elem = doc.createElement("StartDate");
164           elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(startDate.getTime())));
165           lodgingElem.appendChild(elem);
166           elem = doc.createElement("EndDate");
167           elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(endDate.getTime())));
168           lodgingElem.appendChild(elem);
169           elem = doc.createElement("Nights");
170           elem.appendChild(doc.createTextNode(Integer.toString(noNights)));
171           lodgingElem.appendChild(elem);
172           elem = doc.createElement("Rooms");
173           elem.appendChild(doc.createTextNode(Integer.toString(noRooms)));
174           lodgingElem.appendChild(elem);
175          
176           //process the source tree
177
ByteArrayOutputStream baStream = new ByteArrayOutputStream();
178           Result res = new StreamResult(baStream);
179           TransformerFactory transFactory = TransformerFactory.newInstance();
180           Transformer transformer = transFactory.newTransformer();
181           transformer.setOutputProperty(OutputKeys.METHOD, "xml");
182           transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
183       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
184       transformer.transform(new DOMSource(doc), res);
185           lodgingPO = baStream.toString("UTF-8");
186           
187       } catch(Exception JavaDoc exe){
188           throw new XMLException(exe);
189       }
190       return lodgingPO;
191   }
192
193 }
194
Popular Tags