KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > testimonials > CallUpdateTestimonialAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.webapp.action.testimonials;
17
18 import com.blandware.atleap.common.TestimonialModuleConstants;
19 import com.blandware.atleap.model.testimonials.Testimonial;
20 import com.blandware.atleap.model.core.ContentLocale;
21 import com.blandware.atleap.service.testimonials.TestimonialManager;
22 import com.blandware.atleap.webapp.action.core.BaseAction;
23 import com.blandware.atleap.webapp.form.TestimonialForm;
24 import com.blandware.atleap.webapp.util.core.LocaleUtil;
25 import com.blandware.atleap.webapp.util.core.WebappConstants;
26 import com.blandware.atleap.webapp.util.core.WebappUtil;
27 import com.blandware.atleap.webapp.util.testimonials.TestimonialModuleWebConstants;
28 import org.apache.commons.validator.GenericValidator;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessage;
33 import org.apache.struts.action.ActionMessages;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * <p>Prepares form bean to update testimonial's data
43  * </p>
44  * <p><a HREF="CallUpdateTestimonialAction.java.htm"><i>View Source</i></a></p>
45  * <p/>
46  *
47  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
48  * @version $Revision: 1.11 $ $Date: 2006/03/10 17:10:32 $
49  * @struts.action path="/testimonials/callUpdateTestimonial"
50  * name="testimonialForm"
51  * scope="request"
52  * validate="false"
53  * roles="testimonials-item-update, testimonials-item-view"
54  * @struts.action-forward name="updateTestimonial"
55  * path=".testimonials.updateTestimonial"
56  * @struts.action-forward name="listTestimonials"
57  * path="/testimonials/list.do"
58  * redirect="true"
59  */

60 public final class CallUpdateTestimonialAction extends BaseAction {
61     /**
62      * @param mapping The ActionMapping used to select this instance
63      * @param form The optional ActionForm bean for this request (if any)
64      * @param request The HTTP request we are proceeding
65      * @param response The HTTP response we are creating
66      * @return an ActionForward instance describing where and how
67      * control should be forwarded, or null if response
68      * has already been completed
69      */

70     public ActionForward execute(ActionMapping mapping, ActionForm form,
71                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
72
73         request.getSession().removeAttribute(TestimonialModuleWebConstants.TESTIMONIAL_SUBMITTED_ACTION_KEY);
74
75         if ( isCancelled(request) ) {
76             return mapping.findForward("listTestimonials");
77         }
78
79         if ( !request.isUserInRole("testimonials-item-update") ) {
80             response.sendError(HttpServletResponse.SC_FORBIDDEN);
81             return null;
82         }
83
84         TestimonialForm testimonialForm = (TestimonialForm) form;
85         Long JavaDoc testimonialId = null;
86         if ( testimonialForm.getId() != null ) {
87             testimonialId = Long.valueOf(testimonialForm.getId());
88         } else if ( request.getAttribute(TestimonialModuleWebConstants.TESTIMONIAL_ID_KEY) != null ) {
89             testimonialId = (Long JavaDoc) request.getAttribute(TestimonialModuleWebConstants.TESTIMONIAL_ID_KEY);
90         } else if ( request.getSession().getAttribute(TestimonialModuleWebConstants.TESTIMONIAL_ID_KEY) != null ) {
91             testimonialId = (Long JavaDoc) request.getSession().getAttribute(TestimonialModuleWebConstants.TESTIMONIAL_ID_KEY);
92         } else {
93             if ( log.isWarnEnabled() ) {
94                 log.warn("Missing testimonial item ID. Returning to list...");
95             }
96             return mapping.findForward("listTestimonials");
97         }
98
99         String JavaDoc redirectUrl = request.getParameter("redirectUrl");
100         if ( GenericValidator.isBlankOrNull(redirectUrl) ) {
101             redirectUrl = (String JavaDoc) request.getAttribute(WebappConstants.REDIRECT_URL_KEY);
102         }
103         if ( !GenericValidator.isBlankOrNull(redirectUrl) ) {
104             request.getSession().setAttribute(WebappConstants.REDIRECT_URL_KEY, redirectUrl);
105         } else {
106             request.getSession().removeAttribute(WebappConstants.REDIRECT_URL_KEY);
107         }
108
109         String JavaDoc localesMode = testimonialForm.getLocalesMode();
110         if ( !"current".equalsIgnoreCase(localesMode) && !"all".equalsIgnoreCase(localesMode) ) {
111             localesMode = "selected";
112         }
113         testimonialForm.setLocalesMode(localesMode);
114
115         TestimonialManager testimonialManager = (TestimonialManager) getBean(TestimonialModuleConstants.TESTIMONIAL_MANAGER_BEAN);
116         Testimonial testimonial = testimonialManager.retrieveTestimonial(testimonialId);
117         if ( testimonial == null ) {
118             // testimonial not found. it might be deleted by someone else
119
ActionMessages errors = new ActionMessages();
120             errors.add("testimonialNotFound", new ActionMessage("testimonials.errors.notFound"));
121             saveErrors(request, errors);
122             return mapping.findForward("listTestimonials");
123         }
124         WebappUtil.copyProperties(testimonialForm, testimonial, request);
125         testimonialForm.setTitleMap(testimonial.getTitle());
126         testimonialForm.setBodyMap(testimonial.getBody());
127
128         List JavaDoc contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
129
130         request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
131
132         if ("selected".equalsIgnoreCase(localesMode)) {
133             // mark all locales as checked
134
Map JavaDoc bodyCheckedBoxes = testimonialForm.getBodyCheckedBoxes();
135             for (Iterator JavaDoc i = contentLocales.iterator(); i.hasNext();) {
136                 ContentLocale contentLocale = (ContentLocale) i.next();
137                 bodyCheckedBoxes.put(contentLocale.getIdentifier(), Boolean.TRUE);
138             }
139
140             // check 'check all' ckeckbox
141
request.setAttribute(WebappConstants.SELECT_ALL_LOCALES_CHECKBOX_IS_CHECKED, Boolean.TRUE);
142         }
143
144         // save transaction token in request
145
saveToken(request);
146         return mapping.findForward("updateTestimonial");
147     }
148 }
Popular Tags