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.DeliverableForm;
19  import org.figure8.join.core.DuplicateEntityException;
20  import org.figure8.join.businessobjects.commons.Release;
21  import org.figure8.join.businessobjects.artifact.Deliverable;
22  import org.figure8.join.businessobjects.artifact.DeliverableType;
23  import org.figure8.join.businessfacades.artifact.ArtifactManager;
24  import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
25  import org.figure8.join.util.LogUtil;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.beanutils.PropertyUtils;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import javax.servlet.http.HttpSession;
36  import java.util.List;
37  /**
38   * This is a Struts action for managing all the deliverables within Join.
39   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
40   * @version $Revision: 1.2 $
41   *
42   * @struts.action path="/deliverable" name="deliverableForm"
43   *                scope="request" parameter="op" validate="true"
44   *                input="/pages/mainpage.jsp?body=/jsp/artifact/delivery.jsp"
45   * @struts.action-forward name="Delivery" path="/jsp/artifact/delivery.jsp"
46   * @struts.action-forward name="Deliverable" path="/jsp/artifact/deliverable.jsp"
47   * @struts.action-forward name="Deliverables" path="/jsp/artifact/deliverables.jsp"
48   */
49  public class DeliverableActions extends JoinAction{
50  
51     // Static -------------------------------------------------------------------
52  
53     /** Get a commons logger. */
54     private static final Log log = LogUtil.getLog(DeliverableActions.class);
55  
56     /** Operation code for loading a specific Deliverable */
57     public static final String LOAD_OP = "load";
58     /** Operation code fo showing the delivery form */
59     public static final String FORM_OP = "form";
60     /** Operation code for saving a Deliverable */
61     public static final String SAVE_OP = "save";
62     /** Operation code for creating a new Deliverable */
63     public static final String CREATE_OP = "create";
64     /** Operation code for searching deliverables using type and release */
65     public static final String SEARCH_OP = "search";
66     /** Operation code for getting details on a specific Deliverable */
67     public static final String DETAILS_OP = "details";
68  
69     /**
70      * The session scope attribute under which the Deliverable object currently
71      * selected by our logged-in User is stored.
72      */
73     public static final String DELIVERABLE_KEY = "deliverable";
74     /**
75      * The request scope attribute under which the retrivied deliverables
76      * list is stored.
77      */
78     public static final String DELIVERABLES_KEY = "deliverables";
79  
80     /**
81      * The request scope attribute under which is stored the Deliverable that
82      * has raised a DuplicateEntityException during save of another one.
83      */
84     public static final String DUPLICATE_DELIVERABLE_KEY = "duplicate";
85  
86  
87     // Attributes ---------------------------------------------------------------
88  
89     /** The artifact manager implementation to use. */
90     protected ArtifactManager artifactManager = null;
91     /** The integration process manager implementation to use. */
92     protected IntegrationProcessManager processManager = null;
93  
94  
95     // Constructors -------------------------------------------------------------
96  
97     /** Creates a new instance of DeliverableActions. */
98     public DeliverableActions(){
99     }
100 
101 
102    // Public -------------------------------------------------------------------
103 
104    /** @param manager The ArtifactManager implementation to use */
105    public void setArtifactManager(ArtifactManager manager){
106       this.artifactManager = manager;
107    }
108    /** @param manager The integration process manager implementation to use */
109    public void setIntegrationProcessManager(IntegrationProcessManager manager){
110       this.processManager = manager;
111    }
112 
113 
114    // Implementation of JoinAction ---------------------------------------------
115 
116    /**
117     *
118     * @param operation String representing the operation to invoke on Action
119     * @param mapping Mapping between forwards name and path for this action
120     * @param form The form object containing request parameters
121     * @param request The servlet container request wrapper
122     * @param response The servlet container response wrapper
123     * @return A forward to the next view to render and display
124     * @throws Exception such as InfraStructureExceptions ...
125     */
126    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form,
127                                   HttpServletRequest request, HttpServletResponse response) throws Exception{
128       // Trace operation to execute.
129       log.debug("doExecute() called for '" + operation + "' operation");
130 
131       if (LOAD_OP.equals(operation)){
132          return loadDeliverable(mapping, form, request, response);
133       }
134       else if (FORM_OP.equals(operation)){
135          return showDeliverableForm(mapping, form, request, response);
136       }
137       else if (SAVE_OP.equals(operation)){
138          return saveDeliverable(mapping, form, request, response);
139       }
140       else if (CREATE_OP.equals(operation)){
141          return createDeliverable(mapping, form, request, response);
142       }
143       else if (SEARCH_OP.equals(operation)){
144          return searchDeliverables(mapping, form, request, response);
145       }
146       else if (DETAILS_OP.equals(operation)){
147          return getDeliverableDetails(mapping, form, request, response);
148       }
149       else{
150          // Default : deliverables browsing page.
151          request.getSession().removeAttribute(DELIVERABLE_KEY);
152          return mapping.findForward("Deliverables");
153       }
154    }
155 
156 
157    // Protected ----------------------------------------------------------------
158 
159    /**
160     * Load a specified deliverable from datastore and fill form with it.
161     * @return A forward to the next view to render and display
162     */
163    protected ActionForward loadDeliverable(ActionMapping mapping, ActionForm form,
164                                            HttpServletRequest request, HttpServletResponse response) throws Exception{
165       if (form instanceof DeliverableForm){
166          // Get the requested deliverable from manager
167          DeliverableForm dForm = (DeliverableForm)form;
168          Deliverable deliverable = artifactManager.getDeliverable(dForm.getKey());
169 
170          // Store deliverable within session.
171          HttpSession session = request.getSession();
172          session.setAttribute(DELIVERABLE_KEY, deliverable);
173          // Copy deliverable into the form properties.
174          PropertyUtils.copyProperties(dForm, deliverable);
175          // Add the release name and type as form properties.
176          dForm.setReleaseName(deliverable.getRelease().getName());
177          dForm.setTypeKey(deliverable.getDeliverableType().getKey());
178 
179          return mapping.findForward("Delivery");
180       }
181       // This should not happen...
182       return null;
183    }
184 
185    /**
186     * Prepare all we need for displaying a delivery form
187     * @return A forward to the next view to render and display
188     */
189    protected ActionForward showDeliverableForm(ActionMapping mapping, ActionForm form,
190                                                HttpServletRequest request, HttpServletResponse response)
191            throws Exception{
192       if (form instanceof DeliverableForm){
193          DeliverableForm dForm = (DeliverableForm)form;
194 
195          // Retrieve the type for delivery and the latest deliverables ...
196          DeliverableType type = artifactManager.getDeliverableType(dForm.getTypeKey());
197          List deliverables = artifactManager.getLastDeliverables(type, 10);
198          // Put them into request attributes.
199          request.setAttribute(DELIVERABLES_KEY, deliverables);
200          request.setAttribute(DeliverableTypeActions.DELIVERABLE_TYPE_KEY, type);
201 
202          return mapping.findForward("Delivery");
203       }
204       // This should not happen...
205       return null;
206    }
207 
208    /**
209     * Update a deliverable informations into datastore.
210     * @return A forward to the next view to render and display
211     */
212    protected ActionForward saveDeliverable(ActionMapping mapping, ActionForm form,
213                                            HttpServletRequest request, HttpServletResponse response) throws Exception{
214       if (form instanceof DeliverableForm){
215          DeliverableForm dForm = (DeliverableForm)form;
216 
217          // Is there a deliverable to update in session ?
218          HttpSession session = request.getSession();
219          Deliverable deliverable = (Deliverable)session.getAttribute(DELIVERABLE_KEY);
220 
221          if (deliverable != null){
222             log.info("Update of existing Deliverable having key: " + deliverable.getKey());
223             PropertyUtils.copyProperties(deliverable, dForm);
224             // Save deliverable using manager and display details.
225             artifactManager.saveDeliverable(deliverable);
226             return getDeliverableDetails(mapping, form, request, response);
227          }
228 
229          // Forward to deliverables page.
230          return searchDeliverables(mapping, form, request, response);
231       }
232       // This should not happen...
233       return null;
234    }
235 
236    /**
237     * Create and register a new Deliverable within Join application. No deliverable
238     * of this type, for this release and having this version info should aready exists.
239     * @return A forward to the next view to render and display
240     */
241    protected ActionForward createDeliverable(ActionMapping mapping, ActionForm form,
242                                              HttpServletRequest request, HttpServletResponse response) throws Exception{
243       if (form instanceof DeliverableForm){
244          DeliverableForm dForm = (DeliverableForm)form;
245 
246          // Retrieve the release and type of deliverable to create.
247          Release release = processManager.getRelease(dForm.getReleaseName());
248          DeliverableType type = artifactManager.getDeliverableType(dForm.getTypeKey());
249          String supplierId = getUserContainer(request).getView().getUser().getLogin();
250 
251          // Create deliverable and register it using manager.
252          Deliverable deliverable = new Deliverable(dForm.getVersionInfo(), dForm.getComments(),
253                                                       supplierId, release, type);
254          log.info("Creation of a new Deliverable with key: " + deliverable.getKey());
255 
256          try {artifactManager.registerDeliverable(deliverable, dForm.getDeliverableFile().getInputStream());}
257          catch (DuplicateEntityException dee){
258             // Store exception within request.
259             request.setAttribute(DUPLICATE_DELIVERABLE_KEY, dee.getOriginalEntity());
260             return mapping.findForward("Delivery");
261          }
262 
263          // Forward to list of deliverables.
264          return searchDeliverables(mapping, form, request, response);
265       }
266       // This should not happen...
267       return null;
268    }
269 
270    /**
271     * Serch all the deliverables using the type and the bound release criteria.
272     * @return A forward to the next view to render and display
273     */
274    protected ActionForward searchDeliverables(ActionMapping mapping, ActionForm form,
275                                               HttpServletRequest request, HttpServletResponse response)
276            throws Exception{
277       if (form instanceof DeliverableForm){
278          // Get the deliverable form.
279          DeliverableForm dForm = (DeliverableForm)form;
280 
281          // Retrieve the release and type of deliverables to search for.
282          Release release = processManager.getRelease(dForm.getReleaseName());
283          DeliverableType type = artifactManager.getDeliverableType(dForm.getTypeKey());
284 
285          // Retrieve the list and put it into request attribute.
286          List deliverables = artifactManager.getDeliverables(type, release);
287          request.setAttribute(DELIVERABLES_KEY, deliverables);
288          request.setAttribute(ReleaseActions.RELEASE_KEY, release);
289          request.setAttribute(DeliverableTypeActions.DELIVERABLE_TYPE_KEY, type);
290 
291          return mapping.findForward("Deliverables");
292       }
293       // This should not happen...
294       return null;
295    }
296 
297    /**
298     * Get all the details onto an existing Deliverable.
299     * @return A forward to the next view to render and display
300     */
301    protected ActionForward getDeliverableDetails(ActionMapping mapping, ActionForm form,
302                                                  HttpServletRequest request, HttpServletResponse response)
303            throws Exception{
304       if (form instanceof DeliverableForm){
305          // Get the requested deliverable from manager
306          DeliverableForm dForm = (DeliverableForm)form;
307          Deliverable deliverable = artifactManager.getDeliverable(dForm.getKey());
308 
309          // Store deliverable within request.
310          request.setAttribute(DELIVERABLE_KEY, deliverable);
311          // Copy deliverable into the form properties.
312          PropertyUtils.copyProperties(dForm, deliverable);
313          // Add the release name and type as form properties.
314          dForm.setReleaseName(deliverable.getRelease().getName());
315          dForm.setTypeKey(deliverable.getDeliverableType().getKey());
316 
317          return mapping.findForward("Deliverable");
318       }
319       // This should not happen...
320       return null;
321    }
322 }