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.PhysicalEnvironmentForm;
19 import org.figure8.join.core.DuplicateEntityException;
20 import org.figure8.join.businessobjects.environment.PhysicalEnvironment;
21 import org.figure8.join.businessobjects.environment.LogicalEnvironment;
22 import org.figure8.join.businessobjects.environment.EnvironmentMapping;
23 import org.figure8.join.businessfacades.environment.EnvironmentManager;
24 import org.figure8.join.view.PhysicalEnvironmentTrackingView;
25 import org.figure8.join.util.LogUtil;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.beanutils.PropertyUtils;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 import javax.servlet.http.HttpSession;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
37 import java.util.Calendar;
38 /**
39 * Struts action used for managing physical environments within Join application.
40 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
41 * @version $Revision: 1.3 $
42 *
43 * @struts:action path="/physicalEnvironment" name="physicalEnvForm"
44 * scope="request" parameter="op" validate="true"
45 * input="/pages/mainpage.jsp?body=/jsp/environment/physicalenvironment.jsp"
46 * @struts.action-forward name="Environments" path="/jsp/environment/physicalenvironment.jsp"
47 * @struts.action-forward name="EnvironmentViews" path="/jsp/environment/environmentview.jsp"
48 * @struts.action-forward name="EnvironmentTrackingView" path="/jsp/environment/environmenttrackingview.jsp"
49 */
50 public class PhysicalEnvironmentActions extends JoinAction{
51
52
53
54 /** Get a commons logger. */
55 private static final Log log = LogUtil.getLog(PhysicalEnvironmentActions.class);
56
57 /** Operation code for loading a specific PhysicalEnvironment */
58 public static final String LOAD_OP = "load";
59 /** Operation code for saving a PhysicalEnvironment (create or update) */
60 public static final String SAVE_OP = "save";
61 /** Operation code for accessing PhysicalEnvironment view screen */
62 public static final String VIEW_OP = "view";
63 /** Operation code for getting details on a specified PhysicalEnvironment view */
64 public static final String DETAILS_OP = "details";
65 /** Operation code for getting environment configuration details at specified date */
66 public static final String CONFIG_OP = "config";
67 /** Operation code for getting environment configuration changes for tracking period */
68 public static final String CHANGES_OP = "changes";
69
70 /**
71 * The session scope attribute under which the PhysicalEnvironment object
72 * currently selected by our logged-in User is stored.
73 */
74 public static final String ENVIRONMENT_KEY = "environment";
75 /**
76 * The request scope attribute under which the LogicalENvironment object
77 * selected is stored.
78 */
79 public static final String LOGICAL_ENVIRONMENT_KEY = "logicalEnvironment";
80 /**
81 * The request scope attribute under which the retrieved PhysicalEnvironments
82 * list is stored.
83 */
84 public static final String ENVIRONMENTS_KEY = "environments";
85 /**
86 * The request scope attribute under which the date corresponding
87 * to environment configuration view is stored.
88 */
89 public static final String VIEW_DATE_KEY = "viewDate";
90 /**
91 * The request scope attribute under which the configuration changes view
92 * for a tracking period is stored.
93 */
94 public static final String TRACKING_VIEW_KEY = "trackingView";
95 /**
96 * The request scope attribute under which is stored the PhysicalEnvironment that
97 * has raised a DuplicateEntityException during save of another one.
98 */
99 public static final String DUPLICATE_ENVIRONMENT_KEY = "duplicate";
100
101
102
103
104 /** The EnvironmentManager to use. */
105 private EnvironmentManager environmentManager = null;
106
107
108
109
110 /** Creates a new PhysicalEnvironmentActions. */
111 public PhysicalEnvironmentActions(){
112 }
113
114
115
116
117 /** @param envManager The EnvironmentManager implementation instance to use */
118 public void setEnvironmentManager(EnvironmentManager envManager){
119 this.environmentManager = envManager;
120 }
121
122
123
124
125 /**
126 *
127 * @param operation String representing the operation to invoke on Action
128 * @param mapping Mapping between forwards name and path for this action
129 * @param form The form object containing request parameters
130 * @param request The servlet container request wrapper
131 * @param response The servlet container response wrapper
132 * @return A forward to the next view to render and display
133 * @throws Exception such as InfraStructureExceptions ...
134 */
135 public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form,
136 HttpServletRequest request, HttpServletResponse response) throws Exception{
137
138 log.debug("doExecute() called for '" + operation + "' operation");
139
140 if (LOAD_OP.equals(operation)){
141 return loadEnvironment(mapping, form, request, response);
142 }
143 else if (SAVE_OP.equals(operation)){
144 return saveEnvironment(mapping, form, request, response);
145 }
146 else if (DETAILS_OP.equals(operation)){
147 return getEnvironmentDetails(mapping, form, request, response);
148 }
149 else if (CONFIG_OP.equals(operation)){
150 return getEnvironmentConfigAtDate(mapping, form, request, response);
151 }
152 else if (CHANGES_OP.equals(operation)){
153 return getEnvironmentChanges(mapping, form, request, response);
154 }
155 else{
156
157 request.getSession().removeAttribute(ENVIRONMENT_KEY);
158
159 request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getPhysicalEnvironments());
160
161 if (VIEW_OP.equals(operation))
162 return mapping.findForward("EnvironmentViews");
163
164 return mapping.findForward("Environments");
165 }
166 }
167
168
169
170
171 /**
172 * Load a specified physical environment from datastore and fill form with it.
173 * @return A forward to the next view to render and display
174 */
175 protected ActionForward loadEnvironment(ActionMapping mapping, ActionForm form,
176 HttpServletRequest request, HttpServletResponse response) throws Exception{
177 if (form instanceof PhysicalEnvironmentForm){
178
179 PhysicalEnvironmentForm envForm = (PhysicalEnvironmentForm)form;
180 PhysicalEnvironment environment = environmentManager.getPhysicalEnvironment(envForm.getId());
181
182
183 HttpSession session = request.getSession();
184 session.setAttribute(ENVIRONMENT_KEY, environment);
185
186 PropertyUtils.copyProperties(envForm, environment);
187
188
189 request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getPhysicalEnvironments());
190 return mapping.findForward("Environments");
191 }
192
193 return null;
194 }
195
196 /**
197 * Save a physical environment into datastore. The Target may be an already existing
198 * one or a new one (this is indeed a create or update method).
199 * @return A forward to the next view to render and display
200 */
201 protected ActionForward saveEnvironment(ActionMapping mapping, ActionForm form,
202 HttpServletRequest request, HttpServletResponse response) throws Exception{
203 if (form instanceof PhysicalEnvironmentForm){
204
205 PhysicalEnvironmentForm envForm = (PhysicalEnvironmentForm)form;
206
207
208 HttpSession session = request.getSession();
209 PhysicalEnvironment environment = (PhysicalEnvironment)session.getAttribute(ENVIRONMENT_KEY);
210
211
212 if (environment == null){
213 log.info("Creation of a new PhysicalEnvironment with key:" + envForm.getKey());
214 environment = new PhysicalEnvironment(envForm.getKey(), envForm.getName());
215 }
216 else{
217 log.info("Update of existing PhysicalEnvironment having key: " + envForm.getKey());
218 PropertyUtils.copyProperties(environment, envForm);
219 }
220
221 try {environmentManager.savePhysicalEnvironment(environment);}
222 catch (DuplicateEntityException dee){
223
224 request.setAttribute(DUPLICATE_ENVIRONMENT_KEY, dee.getOriginalEntity());
225 request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getPhysicalEnvironments());
226 return mapping.findForward("Environments");
227 }
228
229
230 session.removeAttribute(ENVIRONMENT_KEY);
231 removeObsoleteForm(mapping, request);
232
233
234 request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getPhysicalEnvironments());
235 return mapping.findForward("Environments");
236 }
237
238 return null;
239 }
240
241 /**
242 * Load a specified physical environment from datastore and fill form with it.
243 * @return A forward to the next view to render and display
244 */
245 protected ActionForward getEnvironmentDetails(ActionMapping mapping, ActionForm form,
246 HttpServletRequest request, HttpServletResponse response)
247 throws Exception{
248 if (form instanceof PhysicalEnvironmentForm){
249
250 PhysicalEnvironment environment = null;
251 PhysicalEnvironmentForm envForm = (PhysicalEnvironmentForm)form;
252
253 if (!envForm.isFromLogical()){
254 log.debug("Getting environment details for PhysicalEnvironment '" + envForm.getId() + "'");
255 environment = environmentManager.getPhysicalEnvironment(envForm.getId());
256 request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getPhysicalEnvironments());
257 }
258 else{
259 log.debug("Getting environment details for LogicalEnvironment '" + envForm.getId() + "'");
260 LogicalEnvironment logicalEnvironment = environmentManager.getLogicalEnvironment(envForm.getId());
261 environment = logicalEnvironment.getActiveEnvironmentMapping().getPhysicalEnvironment();
262 request.setAttribute(LOGICAL_ENVIRONMENT_KEY, logicalEnvironment);
263 }
264
265
266 request.setAttribute(ENVIRONMENT_KEY, environment);
267 return mapping.findForward("EnvironmentViews");
268 }
269
270 return null;
271 }
272
273 /**
274 * Load a specified physical environmnet from datastore ad get its configuration
275 * a precise datetime (use startDate, startHour and startMin from form).
276 * @return A forward to the new view to render and display
277 */
278 protected ActionForward getEnvironmentConfigAtDate(ActionMapping mapping, ActionForm form,
279 HttpServletRequest request, HttpServletResponse response)
280 throws Exception{
281 if (form instanceof PhysicalEnvironmentForm){
282
283 PhysicalEnvironmentForm envForm = (PhysicalEnvironmentForm)form;
284
285
286 Calendar date = Calendar.getInstance(request.getLocale());
287 date.setTime(envForm.getStartDate());
288 date.set(Calendar.HOUR_OF_DAY, envForm.getStartHour());
289 date.set(Calendar.MINUTE, envForm.getStartMin());
290
291
292 PhysicalEnvironment view = null;
293 if (!envForm.isFromLogical()){
294 log.debug("Getting environment coniguration for PhysicalEnvironment '" + envForm.getKey() + "'");
295 view = environmentManager.getPhysicalEnvironmentAtDate(envForm.getKey(), date.getTime());
296 request.setAttribute(ENVIRONMENTS_KEY, environmentManager.getPhysicalEnvironments());
297 }
298 else{
299 log.debug("Getting environment configuration for LogicalEnvironment '" + envForm.getKey() + "'");
300
301 LogicalEnvironment logicalEnvironment = environmentManager.getLogicalEnvironment(envForm.getKey());
302 EnvironmentMapping envMapping = environmentManager.getEnvironmentMappingAtDate(logicalEnvironment,
303 date.getTime());
304
305 if (envMapping != null)
306 view = environmentManager.getPhysicalEnvironmentAtDate(envMapping.getPhysicalEnvironment().getKey(),
307 date.getTime());
308 else view = new PhysicalEnvironment();
309
310 request.setAttribute(LOGICAL_ENVIRONMENT_KEY, logicalEnvironment);
311 }
312
313
314 request.setAttribute(ENVIRONMENT_KEY, view);
315 request.setAttribute(VIEW_DATE_KEY, date.getTime());
316 return mapping.findForward("EnvironmentViews");
317 }
318
319 return null;
320 }
321
322
323 /**
324 * Load a specified physical environment from datastore and get its configuration
325 * changes during specified period (use startDate and endDate from form).
326 * @return A forward to the next view to render and display
327 */
328 protected ActionForward getEnvironmentChanges(ActionMapping mapping, ActionForm form,
329 HttpServletRequest request, HttpServletResponse response)
330 throws Exception{
331 if (form instanceof PhysicalEnvironmentForm){
332
333 PhysicalEnvironmentForm envForm = (PhysicalEnvironmentForm)form;
334 PhysicalEnvironmentTrackingView view = environmentManager.getPhysicalEnvironmentTrackingView(envForm.getKey(),
335 envForm.getStartDate(), envForm.getEndDate());
336
337
338 request.setAttribute(TRACKING_VIEW_KEY, view);
339 return mapping.findForward("EnvironmentTrackingView");
340 }
341
342 return null;
343 }
344 }