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.ParameterValueForm;
19  import org.figure8.join.businessobjects.commons.Target;
20  import org.figure8.join.businessobjects.environment.Parameter;
21  import org.figure8.join.businessobjects.environment.ParameterValue;
22  import org.figure8.join.businessobjects.environment.PhysicalEnvironment;
23  import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
24  import org.figure8.join.businessfacades.environment.EnvironmentManager;
25  import org.figure8.join.businessfacades.environment.ParameterManager;
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.HttpSession;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import java.util.Map;
39  import java.util.List;
40  import java.util.HashMap;
41  import java.util.Iterator;
42  import java.util.ArrayList;
43  import java.util.Collections;
44  /**
45   * Struts action used for managing the parameters values used by software
46   * project integration team.  
47   * @author <a href="mailto:jerome.evrard@gmail.com">Jerome Evrard</a>
48   * @version $Revision: 1.3 $
49   * 
50   * @struts.action path="/parameterValue" name="parameterValueForm"
51   *                scope="request" parameter="op" validate="true"
52   *                input="/action/parameterValue?op=null"
53   * @struts.action-forward name="Next" path="/jsp/environment/parametervalues.jsp"
54   * @struts.action-forward name="Rendered" path="/jsp/environment/renderedparametervalues.jsp"
55   * @struts.action-forward name="Historical" path="/jsp/environment/parametervalueshistory.jsp"
56   */
57  public class ParameterValueActions extends JoinAction{
58     
59     // Static -------------------------------------------------------------------
60  
61     /** Get a commons logger. */
62     protected static Log log = LogUtil.getLog(ParameterValueActions.class);
63  
64     /** Operation code for showing an empty value creation form */
65     public static final String FORM_OP = "form";
66     /** Operation code to load a value to re-edit it */
67     public static final String LOAD_OP = "load";
68     /** Operation code to create or update a specific value */
69     public static final String SAVE_OP = "save";
70     /** Operation code for deleting a specific parameter value */
71     public static final String DELETE_OP = "delete";
72     /** Operation to copy values from a target and an environment */
73     public static final String COPY_VALUES_OP = "copy";
74     /** Operation code to paste values into another environment and target */
75     public static final String PASTE_VALUES_OP = "paste";
76     /** Operation to show values of an environment and a target */
77     public static final String SHOW_VALUES_OP = "showValues";
78     /** Operation to render parameter values for all targets on an environment */
79     public static final String RENDER_VALUES_OP = "renderValues";
80     /** Operation to show parameter historical values of an environment and a target */
81     public static final String SHOW_HISTORICAL_VALUE_OP = "historicalValues";
82  
83     /** Request attribute containing value to edit */
84     public static final String VALUE_KEY = "value";
85     /** Request attribute containing displayed values */
86     public static final String VALUES_KEY = "values";
87     /** Request attribute containing copied values */
88     public static final String COPIED_VALUES_KEY = "copiedValues";
89     /** Request attribute containing rendered values */
90     public static final String RENDERED_VALUES_KEY = "renderedValues";
91     /** Request attribute telling if rendered values should be shown */
92     public static final String SHOW_RENDER_KEY = "showRender";
93  
94  
95     // Attributes ---------------------------------------------------------------
96  
97     /** <code>parameterManager</code>: Parameters manager */
98     private ParameterManager parameterManager;
99     /** <code>integrationProcessManager</code>: Integration process manager */
100    private IntegrationProcessManager integrationProcessManager;
101    /** <code>environmentManager</code>: Environment manager */
102    private EnvironmentManager environmentManager;
103 
104 
105    // Constructors -------------------------------------------------------------
106 
107    /** Creates a new instance of ParameterValueActions */
108    public ParameterValueActions(){
109    }
110 
111 
112    // Public -------------------------------------------------------------------
113 
114    /** @param manager The manager to use */
115    public void setParameterManager(ParameterManager manager){
116       this.parameterManager = manager;
117    }
118    /** @param manager The manager to use */
119    public void setEnvironmentManager(EnvironmentManager manager){
120       this.environmentManager = manager;
121    }
122    /** @param manager The manager to use */
123    public void setIntegrationProcessManager(IntegrationProcessManager manager){
124       this.integrationProcessManager = manager;
125    }
126 
127 
128    // Override of JoinAction ---------------------------------------------------
129 
130    /**
131     * Action to manage parameters values.
132     * @param operation String representing the operation to invoke on Action
133     * @param mapping Mapping between forwards name and path for this action
134     * @param form The form object containing request parameters
135     * @param request The servlet container request wrapper
136     * @param response The servlet container response wrapper
137     * @return A forward to the next view to render and display
138     */
139    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
140            throws Exception{
141       // Trace operation to execute.
142       log.debug("doExecute() called for '" + operation + "' operation");
143 
144       if (form instanceof ParameterValueForm){
145          // Prepare default forward and form.
146          String forward= "Next";
147          ParameterValueForm pForm = (ParameterValueForm)form;
148 
149          // Common : we always need environments and targets.
150          request.setAttribute(TargetActions.TARGETS_KEY, integrationProcessManager.getTargets());
151          request.setAttribute(PhysicalEnvironmentActions.ENVIRONMENTS_KEY, environmentManager.getPhysicalEnvironments());
152 
153          if (FORM_OP.equals(operation)){
154             // Load parameters
155             request.setAttribute(ParameterActions.PARAMETERS_KEY, loadParameters());
156          }
157          else if (LOAD_OP.equals(operation)){
158             ParameterValue value = parameterManager.getParameterValue(pForm.getId());
159 
160             // Store value within session.
161             HttpSession session = request.getSession();
162             session.setAttribute(VALUE_KEY, value);
163 
164             // Copy value into the form properties.
165             PropertyUtils.copyProperties(pForm, value);
166             pForm.setParameterName(value.getParameter().getName());
167             pForm.setEditedTarget(value.getTarget().getName());
168             pForm.setEditedEnvironment(value.getEnvironment().getKey());
169 
170             // Also load parameters.
171             request.setAttribute(ParameterActions.PARAMETERS_KEY, loadParameters());
172          }
173          else if (SAVE_OP.equals(operation)){
174             // Check if there's a value in session to update.
175             HttpSession session = request.getSession();
176             ParameterValue value = (ParameterValue)session.getAttribute(VALUE_KEY);
177 
178             if (value == null){
179                // Create value for from properties.
180                log.info("Creating a new parameter value for parameter: " + pForm.getParameterName());
181                createParameterValue(pForm.getParameterName(), pForm.getValue(), pForm.getEditedEnvironment(), pForm.getEditedTarget());
182             }
183             else{
184                // Update value with form new value.
185                log.info("Updating parameter value for parameter: " + pForm.getParameterName());
186                value.setValue(pForm.getValue());
187                parameterManager.saveParameterValue(value);
188             }
189 
190             // Remove form and session attribute if present.
191             request.getSession().removeAttribute(VALUE_KEY);
192             removeObsoleteForm(mapping, request);
193 
194             // Prepare the view of parameters values of current environment and target
195             operation = SHOW_VALUES_OP;
196             pForm.setViewedTarget(pForm.getEditedTarget());
197             pForm.setViewedEnvironment(pForm.getEditedEnvironment());
198          }
199          else if (DELETE_OP.equals(operation)){
200             // Retrieve specified value.
201             ParameterValue value = parameterManager.getParameterValue(pForm.getId());
202             parameterManager.removeParameterValue(value);
203 
204             // Prepare the view of parameters values of current environment and target
205             operation = SHOW_VALUES_OP;
206             pForm.setViewedTarget(value.getTarget().getName());
207             pForm.setViewedEnvironment(value.getEnvironment().getKey());
208          }
209          else if (COPY_VALUES_OP.equals(operation)){
210             // Parse copied values according to their status.
211             Map copiedValues = pForm.getCopiedValuesMap();
212             List values = retrieveParameterValues(copiedValues);
213             // Store list into request attributes.
214             request.setAttribute(COPIED_VALUES_KEY, values);
215          }
216          else if (PASTE_VALUES_OP.equals(operation)){
217             // Paste the selected values
218             String targetName = pForm.getEditedTarget();
219             String environmentKey = pForm.getEditedEnvironment();
220             List lstValueIds = extractValueIds(request);
221             pasteValuesTo(environmentKey, targetName, lstValueIds);
222 
223             // Show the pasted values
224             operation = SHOW_VALUES_OP;
225             pForm.setViewedTarget(targetName);
226             pForm.setViewedEnvironment(environmentKey);
227          }
228 
229          if (SHOW_VALUES_OP.equals(operation)){
230             String targetName = pForm.getViewedTarget();
231             String environmentKey = pForm.getViewedEnvironment();
232             // Load values.
233             request.setAttribute(VALUES_KEY, loadParameterValues(environmentKey, targetName));
234             request.setAttribute(SHOW_RENDER_KEY, String.valueOf(pForm.isRender()));
235             if (pForm.isRender()){
236                log.info("Rendering is requested !");
237                Map renders = loadParameterValueRender(environmentKey, targetName);
238                request.setAttribute(RENDERED_VALUES_KEY , renders);
239             }
240          }
241          else if (RENDER_VALUES_OP.equals(operation)){
242             String environmentKey = pForm.getViewedEnvironment();
243             // Render values for all targets and store result into a map.
244             Map renderedValues = new HashMap();
245             List targets = (List)request.getAttribute(TargetActions.TARGETS_KEY);
246             for (int i=0; i<targets.size(); i++){
247                Target target = (Target)targets.get(i);
248                Map renders = loadParameterValueRender(environmentKey, target.getName());
249                renderedValues.put(target.getName(), renders);
250             }
251             // Store results and what we need into request.
252             request.setAttribute(RENDERED_VALUES_KEY , renderedValues);
253             request.setAttribute(ParameterActions.PARAMETERS_KEY, loadParameters());
254             request.setAttribute(PhysicalEnvironmentActions.ENVIRONMENT_KEY,
255                                     environmentManager.getPhysicalEnvironment(environmentKey));
256             // Forward to view displaying all targets.
257             return mapping.findForward("Rendered");
258          }
259          else if (SHOW_HISTORICAL_VALUE_OP.equals(operation)){
260             long id = pForm.getId();
261             /*
262             ParameterValue value = parameterManager.getParameterValueOfId(id);
263             String parameter = value.getParameter().getName();
264             String env = value.getEnvironment().getKey();
265             request.setAttribute(HISTORICAL_VALUES_KEY, loadHistoricalValues(env, parameter));
266             request.setAttribute(PARAMETER_KEY, parameter);
267             request.setAttribute(ENVIRONMENT_KEY, value.getEnvironment().getName());
268             */
269             forward = "Historical";
270          }
271 
272 
273          return mapping.findForward(forward);
274       }
275       // This should not happen...
276       return null;
277    }
278 
279 
280    // Protected ----------------------------------------------------------------
281 
282    /**
283     * Create (or update if it exists) a value of parameter.
284     * @param parameterName The parameter name.
285     * @param value The parameter value to put.
286     * @param environmentKey The environment key.
287     * @param targetName The target key.
288     */
289    private void createParameterValue(String parameterName, String value, String environmentKey, String targetName){
290       Parameter parameter = parameterManager.getParameter(parameterName);
291       createParameterValue(parameter, value, environmentKey, targetName);
292    }
293 
294    /**
295     * Create (or update if it exists) a value of parameter.
296     * @param parameter The parameter to edit value.
297     * @param value The parameter value to put.
298     * @param environmentKey The environment key.
299     * @param targetName The target key.
300     */
301    private void createParameterValue(Parameter parameter, String value, String environmentKey, String targetName){
302       Target target = integrationProcessManager.getTarget(targetName);
303       PhysicalEnvironment environment = environmentManager.getPhysicalEnvironment(environmentKey) ;
304       ParameterValue parameterValue = new ParameterValue(value, parameter, target, environment);
305       // Save the parameter value
306       parameterManager.saveParameterValue(parameterValue);
307    }
308 
309    /**
310     * Load all values of an environment and a target.
311     * @param environment Environment name.
312     * @param target Target name.
313     * @return The parameters values list.
314     */
315    private List loadParameterValues(String environment, String target){
316       if (environment == null || target == null || environment.trim().length() == 0 || target.trim().length() == 0)
317          return Collections.EMPTY_LIST;
318       return parameterManager.getParameterValues(environment, target);
319    }
320 
321    /**
322     * Load all parameters.
323     * @return The parameters list.
324     */
325    private List loadParameters(){
326       List parameters = parameterManager.getAllParameters();
327       if (parameters == null)
328          parameters = Collections.EMPTY_LIST;
329       return parameters;
330    }
331 
332    /**
333     * Load historical values of a parameter.
334     * @param environmentKey The parameter environment key.
335     * @param parameterName The parameter name.
336     * @return The values list.
337     */
338    private List loadHistoricalValues(String environmentKey, String parameterName){
339 
340       List values = null;// = parameterManager.getHistoricalParameterValues(parameterName, environmentKey);
341       if (values==null)
342          values=Collections.EMPTY_LIST;
343       return values;
344    }
345 
346    /**
347     * Load the render of parameter values.
348     * @param environment The environment key.
349     * @param target The target name.
350     * @return The map with values render.
351     */
352    private Map loadParameterValueRender(String environment, String target){
353       List renders = parameterManager.renderParameterValues(environment, target);
354       // Add values into a map.
355       Map rendersMap = new HashMap();
356       Iterator itRenders = renders.iterator();
357       while (itRenders.hasNext()){
358          ParameterValue value = (ParameterValue)itRenders.next();
359          rendersMap.put(value.getParameter().getName(), value.getValue());
360       }
361       return rendersMap;
362    }
363 
364    /**
365     * Retrieve the parameter values marked "on" into the map.
366     * @param copiedValues The map of the parameter values ids and their "on" or "off" status
367     * @return The parameters values list.
368     */
369    private List retrieveParameterValues(Map copiedValues){
370       // Prepare result.
371       List result = new ArrayList();
372       // Browse values and check if they are "on"
373       Iterator values = copiedValues.keySet().iterator();
374       while (values.hasNext()){
375          String key = (String)values.next();
376          String status = (String)copiedValues.get(key);
377          // Get and store if "on".
378          if ("on".equalsIgnoreCase(status)){
379             ParameterValue value = parameterManager.getParameterValue(Long.parseLong(key));
380             result.add(value);
381          }
382       }
383       return result;
384    }
385 
386    /**
387     * Extract into a list the current parameter values to paste.
388     * @param request The HTTP request that contains parameter values id.
389     * @return The list of parameter values id.
390     */
391    private List extractValueIds(HttpServletRequest request){
392       // Prepare result.
393       List result = new ArrayList();
394       int i = 0;
395       // Browse requets parameters to get pasted values.
396       while (request.getParameter("value" + i) != null){
397          String id = (String)request.getParameter("value" + i);
398          result.add(id);
399          i++;
400       }
401       return result;
402    }
403 
404    /**
405     * Paste the parameter value list into envKey and target name.
406     * @param environmentKey The destination environment of the paste
407     * @param targetName The destination target of the paste
408     * @param valueIds The parameter values id to paste.
409     */
410    private void pasteValuesTo(String environmentKey, String targetName, List valueIds){
411       Iterator ids = valueIds.iterator();
412       // Paste each parameter value on environment for target.
413       while (ids.hasNext()){
414          long id = Long.parseLong(ids.next().toString());
415          // Retrieve existing value.
416          ParameterValue value = parameterManager.getParameterValue(id);
417          createParameterValue(value.getParameter(), value.getValue(), environmentKey, targetName);
418       }
419    }
420 }