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.LogicalEnvironmentForm;
19  import org.figure8.join.core.DuplicateEntityException;
20  import org.figure8.join.businessobjects.commons.Step;
21  import org.figure8.join.businessobjects.commons.Release;
22  import org.figure8.join.businessobjects.environment.LogicalEnvironment;
23  import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
24  import org.figure8.join.businessfacades.environment.EnvironmentManager;
25  import org.figure8.join.view.EnvironmentView;
26  import org.figure8.join.util.LogUtil;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.beanutils.PropertyUtils;
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import javax.servlet.http.HttpSession;
37  
38  import java.util.Map;
39  import java.util.List;
40  import java.util.TreeMap;
41  import java.util.ArrayList;
42  /**
43   * This is a Struts for managing all operations related to LogicalEnvironment entities.
44   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
45   * @version $Revision: 1.1 $
46   *
47   * @struts.action path="/logicalEnvironment" name="logicalEnvForm"
48   *                scope="request" parameter="op" validate="true"
49   *                input="/pages/mainpage.jsp?body=/jsp/environment/logicalenvironment.jsp"
50   * @struts.action-forward name="Environments" path="/jsp/environment/logicalenvironment.jsp"
51   * @struts.action-forward name="EnvironmentViews" path="/jsp/environment/environmentviews.jsp"
52   */
53  public class LogicalEnvironmentActions extends JoinAction{
54  
55     // Static -------------------------------------------------------------------
56  
57     /** A commons logger. */
58     private static final Log log = LogUtil.getLog(LogicalEnvironmentActions.class);
59  
60     /** Operation code for loading a specific LogicalEnvironment */
61     public static final String LOAD_OP = "load";
62     /** Operation code for saving a LogicalEnvironment (create or update) */
63     public static final String SAVE_OP = "save";
64     /** Operation code for accessing LogicalEnvironment view objects */
65     public static final String VIEW_OP = "view";
66  
67     /**
68      * The session scope attribute under which the LogicalEnvironment object
69      * currently selected by our logged-in User is stored.
70      */
71     public static final String ENVIRONMENT_KEY = "environment";
72     /**
73      * The request scope attribute under which the retrieved LogicalEnvironments
74      * list is stored.
75      */
76     public static final String ENVIRONMENTS_KEY = "environments";
77     /**
78      * The request scope attribute under which is stored the LogicalEnvironment that
79      * has raised a DuplicateEntityException during save of another one.
80      */
81     public static final String DUPLICATE_ENVIRONMENT_KEY = "duplicate";
82  
83     /** The map of already loaded environment views (this act as a local cache) */
84     private Map environmentViews = new TreeMap();
85  
86  
87     // Attributes ---------------------------------------------------------------
88  
89     /** The EnvironmentManager to use. */
90     private EnvironmentManager environmentManager = null;
91     /** The integration process manager to use */
92     private IntegrationProcessManager processManager = null;
93  
94  
95     // Constructors -------------------------------------------------------------
96  
97     /** Creates a new LogicalEnvironmentActions. */
98     public LogicalEnvironmentActions(){
99     }
100 
101 
102    // Public -------------------------------------------------------------------
103 
104    /** @param envManager The EnvironmentManager implementation instance to use */
105    public void setEnvironmentManager(EnvironmentManager envManager){
106       this.environmentManager = envManager;
107    }
108    /** @param manager The IntegrationProcessManager implementation instance to use */
109    public void setIntegrationProcessManager(IntegrationProcessManager manager){
110       this.processManager = manager;
111    }
112 
113    /**
114     * This is an helpful method for allt the web-tier components to retrieve loaded
115     * environment views. This method actually loads them if not already done.
116     * @return A Collection of available <code>org.figure8.join.view.EnvironmentView</code>s
117     */
118    public List getEnvironmentViews(){
119       // Build all environment views if cache is empty.
120       if (environmentViews.isEmpty()){
121          List environments = environmentManager.getLogicalEnvironments();
122          for (int i=0; i<environments.size(); i++){
123             LogicalEnvironment environment = (LogicalEnvironment)environments.get(i);
124             EnvironmentView view = environmentManager.getEnvironmentView(environment);
125             environmentViews.put(environment.getKey(), view);
126          }
127       }
128       return new ArrayList(environmentViews.values());
129    }
130 
131 
132    // Implementation of JoinAction ---------------------------------------------
133 
134    /**
135     *
136     * @param operation String representing the operation to invoke on Action
137     * @param mapping Mapping between forwards name and path for this action
138     * @param form The form object containing request parameters
139     * @param request The servlet container request wrapper
140     * @param response The servlet container response wrapper
141     * @return A forward to the next view to render and display
142     * @throws Exception such as InfraStructureExceptions ...
143     */
144    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
145       // Trace operation to execute.
146       log.debug("doExecute() called for '" + operation + "' operation");
147 
148       if (LOAD_OP.equals(operation)){
149          return loadEnvironment(mapping, form, request, response);
150       }
151       else if (SAVE_OP.equals(operation)){
152          return saveEnvironment(mapping, form, request, response);
153       }
154       else if (VIEW_OP.equals(operation)){
155          return loadEnvironmentViews(mapping, form, request, response);
156       }
157       else{
158          // Default : empty environment management page.
159          request.getSession().removeAttribute(ENVIRONMENT_KEY);
160          request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getLogicalEnvironments());
161          return mapping.findForward("Environments");
162       }
163    }
164 
165 
166    // Protected ----------------------------------------------------------------
167 
168    /**
169     * Load a specified logical environment from datastore and fill form with it.
170     * @return A forward to the next view to render and display
171     */
172    protected ActionForward loadEnvironment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
173       throws Exception{
174 
175       if (form instanceof LogicalEnvironmentForm){
176          // Get the request environment from manager.
177          LogicalEnvironmentForm envForm = (LogicalEnvironmentForm)form;
178          LogicalEnvironment environment = environmentManager.getLogicalEnvironment(envForm.getKey());
179 
180          // Store environment within session.
181          HttpSession session = request.getSession();
182          session.setAttribute(ENVIRONMENT_KEY, environment);
183          // Copy environment into form properties.
184          PropertyUtils.copyProperties(envForm, environment);
185          // Add step and release associations as form properties.
186          envForm.setStepId(environment.getStep().getId());
187          envForm.setReleaseName(environment.getRelease().getName());
188 
189          // Add environments list before forwarding.
190          request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getLogicalEnvironments());
191          return mapping.findForward("Environments");
192       }
193       // This should not happen...
194       return null;
195    }
196 
197    /**
198     * Save a logical environment into datastore. The environment may be an already existing
199     * one or a new one (this is indeed a create or update method).
200     * @return A forward to the next view to render and display
201     */
202    protected ActionForward saveEnvironment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
203            throws Exception{
204 
205       if (form instanceof LogicalEnvironmentForm){
206          LogicalEnvironmentForm envForm = (LogicalEnvironmentForm)form;
207 
208          // First, retrieve step and release bound to environment.
209          Step step = processManager.getStep(envForm.getStepId());
210          Release release = processManager.getRelease(envForm.getReleaseName());
211 
212          // Is there an environment to update in session ?
213          HttpSession session = request.getSession();
214          LogicalEnvironment environment = (LogicalEnvironment)session.getAttribute(ENVIRONMENT_KEY);
215 
216          // Create a new envioronment or populate existing with form.
217          if (environment == null){
218             log.info("Creation of a new LogicalEnvironment with key: " + envForm.getKey());
219             environment = new LogicalEnvironment(envForm.getKey(), envForm.getLabel(), envForm.getDescription(), envForm.getManagerId(), step, release);
220          }
221          else{
222             log.info("Update of existing LogicalEnvironemnt having key: " + envForm.getKey());
223             PropertyUtils.copyProperties(environment, envForm);
224             environment.setStep(step);
225             environment.setRelease(release);
226          }
227          // Save environment using manager.
228          try {environmentManager.saveLogicalEnvironment(environment);}
229          catch (DuplicateEntityException dee){
230             // Store exception within request.
231             request.setAttribute(DUPLICATE_ENVIRONMENT_KEY, dee.getOriginalEntity());
232             request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getLogicalEnvironments());
233             return mapping.findForward("Environments");
234          }
235          // Update environment in cache.
236          EnvironmentView view = environmentManager.getEnvironmentView(environment);
237          environmentViews.put(environment.getKey(), view);
238 
239          // Remove form and session attribute if present.
240          session.removeAttribute(ENVIRONMENT_KEY);
241          removeObsoleteForm(mapping, request);
242 
243          // Add environments list before forwarding.
244          request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getLogicalEnvironments());
245          return mapping.findForward("Environments");
246       }
247       // This should not happen...
248       return null;
249    }
250 
251    /**
252     * Load the logical environment views from datastore.
253     * @return A forward to the next view to render and display
254     */
255    protected ActionForward loadEnvironmentViews(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
256       throws Exception{
257 
258       if (form instanceof LogicalEnvironmentForm){
259          // Update the specified environment view if specified.
260          LogicalEnvironment environment = (LogicalEnvironment)request.getAttribute(ENVIRONMENT_KEY);
261          if (environment != null){
262             // Update environment in cache.
263             EnvironmentView view = environmentManager.getEnvironmentView(environment);
264             environmentViews.put(environment.getKey(), view);
265          }
266 
267          // Add views list before forwarding.
268          request.setAttribute(ENVIRONMENTS_KEY, getEnvironmentViews());
269          return mapping.findForward("EnvironmentViews");
270       }
271       // This should not happen...
272       return null;
273    }
274 }