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.ReleaseForm;
19  import org.figure8.join.core.DuplicateEntityException;
20  import org.figure8.join.businessobjects.commons.Release;
21  import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
22  import org.figure8.join.util.LogUtil;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.beanutils.PropertyUtils;
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.HttpSession;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  /**
34   * Struts action used for managing the software project Releases and setup
35   * the integration process with them.
36   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
37   * @version $Revision: 1.1 $
38   *
39   * @struts.action path="/release" name="releaseForm"
40   *                scope="request" parameter="op" validate="true"
41   *                input="/pages/mainpage.jsp?body=/jsp/releases.jsp"
42   * @struts.action-forward name="Releases" path="/jsp/releases.jsp"
43   */
44  public class ReleaseActions extends JoinAction{
45  
46     // Static -------------------------------------------------------------------
47  
48     /** Get a commons logger. */
49     protected static Log log = LogUtil.getLog(ReleaseActions.class);
50  
51     /** Operation code for loading a specific Release */
52     public static final String LOAD_OP = "load";
53     /** Operation code for saving a Release (create or update) */
54     public static final String SAVE_OP = "save";
55  
56     /**
57      * The session scope attribute under which the Release object currently
58      * selected by our logged-in User is stored.
59      */
60     public static final String RELEASE_KEY = "release";
61     /**
62      * The request scope attribute under which is stored the Release that
63      * has raised a DuplicateEntityException during save of another one.
64      */
65     public static final String DUPLICATE_RELEASE_KEY = "duplicate";
66  
67  
68     // Attributes ---------------------------------------------------------------
69  
70     /** Join integration process manager. */
71     protected IntegrationProcessManager processManager;
72  
73  
74     // Constructors -------------------------------------------------------------
75  
76     /** Creates a new instance of ReleaseActions */
77     public ReleaseActions(){
78     }
79  
80  
81     // Public -------------------------------------------------------------------
82  
83     /** @param manager <code>IntegrationProcessManager</code> implementation instance */
84     public void setIntegrationProcessManager(IntegrationProcessManager manager){
85        this.processManager = manager;
86     }
87     
88  
89     // Override of JoinAction ---------------------------------------------------
90  
91     /**
92      *
93      * @param operation String representing the operation to invoke on Action
94      * @param mapping Mapping between forwards name and path for this action
95      * @param form The form object containing request parameters
96      * @param request The servlet container request wrapper
97      * @param response The servlet container response wrapper
98      * @return A forward to the next view to render and display
99      */
100    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
101       // Trace operation to execute.
102       log.debug("doExecute() called for '" + operation + "' operation");
103 
104       if (LOAD_OP.equals(operation)){
105          return loadRelease(mapping, form, request, response);
106       }
107       else if (SAVE_OP.equals(operation)){
108          return saveRelease(mapping, form, request, response);
109       }
110       else{
111          // Default : empty release management page.
112          request.getSession().removeAttribute(RELEASE_KEY);
113          return mapping.findForward("Releases");
114       }
115    }
116 
117 
118    // Protected ----------------------------------------------------------------
119 
120    /**
121     * Load a specified release from datastore and fill form with it.
122     * @return A forward to the next view to render and display
123     */
124    protected ActionForward loadRelease(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
125       throws Exception{
126 
127       if (form instanceof ReleaseForm){
128          // Get the requested release from manager.
129          ReleaseForm rForm = (ReleaseForm)form;
130          Release release = processManager.getRelease(rForm.getName());
131 
132          // Store release within session.
133          HttpSession session = request.getSession();
134          session.setAttribute(RELEASE_KEY, release);
135          // Copy into form the step properties.
136          PropertyUtils.copyProperties(rForm, release);
137 
138          // Forward.
139          return mapping.findForward("Releases");
140       }
141       // This should not happen...
142       return null;
143    }
144 
145    /**
146     * Save a release into datastore. The Release may be an already existing one
147     * or a new one (this is indeed a create or update method).
148     * @return A forward to the next view to render and display
149     */
150    protected ActionForward saveRelease(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
151       throws Exception{
152 
153       if (form instanceof ReleaseForm){
154          // Get the release form.
155          ReleaseForm rForm = (ReleaseForm)form;
156 
157          // Is there's a release to update in session ?
158          HttpSession session = request.getSession();
159          Release release = (Release)session.getAttribute(RELEASE_KEY);
160 
161          // Create a new Release or populate existing with form.
162          if (release == null){
163             log.info("Creation of a new Release with name: " + rForm.getName());
164             release = new Release(rForm.getName(), rForm.getMajor(), rForm.getMinor(), rForm.getShippingDate());
165          }
166          else{
167             log.info("Update of existing Release having name: " + release.getName());
168             PropertyUtils.copyProperties(release, rForm);
169          }
170 
171          // Save step using manager.
172          try {processManager.saveRelease(release);}
173          catch (DuplicateEntityException dee){
174             // Store exception within request.
175             request.setAttribute(DUPLICATE_RELEASE_KEY, dee.getOriginalEntity());
176             return mapping.findForward("Releases");
177          }
178 
179          // Remove form and session attribute if present.
180          session.removeAttribute(RELEASE_KEY);
181          removeObsoleteForm(mapping, request);
182 
183          // Forward.
184          return mapping.findForward("Releases");
185       }
186       // This should not happen...
187       return null;
188    }
189 }