KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constants;
19 import com.blandware.atleap.common.TestimonialModuleConstants;
20 import com.blandware.atleap.common.parsers.html.HTMLPlainTextExtractor;
21 import com.blandware.atleap.common.util.ConvertUtil;
22 import com.blandware.atleap.model.testimonials.Testimonial;
23 import com.blandware.atleap.search.SearchManager;
24 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
25 import com.blandware.atleap.service.testimonials.TestimonialManager;
26 import com.blandware.atleap.webapp.action.core.BaseAction;
27 import com.blandware.atleap.webapp.form.TestimonialForm;
28 import com.blandware.atleap.webapp.util.core.*;
29 import com.blandware.atleap.webapp.util.testimonials.TestimonialModuleWebConstants;
30 import org.apache.commons.validator.GenericValidator;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.action.ActionMessage;
35 import org.apache.struts.action.ActionMessages;
36 import org.springframework.orm.ObjectOptimisticLockingFailureException;
37
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import java.io.ByteArrayInputStream JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.util.*;
43
44 /**
45  * <p>Updates testimonial
46  * </p>
47  * <p><a HREF="UpdateTestimonialAction.java.htm"><i>View Source</i></a></p>
48  * <p/>
49  *
50  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
51  * @version $Revision: 1.16 $ $Date: 2006/03/10 17:10:33 $
52  * @struts.action path="/testimonials/update"
53  * name="testimonialForm"
54  * scope="request"
55  * input="inputForward"
56  * validate="true"
57  * roles="testimonials-item-update"
58  * @struts.action-forward name="inputForward"
59  * path=".testimonials.updateTestimonial"
60  * @struts.action-forward name="listTestimonials"
61  * path="/testimonials/list.do"
62  * redirect="true"
63  * @struts.action-forward name="callUpdateTestimonial"
64  * path="/testimonials/callUpdateTestimonial.do"
65  * redirect="false"
66  */

67 public final class UpdateTestimonialAction extends BaseAction {
68     /**
69      * @param mapping The ActionMapping used to select this instance
70      * @param form The optional ActionForm bean for this request (if any)
71      * @param request The HTTP request we are proceeding
72      * @param response The HTTP response we are creating
73      * @return an ActionForward instance describing where and how
74      * control should be forwarded, or null if response
75      * has already been completed
76      */

77     public ActionForward execute(ActionMapping mapping, ActionForm form,
78                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
79
80         TestimonialForm testimonialForm = (TestimonialForm) form;
81         String JavaDoc localesMode = testimonialForm.getLocalesMode();
82
83         if ( isBackPressed(request) ) {
84             // First check, may be session has been invalidated and we need to
85
// fill content locales again
86
if (request.getSession().getAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY) == null) {
87                 List contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
88                 request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
89             }
90
91             // check 'check all' ckeckbox if needed
92
testimonialForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode);
93
94             return mapping.findForward("inputForward");
95         }
96
97         if ( isCancelled(request) ) {
98             if ( request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY) != null ) {
99                 String JavaDoc redirectUrl = (String JavaDoc) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY);
100                 ActionForward redirect = new ActionForward(redirectUrl, true);
101                 return redirect;
102             }
103             return mapping.findForward("listTestimonials");
104         }
105
106         if ( request.getSession().getAttribute(TestimonialModuleWebConstants.TESTIMONIAL_SUBMITTED_ACTION_KEY) != null ) {
107             return mapping.findForward("listTestimonials");
108         }
109         request.getSession().setAttribute(TestimonialModuleWebConstants.TESTIMONIAL_SUBMITTED_ACTION_KEY, "update");
110
111         Long JavaDoc testimonialId = null;
112         if ( !GenericValidator.isBlankOrNull(testimonialForm.getId()) ) {
113             testimonialId = Long.valueOf(testimonialForm.getId());
114         } else {
115             if ( log.isWarnEnabled() ) {
116                 log.warn("Missing testimonial item ID. Returning to list...");
117             }
118             return mapping.findForward("listTestimonials");
119         }
120
121         request.getSession().setAttribute(TestimonialModuleWebConstants.TESTIMONIAL_ID_KEY, testimonialId);
122
123         TestimonialManager testimonialManager = (TestimonialManager) getBean(TestimonialModuleConstants.TESTIMONIAL_MANAGER_BEAN);
124         Testimonial testimonial = testimonialManager.retrieveTestimonial(testimonialId);
125
126         if ( testimonial == null ) {
127             // testimonial not found. it might be deleted by someone else
128
ActionMessages errors = new ActionMessages();
129             errors.add("testimonialNotFound", new ActionMessage("testimonials.errors.notFound"));
130             saveErrors(request, errors);
131             return mapping.findForward("listTestimonials");
132         }
133
134         Map newBody = testimonial.getBody();
135
136         WebappUtil.copyProperties(testimonial, testimonialForm, request);
137
138 // String localesMode = request.getParameter("localesMode");
139
if (!"current".equalsIgnoreCase(localesMode) && !"all".equalsIgnoreCase(localesMode)) {
140             localesMode = "selected";
141         }
142
143         Map bodyMap = testimonialForm.getBodyMap();
144         Map bodyCheckedBoxes = testimonialForm.getBodyCheckedBoxes();
145         if ("selected".equalsIgnoreCase(localesMode)) {
146             for (Iterator i = bodyMap.entrySet().iterator(); i.hasNext();) {
147                 Map.Entry entry = (Map.Entry) i.next();
148                 String JavaDoc localeIdentifier = (String JavaDoc) entry.getKey();
149                 if (bodyCheckedBoxes.get(localeIdentifier) == null) {
150                     // if this locale is not checked, replace body with old one
151
bodyMap.put(localeIdentifier, newBody.get(localeIdentifier));
152                 }
153             }
154         }
155
156         testimonial.setTitle(testimonialForm.getTitleMap());
157         testimonial.setBody(bodyMap);
158         String JavaDoc uri = TestimonialModuleWebConstants.TESTIMONIAL_URI_PREFIX + "/item" + testimonialForm.getId();
159         testimonial.setUri(uri);
160
161         // get all refs
162
HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor();
163         Set refs = new HashSet();
164         for ( Iterator i = bodyMap.entrySet().iterator(); i.hasNext(); ) {
165             Map.Entry entry = (Map.Entry) i.next();
166             String JavaDoc body = (String JavaDoc) entry.getValue();
167             InputStream JavaDoc bodyIS = new ByteArrayInputStream JavaDoc(ConvertUtil.convertToByteArray(body));
168             refs.addAll(extractor.extractAllRefs(bodyIS, Constants.DEFAULT_ENCODING));
169         }
170
171         Map linkedObjects = WebappUtil.mapObjectsToRefs(refs, request.getSession().getServletContext(), request.getContextPath());
172
173         try {
174             testimonialManager.updateTestimonial(testimonial, linkedObjects);
175
176             // index testimonial item
177
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
178             searchManager.unIndexPage(testimonial.getUri(), request);
179             if ( testimonial.getActive().booleanValue() ) {
180                 searchManager.indexPage(testimonial, request);
181             }
182
183             CacheUtil cacheUtil = CacheUtil.getInstance(request);
184             cacheUtil.flushLocalizableFieldValueCache(testimonialId);
185
186         } catch ( BeanAlreadyExistsException e ) {
187             // testimonial already exists
188

189             // check 'check all' ckeckbox if needed
190
testimonialForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode);
191
192             ActionMessages errors = new ActionMessages();
193             errors.add("testimonialAlreadyExists", new ActionMessage("errors.testimonialAlreadyExists", uri));
194             saveErrors(request, errors);
195             return mapping.getInputForward();
196         } catch ( ObjectOptimisticLockingFailureException e ) {
197             // testimonial item was updated or deleted by another transaction
198
ActionMessages errors = new ActionMessages();
199             errors.add("updateFailed", new ActionMessage("testimonials.errors.updateFailed"));
200             saveErrors(request, errors);
201
202             // merge body
203
Map oldBody = bodyMap;
204             Map mergedBody = new HashMap();
205             for ( Iterator i = oldBody.entrySet().iterator(); i.hasNext(); ) {
206                 Map.Entry entry = (Map.Entry) i.next();
207                 String JavaDoc locale = (String JavaDoc) entry.getKey();
208                 String JavaDoc oldValue = (String JavaDoc) entry.getValue();
209                 String JavaDoc newValue = (String JavaDoc) newBody.get(locale);
210                 String JavaDoc mergedValue = MergeUtil.mergeHtml(oldValue, newValue);
211                 mergedBody.put(locale, mergedValue);
212             }
213             request.getSession().setAttribute(TestimonialModuleWebConstants.TESTIMONIAL_MERGED_BODY_KEY, mergedBody);
214
215             // check 'check all' ckeckbox if needed
216
testimonialForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode);
217
218             return mapping.findForward("callUpdateTestimonial");
219         }
220
221         if ( request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY) != null ) {
222             String JavaDoc redirectUrl = (String JavaDoc) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY);
223             ActionForward redirect = new ActionForward(redirectUrl, true);
224             return redirect;
225         }
226
227         return mapping.findForward("listTestimonials");
228     }
229 }
Popular Tags