KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.servlet.http.*;
41
42 // signon filter
43
import com.sun.j2ee.blueprints.signon.web.SignOnFilter;
44
45 // waf imports
46
import com.sun.j2ee.blueprints.waf.controller.Event;
47 import com.sun.j2ee.blueprints.waf.controller.web.html.*;
48
49 // customer component imports
50
import com.sun.j2ee.blueprints.customer.*;
51
52 // signon filter
53
import com.sun.j2ee.blueprints.signon.web.SignOnFilter;
54 import com.sun.j2ee.blueprints.signon.SignOnFacade;
55
56 // adventure imports
57
import com.sun.j2ee.blueprints.consumerwebsite.*;
58 import com.sun.j2ee.blueprints.consumerwebsite.exceptions.CustomerException;
59 import com.sun.j2ee.blueprints.consumerwebsite.exceptions.SignOnException;
60 /**
61  * Handles responsibilities related to getting HTTP request
62  * info and making the calls to the customer account component
63  * to access the database.
64  */

65 public final class CustomerHTMLAction extends HTMLActionSupport {
66     
67     public static final String JavaDoc ACCOUNT_READ_ACTION = "readAccount";
68     public static final String JavaDoc ACCOUNT_CREATE_ACTION = "createAccount";
69     
70     /**
71      * Handles the http request to create an account, and provides an
72      * appropriate response.
73      *
74      * Post-condition: Set the bean with info to populate response.
75      */

76     public Event perform(HttpServletRequest request)
77         throws HTMLActionException {
78         
79         CustomerBean resultBean = null;
80         
81         //determine which type of request to process
82
String JavaDoc targetAction =request.getParameter("target_action");
83         // get a handle on the Adventure Component Manager
84
HttpSession session = request.getSession();
85         AdventureComponentManager acm =
86         (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER);
87         // Delegate the work to access the customer component.
88
CustomerFacade facade = acm.getCustomerFacade(session);
89         if ((targetAction != null) &&
90                targetAction.equals(ACCOUNT_CREATE_ACTION)) {
91             resultBean = createAccount(request, facade);
92         } else {
93             Boolean JavaDoc signedOn = (Boolean JavaDoc)request.getSession().getAttribute(SignOnFilter.SIGNED_ON_USER);
94             //FOR READ REQUESTS
95
if ((signedOn != null) && signedOn.booleanValue()) {
96                 // Delegate the work to access the customer component.
97
resultBean = readAccount(session,facade);
98             } else {
99                 throw new CustomerException("CustomerHTMLAction: User is not signed on.");
100             }
101         }
102         // places result bean data in the response.
103
session.setAttribute(AdventureKeys.CUSTOMER_BEAN, resultBean);
104         return null;
105     }
106     
107     /**
108      * Validates the given feedback.
109      */

110     protected void validate(String JavaDoc userId)
111         throws CustomerException {
112         if ((userId == null) || userId.trim().length() == 0) {
113             throw new CustomerException("Unfortunately, there was a problem: The userId must have data. Your request has not been sent.");
114         }
115     }
116     
117     /**
118      * Access customer component and retrieve account data in the database
119      */

120     public CustomerBean readAccount(HttpSession session,
121                                                                        CustomerFacade facade)
122             throws CustomerException {
123         
124         String JavaDoc userId = (String JavaDoc)session.getAttribute(SignOnFilter.USER_NAME);
125         Account acct = null;
126         //call customer component
127
try {
128             acct = facade.getAccount(userId);
129             // Catch customer component exceptions and re-throw them as
130
// app application defined exceptions
131
}catch (Exception JavaDoc e) {
132             e.printStackTrace();
133             throw new CustomerException("CustomerHTMLAction:: CustomerAppException accessing Customer Component: ", e);
134         }
135         
136         return new CustomerBean(acct);
137     }
138     
139     /**
140      * Creates a new customer account
141      */

142     private CustomerBean createAccount(HttpServletRequest request,
143                                                                              CustomerFacade facade)
144         throws CustomerException, SignOnException {
145         
146          // create the sign on here
147
Boolean JavaDoc result = new Boolean JavaDoc(false);
148          String JavaDoc userId = null;
149
150         // Delegate the work to access the signon component.
151
result = new Boolean JavaDoc(createSignOn(request));
152         if (result.booleanValue()) {
153              userId = (String JavaDoc)request.getSession().getAttribute(SignOnFilter.USER_NAME);
154         } else {
155             throw new SignOnException("CustomerHTMLAction: failed to create SignOn for " + userId);
156         }
157
158         // places result bean data in the session using the key that the
159
// signonfilter will understand.
160
request.getSession().setAttribute(SignOnFilter.SIGNED_ON_USER, result);
161         String JavaDoc familyName =request.getParameter("acct_familyName");
162         String JavaDoc givenName =request.getParameter("acct_givenName");
163         String JavaDoc telephone =request.getParameter("acct_telephone");
164         String JavaDoc email =request.getParameter("acct_email");
165         String JavaDoc street1 =request.getParameter("acct_street1");
166         String JavaDoc street2 =request.getParameter("acct_street2");
167         String JavaDoc city =request.getParameter("acct_city");
168         String JavaDoc state =request.getParameter("acct_state");
169         String JavaDoc zipCode =request.getParameter("acct_zipCode");
170         String JavaDoc country =request.getParameter("acct_country");
171         
172         com.sun.j2ee.blueprints.customer.Address address =
173         new com.sun.j2ee.blueprints.customer.Address(street1, street2, city, state, zipCode, country);
174         com.sun.j2ee.blueprints.customer.ContactInformation info =
175         new com.sun.j2ee.blueprints.customer.ContactInformation(familyName, givenName,telephone,
176         email, address);
177         
178         com.sun.j2ee.blueprints.customer.Account acct = new com.sun.j2ee.blueprints.customer.Account(userId, info);
179         
180         //call customer component
181
try {
182             facade.createAccount(acct);
183             // Catch customer component exceptions and re-throw them as
184
// app application defined exceptions.
185
} catch (Exception JavaDoc e) {
186             System.out.println("**** Customer Error");
187             e.printStackTrace();
188             throw new CustomerException("CustomerBD:: CustomerAppException Error Creating Customer", e);
189         }
190         
191         //return back same data as input
192
return new CustomerBean(acct);
193     }
194     
195             /**
196      * Creates a new signon in the database
197      */

198     private boolean createSignOn(HttpServletRequest request)
199                               throws SignOnException {
200         try {
201             HttpSession session = request.getSession();
202             String JavaDoc userId = (String JavaDoc)session.getAttribute(AdventureKeys.SIGN_ON_TEMP_USERNAME);
203             String JavaDoc password = (String JavaDoc)session.getAttribute(AdventureKeys.SIGN_ON_TEMP_PASSWORD);
204             session.removeAttribute(AdventureKeys.SIGN_ON_TEMP_USERNAME);
205             session.removeAttribute(AdventureKeys.SIGN_ON_TEMP_PASSWORD);
206             // put the userID in the session for future reference
207
session.setAttribute(SignOnFilter.USER_NAME, userId);
208             AdventureComponentManager acm =
209               (AdventureComponentManager)session.getAttribute(AdventureKeys.COMPONENT_MANAGER);
210             SignOnFacade facade = acm.getSignOnFacade(session);
211             facade.createSignOn(userId, password);
212             return true;
213         // you may chose to catch each of the individual signon exceptions and pass on a
214
// a more detailed message to the user here
215
} catch (Exception JavaDoc e) {
216             throw new SignOnException("SignOnHTMLAction:: Exception creating new signon: ", e);
217         }
218     }
219 }
220
Popular Tags