1 16 package com.blandware.atleap.webapp.action.core.mailTemplate; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.parsers.html.HTMLPlainTextExtractor; 20 import com.blandware.atleap.common.util.ConvertUtil; 21 import com.blandware.atleap.model.core.MailTemplate; 22 import com.blandware.atleap.service.core.MailTemplateManager; 23 import com.blandware.atleap.webapp.action.core.BaseAction; 24 import com.blandware.atleap.webapp.form.MailTemplateForm; 25 import com.blandware.atleap.webapp.util.core.MergeUtil; 26 import com.blandware.atleap.webapp.util.core.WebappConstants; 27 import com.blandware.atleap.webapp.util.core.WebappUtil; 28 import com.blandware.atleap.webapp.util.core.LocaleUtil; 29 import org.apache.commons.validator.GenericValidator; 30 import org.apache.struts.action.ActionForm; 31 import org.apache.struts.action.ActionForward; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionMessage; 34 import org.apache.struts.action.ActionMessages; 35 import org.springframework.orm.ObjectOptimisticLockingFailureException; 36 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import java.io.ByteArrayInputStream ; 40 import java.io.InputStream ; 41 import java.util.*; 42 43 72 public final class UpdateMailTemplateAction extends BaseAction { 73 82 public ActionForward execute(ActionMapping mapping, ActionForm form, 83 HttpServletRequest request, HttpServletResponse response) throws Exception { 84 85 if ( isBackPressed(request) ) { 86 if (request.getSession().getAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY) == null) { 89 List contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales(); 90 request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales); 91 } 92 return mapping.findForward("callUpdateTemplate"); 93 } 94 95 if ( isCancelled(request) ) { 96 return mapping.findForward("listMailTemplates"); 97 } 98 99 if ( request.getSession().getAttribute(WebappConstants.MAIL_TEMPLATE_SUBMITTED_ACTION_KEY) == null ) { 100 101 request.getSession().setAttribute(WebappConstants.MAIL_TEMPLATE_SUBMITTED_ACTION_KEY, "update"); 102 103 MailTemplateForm mailTemplateForm = (MailTemplateForm) form; 104 105 Long mailTemplateId = null; 106 if ( !GenericValidator.isBlankOrNull(mailTemplateForm.getId()) ) { 107 mailTemplateId = Long.valueOf(mailTemplateForm.getId()); 108 } else { 109 if ( log.isWarnEnabled() ) { 110 log.warn("Missing mail template ID. Returning to list..."); 111 } 112 return mapping.findForward("listMailTemplates"); 113 } 114 115 request.getSession().setAttribute(WebappConstants.MAIL_TEMPLATE_ID_KEY, mailTemplateId); 116 117 MailTemplateManager mailTemplateManager = (MailTemplateManager) getBean(Constants.MAIL_TEMPLATE_MANAGER_BEAN); 118 MailTemplate mailTemplate = mailTemplateManager.retrieveMailTemplate(mailTemplateId); 119 120 if ( mailTemplate == null ) { 121 ActionMessages errors = new ActionMessages(); 123 errors.add("mailTemplateNotFound", new ActionMessage("core.mailTemplate.errors.notFound")); 124 saveErrors(request, errors); 125 return mapping.findForward("listMailTemplates"); 126 } 127 128 HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor(); 130 131 Map newBody = mailTemplate.getBody(); 132 133 WebappUtil.copyProperties(mailTemplate, mailTemplateForm, request); 134 mailTemplate.setFrom(mailTemplateForm.getFromMap()); 135 mailTemplate.setSubject(mailTemplateForm.getSubjectMap()); 136 mailTemplate.setBody(mailTemplateForm.getBodyMap()); 137 mailTemplate.setCharset(mailTemplateForm.getCharsetMap()); 138 139 boolean isPlain = mailTemplate.isPlain(); 140 141 Map linkedObjects = new HashMap(); 142 if ( !isPlain ) { 143 Set refs = new HashSet(); 145 for ( Iterator i = mailTemplateForm.getBodyMap().values().iterator(); i.hasNext(); ) { 146 String body = (String ) i.next(); 147 if ( !GenericValidator.isBlankOrNull(body) ) { 148 InputStream bodyIS = new ByteArrayInputStream (ConvertUtil.convertToByteArray(body)); 149 refs.addAll(extractor.extractAllRefs(bodyIS, Constants.DEFAULT_ENCODING)); 150 } 151 } 152 linkedObjects = WebappUtil.mapObjectsToRefs(refs, request.getSession().getServletContext(), request.getContextPath()); 153 } 154 155 try { 156 mailTemplateManager.updateMailTemplate(mailTemplate, linkedObjects); 157 } catch ( ObjectOptimisticLockingFailureException e ) { 158 ActionMessages errors = new ActionMessages(); 160 errors.add("updateFailed", new ActionMessage("core.mailTemplate.errors.updateFailed")); 161 saveErrors(request, errors); 162 163 Map oldBody = mailTemplateForm.getBodyMap(); 165 Map mergedBody = new HashMap(); 166 for ( Iterator i = oldBody.entrySet().iterator(); i.hasNext(); ) { 167 Map.Entry entry = (Map.Entry) i.next(); 168 String locale = (String ) entry.getKey(); 169 String oldValue = (String ) entry.getValue(); 170 String newValue = (String ) newBody.get(locale); 171 String mergedValue = MergeUtil.mergeText(oldValue, newValue); 172 mergedBody.put(locale, mergedValue); 173 } 174 175 request.setAttribute(WebappConstants.MAIL_TEMPLATE_MERGED_BODY_KEY, mergedBody); 176 177 return mapping.findForward("callUpdateMailTemplate"); 178 } finally { 179 request.getSession().removeAttribute(WebappConstants.MAIL_TEMPLATE_SUBMITTED_ACTION_KEY); 180 } 181 } 182 return mapping.findForward("listMailTemplates"); 183 } 184 } | Popular Tags |