1 package org.appfuse.webapp.action; 2 3 import java.text.MessageFormat ; 4 5 import java.util.ArrayList ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.ResourceBundle ; 10 11 import javax.faces.context.FacesContext; 12 13 import javax.servlet.ServletContext ; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 import javax.servlet.http.HttpSession ; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 import org.appfuse.Constants; 22 import org.appfuse.model.User; 23 import org.appfuse.service.MailEngine; 24 import org.appfuse.service.UserManager; 25 26 import org.springframework.mail.SimpleMailMessage; 27 28 public class BasePage { 29 public static final String jstlBundleParam = "javax.servlet.jsp.jstl.fmt.localizationContext"; 30 protected final Log log = LogFactory.getLog(getClass()); 31 protected UserManager userManager = null; 32 protected MailEngine mailEngine = null; 33 protected SimpleMailMessage message = null; 34 protected String templateName = null; 35 protected FacesContext facesContext = null; 36 37 41 public void setFacesContext(FacesContext facesContext) { 42 this.facesContext = facesContext; 43 } 44 45 public FacesContext getFacesContext() { 46 if (facesContext != null){ 47 return facesContext; 49 } 50 return FacesContext.getCurrentInstance(); 51 } 52 53 public void setUserManager(UserManager userManager) { 54 this.userManager = userManager; 55 } 56 57 public String getParameter(String name) { 59 return getRequest().getParameter(name); 60 } 61 62 public String getBundleName() { 63 return getServletContext().getInitParameter(jstlBundleParam); 65 } 66 67 public Map getCountries() { 68 CountryModel model = new CountryModel(); 69 70 return model.getCountries(getRequest().getLocale()); 71 } 72 73 public ResourceBundle getBundle() { 74 return ResourceBundle.getBundle(getBundleName(), getRequest().getLocale()); 75 } 76 77 public String getText(String key) { 78 String message; 79 80 try { 81 message = getBundle().getString(key); 82 } catch (java.util.MissingResourceException mre) { 83 log.warn("Missing key for '" + key + "'"); 84 85 return "???" + key + "???"; 86 } 87 88 return message; 89 } 90 91 public String getText(String key, Object arg) { 92 if (arg == null) { 93 return getBundle().getString(key); 94 } 95 96 MessageFormat form = new MessageFormat (getBundle().getString(key)); 97 98 if (arg instanceof String ) { 99 return form.format(new Object [] { arg }); 100 } else if (arg instanceof Object []) { 101 return form.format(arg); 102 } else { 103 log.error("arg '" + arg + "' not String or Object[]"); 104 105 return ""; 106 } 107 } 108 109 protected void addMessage(String key, Object arg) { 110 List messages = (List ) getSession().getAttribute("messages"); 113 114 if (messages == null) { 115 messages = new ArrayList (); 116 } 117 118 messages.add(getText(key, arg)); 119 getSession().setAttribute("messages", messages); 120 } 121 122 protected void addMessage(String key) { 123 addMessage(key, null); 124 } 125 126 protected void addError(String key, Object arg) { 127 List errors = (List ) getSession().getAttribute("errors"); 130 131 if (errors == null) { 132 errors = new ArrayList (); 133 } 134 135 errors.add(getText(key, arg)); 136 137 getSession().setAttribute("errors", errors); 138 } 139 140 protected void addError(String key) { 141 addError(key, null); 142 } 143 144 148 public boolean hasErrors() { 149 return (getSession().getAttribute("errors") != null); 150 } 151 152 156 protected HttpServletRequest getRequest() { 157 return (HttpServletRequest ) getFacesContext().getExternalContext().getRequest(); 158 } 159 160 164 protected HttpSession getSession() { 165 return getRequest().getSession(); 166 } 167 168 172 protected HttpServletResponse getResponse() { 173 return (HttpServletResponse ) getFacesContext().getExternalContext().getResponse(); 174 } 175 176 180 protected ServletContext getServletContext() { 181 return (ServletContext ) getFacesContext().getExternalContext().getContext(); 182 } 183 184 190 protected Map getConfiguration() { 191 Map config = (HashMap ) getServletContext().getAttribute(Constants.CONFIG); 192 193 if (config == null) { 195 return new HashMap (); 196 } 197 198 return config; 199 } 200 201 207 protected void sendUserMessage(User user, String msg, String url) { 208 if (log.isDebugEnabled()) { 209 log.debug("sending e-mail to user [" + user.getEmail() + "]..."); 210 } 211 212 message.setTo(user.getFullName() + "<" + user.getEmail() + ">"); 213 214 Map model = new HashMap (); 215 model.put("user", user); 216 217 model.put("message", msg); 222 model.put("applicationURL", url); 223 mailEngine.sendMessage(message, templateName, model); 224 } 225 226 public void setMailEngine(MailEngine mailEngine) { 227 this.mailEngine = mailEngine; 228 } 229 230 public void setMessage(SimpleMailMessage message) { 231 this.message = message; 232 } 233 234 public void setTemplateName(String templateName) { 235 this.templateName = templateName; 236 } 237 } 238 | Popular Tags |