KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > consumerwebsite > actions > CartHTMLAction


1 /*
2 * Copyright 2005 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.consumerwebsite.actions;
38
39
40 import java.io.IOException JavaDoc;
41 import java.util.*;
42
43 // j2ee imports
44
import javax.servlet.http.*;
45
46 // waf imports
47
import com.sun.j2ee.blueprints.waf.controller.Event;
48 import com.sun.j2ee.blueprints.waf.controller.web.html.*;
49
50 //adventure imports
51
import com.sun.j2ee.blueprints.consumerwebsite.*;
52
53 // Catalog imports
54
import com.sun.j2ee.blueprints.catalog.*;
55
56 /**
57  * Implementation of HTMLAction that processes a
58  * user change in language.
59  */

60
61 public final class CartHTMLAction extends HTMLActionSupport {
62
63     public Event perform(HttpServletRequest request)
64   throws HTMLActionException {
65         
66         String JavaDoc actionType= (String JavaDoc)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 JavaDoc packageId = request.getParameter("package_id").trim();
74             // look up the adventure package
75
AdventureComponentManager acm =
76                         (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER);
77             Cart ac = acm.getCart(session);
78             // clear the cart
79
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                 // put the info into the Adventure Cart
85
ac.setPackageId(packageId);
86                 ac.setLodgingId(pack.getLodgingId());
87                 ac.setDestination(pack.getLocation());
88                  Iterator it = pack.getActivities().iterator();
89                  // add the activities and set default people to 1
90
while (it.hasNext()) {
91                      String JavaDoc activityId = (String JavaDoc)it.next();
92                      ac.addActivity(activityId.trim(), 1);
93                  }
94             } catch (Exception JavaDoc 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 JavaDoc activityId = request.getParameter("activity_id");
102             String JavaDoc headCountString = request.getParameter("head_count");
103             // don't do anything if a hotel was not specified
104
if (activityId == null && headCountString != null) {
105                 getResultBean(request);
106                 return null;
107             }
108             activityId = activityId.trim();
109             int headCount = Integer.parseInt(headCountString);
110             // look up the cart
111
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 JavaDoc hotelId = request.getParameter("lodging_id");
130             // don't do anything if a hotel was not specified
131
if (hotelId == null) {
132                 getResultBean(request);
133                 return null;
134             }
135             hotelId = hotelId.trim();
136             // look up the adventure package
137
AdventureComponentManager acm =
138                         (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER);
139             Cart cart = acm.getCart(session);
140             cart.setLodgingId(hotelId);
141             // update the room count
142
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 JavaDoc departureFlight = request.getParameter("departure_flight");
153             if (departureFlight != null) departureFlight = departureFlight.trim();
154             String JavaDoc returnFlight = request.getParameter("return_flight");
155             if (returnFlight != null) returnFlight = returnFlight.trim();
156             // look up the adventure package
157
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             // remove these attributes used by the side bar
182
session.removeAttribute("theCategory");
183             session.removeAttribute("categoryId");
184         } else if (actionType.equals("update")) {
185             // not for beta
186
}
187         return null;
188     }
189     
190     private void updateActivityHeadCounts(HttpServletRequest request) {
191          HttpSession session = request.getSession();
192         // look up the cart
193
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 JavaDoc key = (String JavaDoc)it.next();
203             if (key.startsWith("headcount_")) {
204                 String JavaDoc id = key.substring("headcount_".length(), key.length());
205                 String JavaDoc value = request.getParameter(key);
206                 int qty = (new Integer JavaDoc(value)).intValue();
207                 cart.setActivityHeadCount(id,qty);
208             }
209         }
210     }
211     
212     private void purchaseActivities(HttpServletRequest request) {
213          HttpSession session = request.getSession();
214         // look up the cart
215
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 JavaDoc key = (String JavaDoc)it.next();
225             if (key.startsWith("activity_")) {
226                 String JavaDoc id = key.substring("activity_".length(), key.length());
227                 String JavaDoc value = request.getParameter(key);
228                 id = id.trim();
229                 int qty = 0;
230                 try {
231                     if (value != null) qty = (new Integer JavaDoc(value)).intValue();
232                 } catch (NumberFormatException JavaDoc 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         // look up the cart
245
AdventureComponentManager acm =
246                         (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER);
247             Cart cart = acm.getCart(session);
248             String JavaDoc adventureDaysString = request.getParameter("adventure_days");
249             if (adventureDaysString != null) {
250                 int adventureDays = (new Integer JavaDoc(adventureDaysString)).intValue();
251                 cart.setAdventureDays(adventureDays);
252             }
253             String JavaDoc lodgingRoomCountString = request.getParameter("lodging_room_count");
254             if (lodgingRoomCountString != null) {
255                 int lodingRoomCount = (new Integer JavaDoc(lodgingRoomCountString)).intValue();
256                 cart.setLodgingRoomCount(lodingRoomCount);
257             }
258             String JavaDoc headCountString = request.getParameter("head_count");
259             if (headCountString != null) {
260                 int headCount = (new Integer JavaDoc(headCountString)).intValue();
261                 cart.setHeadCount(headCount);
262             }
263             // update the departureDate if specified
264
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 will change after beta
299
Locale locale = new Locale("en","us");
300
301             //assume the the user uses the same hotel for every night of the adventure
302
lodgingDays = cart.getLodgingDays();
303             // this needs to be multiplied by the number of rooms
304
lodgingRoomCount = cart.getLodgingRoomCount();
305             com.sun.j2ee.blueprints.catalog.Lodging lodging = facade.getLodging(cart.getLodgingId(), locale);
306             lodgingTotal = (lodgingDays * lodging.getPrice() * lodgingRoomCount);
307             // add in the actvities
308
HashMap activities = cart.getActivities();
309             Iterator it = null;
310             if (activities != null) it = activities.keySet().iterator();
311             while ((it != null) && it.hasNext()) {
312                 String JavaDoc itemId = (String JavaDoc)it.next();
313                 Integer JavaDoc qty = (Integer JavaDoc)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 JavaDoc 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