KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EditSubscriptionAction.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>SubscriptionForm</code> from the currently specified subscription.
39  *
40  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
41  */

42 public final class EditSubscriptionAction extends Action {
43
44     // ----------------------------------------------------- Instance Variables
45

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

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

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