KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > admin > client > WebServicePetStoreProxy


1 /*
2  * Copyright 2002 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.admin.client;
38
39 import java.rmi.RemoteException;
40 import java.util.Collection;
41 import java.util.ArrayList;
42 import java.util.Iterator;
43 import java.util.Date;
44 import java.util.Calendar;
45 import java.util.GregorianCalendar;
46 import java.util.Map;
47 import java.text.SimpleDateFormat;
48 import java.text.ParseException;
49
50 import javax.xml.rpc.Stub;
51 import javax.xml.rpc.ServiceException;
52
53 import java.net.URL;
54
55 import com.sun.j2ee.blueprints.admin.webservice.AdminServiceImplService;
56 import com.sun.j2ee.blueprints.admin.webservice.AdminService_Order;
57 import com.sun.j2ee.blueprints.admin.webservice.AdminService_OrdersTO;
58 import com.sun.j2ee.blueprints.admin.webservice.AdminService_OrderApprovalTO;
59 import com.sun.j2ee.blueprints.admin.webservice.AdminServiceImpl;
60 import com.sun.j2ee.blueprints.admin.webservice.AdminServiceImplServiceLocator;
61 import com.sun.j2ee.blueprints.admin.webservice.AdminWebServiceSoapBindingStub;
62
63
64 public class WebServicePetStoreProxy implements PetStoreProxy {
65
66     private AdminServiceImpl adminws = null;
67     private String server = null;
68     private String port = null;
69     private String endpoint = null;
70     private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
71
72     public WebServicePetStoreProxy() { }
73
74     public void setup(String server, String port, String endpoint) {
75         this.server = server;
76         this.port = port;
77         this.endpoint = endpoint;
78     }
79
80     private AdminServiceImpl getAdminService() {
81         /*if (adminws == null) {
82             String serviceURL = "http://" + server + ":" + port + endpoint;
83             Stub stub = (Stub) (new AdminWebService_Impl().getAdminServicePort());
84             stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
85             adminws = (AdminService) stub;
86         }*/

87         try {
88             if (adminws == null) {
89                 adminws = (new AdminServiceImplServiceLocator()).getAdminWebService();
90             }
91         } catch (Exception se) {
92             se.printStackTrace();
93         }
94         
95         return adminws;
96     }
97
98     public PetStoreProxy.Order[] getOrders(String status) {
99         PetStoreProxy.Order[] orders = null;
100         try {
101             AdminService_OrdersTO to = getAdminService().getOrdersByStatus(status);
102             if (to == null) {
103                 // TBD: handle properly
104
} else {
105                 orders = convert(to.getOrders());
106             }
107         } catch (Exception ex) {
108             ex.printStackTrace();
109         }
110         return orders;
111     }
112
113     private PetStoreProxy.Order[] convert(AdminService_Order[] orders) {
114         PetStoreProxy.Order[] converted = new PetStoreProxy.Order[orders.length];
115         for (int i = 0; i < orders.length; ++i) {
116             AdminService_Order o = orders[i];
117             converted[i] = new PetStoreProxy.Order(o.getOrderId(), o.getUserId(),
118             parseDate(o.getOrderDate()), o.getOrderValue(), o.getOrderStatus());
119         }
120         return converted;
121     }
122
123     private Date parseDate(String s) {
124         try {
125             return dateFormat.parse(s);
126         } catch (ParseException pe) {
127             // TBD: Just hard-code some date for now
128
pe.printStackTrace();
129             return new Date();
130         }
131     }
132
133     // TBD: check for correctness
134
private Calendar convert(Date date) {
135         Calendar cal = new GregorianCalendar();
136         cal.setTime(date);
137         return cal;
138     }
139
140     public PetStoreProxy.Sales[] getRevenue(Date start, Date end, String category) {
141         try {
142             Map chartInfo = getAdminService().getChartInfo("REVENUE", convert(start), convert(end), category);
143             Collection keys = chartInfo.keySet();
144             ArrayList sales = new ArrayList(keys.size());
145             for (Iterator it = keys.iterator(); it.hasNext(); ) {
146                 String key = (String) it.next();
147                 float revenue = ((Float) chartInfo.get(key)).floatValue();
148                 if (revenue >= 0.0) {
149                     PetStoreProxy.Sales sale = new PetStoreProxy.Sales(key, revenue);
150                     sales.add(sale);
151                 }
152             }
153             return (PetStoreProxy.Sales[])(sales.toArray(new PetStoreProxy.Sales[sales.size()]));
154         } catch (RemoteException re) {
155             re.printStackTrace();
156             return null;
157         }
158     }
159
160     public PetStoreProxy.Sales[] getOrders(Date start, Date end, String category) {
161         try {
162             Map chartInfo = getAdminService().getChartInfo("ORDERS", convert(start), convert(end), category);
163             Collection keys = chartInfo.keySet();
164             ArrayList sales = new ArrayList(keys.size());
165             for (Iterator it = keys.iterator(); it.hasNext(); ) {
166                 String key = (String) it.next();
167                 int nOrders = ((Integer) chartInfo.get(key)).intValue();
168                 if (nOrders >= 0) {
169                     PetStoreProxy.Sales sale = new PetStoreProxy.Sales(key, nOrders);
170                     sales.add(sale);
171                 }
172             }
173             return (PetStoreProxy.Sales[])(sales.toArray(new PetStoreProxy.Sales[sales.size()]));
174         } catch (RemoteException re) {
175             re.printStackTrace();
176             return null;
177         }
178     }
179
180     public void updateStatus(PetStoreProxy.Order[] orders, String status) {
181         try {
182             String[] orderIds = new String[orders.length];
183             String[] statuses = new String[orders.length];
184             for (int i = 0; i < orders.length; ++i) {
185                 orderIds[i] = orders[i].getId();
186                 statuses[i] = status;
187             }
188             AdminService_OrderApprovalTO to = new AdminService_OrderApprovalTO();
189             to.setOrderIds(orderIds);
190             to.setStatuses(statuses);
191             getAdminService().update(to);
192         } catch (RemoteException re) {
193             // TBD: need better handling
194
re.printStackTrace();
195         }
196     }
197
198     public void updateStatus(PetStoreProxy.Order order, String status) {
199         try {
200             getAdminService().update(order.getId(), status);
201         } catch (RemoteException re) {
202             // TBD: need better handling
203
re.printStackTrace();
204         }
205     }
206 }
207
Popular Tags