KickJava   Java API By Example, From Geeks To Geeks.

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


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

18
19 package org.apache.struts.webapp.example;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22
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
28 import org.apache.commons.beanutils.PropertyUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts.action.Action;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
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  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
42  */

43 public final class EditRegistrationAction extends Action {
44
45     // ----------------------------------------------------- Instance Variables
46

47     /**
48      * The <code>Log</code> instance for this application.
49      */

50     private Log log = LogFactory.getLog("org.apache.struts.webapp.Example");
51
52     // --------------------------------------------------------- Public Methods
53

54         // See superclass for Javadoc
55
public ActionForward execute(
56         ActionMapping mapping,
57         ActionForm form,
58         HttpServletRequest JavaDoc request,
59         HttpServletResponse JavaDoc response)
60         throws Exception JavaDoc {
61
62         // Extract attributes we will need
63
HttpSession JavaDoc session = request.getSession();
64         String JavaDoc action = request.getParameter("action");
65         if (action == null) {
66             action = "Create";
67         }
68         
69         if (log.isDebugEnabled()) {
70             log.debug("EditRegistrationAction: Processing " + action + " action");
71         }
72
73         // Is there a currently logged on user?
74
User user = null;
75         if (!"Create".equals(action)) {
76             user = (User) session.getAttribute(Constants.USER_KEY);
77             if (user == null) {
78                 if (log.isDebugEnabled()) {
79                     log.debug(
80                         " User is not logged on in session " + session.getId());
81                 }
82                 return (mapping.findForward("logon"));
83             }
84         }
85
86         RegistrationForm regform = (RegistrationForm) form;
87         if (user != null) {
88             if (log.isTraceEnabled()) {
89                 log.trace(" Populating form from " + user);
90             }
91             
92             try {
93                 PropertyUtils.copyProperties(regform, user);
94                 regform.setAction(action);
95                 regform.setPassword(null);
96                 regform.setPassword2(null);
97                 
98             } catch (InvocationTargetException JavaDoc e) {
99                 Throwable JavaDoc t = e.getTargetException();
100                 if (t == null)
101                     t = e;
102                 log.error("RegistrationForm.populate", t);
103                 throw new ServletException JavaDoc("RegistrationForm.populate", t);
104                 
105             } catch (Throwable JavaDoc t) {
106                 log.error("RegistrationForm.populate", t);
107                 throw new ServletException JavaDoc("RegistrationForm.populate", t);
108             }
109         }
110
111         // Set a transactional control token to prevent double posting
112
if (log.isTraceEnabled()) {
113             log.trace(" Setting transactional control token");
114         }
115         
116         saveToken(request);
117
118         // Forward control to the edit user registration page
119
if (log.isTraceEnabled()) {
120             log.trace(" Forwarding to 'success' page");
121         }
122         
123         return (mapping.findForward("success"));
124
125     }
126
127 }
128
Popular Tags