KickJava   Java API By Example, From Geeks To Geeks.

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


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
38 package com.sun.j2ee.blueprints.consumerwebsite.actions;
39
40
41 import java.io.*;
42 import java.util.*;
43
44
45 // j2ee imports
46
import javax.naming.*;
47 import javax.servlet.http.*;
48 import javax.xml.rpc.*;
49
50
51 // WAF imports
52
import com.sun.j2ee.blueprints.waf.controller.Event;
53 import com.sun.j2ee.blueprints.waf.controller.web.html.*;
54
55 // signon filter - for the userId
56
import com.sun.j2ee.blueprints.signon.web.SignOnFilter;
57
58 // customer component imports
59
import com.sun.j2ee.blueprints.customer.*;
60
61 //adventure imports
62

63 import com.sun.j2ee.blueprints.consumerwebsite.*;
64 import com.sun.j2ee.blueprints.consumerwebsite.exceptions.*;
65
66 // Catalog imports
67
import com.sun.j2ee.blueprints.catalog.CatalogFacade;
68
69 /**
70  * Implementation of HTMLAction that processes a
71  * checkout.
72  */

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         // get the customer and cart
90
String JavaDoc userId = (String JavaDoc)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 JavaDoc(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             // set the transportation info
117
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 JavaDoc(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 JavaDoc(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 JavaDoc itemId = (String JavaDoc)it.next();
158                     int headCount = ((Integer JavaDoc)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 JavaDoc(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             // create a new po
176
PurchaseOrder mypo = new PurchaseOrder();
177             String JavaDoc 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 JavaDoc(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 JavaDoc 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 JavaDoc ret = port.submitPurchaseOrder(mypo);
206             CheckoutBean checkoutBean = new CheckoutBean(ret);
207             request.setAttribute(AdventureKeys.CHECKOUT_BEAN, checkoutBean);
208         } catch (Exception JavaDoc e) {
209             throw new HTMLActionException("CheckoutHTMLAction Exception : " + e.getMessage(),e);
210         }
211         // clear the adventure package
212
cart.clear();
213     }
214     
215     /* parse address form and generate a ContactInfo object */
216     private ContactInfo extractContactInfo(HttpServletRequest request, String JavaDoc suffix)
217     throws HTMLActionException {
218         ArrayList missingFields = null;
219         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc address2 = request.getParameter("address_2" +suffix).trim();
241         if (address2.equals("")) {
242             address2 = null;
243         }
244         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc country = request.getParameter("country" + suffix).trim();
267         String JavaDoc 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 JavaDoc 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