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.ParameterForm;
19 import org.figure8.join.core.DuplicateEntityException;
20 import org.figure8.join.businessobjects.environment.Parameter;
21 import org.figure8.join.businessfacades.environment.ParameterManager;
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.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import java.util.List;
34 /**
35 * Struts action used for managing the parameters used by software
36 * project integration team for delpoyments.
37 * @author <a href="mailto:jerome.evrard@gmail.com">Jerome Evrard</a>
38 * @version $Revision: 1.2 $
39 *
40 * @struts.action path="/parameter" name="parameterForm"
41 * scope="request" parameter="op" validate="true"
42 * input="/pages/mainpage.jsp?body=/jsp/environment/parameters.jsp"
43 * @struts.action-forward name="Next" path="/jsp/environment/parameters.jsp"
44 * @struts.action-forward name="Values" path="/action/parameterValue?op=null"
45 */
46 public class ParameterActions extends JoinAction{
47
48
49
50 /** Get a commons logger. */
51 protected static Log log = LogUtil.getLog(ParameterActions.class);
52
53 /** Operation code for loading a specific Parameter */
54 public static final String LOAD_OP = "load";
55 /** Operation code to create a specific Parameter */
56 public static final String SAVE_OP = "save";
57 /** Operation code to remove a specific Parameter */
58 public static final String REMOVE_OP = "remove";
59
60 /** Add a parameter from the parameter values page */
61 public static final String FROM_VALUES_OP = "fromValues";
62
63 /** The parameter name key */
64 public static final String NAME_KEY = "name";
65 /** The key used for request scope attribute containing loaded parameter */
66 public static final String PARAMETER_KEY = "parameter";
67 /** The key used for request scope attribute containing parameters list */
68 public static final String PARAMETERS_KEY = "parameters";
69 /** Attribute to forward to parameters values page on parameter adding */
70 public static final String FORWARD_TO_VALUES_KEY = "forwardToValues";
71
72 /**
73 * The request scope attribute under which is stored the Parameters that
74 * has raised a DuplicateEntityException during save of another one.
75 */
76 public static final String DUPLICATE_PARAMETER_KEY = "duplicate";
77
78
79
80
81 /** <code>manager</code>: The parameter manager */
82 private ParameterManager parameterManager;
83
84
85
86
87 /** Creates a new instance of ParameterActions. */
88 public ParameterActions(){
89 }
90
91
92
93
94 /** @param manager The parameterManager to set. */
95 public void setParameterManager(ParameterManager manager){
96 this.parameterManager = manager;
97 }
98
99
100
101
102 /**
103 * Action to manage parameters.
104 * @param operation String representing the operation to invoke on Action
105 * @param mapping Mapping between forwards name and path for this action
106 * @param form The form object containing request parameters
107 * @param request The servlet container request wrapper
108 * @param response The servlet container response wrapper
109 * @return A forward to the next view to render and display
110 */
111 public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
112 throws Exception{
113
114 if (form instanceof ParameterForm){
115
116 ParameterForm pForm = (ParameterForm)form;
117
118
119 String forward = "Next";
120 request.setAttribute(FORWARD_TO_VALUES_KEY, "false");
121
122 if (LOAD_OP.equalsIgnoreCase(operation)){
123
124 Parameter parameter = loadParameter(pForm.getName());
125 request.setAttribute(PARAMETER_KEY, parameter);
126
127 PropertyUtils.copyProperties(pForm, parameter);
128 }
129 else if (SAVE_OP.equalsIgnoreCase(operation)){
130
131 try {saveParameter(pForm.getName(), pForm.getDescription());}
132 catch (DuplicateEntityException dee){
133
134 request.setAttribute(DUPLICATE_PARAMETER_KEY, dee.getOriginalEntity());
135 return mapping.findForward(forward);
136 }
137
138 removeObsoleteForm(mapping, request);
139
140 if ("true".equalsIgnoreCase(request.getParameter("goToValues")))
141 forward = "Values";
142 }
143 else if (REMOVE_OP.equalsIgnoreCase(operation)){
144
145 removeParameter(pForm.getName());
146 }
147 else if (FROM_VALUES_OP.equalsIgnoreCase(operation)){
148
149 request.setAttribute(FORWARD_TO_VALUES_KEY, "true");
150 }
151
152
153 request.setAttribute(PARAMETERS_KEY, loadParameters());
154 return mapping.findForward(forward);
155 }
156
157 return null;
158 }
159
160
161
162
163 /**
164 * Load all the existing parameters
165 * @return The parameters list.
166 */
167 private List loadParameters(){
168 return parameterManager.getAllParameters();
169 }
170
171 /**
172 * Find a parameter specified by its name.
173 * @param name The parameter name to load.
174 * @return The parameter found or null if not exists.
175 */
176 protected Parameter loadParameter(String name){
177 return parameterManager.getParameter(name);
178 }
179
180 /**
181 * Create a new parameter.
182 * @param name The parameter name.
183 * @param description The parameter description.
184 */
185 protected void saveParameter(String name, String description) throws DuplicateEntityException{
186
187 Parameter parameter = loadParameter(name);
188 if (parameter == null)
189 parameter = new Parameter(name, description);
190 parameter.setDescription(description);
191
192 log.info("Saving parameter '" + name + "'");
193 parameterManager.saveParameter(parameter);
194 }
195
196 /**
197 * Remove a parameter.
198 * @param name The parameter name to remove.
199 */
200 private void removeParameter(String name){
201 parameterManager.removeParameter(loadParameter(name));
202 }
203 }