View Javadoc

1   /**
2    * Copyright 2005-2007 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.EISMappingForm;
19  import org.figure8.join.businessobjects.environment.EIS;
20  import org.figure8.join.businessobjects.environment.Gateway;
21  import org.figure8.join.businessobjects.environment.EISMapping;
22  import org.figure8.join.businessfacades.environment.ResourceManager;
23  import org.figure8.join.util.LogUtil;
24  
25  import org.apache.commons.logging.Log;
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.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  /**
33   * This is a Struts action for managing all operations related to mappings between EIS and Gateways.
34   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
35   * @version $Revision: 1.1 $
36   *
37   * @struts.action path="/eisMapping" name="eisMappingForm"
38   *                scope="request" parameter="op" validate="true"
39   *                input="/pages/mainpage.jsp?body=/jsp/environment/eismappings.jsp"
40   * @struts.action-forward name="Gateway" path="/action/gateway?category=Gateway&amp;op=details"
41   * @struts.action-forward name="EISMappings" path="/jsp/environment/eismappings.jsp"
42   */
43  public class EISMappingActions extends JoinAction{
44  
45     // Static -------------------------------------------------------------------
46  
47     /** Get a commons logger. */
48     private static final Log log = LogUtil.getLog(EISMappingActions.class);
49  
50     /** Operation code for loading a specific EISMapping */
51     public static final String LOAD_OP = "load";
52     /** Operation code for saving a EISMapping (create) */
53     public static final String CREATE_OP = "create";
54     /** Operation code for closing a EISMapping (close) */
55     public static final String CLOSE_OP = "close";
56     /** Operation code for retrieving EISMappings for a gateway */
57     public static final String VIEW_OP = "view";
58  
59     /**
60      * The session scope attribute under which the EISMapping object
61      * currently selected by our logged-in User is stored.
62      */
63     public static final String MAPPING_KEY = "mapping";
64     /**
65      * The request scope attribute under which the retrieved EISMappings
66      * list is stored.
67      */
68     public static final String MAPPINGS_KEY = "mappings";
69  
70  
71     // Attributes ---------------------------------------------------------------
72  
73     /** The ResourceManager to use */
74     private ResourceManager resourceManager = null;
75  
76  
77     // Constructors -------------------------------------------------------------
78  
79     /** Creates a new instance of EISMappingActions. */
80     public EISMappingActions(){
81     }
82  
83  
84     // Public -------------------------------------------------------------------
85  
86     /** @param manager The ResourceManager implementation to use */
87     public void setResourceManager(ResourceManager manager){
88        this.resourceManager = manager;
89     }
90  
91  
92     // Implementation of JoinAction ---------------------------------------------
93  
94     /**
95      * Abstract method that subclasses must implement. The real execution must
96      * be done here according to the specified <b>operation</b>. ActionForward
97      * returned by execution should only point on page fragment : this superclass
98      * is responsible of including them within a whole page.
99      * @param operation String representing the operation to invoke on Action
100     * @param mapping Mapping between forwards name and path for this action
101     * @param form The form object containing request parameters
102     * @param request The servlet container request wrapper
103     * @param response The servlet container response wrapper
104     * @return A forward to the next view to render and display
105     * @throws Exception such as InfraStructureExceptions ...
106     */
107    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form,
108                                   HttpServletRequest request, HttpServletResponse response) throws Exception{
109       // Trace operation to execute.
110       log.debug("doExecute() called for '" + operation + "' operation");
111 
112       if (LOAD_OP.equals(operation)){
113          return loadMapping(mapping, form, request, response);
114       }
115       else if (CREATE_OP.equals(operation)){
116          return createMapping(mapping, form, request, response);
117       }
118       else if (CLOSE_OP.equals(operation)){
119          return closeMapping(mapping, form, request, response);
120       }
121       // This should not happen...
122       return null;
123    }
124 
125 
126    // Protected ----------------------------------------------------------------
127 
128    /**
129     * Load a specified EIS mapping from datastore and fill form with it.
130     * @return A forward to the next view to render and display
131     */
132    protected ActionForward loadMapping(ActionMapping mapping, ActionForm form,
133                                        HttpServletRequest request, HttpServletResponse response) throws Exception{
134       // This should not happen...
135       return null;
136    }
137 
138    /**
139     * Create an EIS mapping into datastore and bind specified EIS and gateway to it.
140     * @return A forward to the next view to render and display
141     */
142    protected ActionForward createMapping(ActionMapping mapping, ActionForm form,
143                                          HttpServletRequest request, HttpServletResponse response) throws Exception{
144       if (form instanceof EISMappingForm){
145          // Get the requested EIS and Gateway from manager.
146          EISMappingForm eForm = (EISMappingForm)form;
147          EIS eis = (EIS)resourceManager.getResource(eForm.getEISId());
148          Gateway gateway = (Gateway)resourceManager.getResource(eForm.getGatewayName());
149 
150          // Map EIS and gateway by creating a mapping.
151          log.info("Creating a mapping for " + eis.getName() + " EIS onto "
152                  + gateway.getName() + " gateway");
153          resourceManager.mapEISOnGateway(eis, gateway);
154 
155          // Store gateway into request, asking to refresh its view, before forwarding...
156          request.setAttribute(GatewayActions.GATEWAY_KEY, gateway);
157          log.info("Forwarding to " + mapping.findForward("Gateway").getPath());
158          return mapping.findForward("Gateway");
159       }
160       // This should not happen...
161       return null;
162    }
163 
164    /**
165     * Close an EIS mapping and update the gateway.
166     * @return A forward to the next view to render and display
167     */
168    protected ActionForward closeMapping(ActionMapping mapping, ActionForm form,
169                                         HttpServletRequest request, HttpServletResponse response) throws Exception{
170       if (form instanceof EISMappingForm){
171          // Get the request EISMapping from manager.
172          EISMappingForm eForm = (EISMappingForm)form;
173          EISMapping eisMapping = resourceManager.getEISMapping(eForm.getId());
174 
175          // Close eis mapping using manager.
176          log.info("Closing the EIS mapping having identifier '" + eisMapping.getId() + "'");
177          resourceManager.closeEISMapping(eisMapping);
178 
179          // Store gateway into request, asking to refresh its view, before forwarding...
180          request.setAttribute(GatewayActions.GATEWAY_KEY, eisMapping.getGateway());
181          return mapping.findForward("Gateway");
182       }
183       // This should not happen...
184       return null;
185    }
186 }