KickJava   Java API By Example, From Geeks To Geeks.

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


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.search.SearchManager;
21 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
22 import com.blandware.atleap.service.testimonials.TestimonialManager;
23 import com.blandware.atleap.webapp.action.core.BaseAction;
24 import com.blandware.atleap.webapp.form.TestimonialForm;
25 import com.blandware.atleap.webapp.util.core.CacheUtil;
26 import org.apache.commons.validator.GenericValidator;
27 import org.apache.struts.action.*;
28 import org.springframework.orm.ObjectOptimisticLockingFailureException;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 /**
34  * <p>Adds or removes testimonial to or from publishing
35  * </p>
36  * <p><a HREF="ChangeTestimonialActivityAction.java.htm"><i>View Source</i></a></p>
37  * <p/>
38  *
39  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
40  * @version $Revision: 1.9 $ $Date: 2006/03/10 17:10:32 $
41  * @struts.action path="/testimonials/changeTestimonialActivity"
42  * name="testimonialForm"
43  * scope="request"
44  * validate="false"
45  * roles="testimonials-item-changeActivity"
46  * @struts.action-forward name="listTestimonials"
47  * path="/testimonials/list.do"
48  * redirect="true"
49  * @struts.action-forward name="unsatisfiable"
50  * path="/testimonials/list.do"
51  */

52 public final class ChangeTestimonialActivityAction extends BaseAction {
53     /**
54      * @param mapping The ActionMapping used to select this instance
55      * @param form The optional ActionForm bean for this request (if any)
56      * @param request The HTTP request we are proceeding
57      * @param response The HTTP response we are creating
58      * @return an ActionForward instance describing where and how
59      * control should be forwarded, or null if response
60      * has already been completed
61      */

62     public ActionForward execute(ActionMapping mapping, ActionForm form,
63                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
64
65         TestimonialForm testimonialForm = (TestimonialForm) form;
66
67         Long JavaDoc testimonialId = null;
68         if ( !GenericValidator.isBlankOrNull(testimonialForm.getId()) ) {
69             testimonialId = Long.valueOf(testimonialForm.getId());
70         } else {
71             if ( log.isWarnEnabled() ) {
72                 log.warn("Missing content page ID. Returning to list.");
73             }
74             return mapping.findForward("listTestimonials");
75         }
76
77         TestimonialManager testimonialManager = (TestimonialManager) getBean(TestimonialModuleConstants.TESTIMONIAL_MANAGER_BEAN);
78         Testimonial testimonial = testimonialManager.retrieveTestimonial(testimonialId);
79
80         if ( testimonial == null ) {
81             // testimonial not found. it might be deleted by someone else
82
ActionMessages errors = new ActionMessages();
83             errors.add("testimonialNotFound", new ActionMessage("testimonials.errors.notFound"));
84             saveErrors(request, errors);
85             return mapping.findForward("listTestimonials");
86         }
87
88         String JavaDoc mode = request.getParameter("mode");
89         boolean alreadyActive = testimonial.getActive() != null ? testimonial.getActive().booleanValue() : false;
90         boolean active = true;
91         if ( !"active".equalsIgnoreCase(mode) ) {
92             active = false;
93         }
94
95         try {
96
97             if ( (alreadyActive && !active) || (!alreadyActive && active) ) {
98                 testimonial.setActive(Boolean.valueOf(active));
99                 testimonialManager.updateTestimonial(testimonial);
100
101                 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
102                 searchManager.unIndexPage(testimonial.getUri(), request);
103
104                 if ( active ) {
105                     searchManager.indexPage(testimonial, request);
106                 }
107
108                 CacheUtil cacheUtil = CacheUtil.getInstance(request);
109                 cacheUtil.flushLocalizableFieldValueCache(testimonialId);
110             }
111         } catch ( BeanAlreadyExistsException e ) {
112             // testimonial already exists: will never be reached in our case
113
ActionMessages errors = new ActionMessages();
114             errors.add("testimonialAlreadyExists", new ActionMessage("errors.testimonialAlreadyExists"));
115             saveErrors(request, errors);
116             return mapping.findForward("listTestimonials");
117         } catch ( ObjectOptimisticLockingFailureException e ) {
118             // testimonial item was updated or deleted by another transaction
119
ActionMessages errors = new ActionMessages();
120             errors.add("updateFailed", new ActionMessage("testimonials.errors.updateFailed"));
121             saveErrors(request, errors);
122             return mapping.findForward("listTestimonials");
123         }
124         return mapping.findForward("listTestimonials");
125     }
126 }
Popular Tags