1 37 package com.sun.j2ee.blueprints.consumerwebsite.actions; 38 39 40 import java.io.IOException ; 41 import java.util.*; 42 43 import javax.servlet.http.*; 45 46 import com.sun.j2ee.blueprints.waf.controller.Event; 48 import com.sun.j2ee.blueprints.waf.controller.web.html.*; 49 50 import com.sun.j2ee.blueprints.consumerwebsite.*; 52 53 import com.sun.j2ee.blueprints.catalog.*; 55 56 60 61 public final class CartHTMLAction extends HTMLActionSupport { 62 63 public Event perform(HttpServletRequest request) 64 throws HTMLActionException { 65 66 String actionType= (String )request.getParameter("target_action"); 67 HttpSession session = request.getSession(); 68 if (actionType == null) { 69 getResultBean(request); 70 return null; 71 } 72 if (actionType.equals("select_package")) { 73 String packageId = request.getParameter("package_id").trim(); 74 AdventureComponentManager acm = 76 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 77 Cart ac = acm.getCart(session); 78 ac.clear(); 80 CatalogFacade facade = acm.getCatalogFacade(session); 81 try { 82 Locale locale = new Locale("en","us"); 83 AdventurePackage pack = facade.getAdventurePackage(packageId, locale); 84 ac.setPackageId(packageId); 86 ac.setLodgingId(pack.getLodgingId()); 87 ac.setDestination(pack.getLocation()); 88 Iterator it = pack.getActivities().iterator(); 89 while (it.hasNext()) { 91 String activityId = (String )it.next(); 92 ac.addActivity(activityId.trim(), 1); 93 } 94 } catch (Exception ex) { 95 ex.printStackTrace(); 96 } 97 } else if (actionType.equals("purchase_activities")) { 98 purchaseActivities(request); 99 getResultBean(request); 100 } else if (actionType.equals("purchase_activity")) { 101 String activityId = request.getParameter("activity_id"); 102 String headCountString = request.getParameter("head_count"); 103 if (activityId == null && headCountString != null) { 105 getResultBean(request); 106 return null; 107 } 108 activityId = activityId.trim(); 109 int headCount = Integer.parseInt(headCountString); 110 AdventureComponentManager acm = 112 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 113 Cart cart = acm.getCart(session); 114 cart.addActivity(activityId,headCount); 115 getResultBean(request); 116 } else if (actionType.equals("update_activities")) { 117 updateActivityHeadCounts(request); 118 getResultBean(request); 119 } else if (actionType.equals("set_package_options")) { 120 updatePackage(request); 121 getResultBean(request); 122 } else if (actionType.equals("update_package_options")) { 123 updatePackage(request); 124 getResultBean(request); 125 } else if (actionType.equals("update_lodging_room_count")) { 126 updatePackage(request); 127 getResultBean(request); 128 } else if (actionType.equals("purchase_lodging")) { 129 String hotelId = request.getParameter("lodging_id"); 130 if (hotelId == null) { 132 getResultBean(request); 133 return null; 134 } 135 hotelId = hotelId.trim(); 136 AdventureComponentManager acm = 138 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 139 Cart cart = acm.getCart(session); 140 cart.setLodgingId(hotelId); 141 updatePackage(request); 143 getResultBean(request); 144 } else if (actionType.equals("no_transportation")) { 145 AdventureComponentManager acm = 146 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 147 Cart cart = acm.getCart(session); 148 cart.setOrigin("None"); 149 cart.setConfigurationComplete(true); 150 getResultBean(request); 151 } else if (actionType.equals("purchase_transportation")) { 152 String departureFlight = request.getParameter("departure_flight"); 153 if (departureFlight != null) departureFlight = departureFlight.trim(); 154 String returnFlight = request.getParameter("return_flight"); 155 if (returnFlight != null) returnFlight = returnFlight.trim(); 156 AdventureComponentManager acm = 158 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 159 Cart cart = acm.getCart(session); 160 cart.setDepartureFlight(departureFlight); 161 cart.setReturnFlight(returnFlight); 162 cart.setConfigurationComplete(true); 163 getResultBean(request); 164 } else if (actionType.equals("cancel_departure_flight")) { 165 AdventureComponentManager acm = 166 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 167 Cart cart = acm.getCart(session); 168 cart.setDepartureFlight(null); 169 getResultBean(request); 170 } else if (actionType.equals("cancel_return_flight")) { 171 AdventureComponentManager acm = 172 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 173 Cart cart = acm.getCart(session); 174 cart.setReturnFlight(null); 175 getResultBean(request); 176 } else if (actionType.equals("cancel")) { 177 AdventureComponentManager acm = 178 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 179 Cart cart = acm.getCart(session); 180 cart.clear(); 181 session.removeAttribute("theCategory"); 183 session.removeAttribute("categoryId"); 184 } else if (actionType.equals("update")) { 185 } 187 return null; 188 } 189 190 private void updateActivityHeadCounts(HttpServletRequest request) { 191 HttpSession session = request.getSession(); 192 AdventureComponentManager acm = 194 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 195 Cart cart = acm.getCart(session); 196 Map params = (Map)request.getParameterMap(); 197 if (params.isEmpty()) return; 198 Collection keys = params.keySet(); 199 if (keys == null) return; 200 Iterator it = keys.iterator(); 201 while(it.hasNext() ) { 202 String key = (String )it.next(); 203 if (key.startsWith("headcount_")) { 204 String id = key.substring("headcount_".length(), key.length()); 205 String value = request.getParameter(key); 206 int qty = (new Integer (value)).intValue(); 207 cart.setActivityHeadCount(id,qty); 208 } 209 } 210 } 211 212 private void purchaseActivities(HttpServletRequest request) { 213 HttpSession session = request.getSession(); 214 AdventureComponentManager acm = 216 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 217 Cart cart = acm.getCart(session); 218 Map params = (Map)request.getParameterMap(); 219 if (params == null) return; 220 Collection keys = params.keySet(); 221 if (keys == null) return; 222 Iterator it = keys.iterator(); 223 while(it.hasNext() ) { 224 String key = (String )it.next(); 225 if (key.startsWith("activity_")) { 226 String id = key.substring("activity_".length(), key.length()); 227 String value = request.getParameter(key); 228 id = id.trim(); 229 int qty = 0; 230 try { 231 if (value != null) qty = (new Integer (value)).intValue(); 232 } catch (NumberFormatException nex) {} 233 if (qty > 0) { 234 cart.addActivity(id,qty); 235 } else { 236 cart.setActivityHeadCount(id,0); 237 } 238 } 239 } 240 } 241 242 private void updatePackage(HttpServletRequest request) { 243 HttpSession session = request.getSession(); 244 AdventureComponentManager acm = 246 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 247 Cart cart = acm.getCart(session); 248 String adventureDaysString = request.getParameter("adventure_days"); 249 if (adventureDaysString != null) { 250 int adventureDays = (new Integer (adventureDaysString)).intValue(); 251 cart.setAdventureDays(adventureDays); 252 } 253 String lodgingRoomCountString = request.getParameter("lodging_room_count"); 254 if (lodgingRoomCountString != null) { 255 int lodingRoomCount = (new Integer (lodgingRoomCountString)).intValue(); 256 cart.setLodgingRoomCount(lodingRoomCount); 257 } 258 String headCountString = request.getParameter("head_count"); 259 if (headCountString != null) { 260 int headCount = (new Integer (headCountString)).intValue(); 261 cart.setHeadCount(headCount); 262 } 263 if (request.getParameter("start_month") != null) { 265 int startMonth = Integer.parseInt(request.getParameter("start_month")); 266 int startDay = Integer.parseInt(request.getParameter("start_day")); 267 int startYear = Integer.parseInt(request.getParameter("start_year")); 268 269 Calendar c = Calendar.getInstance(); 270 c.set(Calendar.MONTH, startMonth -1); 271 c.set(Calendar.YEAR, startYear); 272 c.set(Calendar.DAY_OF_MONTH, startDay); 273 cart.setDepartureDate(c); 274 } 275 } 276 277 private void getResultBean(HttpServletRequest request) 278 throws HTMLActionException { 279 double grandTotal = 0; 280 double cartTotal = 0; 281 double lodgingTotal = 0; 282 double activityTotal = 0; 283 double departureTotal = 0; 284 double returnTotal = 0; 285 double transportationTotal = 0; 286 int adventureDays =0; 287 int lodgingDays=0; 288 int headCount = 0; 289 int lodgingRoomCount = 1; 290 291 HttpSession session = request.getSession(); 292 AdventureComponentManager acm = 293 (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER); 294 Cart cart = acm.getCart(session); 295 296 CatalogFacade facade = acm.getCatalogFacade(session); 297 try { 298 Locale locale = new Locale("en","us"); 300 301 lodgingDays = cart.getLodgingDays(); 303 lodgingRoomCount = cart.getLodgingRoomCount(); 305 com.sun.j2ee.blueprints.catalog.Lodging lodging = facade.getLodging(cart.getLodgingId(), locale); 306 lodgingTotal = (lodgingDays * lodging.getPrice() * lodgingRoomCount); 307 HashMap activities = cart.getActivities(); 309 Iterator it = null; 310 if (activities != null) it = activities.keySet().iterator(); 311 while ((it != null) && it.hasNext()) { 312 String itemId = (String )it.next(); 313 Integer qty = (Integer )activities.get(itemId); 314 com.sun.j2ee.blueprints.catalog.Activity item = facade.getActivity(itemId, locale); 315 activityTotal += (qty.intValue() * item.getPrice()); 316 } 317 headCount = cart.getHeadCount(); 318 if (cart.getDepartureFlight() != null) { 319 com.sun.j2ee.blueprints.catalog.Transportation departureFlight = 320 facade.getTransportation(cart.getDepartureFlight(), locale); 321 if (departureFlight != null) { 322 departureTotal = (departureFlight.getPrice() * headCount); 323 } 324 } 325 if (cart.getReturnFlight() != null) { 326 com.sun.j2ee.blueprints.catalog.Transportation returnFlight = 327 facade.getTransportation(cart.getReturnFlight(), locale); 328 if (returnFlight != null) { 329 returnTotal = (returnFlight.getPrice() * headCount); 330 } 331 } 332 transportationTotal = departureTotal + returnTotal; 333 } catch (Exception ex) { 334 throw new HTMLActionException("CartHTMLAction error: " + ex); 335 } 336 cartTotal = activityTotal + lodgingTotal + transportationTotal; 337 CartBean bean = 338 new CartBean(cartTotal, 339 lodgingTotal, 340 activityTotal, 341 transportationTotal, 342 lodgingDays, 343 adventureDays, 344 cart.getDepartureDate()); 345 request.setAttribute(AdventureKeys.CART_BEAN, bean); 346 } 347 } 348 349 | Popular Tags |