KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > activitysupplier > powebservice > ActivityOrder


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.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 ActivityOrder implements Serializable {
51
52     protected String JavaDoc orderId;
53     protected ArrayList activities;
54
55     // Constructor
56
public ActivityOrder() {}
57
58     // getter methods
59
public String JavaDoc getOrderId() {
60   return orderId;
61     }
62
63     public ArrayList getActivities() {
64   return activities;
65     }
66
67     // setter methods
68
public void setOrderId(String JavaDoc id) {
69   this.orderId = id;
70     }
71
72     public void setActivities(ArrayList acts) {
73   this.activities = acts;
74     }
75
76     public static ActivityOrder fromXML(String JavaDoc actyPO) throws
77                                           InvalidOrderException{
78       
79   ActivityOrder order = null;
80   String JavaDoc opcPoId = null;
81   ArrayList acts = new ArrayList();
82   try {
83       
84       InputSource source = new InputSource(new StringReader(actyPO));
85       DocumentBuilderFactory docBuilderFactory =
86                        DocumentBuilderFactory.newInstance();
87       docBuilderFactory.setNamespaceAware(true);
88       DocumentBuilder docBuilder =
89                        docBuilderFactory.newDocumentBuilder();
90       
91       //parse the source doc and extract details
92
Document doc = docBuilder.parse(source);
93       Element elem =
94       (Element)doc.getDocumentElement().getFirstChild().getNextSibling();
95       opcPoId = ((Text)(elem.getFirstChild())).getData();
96       for (elem = getNextSibling(elem); elem != null;
97                             elem = getNextSibling(elem)) {
98           ActivityDetails acty = new ActivityDetails();
99           Element actyelem = (Element)elem.getFirstChild().getNextSibling();
100     acty.setActivityId(((Text)(actyelem.getFirstChild())).getData());
101           actyelem = getNextSibling(actyelem);
102           Date date = new SimpleDateFormat("MM-dd-yy").parse(((Text)(actyelem.getFirstChild())).getData());
103           Calendar cal = Calendar.getInstance();
104           cal.setTime(date);
105           acty.setStartDate(cal);
106           actyelem = getNextSibling(actyelem);
107     date = new SimpleDateFormat("MM-dd-yy").parse(((Text)(actyelem.getFirstChild())).getData());
108           cal = Calendar.getInstance();
109           cal.setTime(date);
110           acty.setEndDate(cal);
111           actyelem = getNextSibling(actyelem);
112             acty.setHeadCount(Integer.parseInt(((Text)(actyelem.getFirstChild())).getData()));
113     acts.add(acty);
114       }
115   } catch(Exception JavaDoc exe){
116       exe.printStackTrace(System.err);
117       throw new InvalidOrderException("PO for Activity not valid : " +
118               exe.getMessage());
119   }
120   if(acts.size() != 0) {
121       order = new ActivityOrder();
122       order.setOrderId(opcPoId);
123       order.setActivities(acts);
124   }
125   return order;
126     }
127     
128     public static Element getNextSibling(Element elem) {
129   for(Node sib=elem.getNextSibling(); sib!=null;
130                         sib=sib.getNextSibling()){
131       if(sib.getNodeType() == Node.ELEMENT_NODE){
132     return (Element) sib;
133       }
134   }
135   return null;
136     }
137 }
138
Popular Tags