1 37 38 package com.sun.j2ee.blueprints.consumerwebsite.actions; 39 40 41 import java.io.*; 42 import java.util.*; 43 44 45 import javax.naming.*; 47 import javax.servlet.http.*; 48 import javax.xml.rpc.*; 49 50 51 import com.sun.j2ee.blueprints.waf.controller.Event; 53 import com.sun.j2ee.blueprints.waf.controller.web.html.*; 54 55 import com.sun.j2ee.blueprints.signon.web.SignOnFilter; 57 58 import com.sun.j2ee.blueprints.customer.*; 60 61 63 import com.sun.j2ee.blueprints.consumerwebsite.*; 64 import com.sun.j2ee.blueprints.consumerwebsite.exceptions.*; 65 66 import com.sun.j2ee.blueprints.catalog.CatalogFacade; 68 69 73 74 public final class CheckoutHTMLAction extends HTMLActionSupport { 75 76 public Event perform(HttpServletRequest request) 77 throws HTMLActionException { 78 79 buildPurchaseOrder(request); 80 return null; 81 } 82 83 private void buildPurchaseOrder(HttpServletRequest request) 84 throws HTMLActionException { 85 86 HttpSession session = request.getSession(); 87 AdventureComponentManager acm = 88 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 89 String userId = (String )request.getSession().getAttribute(SignOnFilter.USER_NAME); 91 CatalogFacade catalog = acm.getCatalogFacade(session); 92 CustomerFacade cfacade = acm.getCustomerFacade(session); 93 Cart cart = acm.getCart(session); 94 try { 95 Locale locale = new Locale("en","us"); 96 double totalPrice = 0; 97 ContactInfo shippingInfo = extractContactInfo(request, "_b"); 98 ContactInfo billingInfo = extractContactInfo(request, "_a"); 99 CreditCard card = new CreditCard(); 100 card.setCardNumber(request.getParameter("credit_card_number")); 101 card.setCardExpiryDate(request.getParameter("credit_card_month") + "/" + request.getParameter("credit_card_year")); 102 card.setCardType(request.getParameter("credit_card_name")); 103 104 com.sun.j2ee.blueprints.catalog.Lodging lodging = catalog.getLodging(cart.getLodgingId(), locale); 105 Lodging lodgingPO = new Lodging(); 106 lodgingPO.setLodgingId(lodging.getLodgingId()); 107 lodgingPO.setName(lodging.getName()); 108 lodgingPO.setPricePerNight((new Double (lodging.getPrice())).floatValue()); 109 lodgingPO.setLocation(lodging.getLocation()); 110 lodgingPO.setStartDate(cart.getDepartureDate()); 111 lodgingPO.setEndDate(cart.getReturnDate()); 112 lodgingPO.setNoNights(cart.getLodgingDays()); 113 lodgingPO.setNoRooms(cart.getLodgingRoomCount()); 114 totalPrice += lodging.getPrice(); 115 116 Transportation dF = null; 118 if (cart.getDepartureFlight() != null) { 119 dF= new Transportation(); 120 com.sun.j2ee.blueprints.catalog.Transportation departureFlight = 121 catalog.getTransportation(cart.getDepartureFlight(), locale); 122 dF.setTransportationId(departureFlight.getTransportationId()); 123 dF.setCarrier(departureFlight.getCarrier()); 124 dF.setOrigin(departureFlight.getOrigin()); 125 dF.setDestination(departureFlight.getDestination()); 126 dF.setDepartureDate(cart.getDepartureDate()); 127 dF.setDepartureTime(departureFlight.getDepartureTime()); 128 dF.setPrice((new Double (departureFlight.getPrice())).floatValue()); 129 dF.setTravelClass(departureFlight.getTravelClass()); 130 dF.setHeadCount(cart.getHeadCount()); 131 totalPrice += departureFlight.getPrice(); 132 } 133 Transportation rF = null; 134 if (cart.getReturnFlight() != null) { 135 rF = new Transportation(); 136 com.sun.j2ee.blueprints.catalog.Transportation returnFlight = 137 catalog.getTransportation(cart.getReturnFlight(), locale); 138 139 rF.setTransportationId(returnFlight.getTransportationId()); 140 rF.setCarrier(returnFlight.getCarrier()); 141 rF.setOrigin(returnFlight.getOrigin()); 142 rF.setDestination(returnFlight.getDestination()); 143 rF.setDepartureDate(cart.getReturnDate()); 144 rF.setDepartureTime(returnFlight.getDepartureTime()); 145 rF.setPrice((new Double (returnFlight.getPrice())).floatValue()); 146 rF.setTravelClass(returnFlight.getTravelClass()); 147 rF.setHeadCount(cart.getHeadCount()); 148 totalPrice += returnFlight.getPrice(); 149 } 150 151 ArrayList poActivities = new ArrayList(); 152 HashMap activities = cart.getActivities(); 153 Iterator it = null; 154 if (activities != null) { 155 it = activities.keySet().iterator(); 156 while ((it != null) && it.hasNext()) { 157 String itemId = (String )it.next(); 158 int headCount = ((Integer )activities.get(itemId)).intValue(); 159 com.sun.j2ee.blueprints.catalog.Activity item = catalog.getActivity(itemId, locale); 160 Activity a1 = new Activity(); 161 a1.setActivityId(item.getActivityId()); 162 a1.setName(item.getName()); 163 a1.setPrice((new Double (item.getPrice())).floatValue()); 164 a1.setLocation(item.getLocation()); 165 a1.setStartDate(cart.getDepartureDate()); 166 a1.setEndDate(cart.getReturnDate()); 167 a1.setHeadCount(headCount); 168 totalPrice += item.getPrice(); 169 poActivities.add(a1); 170 } 171 } 172 Activity[] myPoActivities = new Activity[poActivities.size()]; 173 poActivities.toArray(myPoActivities); 174 175 PurchaseOrder mypo = new PurchaseOrder(); 177 String poId = "AB-" +userId + "-" + System.currentTimeMillis(); 178 mypo.setPoId(poId); 179 mypo.setUserId(userId); 180 mypo.setEmailId(shippingInfo.getEmail()); 181 mypo.setLocale("US"); 182 mypo.setOrderDate(Calendar.getInstance()); 183 mypo.setShippingInfo(shippingInfo); 184 mypo.setBillingInfo(billingInfo); 185 mypo.setTotalPrice((new Double (totalPrice)).floatValue()); 186 mypo.setCreditCard(card); 187 mypo.setHeadCount(cart.getHeadCount()); 188 mypo.setStartDate(Calendar.getInstance()); 189 mypo.setEndDate(Calendar.getInstance()); 190 mypo.setDepartureCity(cart.getOrigin()); 191 mypo.setActivities(myPoActivities); 192 mypo.setLodging(lodgingPO); 193 if (dF != null) mypo.setDepartureFlightInfo(dF); 194 if (rF != null) mypo.setReturnFlightInfo(rF); 195 196 Context ic = new InitialContext(); 197 Service opcPurchaseOrderSvc = 198 (Service) ic.lookup("java:comp/env/service/OpcPurchaseOrderService"); 199 PurchaseOrderIntf port = (PurchaseOrderIntf)opcPurchaseOrderSvc.getPort(PurchaseOrderIntf.class); 200 String server = (request.getServerPort() != 0) ? 201 (request.getServerName()+":"+request.getServerPort()) : 202 (request.getServerName()); 203 ((Stub)port)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, 204 "http://" + server + "/webservice/OpcPurchaseOrderService"); 205 String ret = port.submitPurchaseOrder(mypo); 206 CheckoutBean checkoutBean = new CheckoutBean(ret); 207 request.setAttribute(AdventureKeys.CHECKOUT_BEAN, checkoutBean); 208 } catch (Exception e) { 209 throw new HTMLActionException("CheckoutHTMLAction Exception : " + e.getMessage(),e); 210 } 211 cart.clear(); 213 } 214 215 216 private ContactInfo extractContactInfo(HttpServletRequest request, String suffix) 217 throws HTMLActionException { 218 ArrayList missingFields = null; 219 String familyName = request.getParameter("family_name" +suffix).trim(); 220 if (familyName.equals("")) { 221 if (missingFields == null) { 222 missingFields = new ArrayList(); 223 } 224 missingFields.add("Last Name"); 225 } 226 String givenName = request.getParameter("given_name" +suffix).trim(); 227 if (givenName.equals("")) { 228 if (missingFields == null) { 229 missingFields = new ArrayList(); 230 } 231 missingFields.add("First Name"); 232 } 233 String address1 = request.getParameter("address_1" +suffix).trim(); 234 if (address1.equals("")){ 235 if (missingFields == null) { 236 missingFields = new ArrayList(); 237 } 238 missingFields.add("Street Address"); 239 } 240 String address2 = request.getParameter("address_2" +suffix).trim(); 241 if (address2.equals("")) { 242 address2 = null; 243 } 244 String city = request.getParameter("city" +suffix).trim(); 245 if (city.equals("")){ 246 if (missingFields == null) { 247 missingFields = new ArrayList(); 248 } 249 missingFields.add("City"); 250 } 251 String stateOrProvince = request.getParameter("state_or_province" +suffix).trim(); 252 if (stateOrProvince.equals("")) { 253 if (missingFields == null) { 254 missingFields = new ArrayList(); 255 } 256 missingFields.add("State or Province" +suffix); 257 } 258 String postalCode = request.getParameter("postal_code" +suffix).trim(); 259 if (postalCode.equals("")){ 260 if (missingFields == null) { 261 missingFields = new ArrayList(); 262 } 263 missingFields.add("Postal Code"); 264 } 265 266 String country = request.getParameter("country" + suffix).trim(); 267 String telephone = request.getParameter("telephone_number" +suffix).trim(); 268 if (telephone.equals("")){ 269 if (missingFields == null) { 270 missingFields = new ArrayList(); 271 } 272 missingFields.add("Telephone Number"); 273 } 274 String email = null; 275 if (request.getParameter("email" +suffix) != null) { 276 email = request.getParameter("email" +suffix).trim(); 277 } 278 if (missingFields != null) { 279 MissingFormDataException ex = new MissingFormDataException("Missing Address Data", missingFields); 280 request.setAttribute(AdventureKeys.MISSING_FORM_DATA_EXCEPTION_KEY, ex); 281 return null; 282 } 283 284 ContactInfo cInfo = new ContactInfo(); 285 Address addr = new Address(); 286 addr.setStreetName1(address1); 287 addr.setStreetName2(address2); 288 addr.setCity(city); 289 addr.setState(stateOrProvince); 290 addr.setCountry(country); 291 addr.setPostalCode(postalCode); 292 cInfo.setFamilyName(familyName); 293 cInfo.setGivenName(givenName); 294 cInfo.setAddress(addr); 295 cInfo.setEmail(email); 296 cInfo.setPhone(telephone); 297 return(cInfo); 298 } 299 } 300 301 302 | Popular Tags |