View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.webapp.action;
24  
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import javax.servlet.http.HttpSession;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.struts2.ServletActionContext;
37  import org.springframework.beans.factory.annotation.Required;
38  import org.springframework.mail.SimpleMailMessage;
39  
40  import com.gisgraphy.Constants;
41  import com.gisgraphy.model.User;
42  import com.gisgraphy.service.MailEngine;
43  import com.gisgraphy.service.RoleManager;
44  import com.gisgraphy.service.UserManager;
45  import com.opensymphony.xwork2.ActionSupport;
46  
47  /**
48   * Implementation of <strong>ActionSupport</strong> that contains convenience
49   * methods for subclasses. For example, getting the current user and saving
50   * messages/errors. This class is intended to be a base class for all Action
51   * classes.
52   * 
53   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
54   */
55  public class BaseAction extends ActionSupport {
56      private static final long serialVersionUID = 3525445612504421307L;
57  
58      /**
59       * Constant for cancel result String
60       */
61      public static final String CANCEL = "cancel";
62  
63      /**
64       * Transient log to prevent session synchronization issues - children can
65       * use instance for logging.
66       */
67      protected transient final Log log = LogFactory.getLog(getClass());
68  
69      /**
70       * The UserManager
71       */
72      protected UserManager userManager;
73  
74      /**
75       * The RoleManager
76       */
77      protected RoleManager roleManager;
78  
79      /**
80       * Indicator if the user clicked cancel
81       */
82      protected String cancel;
83  
84      /**
85       * Indicator for the page the user came from.
86       */
87      protected String from;
88  
89      /**
90       * Set to "delete" when a "delete" request parameter is passed in
91       */
92      protected String delete;
93  
94      /**
95       * Set to "save" when a "save" request parameter is passed in
96       */
97      protected String save;
98  
99      /**
100      * MailEngine for sending e-mail
101      */
102     protected MailEngine mailEngine;
103 
104     /**
105      * A message pre-populated with default data
106      */
107     protected SimpleMailMessage mailMessage;
108 
109     /**
110      * Velocity template to use for e-mailing
111      */
112     protected String templateName;
113 
114     /**
115      * Simple method that returns "cancel" result
116      * 
117      * @return "cancel"
118      */
119     public String cancel() {
120 	return CANCEL;
121     }
122 
123     /**
124      * Save the message in the session, appending if messages already exist
125      * 
126      * @param msg
127      *                the message to put in the session
128      */
129     @SuppressWarnings("unchecked")
130     protected void saveMessage(String msg) {
131 	List messages = (List) getRequest().getSession().getAttribute(
132 		"messages");
133 	if (messages == null) {
134 	    messages = new ArrayList();
135 	}
136 	messages.add(msg);
137 	getRequest().getSession().setAttribute("messages", messages);
138     }
139 
140     /**
141      * Convenience method to get the Configuration HashMap from the servlet
142      * context.
143      * 
144      * @return the user's populated form from the session
145      */
146     @SuppressWarnings("unchecked")
147     protected Map getConfiguration() {
148 	Map config = (HashMap) getSession().getServletContext().getAttribute(
149 		Constants.CONFIG);
150 	// so unit tests don't puke when nothing's been set
151 	if (config == null) {
152 	    return new HashMap();
153 	}
154 	return config;
155     }
156 
157     /**
158      * Convenience method to get the request
159      * 
160      * @return current request
161      */
162     protected HttpServletRequest getRequest() {
163 	return ServletActionContext.getRequest();
164     }
165 
166     /**
167      * Convenience method to get the response
168      * 
169      * @return current response
170      */
171     protected HttpServletResponse getResponse() {
172 	return ServletActionContext.getResponse();
173     }
174 
175     /**
176      * Convenience method to get the session. This will create a session if one
177      * doesn't exist.
178      * 
179      * @return the session from the request (request.getSession()).
180      */
181     protected HttpSession getSession() {
182 	return getRequest().getSession();
183     }
184 
185     /**
186      * Convenience method to send e-mail to users
187      * 
188      * @param user
189      *                the user to send to
190      * @param msg
191      *                the message to send
192      * @param url
193      *                the URL to the application (or where ever you'd like to
194      *                send them)
195      */
196     protected void sendUserMessage(User user, String msg, String url) {
197 	if (log.isDebugEnabled()) {
198 	    log.debug("sending e-mail to user [" + user.getEmail() + "]...");
199 	}
200 
201 	mailMessage.setTo(user.getFullName() + "<" + user.getEmail() + ">");
202 
203 	Map<String, Object> model = new HashMap<String, Object>();
204 	model.put("user", user);
205 	model.put("message", msg);
206 	model.put("applicationURL", url);
207 	mailEngine.sendMessage(mailMessage, templateName, model);
208     }
209 
210     @Required
211     public void setUserManager(UserManager userManager) {
212 	this.userManager = userManager;
213     }
214 
215     @Required
216     public void setRoleManager(RoleManager roleManager) {
217 	this.roleManager = roleManager;
218     }
219 
220     @Required
221     public void setMailEngine(MailEngine mailEngine) {
222 	this.mailEngine = mailEngine;
223     }
224 
225     @Required
226     public void setMailMessage(SimpleMailMessage mailMessage) {
227 	this.mailMessage = mailMessage;
228     }
229 
230     public void setTemplateName(String templateName) {
231 	this.templateName = templateName;
232     }
233 
234     /**
235      * Convenience method for setting a "from" parameter to indicate the
236      * previous page.
237      * 
238      * @param from
239      *                indicator for the originating page
240      */
241     public void setFrom(String from) {
242 	this.from = from;
243     }
244 
245     public void setDelete(String delete) {
246 	this.delete = delete;
247     }
248 
249     public void setSave(String save) {
250 	this.save = save;
251     }
252 }