1 package com.panogenesis.webapp.action;
2
3 import java.text.NumberFormat;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Locale;
9 import java.util.Map;
10
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 import com.panogenesis.Constants;
18 import com.panogenesis.model.User;
19 import com.panogenesis.service.MailEngine;
20 import com.panogenesis.service.UserManager;
21
22 import org.springframework.beans.propertyeditors.CustomNumberEditor;
23 import org.springframework.mail.SimpleMailMessage;
24 import org.springframework.validation.BindException;
25 import org.springframework.web.bind.ServletRequestDataBinder;
26 import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
27 import org.springframework.web.servlet.ModelAndView;
28 import org.springframework.web.servlet.mvc.SimpleFormController;
29 import org.springframework.web.servlet.view.RedirectView;
30
31 /***
32 * Implementation of <strong>SimpleFormController</strong> that contains
33 * convenience methods for subclasses. For example, getting the current
34 * user and saving messages/errors. This class is intended to
35 * be a base class for all Form controllers.
36 *
37 * <p><a href="BaseFormController.java.html"><i>View Source</i></a></p>
38 *
39 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
40 */
41 public class BaseFormController extends SimpleFormController {
42 protected final transient Log log = LogFactory.getLog(getClass());
43 protected UserManager mgr = null;
44 protected MailEngine mailEngine = null;
45 protected SimpleMailMessage message = null;
46 protected String templateName = null;
47
48 public void setUserManager(UserManager userManager) {
49 this.mgr = userManager;
50 }
51
52 public UserManager getUserManager() {
53 return this.mgr;
54 }
55
56 public void saveMessage(HttpServletRequest request, String msg) {
57 List messages = (List) request.getSession().getAttribute("messages");
58
59 if (messages == null) {
60 messages = new ArrayList();
61 }
62
63 messages.add(msg);
64 request.getSession().setAttribute("messages", messages);
65 }
66
67 /***
68 * Convenience method for getting a i18n key's value. Calling
69 * getMessageSourceAccessor() is used because the RequestContext variable
70 * is not set in unit tests b/c there's no DispatchServlet Request.
71 *
72 * @param msgKey
73 * @param locale the current locale
74 * @return
75 */
76 public String getText(String msgKey, Locale locale) {
77 return getMessageSourceAccessor().getMessage(msgKey, locale);
78 }
79
80 /***
81 * Convenient method for getting a i18n key's value with a single
82 * string argument.
83 *
84 * @param msgKey
85 * @param arg
86 * @param locale the current locale
87 * @return
88 */
89 public String getText(String msgKey, String arg, Locale locale) {
90 return getText(msgKey, new Object[] { arg }, locale);
91 }
92
93 /***
94 * Convenience method for getting a i18n key's value with arguments.
95 *
96 * @param msgKey
97 * @param args
98 * @param locale the current locale
99 * @return
100 */
101 public String getText(String msgKey, Object[] args, Locale locale) {
102 return getMessageSourceAccessor().getMessage(msgKey, args, locale);
103 }
104
105 /***
106 * Convenience method to get the user object from the session
107 *
108 * @param request the current request
109 * @return the user's populated object from the session
110 */
111 protected User getUser(HttpServletRequest request) {
112 return (User) request.getSession().getAttribute(Constants.USER_KEY);
113 }
114
115 /***
116 * Convenience method to get the Configuration HashMap
117 * from the servlet context.
118 *
119 * @return the user's populated form from the session
120 */
121 public Map getConfiguration() {
122 Map config =
123 (HashMap) getServletContext().getAttribute(Constants.CONFIG);
124
125
126 if (config == null) {
127 return new HashMap();
128 }
129
130 return config;
131 }
132
133 /***
134 * Default behavior for FormControllers - redirect to the successView
135 * when the cancel button has been pressed.
136 */
137 public ModelAndView processFormSubmission(HttpServletRequest request,
138 HttpServletResponse response,
139 Object command,
140 BindException errors)
141 throws Exception {
142 if (request.getParameter("cancel") != null) {
143 return new ModelAndView(new RedirectView(getSuccessView()));
144 }
145
146 return super.processFormSubmission(request, response, command, errors);
147 }
148
149 /***
150 * Set up a custom property editor for converting form inputs to real objects
151 */
152 protected void initBinder(HttpServletRequest request,
153 ServletRequestDataBinder binder) {
154 NumberFormat nf = NumberFormat.getNumberInstance();
155 binder.registerCustomEditor(Integer.class, null,
156 new CustomNumberEditor(Integer.class, nf, true));
157 binder.registerCustomEditor(Long.class, null,
158 new CustomNumberEditor(Long.class, nf, true));
159 binder.registerCustomEditor(byte[].class,
160 new ByteArrayMultipartFileEditor());
161 }
162
163 /***
164 * Convenience message to send messages to users, includes app URL as footer.
165 * @param user
166 * @param msg
167 * @param url
168 */
169 protected void sendUserMessage(User user, String msg, String url) {
170 if (log.isDebugEnabled()) {
171 log.debug("sending e-mail to user [" + user.getEmail() + "]...");
172 }
173
174 message.setTo(user.getFullName() + "<" + user.getEmail() + ">");
175
176 Map model = new HashMap();
177 model.put("user", user);
178
179
180
181
182
183 model.put("message", msg);
184 model.put("applicationURL", url);
185 mailEngine.sendMessage(message, templateName, model);
186 }
187
188 public void setMailEngine(MailEngine mailEngine) {
189 this.mailEngine = mailEngine;
190 }
191
192 public void setMessage(SimpleMailMessage message) {
193 this.message = message;
194 }
195
196 public void setTemplateName(String templateName) {
197 this.templateName = templateName;
198 }
199 }