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.AssemblyForm;
19  import org.figure8.join.core.DuplicateEntityException;
20  import org.figure8.join.businessobjects.commons.Status;
21  import org.figure8.join.businessobjects.commons.Release;
22  import org.figure8.join.businessobjects.artifact.Assembly;
23  import org.figure8.join.businessobjects.artifact.Deliverable;
24  import org.figure8.join.businessobjects.artifact.DeliverableType;
25  import org.figure8.join.businessobjects.environment.Deployment;
26  import org.figure8.join.businessfacades.artifact.ArtifactManager;
27  import org.figure8.join.businessfacades.artifact.AssemblyManager;
28  import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
29  import org.figure8.join.util.LogUtil;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.beanutils.PropertyUtils;
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  import java.util.List;
41  import java.util.HashMap;
42  import java.util.Iterator;
43  import java.util.ArrayList;
44  /**
45   * This is a Struts action for managing and viewing assemblies in Join system.
46   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
47   * @version $Revision: 1.2 $
48   *
49   * @struts.action path="/assembly" name="assemblyForm"
50   *                scope="request" parameter="op" validate="true"
51   *                input="/pages/mainpage.jsp?body=/jsp/artifact/assemblies.jsp"
52   * @struts.action-forward name="Form" path="/jsp/artifact/assemblyform.jsp"
53   * @struts.action-forward name="Status" path="/jsp/forcestatus.jsp"
54   * @struts.action-forward name="Assembly" path="/jsp/artifact/assembly.jsp"
55   * @struts.action-forward name="Assemblies" path="/jsp/artifact/assemblies.jsp"
56   */
57  public class AssemblyActions extends JoinAction{
58  
59     // Static -------------------------------------------------------------------
60  
61     /** Get a commons logger. */
62     private static final Log log = LogUtil.getLog(AssemblyActions.class);
63  
64     /** Operation code fo showing the assembly creation form */
65     public static final String FORM_OP = "form";
66     /** Operation code for creating a new Assembly */
67     public static final String CREATE_OP = "create";
68     /** Operation code for searching assemblies using release */
69     public static final String SEARCH_OP = "search";
70     /** Operation code for getting details on a specific Assembly */
71     public static final String DETAILS_OP = "details";
72     /** Operation code for forcing status of a specified Assembly */
73     public static final String STATUS_OP = "forceStatus";
74  
75     /**
76      * The session scope attribute under which the Assembly object currently
77      * selected by our logged-in User is stored.
78      */
79     public static final String ASSEMBLY_KEY = "assembly";
80     /**
81      * The request scope attribute under which the retrivied assemblies
82      * list is stored.
83      */
84     public static final String ASSEMBLIES_KEY = "assemblies";
85     /**
86      * The request scope attribute under which the available deliverables
87      * mapping is stored.
88      */
89     public static final String DELIVERABLES_KEY = "deliverablesMap";
90  
91     /**
92      * The request scope attribute under which is stored the Assembly that
93      * has raised a DuplicateEntityException during save of another one.
94      */
95     public static final String DUPLICATE_ASSEMBLY_KEY = "duplicate";
96  
97  
98     // Attributes ---------------------------------------------------------------
99  
100    /** The artifact manager implementation to use. */
101    protected ArtifactManager artifactManager = null;
102    /** The assembly manager implementation to use. */
103    protected AssemblyManager assemblyManager = null;
104    /** The integration process manager implementation to use. */
105    protected IntegrationProcessManager processManager = null;
106 
107 
108    // Constructors -------------------------------------------------------------
109 
110    /** Creates a new instance of AssemblyActions. */
111    public AssemblyActions(){
112    }
113 
114 
115    // Public -------------------------------------------------------------------
116 
117    /** @param manager The ArtifactManager implementation to use */
118    public void setArtifactManager(ArtifactManager manager){
119       this.artifactManager = manager;
120    }
121    /** @param manager The AssemblyManager implementation to use */
122    public void setAssemblyManager(AssemblyManager manager){
123       this.assemblyManager = manager;
124    }
125    /** @param manager The integration process manager implementation to use */
126    public void setIntegrationProcessManager(IntegrationProcessManager manager){
127       this.processManager = manager;
128    }
129 
130 
131    // Implementation of JoinAction ---------------------------------------------
132 
133    /**
134     *
135     * @param operation String representing the operation to invoke on Action
136     * @param mapping Mapping between forwards name and path for this action
137     * @param form The form object containing request parameters
138     * @param request The servlet container request wrapper
139     * @param response The servlet container response wrapper
140     * @return A forward to the next view to render and display
141     * @throws Exception such as InfraStructureExceptions ...
142     */
143    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
144            throws Exception{
145       // Trace operation to execute.
146       log.debug("doExecute() called for '" + operation + "' operation");
147 
148       if (FORM_OP.equals(operation)){
149          return showAssemblyForm(mapping, form, request, response);
150       }
151       else if (CREATE_OP.equals(operation)){
152          return createAssembly(mapping, form, request, response);
153       }
154       else if (SEARCH_OP.equals(operation)){
155          return searchAssemblies(mapping, form, request, response);
156       }
157       else if (DETAILS_OP.equals(operation)){
158          return getAssemblyDetails(mapping, form, request, response);
159       }
160       else if (STATUS_OP.equals(operation)){
161          return forceStatus(mapping, form, request, response);
162       }
163       else{
164          // Default : assemblies browsing page.
165          request.getSession().removeAttribute(ASSEMBLY_KEY);
166          return mapping.findForward("Assemblies");
167       }
168    }
169 
170 
171    // Protected ----------------------------------------------------------------
172 
173    /**
174     * Prepare all we need for displaying an assembly creation form
175     * @return A forward to the next view to render and display
176     */
177    protected ActionForward showAssemblyForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
178          throws Exception{
179 
180       if (form instanceof AssemblyForm){
181          AssemblyForm aForm = (AssemblyForm)form;
182 
183          if (aForm.getReleaseName() != null){
184             // Retrieve the release of assembly and the available assemblies & deliverables ...
185             HashMap deliverablesMap = new HashMap();
186             Release release = processManager.getRelease(aForm.getReleaseName());
187             List assemblies = assemblyManager.getAssemblies(release);
188             List types = artifactManager.getDeliverableTypes();
189             // Organize available deliverables into a map.
190             for (int i=0; i<types.size(); i++){
191                DeliverableType type = (DeliverableType)types.get(i);
192                if (type.getVersionable())
193                   deliverablesMap.put(type, artifactManager.getDeliverables(type, release));
194             }
195 
196             // If an assembly is specified for using as template, retrive it.
197             if (aForm.getKey() != null){
198                Assembly template = assemblyManager.getAssembly(aForm.getKey());
199                // Fill form with template.
200                PropertyUtils.copyProperties(aForm, template);
201                Iterator deliverables = template.getDeliverables().values().iterator();
202                while (deliverables.hasNext())
203                   aForm.addDeliverable((Deliverable)deliverables.next());
204             }
205             // Put them into request attributes and form property.
206             aForm.setReleaseName(release.getName());
207             request.setAttribute(ASSEMBLIES_KEY, assemblies);
208             request.setAttribute(DELIVERABLES_KEY, deliverablesMap);
209             request.setAttribute(ReleaseActions.RELEASE_KEY, release);
210          }
211 
212          return mapping.findForward("Form");
213       }
214       // This should not happen...
215       return null;
216    }
217 
218    /**
219     * Create and register a new Assembly within Join application. No assembly for this
220     * release and having this version info should aready exists.
221     * @return A forward to the next view to render and display
222     */
223    protected ActionForward createAssembly(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
224          throws Exception{
225 
226       if (form instanceof AssemblyForm){
227          AssemblyForm aForm = (AssemblyForm)form;
228 
229          // Retrieve release and deliverables for assembly to create.
230          List deliverables = new ArrayList();
231          Iterator keys = aForm.getDeliverables().values().iterator();
232          while (keys.hasNext()){
233             String key = (String)keys.next();
234             if (key != null && key.length() > 0)
235                deliverables.add(artifactManager.getDeliverable(key));
236          }
237          Release release = processManager.getRelease(aForm.getReleaseName());
238          String composerId = getUserContainer(request).getView().getUser().getLogin();
239 
240          // Create a new assembly and register its deliverables into manager.
241          Assembly assembly = new Assembly(aForm.getVersionInfo(), aForm.getComments(), composerId, release);
242          log.info("Creation of a new Assembly with key: " + assembly.getKey());
243 
244          try {assemblyManager.saveAssembly(assembly, deliverables);}
245          catch (DuplicateEntityException dee){
246             // Store exception within request.
247             request.setAttribute(DUPLICATE_ASSEMBLY_KEY, dee.getOriginalEntity());
248             return mapping.findForward("Form");
249          }
250 
251          // Forward to list of assemblies for release.
252          return searchAssemblies(mapping, form, request, response);
253       }
254       // This should not happen...
255       return null;
256    }
257 
258    /**
259     * Serch all the assemblies using the bound release criteria.
260     * @return A forward to the next view to render and display
261     */
262    protected ActionForward searchAssemblies(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
263          throws Exception{
264 
265       if (form instanceof AssemblyForm){
266          // Get the assembly form.
267          AssemblyForm aForm = (AssemblyForm)form;
268 
269          // Retrieve the release of assemblies to search for.
270          Release release = processManager.getRelease(aForm.getReleaseName());
271 
272          // Retrieve the list and put it into request attribute.
273          List assemblies = assemblyManager.getAssemblies(release);
274          request.setAttribute(ASSEMBLIES_KEY, assemblies);
275          request.setAttribute(ReleaseActions.RELEASE_KEY, release);
276 
277          return mapping.findForward("Assemblies");
278       }
279       // This should not happen...
280       return null;
281    }
282 
283    /**
284     * Get all the details onto an existing Assembly.
285     * @return A forward to the next view to render and display
286     */
287    protected ActionForward getAssemblyDetails(ActionMapping mapping, ActionForm form,
288                                               HttpServletRequest request, HttpServletResponse response)
289          throws Exception{
290 
291       if (form instanceof AssemblyForm){
292          // Get the requested assembly from manager.
293          AssemblyForm aForm = (AssemblyForm)form;
294          Assembly assembly = assemblyManager.getAssembly(aForm.getKey());
295 
296          // Store assembly within request.
297          request.setAttribute(ASSEMBLY_KEY, assembly);
298          // Copy assembly into the form properties.
299          PropertyUtils.copyProperties(aForm, assembly);
300 
301          return mapping.findForward("Assembly");
302       }
303       // This should not happen...
304       return null;
305    }
306 
307    /**
308     * Force the status of a given assembly. This is a 2 step operation : retrieve possible
309     * status first and display a form ; then set the choosen status for deployment.
310     * @return A forward to the next view to render and display
311     */
312    protected ActionForward forceStatus(ActionMapping mapping, ActionForm form,
313                                             HttpServletRequest request, HttpServletResponse response)
314            throws Exception{
315 
316       if (form instanceof AssemblyForm){
317          // Get the assembly form.
318          AssemblyForm aForm = (AssemblyForm)form;
319 
320          // If no status, forward to page.
321          if (aForm.getStatusKey() == null){
322             request.setAttribute("statusType", "assembly");
323             request.setAttribute(ASSEMBLY_KEY, assemblyManager.getAssembly(aForm.getKey()));
324 
325             // Build a list of possible status.
326             ArrayList statusList = new ArrayList();
327             statusList.addAll(processManager.getStatusForType(Status.ASSEMBLY_TYPE));
328             request.setAttribute(StatusActions.STATUS_LIST_KEY, statusList);
329             return mapping.findForward("Status");
330          }
331 
332          // Find and set status to deployment.
333          Status status = processManager.getStatus(aForm.getStatusKey());
334          Assembly assembly = assemblyManager.getAssembly(aForm.getKey());
335          assembly.setStatus(status);
336          assemblyManager.saveAssembly(assembly, null);
337 
338          // Forward to details.
339          return getAssemblyDetails(mapping, form, request, response);
340       }
341       // This should not happen...
342       return null;
343    }
344 }