KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: SaveSubscriptionAction.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 import org.apache.struts.util.MessageResources;
36
37 /**
38  * Implementation of <strong>Action</strong> that validates and creates or
39  * updates the mail subscription entered by the user.
40  *
41  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
42  */

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

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

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

55         // See superclass for Javadoc
56
public ActionForward execute(
57         ActionMapping mapping,
58         ActionForm form,
59         HttpServletRequest JavaDoc request,
60         HttpServletResponse JavaDoc response)
61         throws Exception JavaDoc {
62
63         // Extract attributes and parameters we will need
64
MessageResources messages = getResources(request);
65         HttpSession JavaDoc session = request.getSession();
66         SubscriptionForm subform = (SubscriptionForm) form;
67         String JavaDoc action = subform.getAction();
68         if (action == null) {
69             action = "?";
70         }
71         if (log.isDebugEnabled()) {
72             log.debug("SaveSubscriptionAction: Processing " + action + " action");
73         }
74
75         // Is there a currently logged on user?
76
User user = (User) session.getAttribute(Constants.USER_KEY);
77         if (user == null) {
78             if (log.isTraceEnabled()) {
79                 log.trace(" User is not logged on in session " + session.getId());
80             }
81             return (mapping.findForward("logon"));
82         }
83
84         // Was this transaction cancelled?
85
if (isCancelled(request)) {
86             if (log.isTraceEnabled()) {
87                 log.trace(" Transaction '" + action + "' was cancelled");
88             }
89             session.removeAttribute(Constants.SUBSCRIPTION_KEY);
90             return (mapping.findForward("success"));
91         }
92
93         // Is there a related Subscription object?
94
Subscription subscription =
95             (Subscription) session.getAttribute(Constants.SUBSCRIPTION_KEY);
96         if ("Create".equals(action)) {
97             subscription = user.createSubscription(request.getParameter("host"));
98         }
99         if (subscription == null) {
100             if (log.isTraceEnabled()) {
101                 log.trace(
102                     " Missing subscription for user '" + user.getUsername() + "'");
103             }
104             response.sendError(
105                 HttpServletResponse.SC_BAD_REQUEST,
106                 messages.getMessage("error.noSubscription"));
107             return (null);
108         }
109
110         // Was this transaction a Delete?
111
if (action.equals("Delete")) {
112             if (log.isTraceEnabled()) {
113                 log.trace(
114                     " Deleting mail server '"
115                         + subscription.getHost()
116                         + "' for user '"
117                         + user.getUsername()
118                         + "'");
119             }
120             user.removeSubscription(subscription);
121             session.removeAttribute(Constants.SUBSCRIPTION_KEY);
122             try {
123                 UserDatabase database =
124                     (UserDatabase) servlet.getServletContext().getAttribute(
125                         Constants.DATABASE_KEY);
126                 database.save();
127             } catch (Exception JavaDoc e) {
128                 log.error("Database save", e);
129             }
130             return (mapping.findForward("success"));
131         }
132
133         // All required validations were done by the form itself
134

135         // Update the persistent subscription information
136
if (log.isTraceEnabled()) {
137             log.trace(" Populating database from form bean");
138         }
139         try {
140             PropertyUtils.copyProperties(subscription, subform);
141         } catch (InvocationTargetException JavaDoc e) {
142             Throwable JavaDoc t = e.getTargetException();
143             if (t == null)
144                 t = e;
145             log.error("Subscription.populate", t);
146             throw new ServletException JavaDoc("Subscription.populate", t);
147         } catch (Throwable JavaDoc t) {
148             log.error("Subscription.populate", t);
149             throw new ServletException JavaDoc("Subscription.populate", t);
150         }
151
152         try {
153             UserDatabase database =
154                 (UserDatabase) servlet.getServletContext().getAttribute(
155                     Constants.DATABASE_KEY);
156             database.save();
157         } catch (Exception JavaDoc e) {
158             log.error("Database save", e);
159         }
160
161         // Remove the obsolete form bean and current subscription
162
if (mapping.getAttribute() != null) {
163             if ("request".equals(mapping.getScope()))
164                 request.removeAttribute(mapping.getAttribute());
165             else
166                 session.removeAttribute(mapping.getAttribute());
167         }
168         session.removeAttribute(Constants.SUBSCRIPTION_KEY);
169
170         // Forward control to the specified success URI
171
if (log.isTraceEnabled()) {
172             log.trace(" Forwarding to success page");
173         }
174         return (mapping.findForward("success"));
175
176     }
177
178 }
179
Popular Tags