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.MachineForm;
19  import org.figure8.join.core.DuplicateEntityException;
20  import org.figure8.join.businessobjects.environment.Machine;
21  import org.figure8.join.businessfacades.environment.ResourceManager;
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 physical machines used by software
35   * project integration team.
36   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
37   * @version $Revision: 1.1 $
38   *
39   * @struts.action path="/machine" name="machineForm"
40   *                scope="request" parameter="op" validate="true"
41   *                input="/pages/mainpage.jsp?body=/jsp/environment/machines.jsp"
42   * @struts.action-forward name="Machine" path="/jsp/environment/machine.jsp"
43   * @struts.action-forward name="Machines" path="/jsp/environment/machines.jsp"
44   */
45  public class MachineActions extends JoinAction{
46  
47     // Static -------------------------------------------------------------------
48  
49     /** Get a commons logger. */
50     protected static Log log = LogUtil.getLog(MachineActions.class);
51  
52     /** Operation code for loading a specific Machine */
53     public static final String LOAD_OP = "load";
54     /** Operation code for saving a Machine (create or update) */
55     public static final String SAVE_OP = "save";
56     /** Operation code for getting details on a Machine */
57     public static final String DETAILS_OP = "details";
58  
59     /**
60      * The session scope attribute under which the Machine object currently
61      * selected by our logged-in User is stored.
62      */
63     public static final String MACHINE_KEY = "machine";
64     /**
65      * The request scope attribute under which the retrieved Machines
66      * list is stored.
67      */
68     public static final String MACHINES_KEY = "machines";
69     /**
70      * The request scope attribute under which is stored the Machine that
71      * has raised a DuplicateEntityException during save of another one.
72      */
73     public static final String DUPLICATE_MACHINE_KEY = "duplicate";
74  
75  
76     // Attributes ---------------------------------------------------------------
77  
78     /** The ResourceManager to use */
79     private ResourceManager resourceManager = null;
80  
81  
82     // Constructors -------------------------------------------------------------
83  
84     /** Creates a new instance of MachineActions. */
85     public MachineActions(){
86     }
87  
88  
89     // Public -------------------------------------------------------------------
90  
91     /** @param manager The ResourceManager to use */
92     public void setResourceManager(ResourceManager manager){
93        this.resourceManager = manager;
94     }
95  
96  
97     // Override of JoinAction ---------------------------------------------------
98  
99     /**
100     *
101     * @param operation String representing the operation to invoke on Action
102     * @param mapping Mapping between forwards name and path for this action
103     * @param form The form object containing request parameters
104     * @param request The servlet container request wrapper
105     * @param response The servlet container response wrapper
106     * @return A forward to the next view to render and display
107     */
108    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
109       // Trace operation to execute.
110       log.debug("doExecute() called for '" + operation + "' operation");
111 
112       if (LOAD_OP.equals(operation)){
113          return loadMachine(mapping, form, request, response);
114       }
115       else if (SAVE_OP.equals(operation)){
116          return saveMachine(mapping, form, request, response);
117       }
118       else if (DETAILS_OP.equals(operation)){
119          return getMachineDetails(mapping, form, request, response);
120       }
121       else{
122          // Default : empty step management page.
123          request.getSession().removeAttribute(MACHINE_KEY);
124          request.setAttribute(MACHINES_KEY, resourceManager.getMachines());
125          return mapping.findForward("Machines");
126       }
127    }
128 
129 
130    // Protected ----------------------------------------------------------------
131 
132    /**
133     * Load a specified machine from datastore and fill form with it.
134     * @return A forward to the next view to render and display
135     */
136    protected ActionForward loadMachine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
137       throws Exception{
138 
139       if (form instanceof MachineForm){
140          // Get the request machine from manager.
141          MachineForm mForm = (MachineForm)form;
142          Machine machine = resourceManager.getMachine(mForm.getId());
143 
144          // Store machine within session.
145          HttpSession session = request.getSession();
146          session.setAttribute(MACHINE_KEY, machine);
147          // Copy machine into the form properties.
148          PropertyUtils.copyProperties(mForm, machine);
149 
150          // Add machines list to request before forwarding.
151          request.setAttribute(MACHINES_KEY, resourceManager.getMachines());
152          return mapping.findForward("Machines");
153       }
154       // This should not happen...
155       return null;
156    }
157 
158    /**
159     * Save a machine into datastore. The Machine may be an already existing one
160     * or a new one (this is indeed a create or update method).
161     * @return A forward to the next view to render and display
162     */
163    protected ActionForward saveMachine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
164       throws Exception{
165 
166       if (form instanceof MachineForm){
167          MachineForm mForm = (MachineForm)form;
168 
169          // Is there a machine to update in session ?
170          HttpSession session = request.getSession();
171          Machine machine = (Machine)session.getAttribute(MACHINE_KEY);
172 
173          if (machine == null){
174             log.info("Creation of a new Machine with name: " + mForm.getName());
175             machine = new Machine(mForm.getName(), mForm.getIpAddress());
176             if (mForm.getOsInfo() != null) machine.setOsInfo(mForm.getOsInfo());
177             if (mForm.getCpuInfo() != null) machine.setCpuInfo(mForm.getCpuInfo());
178             if (mForm.getRamAmount() != -1) machine.setRamAmount(mForm.getRamAmount());
179             if (mForm.getRomAmount() != -1) machine.setRomAmount(mForm.getRomAmount());
180          }
181          else{
182             log.info("Update of existing Machine having name: " + machine.getName());
183             PropertyUtils.copyProperties(machine, mForm);
184          }
185          // Save machine using manager.
186          try {resourceManager.saveMachine(machine);}
187          catch (DuplicateEntityException dee){
188 
189             return mapping.findForward("Machines");
190          }
191 
192          // Remove form and session attribute if present.
193          session.removeAttribute(MACHINE_KEY);
194          removeObsoleteForm(mapping, request);
195 
196          // Add machines list to request before forwarding.
197          request.setAttribute(MACHINES_KEY, resourceManager.getMachines());
198          return mapping.findForward("Machines");
199       }
200       // This should not happen...
201       return null;
202    }
203 
204    /**
205     * Retrieve the details onto a specified machine.
206     * @return A forward to the next view to render and display
207     */
208    protected ActionForward getMachineDetails(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
209       throws Exception{
210 
211       if (form instanceof MachineForm){
212          // Get the requested machine.
213          MachineForm mForm = (MachineForm)form;
214          Machine machine = resourceManager.getMachine(mForm.getId());
215 
216          // Add machine to request before forwarding.
217          request.setAttribute(MACHINE_KEY, machine);
218          return mapping.findForward("Machine");
219       }
220       // This should not happen...
221       return null;
222    }
223 }