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