KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2001,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>SubscriptionForm</code> from the currently specified subscription.
39  *
40  * @author Craig R. McClanahan
41  * @version $Rev: 155886 $ $Date: 2005-03-02 06:04:47 +0000 (Wed, 02 Mar 2005) $
42  */

43
44 public final class EditSubscriptionAction extends Action {
45
46
47     // ----------------------------------------------------- Instance Variables
48

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

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

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

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