KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > example2 > EditRegistrationAction


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.struts.webapp.example2;
19
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.util.Locale JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import org.apache.commons.beanutils.PropertyUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.action.Action;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34
35
36 /**
37  * Implementation of <strong>Action</strong> that populates an instance of
38  * <code>RegistrationForm</code> from the profile of the currently logged on
39  * User (if any).
40  *
41  * @author Craig R. McClanahan
42  * @version $Rev: 155886 $ $Date: 2005-03-02 06:04:47 +0000 (Wed, 02 Mar 2005) $
43  */

44
45 public final class EditRegistrationAction extends Action {
46
47
48     // ----------------------------------------------------- Instance Variables
49

50
51     /**
52      * The <code>Log</code> instance for this application.
53      */

54     private Log log =
55         LogFactory.getLog("org.apache.struts.webapp.Example");
56
57
58     // --------------------------------------------------------- Public Methods
59

60
61     /**
62      * Process the specified HTTP request, and create the corresponding HTTP
63      * response (or forward to another web component that will create it).
64      * Return an <code>ActionForward</code> instance describing where and how
65      * control should be forwarded, or <code>null</code> if the response has
66      * already been completed.
67      *
68      * @param mapping The ActionMapping used to select this instance
69      * @param form The optional ActionForm bean for this request (if any)
70      * @param request The HTTP request we are processing
71      * @param response The HTTP response we are creating
72      *
73      * @exception Exception if the application business logic throws
74      * an exception
75      */

76     public ActionForward execute(ActionMapping mapping,
77                  ActionForm form,
78                  HttpServletRequest JavaDoc request,
79                  HttpServletResponse JavaDoc response)
80     throws Exception JavaDoc {
81
82     // Extract attributes we will need
83
Locale JavaDoc locale = getLocale(request);
84     HttpSession JavaDoc session = request.getSession();
85     String JavaDoc action = request.getParameter("action");
86     if (action == null)
87         action = "Create";
88         if (log.isDebugEnabled()) {
89             log.debug("EditRegistrationAction: Processing " + action +
90                         " action");
91         }
92
93     // Is there a currently logged on user?
94
User user = null;
95     if (!"Create".equals(action)) {
96         user = (User) session.getAttribute(Constants.USER_KEY);
97         if (user == null) {
98                 if (log.isDebugEnabled()) {
99                     log.debug(" User is not logged on in session "
100                               + session.getId());
101                 }
102         return (mapping.findForward("logon"));
103         }
104     }
105
106     // Populate the user registration form
107
if (form == null) {
108             if (log.isTraceEnabled()) {
109                 log.trace(" Creating new RegistrationForm bean under key "
110                           + mapping.getAttribute());
111             }
112         form = new RegistrationForm();
113             if ("request".equals(mapping.getScope()))
114                 request.setAttribute(mapping.getAttribute(), form);
115             else
116                 session.setAttribute(mapping.getAttribute(), form);
117     }
118     RegistrationForm regform = (RegistrationForm) form;
119     if (user != null) {
120             if (log.isTraceEnabled()) {
121                 log.trace(" Populating form from " + user);
122             }
123             try {
124                 PropertyUtils.copyProperties(regform, user);
125                 regform.setAction(action);
126                 regform.setPassword(null);
127                 regform.setPassword2(null);
128             } catch (InvocationTargetException JavaDoc e) {
129                 Throwable JavaDoc t = e.getTargetException();
130                 if (t == null)
131                     t = e;
132                 log.error("RegistrationForm.populate", t);
133                 throw new ServletException JavaDoc("RegistrationForm.populate", t);
134             } catch (Throwable JavaDoc t) {
135                 log.error("RegistrationForm.populate", t);
136                 throw new ServletException JavaDoc("RegistrationForm.populate", t);
137             }
138     }
139
140         // Set a transactional control token to prevent double posting
141
if (log.isTraceEnabled()) {
142             log.trace(" Setting transactional control token");
143         }
144         saveToken(request);
145
146     // Forward control to the edit user registration page
147
if (log.isTraceEnabled()) {
148             log.trace(" Forwarding to 'success' page");
149         }
150         if ("Create".equals(action)) {
151             return (mapping.findForward("register"));
152         } else {
153             return (mapping.findForward("success"));
154         }
155
156     }
157
158
159 }
160
Popular Tags