View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.control.action;
16  
17  import org.figure8.join.control.JoinAction;
18  import org.figure8.join.control.form.MessageForm;
19  import org.figure8.join.businessobjects.commons.Release;
20  import org.figure8.join.businessobjects.reporting.Message;
21  import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
22  import org.figure8.join.businessfacades.reporting.ReportingManager;
23  import org.figure8.join.util.LogUtil;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.beanutils.PropertyUtils;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  
31  import javax.servlet.http.HttpSession;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import java.util.List;
36  /**
37   * Struts action used for managing news messages within Join application.
38   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
39   * @version $Revision: 1.1 $
40   *
41   * @struts.action path="/message" name="messageForm"
42   *                scope="request" parameter="op" validate="true"
43   *                input="/pages/mainpage.jsp?body=/jsp/messages.jsp"
44   * @struts.action-forward name="Infos" path="/jsp/info.jsp"
45   * @struts.action-forward name="Messages" path="/jsp/messages.jsp"
46   */
47  public class MessageActions extends JoinAction{
48  
49     // Static -------------------------------------------------------------------
50  
51     /** Get a commons logger. */
52     private static final Log log = LogUtil.getLog(MessageActions.class);
53  
54     /** Operation code for loading a specific Message */
55     public static final String LOAD_OP = "load";
56     /** Operation code for saving a Message (create or update) */
57     public static final String SAVE_OP = "save";
58     /** Operation code for getting open messages using release */
59     public static final String OPEN_OP = "open";
60     /** Operation code for searching messages using dates and release */
61     public static final String SEARCH_OP = "search";
62  
63     /**
64      * The session scope attribute under which the Message object currently
65      * selected by our logged-in User is stored.
66      */
67     public static final String MESSAGE_KEY = "message";
68     /**
69      * The request scope attribute under which the retrieved Messages
70      * list is stored.
71      */
72     public static final String MESSAGES_KEY = "messages";
73  
74  
75     // Attributes ---------------------------------------------------------------
76  
77     /** The reporting manager implementation to use. */
78     protected ReportingManager reportingManager = null;
79     /** The integration process manager implementation to use. */
80     protected IntegrationProcessManager processManager = null;
81  
82  
83     // Constructors -------------------------------------------------------------
84  
85     /** Creates a new instance of MessageActions */
86     public MessageActions(){
87     }
88  
89  
90     // Public -------------------------------------------------------------------
91  
92     /** @param manager The reporting manager implementation instance to use */
93     public void setReportingManager(ReportingManager manager){
94        this.reportingManager = manager;
95     }
96     /** @param manager The integration process manager implementation to use */
97     public void setIntegrationProcessManager(IntegrationProcessManager manager){
98        this.processManager = manager;
99     }
100 
101 
102    // Implementation of JoinAction ---------------------------------------------
103 
104    /**
105     *
106     * @param operation String representing the operation to invoke on Action
107     * @param mapping Mapping between forwards name and path for this action
108     * @param form The form object containing request parameters
109     * @param request The servlet container request wrapper
110     * @param response The servlet container response wrapper
111     * @return A forward to the next view to render and display
112     * @throws Exception such as InfraStructureExceptions ...
113     */
114    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
115            throws Exception{
116       // Trace operation to execute.
117       log.debug("doExecute() called for '" + operation + "' operation");
118 
119       if (LOAD_OP.equals(operation)){
120          return loadMessage(mapping, form, request, response);
121       }
122       else if (SAVE_OP.equals(operation)){
123          return saveMessage(mapping, form, request, response);
124       }
125       else if (OPEN_OP.equals(operation)){
126          return getNewMessages(mapping, form, request, response);
127       }
128       else if (SEARCH_OP.equals(operation)){
129          return searchMessages(mapping, form, request, response);
130       }
131       else{
132          // Default : empty messages management page.
133          request.getSession().removeAttribute(MESSAGE_KEY);
134          return mapping.findForward("Messages");
135       }
136    }
137 
138 
139    // Protected ----------------------------------------------------------------
140 
141    /**
142     * Load a specified message from datastore and fill form with it.
143     * @return A forward to the next view to render and display
144     */
145    protected ActionForward loadMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
146       throws Exception{
147 
148       if (form instanceof MessageForm){
149          // Get the requested message from manager.
150          MessageForm mForm = (MessageForm)form;
151          Message message = reportingManager.getMessage(mForm.getId());
152 
153          // Store message within session.
154          HttpSession session = request.getSession();
155          session.setAttribute(MESSAGE_KEY, message);
156          // Copy message into the form properties.
157          PropertyUtils.copyProperties(mForm, message);
158          // Add the release name as form property.
159          if (message.getRelease() != null)
160             mForm.setReleaseName(message.getRelease().getName());
161 
162          return mapping.findForward("Messages");
163       }
164       // This should not happen...
165       return null;
166    }
167 
168    /**
169     * Save a message into datastore. The Message may be an already existing one
170     * or a new one (this is indeed a create or update method).
171     * @return A forward to the next view to render and display
172     */
173    protected ActionForward saveMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
174       throws Exception{
175 
176       if (form instanceof MessageForm){
177          // Get the message form.
178          MessageForm mForm = (MessageForm)form;
179 
180          // Is there a message to update in session ?
181          HttpSession session = request.getSession();
182          Message message = (Message)session.getAttribute(MESSAGE_KEY);
183 
184          if (message == null){
185             log.info("Creation of a new Message with title: " + mForm.getTitle());
186             message = new Message(mForm.getTitle(), mForm.getContent(), mForm.getPublicationDate(), mForm.getExpiryDate());
187             message.setLink(mForm.getLink());
188          }
189          else{
190             log.info("Update of existing Message having title: " + message.getTitle());
191             PropertyUtils.copyProperties(message, mForm);
192          }
193          // Add release if specified.
194          if (mForm.getReleaseName() != null && mForm.getReleaseName().length() > 0)
195             message.setRelease(processManager.getRelease(mForm.getReleaseName()));
196          else
197             message.setRelease(null);
198 
199          // Save message using manager.
200          reportingManager.saveMessage(message);
201 
202          // Remove form and session attribute if present.
203          session.removeAttribute(MESSAGE_KEY);
204          removeObsoleteForm(mapping, request);
205 
206          // Load the messages list and forward.
207          return mapping.findForward("Messages");
208       }
209       // This should not happen...
210       return null;
211    }
212 
213    /**
214     * Retrieve a list of open messages (ie. having publication dates corresponding to today).
215     * @return A forward to the next view to render and display.
216     */
217    protected ActionForward getNewMessages(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
218       throws Exception{
219 
220       if (form instanceof MessageForm){
221          // Get the message form.
222          MessageForm mForm = (MessageForm)form;
223 
224          // Retrieve release if specified.
225          Release release = null;
226          if (mForm.getReleaseName() != null && mForm.getReleaseName().length() > 0)
227             release = processManager.getRelease(mForm.getReleaseName());
228 
229          // Retrieve list and put it into request attribute.
230          List messages = reportingManager.getOpenMessages(release);
231          request.setAttribute(MESSAGES_KEY, messages);
232 
233          // Forward after having cleaned the session.
234          request.getSession().removeAttribute(MESSAGE_KEY);
235          return mapping.findForward("Infos");
236       }
237       // This should not happen...
238       return null;
239    }
240 
241    /**
242     * Retrieve a list of Messages using the release, publication and expiry date criteria.
243     * @return A forward to the next view to render and display
244     */
245    protected ActionForward searchMessages(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
246       throws Exception{
247 
248       if (form instanceof MessageForm){
249          // Get the message form.
250          MessageForm mForm = (MessageForm)form;
251 
252          // Retrieve release if specified.
253          Release release = null;
254          if (mForm.getReleaseName() != null && mForm.getReleaseName().length() > 0)
255             release = processManager.getRelease(mForm.getReleaseName());
256 
257          // Retrieve list and put it into request attribute.
258          List messages = reportingManager.getMessages(mForm.getPublicationDate(), mForm.getExpiryDate(), release);
259          request.setAttribute(MESSAGES_KEY, messages);
260 
261          // Forward after having cleaned the session.
262          request.getSession().removeAttribute(MESSAGE_KEY);
263          return mapping.findForward("Messages");
264       }
265       // This should not happen.
266       return null;
267    }
268 }