1 package org.appfuse.webapp.action; 2 3 import java.text.SimpleDateFormat ; 4 import java.util.Date ; 5 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.List ; 9 import java.util.Locale ; 10 import java.util.Map ; 11 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 18 import org.appfuse.Constants; 19 import org.appfuse.model.User; 20 import org.appfuse.service.MailEngine; 21 import org.appfuse.service.UserManager; 22 23 import org.springframework.beans.propertyeditors.CustomDateEditor; 24 import org.springframework.beans.propertyeditors.CustomNumberEditor; 25 import org.springframework.mail.SimpleMailMessage; 26 import org.springframework.validation.BindException; 27 import org.springframework.web.bind.ServletRequestDataBinder; 28 import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor; 29 import org.springframework.web.servlet.ModelAndView; 30 import org.springframework.web.servlet.mvc.SimpleFormController; 31 32 42 public class BaseFormController extends SimpleFormController { 43 protected final transient Log log = LogFactory.getLog(getClass()); 44 protected final String MESSAGES_KEY = "successMessages"; 45 private UserManager userManager = null; 46 protected MailEngine mailEngine = null; 47 protected SimpleMailMessage message = null; 48 protected String templateName = null; 49 protected String cancelView; 50 51 public void setUserManager(UserManager userManager) { 52 this.userManager = userManager; 53 } 54 55 public UserManager getUserManager() { 56 return this.userManager; 57 } 58 59 public void saveMessage(HttpServletRequest request, String msg) { 60 List messages = (List ) request.getSession().getAttribute(MESSAGES_KEY); 61 62 if (messages == null) { 63 messages = new ArrayList (); 64 } 65 66 messages.add(msg); 67 request.getSession().setAttribute(MESSAGES_KEY, messages); 68 } 69 70 79 public String getText(String msgKey, Locale locale) { 80 return getMessageSourceAccessor().getMessage(msgKey, locale); 81 } 82 83 92 public String getText(String msgKey, String arg, Locale locale) { 93 return getText(msgKey, new Object [] { arg }, locale); 94 } 95 96 104 public String getText(String msgKey, Object [] args, Locale locale) { 105 return getMessageSourceAccessor().getMessage(msgKey, args, locale); 106 } 107 108 114 public Map getConfiguration() { 115 Map config = 116 (HashMap ) getServletContext().getAttribute(Constants.CONFIG); 117 118 if (config == null) { 120 return new HashMap (); 121 } 122 123 return config; 124 } 125 126 130 public ModelAndView processFormSubmission(HttpServletRequest request, 131 HttpServletResponse response, 132 Object command, 133 BindException errors) 134 throws Exception { 135 if (request.getParameter("cancel") != null) { 136 return new ModelAndView(getCancelView()); 137 } 138 139 return super.processFormSubmission(request, response, command, errors); 140 } 141 142 145 protected void initBinder(HttpServletRequest request, 146 ServletRequestDataBinder binder) { 147 binder.registerCustomEditor(Integer .class, null, 148 new CustomNumberEditor(Integer .class, null, true)); 149 binder.registerCustomEditor(Long .class, null, 150 new CustomNumberEditor(Long .class, null, true)); 151 binder.registerCustomEditor(byte[].class, 152 new ByteArrayMultipartFileEditor()); 153 SimpleDateFormat dateFormat = 154 new SimpleDateFormat (getText("date.format", request.getLocale())); 155 dateFormat.setLenient(false); 156 binder.registerCustomEditor(Date .class, null, 157 new CustomDateEditor(dateFormat, true)); 158 } 159 160 166 protected void sendUserMessage(User user, String msg, String url) { 167 if (log.isDebugEnabled()) { 168 log.debug("sending e-mail to user [" + user.getEmail() + "]..."); 169 } 170 171 message.setTo(user.getFullName() + "<" + user.getEmail() + ">"); 172 173 Map model = new HashMap (); 174 model.put("user", user); 175 176 model.put("message", msg); 181 model.put("applicationURL", url); 182 mailEngine.sendMessage(message, templateName, model); 183 } 184 185 public void setMailEngine(MailEngine mailEngine) { 186 this.mailEngine = mailEngine; 187 } 188 189 public void setMessage(SimpleMailMessage message) { 190 this.message = message; 191 } 192 193 public void setTemplateName(String templateName) { 194 this.templateName = templateName; 195 } 196 199 public final void setCancelView(String cancelView) { 200 this.cancelView = cancelView; 201 } 202 203 public final String getCancelView() { 204 if (this.cancelView == null || this.cancelView.length()==0) { 206 return getSuccessView(); 207 } 208 return this.cancelView; 209 } 210 211 } 212 | Popular Tags |