1 /**
2 * Copyright 2005-2008 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.EnvironmentMappingForm;
19 import org.figure8.join.businessobjects.environment.EnvironmentMapping;
20 import org.figure8.join.businessobjects.environment.LogicalEnvironment;
21 import org.figure8.join.businessobjects.environment.PhysicalEnvironment;
22 import org.figure8.join.businessfacades.environment.EnvironmentManager;
23 import org.figure8.join.util.LogUtil;
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 import java.util.ArrayList;
35 /**
36 * This is a Struts action for managing all operations related mappings between Logical and Physical environments.
37 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
38 * @version $Revision: 1.2 $
39 *
40 * @struts.action path="/environmentMapping" name="envMappingForm"
41 * scope="request" parameter="op" validate="true"
42 * input="/pages/mainpage.jsp?body=/jsp/environment/environmentmappings.jsp"
43 * @struts.action-forward name="Environments" path="/action/logicalEnvironment?op=view"
44 * @struts.action-forward name="EnvironmentMapping" path="/jsp/environment/environmentmapping.jsp"
45 * @struts.action-forward name="EnvironmentMappings" path="/jsp/environment/environmentmappings.jsp"
46 */
47 public class EnvironmentMappingActions extends JoinAction{
48
49
50
51 /** Get a commons logger */
52 private static final Log log = LogUtil.getLog(EnvironmentMappingActions.class);
53
54 /** Operation code for showing an environment mapping form */
55 public static final String FORM_OP = "form";
56 /** Operation code for loading a specific EnvironmentMapping */
57 public static final String LOAD_OP = "load";
58 /** Operation code for saving a EnvironmentMapping (create) */
59 public static final String CREATE_OP = "create";
60 /** Operation code for closing a EnvironmentMapping (close) */
61 public static final String CLOSE_OP = "close";
62 /** Operation code for retrieving EnvironmentMappings for an environment (logical or physical) */
63 public static final String VIEW_OP = "view";
64
65 /**
66 * The session scope attribute under which the EnvironmentMapping object
67 * currently selected by our logged-in User is stored.
68 */
69 public static final String MAPPING_KEY = "mapping";
70 /**
71 * The request scope attribute under which the retrieved EnvironmentMappings
72 * list is stored.
73 */
74 public static final String MAPPINGS_KEY = "mappings";
75
76
77
78
79 /** The EnvironmentManager to use. */
80 private EnvironmentManager environmentManager = null;
81
82
83
84
85 /** Creates a new instance of EnvironmentMappingActions */
86 public EnvironmentMappingActions(){
87 }
88
89
90
91
92 /** @param envManager The EnvironmentManager implementation instance to use */
93 public void setEnvironmentManager(EnvironmentManager envManager){
94 this.environmentManager = envManager;
95 }
96
97
98
99
100 /**
101 *
102 * @param operation String representing the operation to invoke on Action
103 * @param mapping Mapping between forwards name and path for this action
104 * @param form The form object containing request parameters
105 * @param request The servlet container request wrapper
106 * @param response The servlet container response wrapper
107 * @return A forward to the next view to render and display
108 * @throws Exception such as InfraStructureExceptions ...
109 */
110 public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form,
111 HttpServletRequest request, HttpServletResponse response)
112 throws Exception{
113
114 log.debug("doExecute() called for '" + operation + "' operation");
115
116 if (FORM_OP.equals(operation)){
117 return showMappingForm(mapping, form, request, response);
118 }
119 else if (LOAD_OP.equals(operation)){
120 return loadMapping(mapping, form, request, response);
121 }
122 else if (CREATE_OP.equals(operation)){
123 return createMapping(mapping, form, request, response);
124 }
125 else if (CLOSE_OP.equals(operation)){
126 return closeMapping(mapping, form, request, response);
127 }
128 else if (VIEW_OP.equals(operation)){
129 return loadEnvironmentMappings(mapping, form, request, response);
130 }
131
132 return null;
133 }
134
135
136
137
138 /**
139 * Prepare all we need for displaying a mapping creation form
140 * @return A forward to the next view to render and display
141 */
142 protected ActionForward showMappingForm(ActionMapping mapping, ActionForm form,
143 HttpServletRequest request, HttpServletResponse response)
144 throws Exception{
145
146 if (form instanceof EnvironmentMappingForm){
147
148 EnvironmentMappingForm mForm = (EnvironmentMappingForm)form;
149 LogicalEnvironment logicalEnvironment = environmentManager.getLogicalEnvironment(mForm.getLogicalEnvKey());
150
151
152 List candidates = new ArrayList();
153 List environments = environmentManager.getPhysicalEnvironments();
154 for (int i=0; i<environments.size(); i++){
155 PhysicalEnvironment environment = (PhysicalEnvironment)environments.get(i);
156 if (environment.getActiveEnvironmentMapping() == null)
157 candidates.add(environment);
158 }
159
160 request.setAttribute(LogicalEnvironmentActions.ENVIRONMENT_KEY, logicalEnvironment);
161 request.setAttribute(PhysicalEnvironmentActions.ENVIRONMENTS_KEY, candidates);
162
163
164 if (logicalEnvironment.getActiveEnvironmentMapping() != null){
165 EnvironmentMapping envMapping = logicalEnvironment.getActiveEnvironmentMapping();
166 request.setAttribute(MAPPING_KEY, envMapping);
167 }
168 return mapping.findForward("EnvironmentMapping");
169 }
170
171 return null;
172 }
173
174 /**
175 * Load a specified environment mapping from datastore and fill form with it.
176 * @return A forward to the next view to render and display
177 */
178 protected ActionForward loadMapping(ActionMapping mapping, ActionForm form,
179 HttpServletRequest request, HttpServletResponse response)
180 throws Exception{
181
182 if (form instanceof EnvironmentMappingForm){
183
184 EnvironmentMappingForm mForm = (EnvironmentMappingForm)form;
185 EnvironmentMapping envMapping = environmentManager.getEnvironmentMapping(mForm.getId());
186
187
188 request.setAttribute(MAPPING_KEY, envMapping);
189 return mapping.findForward("EnvironmentMapping");
190 }
191
192 return null;
193 }
194
195 /**
196 * Create an environment mapping into datastore and bind specified logical
197 * and physical environments to it.
198 * @return A forward to the next view to render and display
199 */
200 protected ActionForward createMapping(ActionMapping mapping, ActionForm form,
201 HttpServletRequest request, HttpServletResponse response)
202 throws Exception{
203
204 if (form instanceof EnvironmentMappingForm){
205
206 EnvironmentMappingForm mForm = (EnvironmentMappingForm)form;
207 LogicalEnvironment logicalEnv = environmentManager.getLogicalEnvironment(mForm.getLogicalEnvKey());
208 PhysicalEnvironment physicalEnv = environmentManager.getPhysicalEnvironment(mForm.getPhysicalEnvKey());
209
210
211 log.info("Creating a mapping for " + logicalEnv.getKey() + " logical environment onto "
212 + physicalEnv.getKey() + " physical one");
213 environmentManager.mapEnvironments(logicalEnv, physicalEnv);
214
215
216 request.setAttribute(LogicalEnvironmentActions.ENVIRONMENT_KEY, logicalEnv);
217 return mapping.findForward("Environments");
218 }
219
220 return null;
221 }
222
223 /**
224 * Close an environment mapping and update the related logical and physical environments.
225 * @return A forward to the next view to render and display
226 */
227 protected ActionForward closeMapping(ActionMapping mapping, ActionForm form,
228 HttpServletRequest request, HttpServletResponse response)
229 throws Exception{
230
231 if (form instanceof EnvironmentMappingForm){
232
233 EnvironmentMappingForm mForm = (EnvironmentMappingForm)form;
234 EnvironmentMapping envMapping = environmentManager.getEnvironmentMapping(mForm.getId());
235
236
237 log.info("Closing the environment mapping having identifier '" + envMapping.getId() + "'");
238 environmentManager.closeEnvironmentMapping(envMapping);
239
240
241 request.setAttribute(LogicalEnvironmentActions.ENVIRONMENT_KEY, envMapping.getLogicalEnvironment());
242 return mapping.findForward("Environments");
243 }
244
245 return null;
246 }
247
248 /**
249 * Retrieve and load into request all the environment mappings history corresponding to
250 * a logical or a physical environment.
251 * @return A forward to the next view to render and display
252 */
253 protected ActionForward loadEnvironmentMappings(ActionMapping mapping, ActionForm form,
254 HttpServletRequest request, HttpServletResponse response)
255 throws Exception{
256
257 if (form instanceof EnvironmentMappingForm){
258
259 EnvironmentMappingForm mForm = (EnvironmentMappingForm)form;
260 List mappings = null;
261
262 if (mForm.getLogicalEnvKey() != null){
263
264 log.info("Retrieving the EnvironmentMapping history for logical environment: " + mForm.getLogicalEnvKey());
265 LogicalEnvironment logicalEnv = environmentManager.getLogicalEnvironment(mForm.getLogicalEnvKey());
266 mappings = environmentManager.getEnvironmentMappings(logicalEnv);
267
268 request.setAttribute("criterion", "logicalenv");
269 request.setAttribute("environment", logicalEnv);
270 request.setAttribute("title.key", "title.envmappings.bylogicalenv");
271 }
272 else if (mForm.getPhysicalEnvKey() != null){
273
274 log.info("Retrieving the EnvironmentMapping history for physical environment: "
275 + mForm.getPhysicalEnvKey());
276 PhysicalEnvironment physicalEnv = environmentManager.getPhysicalEnvironment(mForm.getPhysicalEnvKey());
277 mappings = environmentManager.getEnvironmentMappings(physicalEnv);
278
279 request.setAttribute("criterion", "physicalenv");
280 request.setAttribute("environment", physicalEnv);
281 request.setAttribute("title.key", "title.envmappings.byphysicalenv");
282 }
283
284
285 request.setAttribute(MAPPINGS_KEY, mappings);
286 return mapping.findForward("EnvironmentMappings");
287 }
288
289 return null;
290 }
291 }