View Javadoc

1   /**
2    * Copyright 2005-2008 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.MailingListForm;
19  import org.figure8.join.core.DuplicateEntityException;
20  import org.figure8.join.businessobjects.reporting.Event;
21  import org.figure8.join.businessobjects.reporting.MailingList;
22  import org.figure8.join.businessobjects.reporting.Subscription;
23  import org.figure8.join.businessfacades.reporting.ReportingManager;
24  import org.figure8.join.util.LogUtil;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.beanutils.PropertyUtils;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  
32  import javax.servlet.http.HttpSession;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import java.util.List;
37  import java.util.HashMap;
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  /**
41   * This is a Struts action for managing Mailing lists and their subscriptions.
42   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
43   * @version $Revision: 1.3 $
44   *
45   * @struts.action path="/mailingList" name="mailingListForm"
46   *                scope="request" parameter="op" validate="true"
47   *                input="/pages/mainpage.jsp?body=/jsp/mailinglists.jsp"
48   * @struts.action-forward name="MailingLists" path="/jsp/mailinglists.jsp"
49   * @struts.action-forward name="Subscriptions" path="/jsp/subscriptions.jsp"
50   */
51  public class MailingListActions extends JoinAction{
52  
53     // Static -------------------------------------------------------------------
54  
55     /** Get a commons logger */
56     private static final Log log = LogUtil.getLog(MailingListActions.class);
57  
58     /** Operation code for loading a specified MailingList */
59     public static final String LOAD_OP = "load";
60     /** Operation code for saving (create or update) a MailingList */
61     public static final String SAVE_OP = "save";
62     /** Operation code for removing a MailingList (subscriptions are removed as well) */
63     public static final String REMOVE_OP = "remove";
64     /** Operation code for user subscribing to a MailingList */
65     public static final String SUBSCRIBE_OP = "subscribe";
66     /** Operation code for getting a user subscriptions to MailingLists */
67     public static final String SUBSCRIPTIONS_OP = "subscriptions";
68  
69     /**
70      * The session attribute key used for storing a specified MailingList
71      * for later update.
72      */
73     public static final String MAILING_LIST_KEY = "mailingList";
74     /**
75      * The request scope attribute under which the retrieved mailing list
76      * list is stored.
77      */
78     public static final String MAILING_LISTS_KEY = "mailingLists";
79     /**
80      * The request scope attribute under which the retrieved subscriptions
81      * list is stored.
82      */
83     public static final String SUBSCRIPTIONS_KEY = "subscriptions";
84     /**
85      * The request scope attribute under which is stored the MailingList that
86      * has raised a DuplicateEntityException during save of another one.
87      */
88     public static final String DUPLICATE_MAILING_LIST_KEY = "duplicate";
89  
90  
91  
92     // Attributes ---------------------------------------------------------------
93  
94     /** The Reporting manager implementation to use */
95     protected ReportingManager reportingManager = null;
96  
97  
98     // Constructors -------------------------------------------------------------
99  
100    /** Creates a new instance of MailingListActions */
101    public MailingListActions(){
102    }
103 
104 
105    // Public -------------------------------------------------------------------
106 
107    /** @param manager The ReportingManager implementation to use */
108    public void setReportingManager(ReportingManager manager){
109       this.reportingManager = manager;
110    }
111 
112 
113    // Implementation of JoinAction ---------------------------------------------
114 
115    /**
116     * @param operation String representing the operation to invoke on Action
117     * @param mapping Mapping between forwards name and path for this action
118     * @param form The form object containing request parameters
119     * @param request The servlet container request wrapper
120     * @param response The servlet container response wrapper
121     * @return A forward to the next view to render and display
122     * @throws Exception such as InfraStructureExceptions ...
123     */
124    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form,
125                                   HttpServletRequest request, HttpServletResponse response)
126            throws Exception{
127 
128       // Trace operation to execute.
129       log.debug("doExecute() called for '" + operation + "' operation");
130 
131       if (form instanceof MailingListForm){
132          // Get the mailing list form and existing lists.
133          MailingListForm mForm = (MailingListForm)form;
134 
135          // Route to protected method depending on operation.
136          if (LOAD_OP.equals(operation)){
137             return loadMailingList(mapping, mForm, request, response);
138          }
139          else if (SAVE_OP.equals(operation)){
140             return saveMailingList(mapping, mForm, request, response);
141          }
142          else if (SUBSCRIBE_OP.equals(operation)){
143             return subscribeMailingLists(mapping, mForm, request, response);
144          }
145          else if (SUBSCRIPTIONS_OP.equals(operation)){
146             return getSubscriptions(mapping, mForm, request, response);
147          }
148          else{
149             // Add events if form is requested.
150             if ("true".equals(request.getParameter("showForm")))
151                request.setAttribute("events", reportingManager.getEvents());
152             // Default : empty mailing lists management page.
153             request.getSession().removeAttribute(MAILING_LIST_KEY);
154             request.setAttribute(MAILING_LISTS_KEY, reportingManager.getMailingLists());
155             return mapping.findForward("MailingLists");
156          }
157       }
158       // This should not happen...
159       return null;
160    }
161 
162 
163    // Protected ----------------------------------------------------------------
164 
165    /**
166     * Load a specified mailing list into request attribute for update.
167     * @return A forward to the next view to render and display.
168     */
169    protected ActionForward loadMailingList(ActionMapping mapping, MailingListForm form,
170                                            HttpServletRequest request, HttpServletResponse response)
171            throws Exception{
172       // Retrieve what we need in form.
173       request.setAttribute("events", reportingManager.getEvents());
174       request.setAttribute(MAILING_LISTS_KEY, reportingManager.getMailingLists());
175 
176       // Get the requested mailing list.
177       log.debug("Retrieving mailing list '" + form.getName() + "' from reporting manager");
178       MailingList list = reportingManager.getMailingList(form.getName());
179       // Copy list into the form properties.
180       PropertyUtils.copyProperties(form, list);
181       form.setEventKey(list.getEvent().getKey());
182       form.setResourceId(list.getResourceId());
183 
184       // Store into session attribute before forwarding.
185       HttpSession session = request.getSession();
186       session.setAttribute(MAILING_LIST_KEY, list);
187       return mapping.findForward("MailingLists");
188    }
189 
190    /**
191     * Save a mailing list within datastore (this is a create or update method).
192     * @return A forward to the next view to render and display.
193     */
194    protected ActionForward saveMailingList(ActionMapping mapping, MailingListForm form,
195                                            HttpServletRequest request, HttpServletResponse response)
196            throws Exception{
197       // Is there a mailing list to update in session ?
198       HttpSession session = request.getSession();
199       MailingList list = (MailingList)session.getAttribute(MAILING_LIST_KEY);
200 
201       // Create a new mailing list or populate existing with form.
202       if (list == null){
203          log.info("Creation of a new MailingList with name: " + form.getName());
204          Event event = reportingManager.getEvent(form.getEventKey());
205          list = new MailingList(form.getName(), form.getMsgTitleTemplate(), form.getMsgContentTemplate(), event);
206          // Specialize event for a resource if specified.
207          if (form.getResourceId() != null) list.setResourceId(form.getResourceId());
208       }
209       else{
210          log.info("Update of existing MailingList having name: " + form.getName());
211          PropertyUtils.copyProperties(list, form);
212       }
213       // Save the mailing list before forwarding.
214       try {reportingManager.saveMailingList(list);}
215       catch (DuplicateEntityException dee){
216          // Store exception within request.
217          request.setAttribute(DUPLICATE_MAILING_LIST_KEY, dee.getOriginalEntity());
218       }
219       // Retrieve existing mailing lists before forwarding.
220       request.setAttribute(MAILING_LISTS_KEY, reportingManager.getMailingLists());
221       return mapping.findForward("MailingLists");
222    }
223 
224    /**
225     * Subscribe a user to a set of MailingLists.
226     * @return A forward to the next view to render and display.
227     */
228    protected ActionForward subscribeMailingLists(ActionMapping mapping, MailingListForm form,
229                                                  HttpServletRequest request, HttpServletResponse response)
230            throws Exception{
231       // Prepare a list of selected mailing lists.
232       List lists = new ArrayList();
233 
234       // Browse available lists to find selected ones.
235       Iterator subscribedLists = form.getSubscribedLists().keySet().iterator();
236       while (subscribedLists.hasNext()){
237          // Get corresponding mailing list domain object.
238          String listName = (String)subscribedLists.next();
239          MailingList list = reportingManager.getMailingList(listName);
240 
241          // Add this list to the subscribed lists if selected.
242          if (list != null && "on".equals(form.getSubscribedList(listName)))
243             lists.add(list);
244       }
245       // Subscribe user before forwarding.
246       reportingManager.susbcribeUser(form.getUserId(), lists);
247       return new ActionForward("/action/user?op=load&login=" + form.getUserId());
248    }
249 
250    /**
251     * Retrieve subscription for a given list or for a given user.
252     * @return A forward to the next view to render and display.
253     */
254    protected ActionForward getSubscriptions(ActionMapping mapping, MailingListForm form,
255                                             HttpServletRequest request, HttpServletResponse response)
256            throws Exception{
257 
258       if (form.getName() != null){
259          // Get the subscriptions of a given mailing list.
260          MailingList list = reportingManager.getMailingList(form.getName());
261          request.setAttribute(MAILING_LIST_KEY, list);
262          request.setAttribute(SUBSCRIPTIONS_KEY, list.getSubscriptions());
263       }
264       else if (form.getUserId() != null){
265          log.info("Retrieving subscriptions for user: " + form.getUserId());
266          // Get the subscriptions of a given user and fill form.
267          List subscriptions = reportingManager.getSubscriptionsForUser(form.getUserId());
268          for (int i=0; i<subscriptions.size(); i++){
269             Subscription subscription = (Subscription)subscriptions.get(i);
270             form.setSubscribedList(subscription.getMailingList().getName(), "on");
271          }
272          // Get the available mailing lists and organize them using Event.
273          HashMap eventMap = new HashMap();
274          List lists = reportingManager.getMailingLists();
275          for (int i=0; i<lists.size(); i++){
276             MailingList list = (MailingList)lists.get(i);
277             ArrayList eventLists = (ArrayList)eventMap.get(list.getEvent());
278             // Add list within this event mailing lists.
279             if (eventLists == null)
280                eventLists = new ArrayList();
281             eventLists.add(list);
282             eventMap.put(list.getEvent(), eventLists);
283          }
284          request.setAttribute(MAILING_LISTS_KEY, eventMap);
285       }
286       // Forward to subscriptions screen.
287       return mapping.findForward("Subscriptions");
288    }
289 }
290