View Javadoc

1   package net.ramapuram.thomas.webapp.action;
2   
3   import com.opensymphony.xwork2.ActionSupport;
4   import org.apache.commons.logging.Log;
5   import org.apache.commons.logging.LogFactory;
6   import org.apache.struts2.ServletActionContext;
7   import net.ramapuram.thomas.Constants;
8   import net.ramapuram.thomas.model.User;
9   import net.ramapuram.thomas.service.MailEngine;
10  import net.ramapuram.thomas.service.RoleManager;
11  import net.ramapuram.thomas.service.UserManager;
12  import org.springframework.mail.SimpleMailMessage;
13  
14  import javax.servlet.http.HttpServletRequest;
15  import javax.servlet.http.HttpServletResponse;
16  import javax.servlet.http.HttpSession;
17  import java.util.ArrayList;
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  
23  /**
24   * Implementation of <strong>ActionSupport</strong> that contains
25   * convenience methods for subclasses.  For example, getting the current
26   * user and saving messages/errors. This class is intended to
27   * be a base class for all Action classes.
28   *
29   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
30   */
31  public class BaseAction extends ActionSupport {
32      private static final long serialVersionUID = 3525445612504421307L;
33  
34      /**
35       * Constant for cancel result String
36       */
37      public static final String CANCEL = "cancel";
38  
39      /**
40       * Transient log to prevent session synchronization issues - children can use instance for logging.
41       */
42      protected final transient Log log = LogFactory.getLog(getClass());
43  
44      /**
45       * The UserManager
46       */
47      protected UserManager userManager;
48  
49      /**
50       * The RoleManager
51       */
52      protected RoleManager roleManager;
53  
54      /**
55       * Indicator if the user clicked cancel
56       */
57      protected String cancel;
58  
59      /**
60       * Indicator for the page the user came from.
61       */
62      protected String from;
63  
64      /**
65       * Set to "delete" when a "delete" request parameter is passed in
66       */
67      protected String delete;
68  
69      /**
70       * Set to "save" when a "save" request parameter is passed in
71       */
72      protected String save;
73  
74      /**
75       * MailEngine for sending e-mail
76       */
77      protected MailEngine mailEngine;
78  
79      /**
80       * A message pre-populated with default data
81       */
82      protected SimpleMailMessage mailMessage;
83  
84      /**
85       * Velocity template to use for e-mailing
86       */
87      protected String templateName;
88  
89      /**
90       * Simple method that returns "cancel" result
91       *
92       * @return "cancel"
93       */
94      public String cancel() {
95          return CANCEL;
96      }
97  
98      /**
99       * Save the message in the session, appending if messages already exist
100      *
101      * @param msg the message to put in the session
102      */
103     @SuppressWarnings("unchecked")
104     protected void saveMessage(String msg) {
105         List messages = (List) getRequest().getSession().getAttribute("messages");
106         if (messages == null) {
107             messages = new ArrayList();
108         }
109         messages.add(msg);
110         getRequest().getSession().setAttribute("messages", messages);
111     }
112 
113     /**
114      * Convenience method to get the Configuration HashMap
115      * from the servlet context.
116      *
117      * @return the user's populated form from the session
118      */
119     protected Map getConfiguration() {
120         Map config = (HashMap) getSession().getServletContext().getAttribute(Constants.CONFIG);
121         // so unit tests don't puke when nothing's been set
122         if (config == null) {
123             return new HashMap();
124         }
125         return config;
126     }
127 
128     /**
129      * Convenience method to get the request
130      *
131      * @return current request
132      */
133     protected HttpServletRequest getRequest() {
134         return ServletActionContext.getRequest();
135     }
136 
137     /**
138      * Convenience method to get the response
139      *
140      * @return current response
141      */
142     protected HttpServletResponse getResponse() {
143         return ServletActionContext.getResponse();
144     }
145 
146     /**
147      * Convenience method to get the session. This will create a session if one doesn't exist.
148      *
149      * @return the session from the request (request.getSession()).
150      */
151     protected HttpSession getSession() {
152         return getRequest().getSession();
153     }
154 
155     /**
156      * Convenience method to send e-mail to users
157      *
158      * @param user the user to send to
159      * @param msg the message to send
160      * @param url the URL to the application (or where ever you'd like to send them)
161      */
162     protected void sendUserMessage(User user, String msg, String url) {
163         if (log.isDebugEnabled()) {
164             log.debug("sending e-mail to user [" + user.getEmail() + "]...");
165         }
166 
167         mailMessage.setTo(user.getFullName() + "<" + user.getEmail() + ">");
168 
169         Map<String, Object> model = new HashMap<String, Object>();
170         model.put("user", user);
171         // TODO: figure out how to get bundle specified in struts.xml
172         // model.put("bundle", getTexts());
173         model.put("message", msg);
174         model.put("applicationURL", url);
175         mailEngine.sendMessage(mailMessage, templateName, model);
176     }
177 
178     public void setUserManager(UserManager userManager) {
179         this.userManager = userManager;
180     }
181 
182     public void setRoleManager(RoleManager roleManager) {
183         this.roleManager = roleManager;
184     }
185 
186     public void setMailEngine(MailEngine mailEngine) {
187         this.mailEngine = mailEngine;
188     }
189 
190     public void setMailMessage(SimpleMailMessage mailMessage) {
191         this.mailMessage = mailMessage;
192     }
193 
194     public void setTemplateName(String templateName) {
195         this.templateName = templateName;
196     }
197 
198     /**
199      * Convenience method for setting a "from" parameter to indicate the previous page.
200      *
201      * @param from indicator for the originating page
202      */
203     public void setFrom(String from) {
204         this.from = from;
205     }
206 
207     public void setDelete(String delete) {
208         this.delete = delete;
209     }
210 
211     public void setSave(String save) {
212         this.save = save;
213     }
214 }