001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037: package com.sun.j2ee.blueprints.admin.client;
038:
039: import java.rmi.RemoteException;
040: import java.util.Collection;
041: import java.util.ArrayList;
042: import java.util.Iterator;
043: import java.util.Date;
044: import java.util.Calendar;
045: import java.util.GregorianCalendar;
046: import java.util.Map;
047: import java.text.SimpleDateFormat;
048: import java.text.ParseException;
049:
050: import javax.xml.rpc.Stub;
051: import javax.xml.rpc.ServiceException;
052:
053: import java.net.URL;
054:
055: import com.sun.j2ee.blueprints.admin.webservice.AdminServiceImplService;
056: import com.sun.j2ee.blueprints.admin.webservice.AdminService_Order;
057: import com.sun.j2ee.blueprints.admin.webservice.AdminService_OrdersTO;
058: import com.sun.j2ee.blueprints.admin.webservice.AdminService_OrderApprovalTO;
059: import com.sun.j2ee.blueprints.admin.webservice.AdminServiceImpl;
060: import com.sun.j2ee.blueprints.admin.webservice.AdminServiceImplServiceLocator;
061: import com.sun.j2ee.blueprints.admin.webservice.AdminWebServiceSoapBindingStub;
062:
063: public class WebServicePetStoreProxy implements PetStoreProxy {
064:
065: private AdminServiceImpl adminws = null;
066: private String server = null;
067: private String port = null;
068: private String endpoint = null;
069: private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
070: "MM/dd/yyyy");
071:
072: public WebServicePetStoreProxy() {
073: }
074:
075: public void setup(String server, String port, String endpoint) {
076: this .server = server;
077: this .port = port;
078: this .endpoint = endpoint;
079: }
080:
081: private AdminServiceImpl getAdminService() {
082: /*if (adminws == null) {
083: String serviceURL = "http://" + server + ":" + port + endpoint;
084: Stub stub = (Stub) (new AdminWebService_Impl().getAdminServicePort());
085: stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
086: adminws = (AdminService) stub;
087: }*/
088: try {
089: if (adminws == null) {
090: adminws = (new AdminServiceImplServiceLocator())
091: .getAdminWebService();
092: }
093: } catch (Exception se) {
094: se.printStackTrace();
095: }
096:
097: return adminws;
098: }
099:
100: public PetStoreProxy.Order[] getOrders(String status) {
101: PetStoreProxy.Order[] orders = null;
102: try {
103: AdminService_OrdersTO to = getAdminService()
104: .getOrdersByStatus(status);
105: if (to == null) {
106: // TBD: handle properly
107: } else {
108: orders = convert(to.getOrders());
109: }
110: } catch (Exception ex) {
111: ex.printStackTrace();
112: }
113: return orders;
114: }
115:
116: private PetStoreProxy.Order[] convert(AdminService_Order[] orders) {
117: PetStoreProxy.Order[] converted = new PetStoreProxy.Order[orders.length];
118: for (int i = 0; i < orders.length; ++i) {
119: AdminService_Order o = orders[i];
120: converted[i] = new PetStoreProxy.Order(o.getOrderId(), o
121: .getUserId(), parseDate(o.getOrderDate()), o
122: .getOrderValue(), o.getOrderStatus());
123: }
124: return converted;
125: }
126:
127: private Date parseDate(String s) {
128: try {
129: return dateFormat.parse(s);
130: } catch (ParseException pe) {
131: // TBD: Just hard-code some date for now
132: pe.printStackTrace();
133: return new Date();
134: }
135: }
136:
137: // TBD: check for correctness
138: private Calendar convert(Date date) {
139: Calendar cal = new GregorianCalendar();
140: cal.setTime(date);
141: return cal;
142: }
143:
144: public PetStoreProxy.Sales[] getRevenue(Date start, Date end,
145: String category) {
146: try {
147: Map chartInfo = getAdminService().getChartInfo("REVENUE",
148: convert(start), convert(end), category);
149: Collection keys = chartInfo.keySet();
150: ArrayList sales = new ArrayList(keys.size());
151: for (Iterator it = keys.iterator(); it.hasNext();) {
152: String key = (String) it.next();
153: float revenue = ((Float) chartInfo.get(key))
154: .floatValue();
155: if (revenue >= 0.0) {
156: PetStoreProxy.Sales sale = new PetStoreProxy.Sales(
157: key, revenue);
158: sales.add(sale);
159: }
160: }
161: return (PetStoreProxy.Sales[]) (sales
162: .toArray(new PetStoreProxy.Sales[sales.size()]));
163: } catch (RemoteException re) {
164: re.printStackTrace();
165: return null;
166: }
167: }
168:
169: public PetStoreProxy.Sales[] getOrders(Date start, Date end,
170: String category) {
171: try {
172: Map chartInfo = getAdminService().getChartInfo("ORDERS",
173: convert(start), convert(end), category);
174: Collection keys = chartInfo.keySet();
175: ArrayList sales = new ArrayList(keys.size());
176: for (Iterator it = keys.iterator(); it.hasNext();) {
177: String key = (String) it.next();
178: int nOrders = ((Integer) chartInfo.get(key)).intValue();
179: if (nOrders >= 0) {
180: PetStoreProxy.Sales sale = new PetStoreProxy.Sales(
181: key, nOrders);
182: sales.add(sale);
183: }
184: }
185: return (PetStoreProxy.Sales[]) (sales
186: .toArray(new PetStoreProxy.Sales[sales.size()]));
187: } catch (RemoteException re) {
188: re.printStackTrace();
189: return null;
190: }
191: }
192:
193: public void updateStatus(PetStoreProxy.Order[] orders, String status) {
194: try {
195: String[] orderIds = new String[orders.length];
196: String[] statuses = new String[orders.length];
197: for (int i = 0; i < orders.length; ++i) {
198: orderIds[i] = orders[i].getId();
199: statuses[i] = status;
200: }
201: AdminService_OrderApprovalTO to = new AdminService_OrderApprovalTO();
202: to.setOrderIds(orderIds);
203: to.setStatuses(statuses);
204: getAdminService().update(to);
205: } catch (RemoteException re) {
206: // TBD: need better handling
207: re.printStackTrace();
208: }
209: }
210:
211: public void updateStatus(PetStoreProxy.Order order, String status) {
212: try {
213: getAdminService().update(order.getId(), status);
214: } catch (RemoteException re) {
215: // TBD: need better handling
216: re.printStackTrace();
217: }
218: }
219: }
|