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.ResourceVersionForm;
19 import org.figure8.join.core.DuplicateEntityException;
20 import org.figure8.join.businessobjects.environment.ResourceVersion;
21 import org.figure8.join.businessobjects.environment.VersionedResourceType;
22 import org.figure8.join.businessfacades.environment.ResourceManager;
23 import org.figure8.join.util.LogUtil;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.beanutils.PropertyUtils;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30
31 import javax.servlet.http.HttpSession;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import java.util.List;
36 import java.lang.reflect.Constructor;
37 /**
38 * This is a Struts action for managing resource versions.
39 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
40 * @version $Revision: 1.1 $
41 *
42 * @struts.action path="/resourceVersion" name="resourceVersionForm"
43 * scope="request" parameter="op" validate="true"
44 * input="/pages/mainpage.jsp?body=/jsp/environment/resourceversions.jsp"
45 * @struts.action-forward name="ResourceVersions" path="/jsp/environment/resourceversions.jsp"
46 */
47 public class ResourceVersionActions extends JoinAction{
48
49
50
51 /** Get a commons logger. */
52 private static final Log log = LogUtil.getLog(ResourceVersionActions.class);
53
54 /** Operation code for loading a specified ResourceVersion */
55 public static final String LOAD_OP = "load";
56 /** Operation code for saving (create or update) a specified ResourceVersion */
57 public static final String SAVE_OP = "save";
58 /** Operation code for searching resource versions using their type */
59 public static final String SEARCH_OP = "search";
60
61 /**
62 * The session scope attribute under which the ResourceVersion object
63 * currently selected by our logged-in User is stored.
64 */
65 public static final String RESOURCE_VERSION_KEY = "resourceVersion";
66 /**
67 * The request scope attribute under which the retrieved ResourceVersions
68 * list is stored.
69 */
70 public static final String RESOURCE_VERSIONS_KEY = "resourceVersions";
71 /**
72 * The request scope attribute under which is stored the ResourceVersion that
73 * has raised a DuplicateEntityException during save of another one.
74 */
75 public static final String DUPLICATE_RESOURCE_VERSION_KEY = "duplicate";
76
77
78
79
80 /** The ResourceManager to use */
81 private ResourceManager resourceManager = null;
82
83
84
85
86 /** Creates a new instance of ResourceVersionActions. */
87 public ResourceVersionActions(){
88 }
89
90
91
92
93 /** @param manager The ResourceManager to use */
94 public void setResourceManager(ResourceManager manager){
95 this.resourceManager = manager;
96 }
97
98
99
100
101 /**
102 *
103 * @param operation String representing the operation to invoke on Action
104 * @param mapping Mapping between forwards name and path for this action
105 * @param form The form object containing request parameters
106 * @param request The servlet container request wrapper
107 * @param response The servlet container response wrapper
108 * @return A forward to the next view to render and display
109 * @throws Exception such as InfraStructureExceptions ...
110 */
111 public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
112
113 log.debug("doExecute() called for '" + operation + "' operation");
114
115
116 List resourceTypes = resourceManager.getResourceTypes(request.getParameter("category"));
117 request.setAttribute(ResourceTypeActions.RESOURCE_TYPES_KEY, resourceTypes);
118
119 if (LOAD_OP.equals(operation)){
120 return loadResourceVersion(mapping, form, request, response);
121 }
122 else if (SAVE_OP.equals(operation)){
123 return saveResourceVersion(mapping, form, request, response);
124 }
125 else if (SEARCH_OP.equals(operation)){
126 return searchResourceVersions(mapping, form, request, response);
127 }
128 else{
129
130 request.getSession().removeAttribute(RESOURCE_VERSION_KEY);
131 return mapping.findForward("ResourceVersions");
132 }
133 }
134
135
136
137
138 /**
139 * Load a specified resource versin from datastore and fill form with it.
140 * @return A forward to the next view to render and display
141 */
142 protected ActionForward loadResourceVersion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
143 throws Exception{
144
145 if (form instanceof ResourceVersionForm){
146
147 ResourceVersionForm rForm = (ResourceVersionForm)form;
148 ResourceVersion version = resourceManager.getResourceVersion(rForm.getId());
149
150
151 HttpSession session = request.getSession();
152 session.setAttribute(RESOURCE_VERSION_KEY, version);
153
154 PropertyUtils.copyProperties(rForm, version);
155
156 return mapping.findForward("ResourceVersions");
157 }
158
159 return null;
160 }
161
162 /**
163 * Save a resource version into datastore. The resource version may be an already existing
164 * one or a new one (this is indeed a create or update method).
165 * @return A forward to the next view to render and display
166 */
167 protected ActionForward saveResourceVersion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
168 throws Exception{
169
170 if (form instanceof ResourceVersionForm){
171 ResourceVersionForm rForm = (ResourceVersionForm)form;
172
173
174 HttpSession session = request.getSession();
175 ResourceVersion version = (ResourceVersion)session.getAttribute(RESOURCE_VERSION_KEY);
176
177
178 if (version == null){
179 log.info("Creation of a new ResourceVersion with name: " + rForm.getName());
180 VersionedResourceType resourceType = (VersionedResourceType)resourceManager.getResourceType(
181 rForm.getResourceTypeKey());
182 version = new ResourceVersion(rForm.getName(), rForm.getDescription(), resourceType);
183 }
184 else{
185 log.info("Update of existing ResourceVersion having name: " + rForm.getName());
186 PropertyUtils.copyProperties(version, rForm);
187 }
188
189 try {resourceManager.saveResourceVersion(version);}
190 catch (DuplicateEntityException dee){
191
192 request.setAttribute(DUPLICATE_RESOURCE_VERSION_KEY, dee);
193 return searchResourceVersions(mapping, form, request, response);
194 }
195
196
197 session.removeAttribute(RESOURCE_VERSION_KEY);
198 removeObsoleteForm(mapping, request);
199
200
201 return searchResourceVersions(mapping, form, request, response);
202 }
203
204 return null;
205 }
206
207 /**
208 * Retrieve a list of ResourceVersions using the resource type criteria.
209 * @return A forward to the next view to render and display
210 */
211 protected ActionForward searchResourceVersions(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
212 throws Exception{
213
214 if (form instanceof ResourceVersionForm){
215 ResourceVersionForm rForm = (ResourceVersionForm)form;
216
217
218 VersionedResourceType resourceType = (VersionedResourceType)resourceManager.getResourceType(rForm.getResourceTypeKey());
219 List resourceVersions = resourceManager.getResourceVersions(resourceType);
220
221
222 request.setAttribute(RESOURCE_VERSIONS_KEY, resourceVersions);
223 request.setAttribute("resourceTypeKey", rForm.getResourceTypeKey());
224 return mapping.findForward("ResourceVersions");
225 }
226
227 return null;
228 }
229 }