KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > activitysupplier > pomessagebean > Invoice


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.activitysupplier.pomessagebean;
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 import com.sun.j2ee.blueprints.activitysupplier.powebservice.*;
50
51 public class Invoice implements Serializable {
52
53     protected String JavaDoc invoiceId;
54     protected String JavaDoc opcPoId;
55     protected String JavaDoc supplierId;
56     protected ActivityOrder actOrder;
57     protected String JavaDoc status;
58
59     // Constructor
60
public Invoice() {}
61
62     public Invoice(String JavaDoc invoiceId,
63                                 String JavaDoc opcPoId,
64                                 String JavaDoc supplier,
65                                 ActivityOrder actOrder,
66                                 String JavaDoc status) {
67   this.invoiceId = invoiceId;
68   this.opcPoId = opcPoId;
69   this.supplierId = supplier;
70         this.actOrder = actOrder;
71   this.status = status;
72     }
73
74     // getter methods
75
public String JavaDoc getInvoiceId() {
76   return invoiceId;
77     }
78
79     public String JavaDoc getOpcPoId() {
80   return opcPoId;
81     }
82
83     public String JavaDoc getSupplierId() {
84   return supplierId;
85     }
86
87     public String JavaDoc getStatus() {
88   return status;
89     }
90
91     // setter methods
92
public void setInvoiceId(String JavaDoc invoiceId) {
93   this.invoiceId = invoiceId;
94     }
95
96     public void setOpcPoId(String JavaDoc id) {
97   this.opcPoId = id;
98     }
99
100     public void setSupplierId(String JavaDoc id) {
101   this.supplierId = id;
102     }
103
104     public void setStatus(String JavaDoc stat) {
105   this.status = stat;
106     }
107
108     //XML serialization/deserialization methods
109
public String JavaDoc toXML() throws ParserConfigurationException,
110                                  TransformerConfigurationException,
111                                  TransformerException,
112                                  UnsupportedEncodingException{
113   String JavaDoc inv = null;
114
115   //construct the DOM tree
116
SimpleDateFormat df =
117       new SimpleDateFormat("yyyy/MM/dd E HH:mm.ss a");
118   DocumentBuilderFactory docBuilderFactory =
119       DocumentBuilderFactory.newInstance();
120   docBuilderFactory.setNamespaceAware(true);
121   DocumentBuilder docBuilder =
122       docBuilderFactory.newDocumentBuilder();
123   Document doc = docBuilder.newDocument();
124   Element invElem = doc.createElement("Invoice");
125   invElem.setAttribute("xmlns:xsi",
126            "http://www.w3.org/2001/XMLSchema-instance");
127   invElem.setAttribute("xsi:schemaLocation",
128     "http://java.sun.com/blueprints/ns/invoice " +
129     "http://java.sun.com/blueprints/schemas/invoice-activity.xsd");
130   invElem.setAttribute("xmlns",
131          "http://java.sun.com/blueprints/ns/invoice");
132   doc.appendChild(invElem);
133   Element elem = doc.createElement("ID");
134   elem.appendChild(doc.createTextNode(invoiceId));
135   invElem.appendChild(elem);
136   elem = doc.createElement("OPCPoId");
137   elem.appendChild(doc.createTextNode(opcPoId));
138   invElem.appendChild(elem);
139   elem = doc.createElement("SupplierId");
140   elem.appendChild(doc.createTextNode(supplierId));
141   invElem.appendChild(elem);
142   // create the activities
143
Element acts = doc.createElement("activities");
144             
145   // go through the list of activities
146
ArrayList actList = actOrder.getActivities();
147   Iterator it = null;
148   if (actList != null) it = actList.iterator();
149   while ((it != null) && it.hasNext()) {
150       ActivityDetails ad = (ActivityDetails)it.next();
151       Element act = doc.createElement("activity");
152       Element actElem = doc.createElement("ACT-ID");
153       actElem.appendChild(doc.createTextNode(ad.getActivityId()));
154       act.appendChild(actElem);
155       actElem = doc.createElement("Start-Date");
156       actElem.appendChild(doc.createTextNode(df.format(ad.getStartDate().getTime())));
157       act.appendChild(actElem);
158       actElem = doc.createElement("End-Date");
159       actElem.appendChild(doc.createTextNode(df.format(ad.getEndDate().getTime())));
160       act.appendChild(actElem);
161       actElem = doc.createElement("Head-Count");
162       actElem.appendChild(doc.createTextNode(ad.getHeadCount() + ""));
163       act.appendChild(actElem);
164       acts.appendChild(act);
165   }
166   // add the activitiy to the activities
167
invElem.appendChild(acts);
168             
169   elem = doc.createElement("Status");
170   elem.appendChild(doc.createTextNode(status));
171   invElem.appendChild(elem);
172                    
173   //process the source tree
174
ByteArrayOutputStream baStream = new ByteArrayOutputStream();
175   Result res = new StreamResult(baStream);
176   TransformerFactory transFactory = TransformerFactory.newInstance();
177   Transformer transformer = transFactory.newTransformer();
178   transformer.setOutputProperty(OutputKeys.METHOD, "xml");
179   transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
180   transformer.setOutputProperty(OutputKeys.INDENT, "yes");
181   transformer.transform(new DOMSource(doc), res);
182   inv = baStream.toString("UTF-8");
183   return inv;
184     }
185 }
186
Popular Tags