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.StatusForm;
19  import org.figure8.join.core.DuplicateEntityException;
20  import org.figure8.join.businessobjects.commons.Status;
21  import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
22  import org.figure8.join.util.LogUtil;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.beanutils.PropertyUtils;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  
30  import javax.servlet.http.HttpSession;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  /**
34   * Struts action used for managing Integration process status within Join application
35   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
36   * @version $Revision: 1.1 $
37   *
38   * @struts.action path="/status" name="statusForm"
39   *                scope="request" parameter="op" validate="true"
40   *                input="/pages/mainpage.jsp?body=/jsp/status.jsp"
41   * @struts.action-forward name="Status" path="/jsp/status.jsp"
42   */
43  public class StatusActions extends JoinAction{
44  
45     // Static -------------------------------------------------------------------
46  
47     /** Get a commons logger. */
48     private static final Log log = LogUtil.getLog(StatusActions.class);
49  
50     /** Operation code for loading a specific Status */
51     public static final String LOAD_OP = "load";
52     /** Operation code for saving a Status (create or update) */
53     public static final String SAVE_OP = "save";
54  
55     /**
56      * The session scope attribute under which the Status object currently
57      * selected by our logged-in User is stored.
58      */
59     public static final String STATUS_KEY = "status";
60     /**
61      * The request scope attribute under which is stored the Status that
62      * has raised a DuplicateEntityException during save of another one.
63      */
64     public static final String DUPLICATE_STATUS_KEY = "duplicate";
65     
66     /** The request scope attribute under which is store the list of managed status. */
67     public static final String STATUS_LIST_KEY = "statusList";
68     
69  
70     // Attributes ---------------------------------------------------------------
71  
72     /** Join integration process manager. */
73     protected IntegrationProcessManager processManager;
74     
75     
76     // Constructors -------------------------------------------------------------
77  
78     /** Creates a new instance of StatusActions. */
79     public StatusActions(){
80     }
81   
82     
83     // Public -------------------------------------------------------------------
84  
85     /** @param manager <code>IntegrationProcessManager</code> implementation instance */
86     public void setIntegrationProcessManager(IntegrationProcessManager manager){
87        this.processManager = manager;
88     }
89  
90  
91     // Implementation of JoinAction ---------------------------------------------
92  
93     /**
94      *
95      * @param operation String representing the operation to invoke on Action
96      * @param mapping Mapping between forwards name and path for this action
97      * @param form The form object containing request parameters
98      * @param request The servlet container request wrapper
99      * @param response The servlet container response wrapper
100     * @return A forward to the next view to render and display
101     */
102    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
103       // Trace operation to execute.
104       log.debug("doExecute() called for '" + operation + "' operation");
105 
106       if (LOAD_OP.equals(operation)){
107          return loadStatus(mapping, form, request, response);
108       }
109       else if (SAVE_OP.equals(operation)){
110          return saveStatus(mapping, form, request, response);
111       }
112       else{
113          // Default : empty status management page.
114          request.getSession().removeAttribute(STATUS_KEY);
115          request.setAttribute(STATUS_LIST_KEY, processManager.getStatus());
116          return mapping.findForward("Status");
117       }
118    }
119    
120    
121    // Protected ----------------------------------------------------------------
122    
123    /**
124     * Load a specified status from datastore and fill form with it.
125     * @return A forward to the next view to render and display
126     */
127    protected ActionForward loadStatus(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
128       throws Exception{
129 
130       if (form instanceof StatusForm){
131          // Get the requested status from manager.
132          StatusForm sForm = (StatusForm)form;
133          Status status = processManager.getStatus(sForm.getId());
134 
135          // Store status within session.
136          HttpSession session = request.getSession();
137          session.setAttribute(STATUS_KEY, status);
138          // Copy form into the status properties.
139          PropertyUtils.copyProperties(sForm, status);
140 
141          // Forward afert having refreshed list.
142          request.setAttribute(STATUS_LIST_KEY, processManager.getStatus());
143          return mapping.findForward("Status");
144       }
145       // This should not happen...
146       return null;
147    }
148 
149    /**
150     * Save a status into datastore. The Status may be an already existing one or a
151     * new one (this is indeed a create or update method).
152     * @return A forward to the next view to render and display
153     */
154    protected ActionForward saveStatus(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
155       throws Exception{
156 
157       if (form instanceof StatusForm){
158          // Get the status form.
159          StatusForm sForm = (StatusForm)form;
160 
161          // Is there's a status to update in session ?
162          HttpSession session = request.getSession();
163          Status status = (Status)session.getAttribute(STATUS_KEY);
164 
165          // Create a new Status or populate existing with form.
166          if (status == null){
167             log.info("Creation of a new Status with key: " + sForm.getKey());
168             status = new Status(sForm.getKey(), sForm.getLabel(), sForm.getType());
169             if (sForm.isTerminal())
170                status.setTerminal(true);
171             if (sForm.isCancelled())
172                status.setCancelled(true);
173          }
174          else{
175             log.info("Update of existing Status having key: " + status.getKey());
176             PropertyUtils.copyProperties(status, sForm);
177          }
178          
179          // Save status using manager.
180          try {processManager.saveStatus(status);}
181          catch (DuplicateEntityException dee){
182             // Store exception within request.
183             request.setAttribute(DUPLICATE_STATUS_KEY, dee.getOriginalEntity());
184          }
185 
186          // Remove form and session attribute if present.
187          session.removeAttribute(STATUS_KEY);
188          removeObsoleteForm(mapping, request);
189 
190          // Forward after having refreshed list.
191          request.setAttribute(STATUS_LIST_KEY, processManager.getStatus());
192          return mapping.findForward("Status");
193       }
194       // This should not happen...
195       return null;
196    }
197 }