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.WebappUtil; 29 import com.blandware.atleap.webapp.util.core.WebappConstants; 30 import com.blandware.atleap.webapp.util.core.LocaleUtil; 31 import com.blandware.atleap.webapp.util.testimonials.TestimonialModuleWebConstants; 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.action.ActionMessage; 36 import org.apache.struts.action.ActionMessages; 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 66 public final class CreateTestimonialAction extends BaseAction { 67 76 public ActionForward execute(ActionMapping mapping, ActionForm form, 77 HttpServletRequest request, HttpServletResponse response) throws Exception { 78 79 TestimonialForm testimonialForm = (TestimonialForm) form; 80 String localesMode = testimonialForm.getLocalesMode(); 81 82 if ( isBackPressed(request) ) { 83 if (request.getSession().getAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY) == null) { 86 List contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales(); 87 request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales); 88 } 89 90 testimonialForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode); 92 93 return mapping.findForward("inputForward"); 94 } 95 96 if ( isCancelled(request) ) { 97 return mapping.findForward("listTestimonials"); 98 } 99 100 if ( request.getSession().getAttribute(TestimonialModuleWebConstants.TESTIMONIAL_SUBMITTED_ACTION_KEY) != null ) { 101 return mapping.findForward("listTestimonials"); 102 } 103 request.getSession().setAttribute(TestimonialModuleWebConstants.TESTIMONIAL_SUBMITTED_ACTION_KEY, "create"); 104 105 Testimonial testimonial = new Testimonial(); 106 WebappUtil.copyProperties(testimonial, testimonialForm, request); 107 testimonial.setActive(Boolean.TRUE); 108 109 Map bodyMap = testimonialForm.getBodyMap(); 110 Map bodyCheckedBoxes = testimonialForm.getBodyCheckedBoxes(); 111 for (Iterator i = bodyMap.entrySet().iterator(); i.hasNext();) { 112 Map.Entry entry = (Map.Entry) i.next(); 113 String localeIdentifier = (String ) entry.getKey(); 114 if (bodyCheckedBoxes.get(localeIdentifier) == null) { 115 bodyMap.put(localeIdentifier, ""); 117 } 118 } 119 120 testimonial.setTitle(testimonialForm.getTitleMap()); 121 testimonial.setBody(bodyMap); 122 TestimonialManager testimonialManager = (TestimonialManager) getBean(TestimonialModuleConstants.TESTIMONIAL_MANAGER_BEAN); 123 124 HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor(); 126 Set refs = new HashSet(); 127 for ( Iterator i = bodyMap.entrySet().iterator(); i.hasNext(); ) { 128 Map.Entry entry = (Map.Entry) i.next(); 129 String body = (String ) entry.getValue(); 130 InputStream bodyIS = new ByteArrayInputStream (ConvertUtil.convertToByteArray(body)); 131 refs.addAll(extractor.extractAllRefs(bodyIS, Constants.DEFAULT_ENCODING)); 132 } 133 134 Map linkedObjects = WebappUtil.mapObjectsToRefs(refs, request.getSession().getServletContext(), request.getContextPath()); 135 136 Long testimonialId = null; 137 String uri = null; 138 139 try { 140 testimonialId = testimonialManager.createTestimonial(testimonial, linkedObjects); 142 143 uri = TestimonialModuleWebConstants.TESTIMONIAL_URI_PREFIX + "/item" + testimonialId; 144 testimonial.setUri(uri); 145 testimonialManager.updateTestimonial(testimonial, linkedObjects); 146 147 if ( testimonial.getActive().booleanValue() ) { 148 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext()); 150 searchManager.reIndexPage(testimonial, request); 151 } 152 153 } catch ( BeanAlreadyExistsException e ) { 154 156 testimonialForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode); 158 159 ActionMessages errors = new ActionMessages(); 160 errors.add("testimonialAlreadyExists", new ActionMessage("testimonials.errors.alreadyExists", uri)); 161 saveErrors(request, errors); 162 saveToken(request); 163 return mapping.getInputForward(); 164 } 165 return mapping.findForward("listTestimonials"); 166 } 167 } | Popular Tags |