KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.appfuse.webapp.action;
2
3 import java.text.MessageFormat JavaDoc;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.ResourceBundle JavaDoc;
10
11 import javax.faces.context.FacesContext;
12
13 import javax.servlet.ServletContext JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16 import javax.servlet.http.HttpSession JavaDoc;
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 JavaDoc 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 JavaDoc templateName = null;
35     protected FacesContext facesContext = null;
36
37     /**
38      * Allow overriding of facesContext for unit tests
39      * @param userManager
40      */

41     public void setFacesContext(FacesContext facesContext) {
42         this.facesContext = facesContext;
43     }
44     
45     public FacesContext getFacesContext() {
46         if (facesContext != null){
47             // for unit tests
48
return facesContext;
49         }
50         return FacesContext.getCurrentInstance();
51     }
52
53     public void setUserManager(UserManager userManager) {
54         this.userManager = userManager;
55     }
56
57     // Convenience methods ====================================================
58
public String JavaDoc getParameter(String JavaDoc name) {
59         return getRequest().getParameter(name);
60     }
61
62     public String JavaDoc getBundleName() {
63         // get name of resource bundle from JSTL settings, JSF makes this too hard
64
return getServletContext().getInitParameter(jstlBundleParam);
65     }
66
67     public Map JavaDoc getCountries() {
68         CountryModel model = new CountryModel();
69
70         return model.getCountries(getRequest().getLocale());
71     }
72
73     public ResourceBundle JavaDoc getBundle() {
74         return ResourceBundle.getBundle(getBundleName(), getRequest().getLocale());
75     }
76
77     public String JavaDoc getText(String JavaDoc key) {
78         String JavaDoc message;
79
80         try {
81             message = getBundle().getString(key);
82         } catch (java.util.MissingResourceException JavaDoc mre) {
83             log.warn("Missing key for '" + key + "'");
84
85             return "???" + key + "???";
86         }
87
88         return message;
89     }
90
91     public String JavaDoc getText(String JavaDoc key, Object JavaDoc arg) {
92         if (arg == null) {
93             return getBundle().getString(key);
94         }
95
96         MessageFormat JavaDoc form = new MessageFormat JavaDoc(getBundle().getString(key));
97
98         if (arg instanceof String JavaDoc) {
99             return form.format(new Object JavaDoc[] { arg });
100         } else if (arg instanceof Object JavaDoc[]) {
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 JavaDoc key, Object JavaDoc arg) {
110         // JSF Success Messages won't live past a redirect, so I don't use it
111
//FacesUtils.addInfoMessage(formatMessage(key, arg));
112
List JavaDoc messages = (List JavaDoc) getSession().getAttribute("messages");
113
114         if (messages == null) {
115             messages = new ArrayList JavaDoc();
116         }
117
118         messages.add(getText(key, arg));
119         getSession().setAttribute("messages", messages);
120     }
121
122     protected void addMessage(String JavaDoc key) {
123         addMessage(key, null);
124     }
125
126     protected void addError(String JavaDoc key, Object JavaDoc arg) {
127         // The "JSF Way" doesn't allow you to put HTML in your error messages, so I don't use it.
128
// FacesUtils.addErrorMessage(formatMessage(key, arg));
129
List JavaDoc errors = (List JavaDoc) getSession().getAttribute("errors");
130
131         if (errors == null) {
132             errors = new ArrayList JavaDoc();
133         }
134
135         errors.add(getText(key, arg));
136
137         getSession().setAttribute("errors", errors);
138     }
139
140     protected void addError(String JavaDoc key) {
141         addError(key, null);
142     }
143     
144     /**
145      * Convenience method for unit tests.
146      * @return
147      */

148     public boolean hasErrors() {
149         return (getSession().getAttribute("errors") != null);
150     }
151
152     /**
153      * Servlet API Convenience method
154      * @return
155      */

156     protected HttpServletRequest JavaDoc getRequest() {
157         return (HttpServletRequest JavaDoc) getFacesContext().getExternalContext().getRequest();
158     }
159
160     /**
161      * Servlet API Convenience method
162      * @return
163      */

164     protected HttpSession JavaDoc getSession() {
165         return getRequest().getSession();
166     }
167
168     /**
169      * Servlet API Convenience method
170      * @return
171      */

172     protected HttpServletResponse JavaDoc getResponse() {
173         return (HttpServletResponse JavaDoc) getFacesContext().getExternalContext().getResponse();
174     }
175
176     /**
177      * Servlet API Convenience method
178      * @return
179      */

180     protected ServletContext JavaDoc getServletContext() {
181         return (ServletContext JavaDoc) getFacesContext().getExternalContext().getContext();
182     }
183
184     /**
185      * Convenience method to get the Configuration HashMap
186      * from the servlet context.
187      *
188      * @return the user's populated form from the session
189      */

190     protected Map JavaDoc getConfiguration() {
191         Map JavaDoc config = (HashMap JavaDoc) getServletContext().getAttribute(Constants.CONFIG);
192
193         // so unit tests don't puke when nothing's been set
194
if (config == null) {
195             return new HashMap JavaDoc();
196         }
197
198         return config;
199     }
200
201     /**
202      * Convenience message to send messages to users, includes app URL as footer.
203      * @param user
204      * @param msg
205      * @param url
206      */

207     protected void sendUserMessage(User user, String JavaDoc msg, String JavaDoc 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 JavaDoc model = new HashMap JavaDoc();
215         model.put("user", user);
216
217         // TODO: once you figure out how to get the global resource bundle in
218
// WebWork, then figure it out here too. In the meantime, the Username
219
// and Password labels are hard-coded into the template.
220
// model.put("bundle", getTexts());
221
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 JavaDoc templateName) {
235         this.templateName = templateName;
236     }
237 }
238
Popular Tags