KickJava   Java API By Example, From Geeks To Geeks.

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


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