1 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 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 } 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 pe.printStackTrace(); 129 return new Date(); 130 } 131 } 132 133 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 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 re.printStackTrace(); 204 } 205 } 206 } 207 | Popular Tags |