KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > action > BaseFormController


1 package org.appfuse.webapp.action;
2
3 import java.text.SimpleDateFormat JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Locale JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
33  * Implementation of <strong>SimpleFormController</strong> that contains
34  * convenience methods for subclasses. For example, getting the current
35  * user and saving messages/errors. This class is intended to
36  * be a base class for all Form controllers.
37  *
38  * <p><a HREF="BaseFormController.java.htm"><i>View Source</i></a></p>
39  *
40  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
41  */

42 public class BaseFormController extends SimpleFormController {
43     protected final transient Log log = LogFactory.getLog(getClass());
44     protected final String JavaDoc MESSAGES_KEY = "successMessages";
45     private UserManager userManager = null;
46     protected MailEngine mailEngine = null;
47     protected SimpleMailMessage message = null;
48     protected String JavaDoc templateName = null;
49     protected String JavaDoc 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 JavaDoc request, String JavaDoc msg) {
60         List JavaDoc messages = (List JavaDoc) request.getSession().getAttribute(MESSAGES_KEY);
61
62         if (messages == null) {
63             messages = new ArrayList JavaDoc();
64         }
65
66         messages.add(msg);
67         request.getSession().setAttribute(MESSAGES_KEY, messages);
68     }
69
70     /**
71      * Convenience method for getting a i18n key's value. Calling
72      * getMessageSourceAccessor() is used because the RequestContext variable
73      * is not set in unit tests b/c there's no DispatchServlet Request.
74      *
75      * @param msgKey
76      * @param locale the current locale
77      * @return
78      */

79     public String JavaDoc getText(String JavaDoc msgKey, Locale JavaDoc locale) {
80         return getMessageSourceAccessor().getMessage(msgKey, locale);
81     }
82
83     /**
84      * Convenient method for getting a i18n key's value with a single
85      * string argument.
86      *
87      * @param msgKey
88      * @param arg
89      * @param locale the current locale
90      * @return
91      */

92     public String JavaDoc getText(String JavaDoc msgKey, String JavaDoc arg, Locale JavaDoc locale) {
93         return getText(msgKey, new Object JavaDoc[] { arg }, locale);
94     }
95
96     /**
97      * Convenience method for getting a i18n key's value with arguments.
98      *
99      * @param msgKey
100      * @param args
101      * @param locale the current locale
102      * @return
103      */

104     public String JavaDoc getText(String JavaDoc msgKey, Object JavaDoc[] args, Locale JavaDoc locale) {
105         return getMessageSourceAccessor().getMessage(msgKey, args, locale);
106     }
107
108     /**
109      * Convenience method to get the Configuration HashMap
110      * from the servlet context.
111      *
112      * @return the user's populated form from the session
113      */

114     public Map JavaDoc getConfiguration() {
115         Map JavaDoc config =
116             (HashMap JavaDoc) getServletContext().getAttribute(Constants.CONFIG);
117
118         // so unit tests don't puke when nothing's been set
119
if (config == null) {
120             return new HashMap JavaDoc();
121         }
122
123         return config;
124     }
125
126     /**
127      * Default behavior for FormControllers - redirect to the successView
128      * when the cancel button has been pressed.
129      */

130     public ModelAndView processFormSubmission(HttpServletRequest JavaDoc request,
131                                               HttpServletResponse JavaDoc response,
132                                               Object JavaDoc command,
133                                               BindException errors)
134     throws Exception JavaDoc {
135         if (request.getParameter("cancel") != null) {
136             return new ModelAndView(getCancelView());
137         }
138
139         return super.processFormSubmission(request, response, command, errors);
140     }
141     
142     /**
143      * Set up a custom property editor for converting form inputs to real objects
144      */

145     protected void initBinder(HttpServletRequest JavaDoc request,
146                               ServletRequestDataBinder binder) {
147         binder.registerCustomEditor(Integer JavaDoc.class, null,
148                                     new CustomNumberEditor(Integer JavaDoc.class, null, true));
149         binder.registerCustomEditor(Long JavaDoc.class, null,
150                                     new CustomNumberEditor(Long JavaDoc.class, null, true));
151         binder.registerCustomEditor(byte[].class,
152                                     new ByteArrayMultipartFileEditor());
153         SimpleDateFormat JavaDoc dateFormat =
154             new SimpleDateFormat JavaDoc(getText("date.format", request.getLocale()));
155         dateFormat.setLenient(false);
156         binder.registerCustomEditor(Date JavaDoc.class, null,
157                                     new CustomDateEditor(dateFormat, true));
158     }
159
160     /**
161      * Convenience message to send messages to users, includes app URL as footer.
162      * @param user
163      * @param msg
164      * @param url
165      */

166     protected void sendUserMessage(User user, String JavaDoc msg, String JavaDoc 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 JavaDoc model = new HashMap JavaDoc();
174         model.put("user", user);
175
176         // TODO: once you figure out how to get the global resource bundle in
177
// WebWork, then figure it out here too. In the meantime, the Username
178
// and Password labels are hard-coded into the template.
179
// model.put("bundle", getTexts());
180
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 JavaDoc templateName) {
194         this.templateName = templateName;
195     }
196     /**
197      * Indicates what view to use when the cancel button has been pressed.
198      */

199     public final void setCancelView(String JavaDoc cancelView) {
200         this.cancelView = cancelView;
201     }
202
203     public final String JavaDoc getCancelView() {
204         // Default to successView if cancelView is invalid
205
if (this.cancelView == null || this.cancelView.length()==0) {
206             return getSuccessView();
207         }
208         return this.cancelView;
209     }
210
211 }
212
Popular Tags