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.ResourceUpdateForm;
19  import org.figure8.join.businessfacades.environment.ResourceManager;
20  import org.figure8.join.util.LogUtil;
21  import org.figure8.join.businessobjects.environment.VersionedResource;
22  import org.figure8.join.businessobjects.environment.VersionedResourceType;
23  import org.figure8.join.businessobjects.environment.ResourceVersion;
24  
25  import org.apache.commons.logging.Log;
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.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import java.util.List;
34  /**
35   * This is a Struts action for manipulating ResourceUpdates domain objects.
36   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
37   * @version $Revision: 1.1 $
38   *
39   * @struts.action path="/resourceUpdate" name="resourceUpdateForm"
40   *                scope="request" parameter="op" validate="true"
41   *                input="/pages/mainpage.jsp?body=/jsp/environment/resourceupdates.jsp"
42   * @struts.action-forward name="ResourceUpdates" path="/jsp/environment/resourceupdates.jsp"
43   */
44  public class ResourceUpdateActions extends JoinAction{
45  
46     // Static -------------------------------------------------------------------
47  
48     /** Get a commons logger. */
49     private static final Log log = LogUtil.getLog(ResourceUpdateActions.class);
50  
51     /** Operation code for showing a resource update form */
52     public static final String FORM_OP = "form";
53     /** Operation code for saving a new ResourceUpdate  */
54     public static final String SAVE_OP = "save";
55  
56  
57     // Attributes ---------------------------------------------------------------
58  
59     /** The ResourceManager to use */
60     private ResourceManager resourceManager = null;
61  
62  
63     // Constructors -------------------------------------------------------------
64  
65     /** Creates a new instance of ResourceUpdateActions */
66     public ResourceUpdateActions(){
67     }
68  
69  
70     // Public -------------------------------------------------------------------
71  
72     /** @param manager The ResourceManager to use */
73     public void setResourceManager(ResourceManager manager){
74        this.resourceManager = manager;
75     }
76  
77  
78     // Implementation of JoinAction ---------------------------------------------
79  
80     /**
81      *
82      * @param operation String representing the operation to invoke on Action
83      * @param mapping Mapping between forwards name and path for this action
84      * @param form The form object containing request parameters
85      * @param request The servlet container request wrapper
86      * @param response The servlet container response wrapper
87      * @return A forward to the next view to render and display
88      * @throws Exception such as InfraStructureExceptions ...
89      */
90     public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
91             throws Exception{
92        // Trace operation to execute.
93        log.debug("doExecute() called for '" + operation + "' operation");
94  
95        if (form instanceof ResourceUpdateForm){
96           ResourceUpdateForm uForm = (ResourceUpdateForm)form;
97  
98           if (FORM_OP.equals(operation)){
99              VersionedResource resource = (VersionedResource)resourceManager.getResource(uForm.getResourceName());
100             List versions = resourceManager.getResourceVersions((VersionedResourceType)resource.getResourceType());
101             // Publish objects into request attributes.
102             request.setAttribute(ResourceActions.RESOURCE_KEY, resource);
103             request.setAttribute(ResourceVersionActions.RESOURCE_VERSIONS_KEY, versions);
104 
105             return mapping.findForward("ResourceUpdates");
106          }
107          else if (SAVE_OP.equals(operation)){
108             // Find required objects and request update of resource.
109             VersionedResource resource = (VersionedResource)resourceManager.getResource(uForm.getResourceName());
110             ResourceVersion version = resourceManager.getResourceVersion(uForm.getResourceVersionName());
111             resourceManager.updateResource(resource, version);
112 
113             // Store a message into request.
114             request.setAttribute("message", "Resource '" + resource.getName() +
115                                           "' has been updated with version '" + version.getName() + "'");
116             // Build a custom ActionForward.
117             return new ActionForward("/action/resource?op=search&category="
118                     + resource.getCategory() + "&resourceTypeKey=" + resource.getResourceType().getKey());
119          }
120       }
121       // This should not happen...
122       return null;
123    }
124 }