1 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 ; 39 import javax.servlet.http.HttpServletResponse ; 40 import java.io.ByteArrayInputStream ; 41 import java.io.InputStream ; 42 import java.util.*; 43 44 67 public final class UpdateTestimonialAction extends BaseAction { 68 77 public ActionForward execute(ActionMapping mapping, ActionForm form, 78 HttpServletRequest request, HttpServletResponse response) throws Exception { 79 80 TestimonialForm testimonialForm = (TestimonialForm) form; 81 String localesMode = testimonialForm.getLocalesMode(); 82 83 if ( isBackPressed(request) ) { 84 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 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 redirectUrl = (String ) 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 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 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 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 localeIdentifier = (String ) entry.getKey(); 149 if (bodyCheckedBoxes.get(localeIdentifier) == null) { 150 bodyMap.put(localeIdentifier, newBody.get(localeIdentifier)); 152 } 153 } 154 } 155 156 testimonial.setTitle(testimonialForm.getTitleMap()); 157 testimonial.setBody(bodyMap); 158 String uri = TestimonialModuleWebConstants.TESTIMONIAL_URI_PREFIX + "/item" + testimonialForm.getId(); 159 testimonial.setUri(uri); 160 161 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 body = (String ) entry.getValue(); 167 InputStream bodyIS = new ByteArrayInputStream (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 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 189 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 ActionMessages errors = new ActionMessages(); 199 errors.add("updateFailed", new ActionMessage("testimonials.errors.updateFailed")); 200 saveErrors(request, errors); 201 202 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 locale = (String ) entry.getKey(); 208 String oldValue = (String ) entry.getValue(); 209 String newValue = (String ) newBody.get(locale); 210 String mergedValue = MergeUtil.mergeHtml(oldValue, newValue); 211 mergedBody.put(locale, mergedValue); 212 } 213 request.getSession().setAttribute(TestimonialModuleWebConstants.TESTIMONIAL_MERGED_BODY_KEY, mergedBody); 214 215 testimonialForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode); 217 218 return mapping.findForward("callUpdateTestimonial"); 219 } 220 221 if ( request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY) != null ) { 222 String redirectUrl = (String ) 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 |