View Javadoc

1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.actions.admin.configuration;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Locale;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpSession;
31  
32  import org.apache.commons.beanutils.PropertyUtils;
33  import org.apache.log4j.Logger;
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  import org.apache.struts.action.ActionMessage;
38  import org.apache.struts.action.ActionMessages;
39  import org.itracker.core.resources.ITrackerResources;
40  import org.itracker.model.Configuration;
41  import org.itracker.model.Issue;
42  import org.itracker.model.Language;
43  import org.itracker.model.User;
44  import org.itracker.services.ConfigurationService;
45  import org.itracker.services.IssueService;
46  import org.itracker.services.exceptions.SystemConfigurationException;
47  import org.itracker.services.util.SystemConfigurationUtilities;
48  import org.itracker.web.actions.base.ItrackerBaseAction;
49  import org.itracker.web.util.Constants;
50  
51  public class EditConfigurationAction extends ItrackerBaseAction {
52  
53  	private static final Logger log = Logger
54  			.getLogger(EditConfigurationAction.class);
55  
56  	@SuppressWarnings("unchecked")
57  	public ActionForward execute(ActionMapping mapping, ActionForm form,
58  			HttpServletRequest request, HttpServletResponse response)
59  			throws ServletException, IOException {
60  		ActionMessages errors = new ActionMessages();
61  		// TODO: Action Cleanup
62  
63  		if (!isTokenValid(request)) {
64  			log.debug("Invalid request token while editing configuration.");
65  			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
66  					"itracker.web.error.transaction"));
67  			saveErrors(request, errors);
68  			return mapping.getInputForward();
69  			// return mapping.findForward("listconfiguration");
70  		}
71  		resetToken(request);
72  		HttpSession session = request.getSession(true);
73  
74  		try {
75  			ConfigurationService configurationService = getITrackerServices()
76  					.getConfigurationService();
77  
78  			String action = (String) PropertyUtils.getSimpleProperty(form,
79  					"action");
80  			String formValue = (String) PropertyUtils.getSimpleProperty(form,
81  					"value");
82  
83  			String initialLanguageKey = null;
84  			HashMap<String, String> translations = (HashMap<String, String>) PropertyUtils
85  					.getSimpleProperty(form, "translations");
86  
87  			if (action == null) {
88  				return mapping.findForward("listconfiguration");
89  			}
90  
91  			Configuration configItem = null;
92  			if ("createresolution".equals(action)) {
93  				int value = 0;
94  				int order = 0;
95  
96  				try {
97  					List<Configuration> resolutions = configurationService
98  							.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_RESOLUTION);
99  					for (int i = 0; i < resolutions.size(); i++) {
100 						value = Math.max(value, Integer.parseInt(resolutions
101 								.get(i).getValue()));
102 						order = resolutions.get(i).getOrder();
103 					}
104 					if (value > 0) {
105 						String version = configurationService
106 								.getProperty("version");
107 						configItem = new Configuration(
108 								SystemConfigurationUtilities.TYPE_RESOLUTION,
109 								Integer.toString(++value), version, ++order);
110 					}
111 				} catch (NumberFormatException nfe) {
112 					log.debug("Found invalid value or order for a resolution.",
113 							nfe);
114 					throw new SystemConfigurationException(
115 							"Found invalid value or order for a resolution.");
116 				}
117 			} else if ("createseverity".equals(action)) {
118 				int value = 0;
119 				int order = 0;
120 
121 				try {
122 					List<Configuration> severities = configurationService
123 							.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_SEVERITY);
124 					for (int i = 0; i < severities.size(); i++) {
125 						value = Math.max(value, Integer.parseInt(severities
126 								.get(i).getValue()));
127 						order = severities.get(i).getOrder();
128 					}
129 					if (value > 0) {
130 						String version = configurationService
131 								.getProperty("version");
132 						configItem = new Configuration(
133 								SystemConfigurationUtilities.TYPE_SEVERITY,
134 								Integer.toString(++value), version, ++order);
135 					}
136 				} catch (NumberFormatException nfe) {
137 					log.debug("Found invalid value or order for a severity.",
138 							nfe);
139 					throw new SystemConfigurationException(
140 							"Found invalid value or order for a severity.");
141 				}
142 			} else if ("createstatus".equals(action)) {
143 				try {
144 					int value = Integer.parseInt(formValue);
145 					List<Configuration> statuses = configurationService
146 							.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_STATUS);
147 					for (int i = 0; i < statuses.size(); i++) {
148 						if (value == Integer.parseInt(statuses.get(i)
149 								.getValue())) {
150 							throw new SystemConfigurationException(
151 									"Supplied status value already equals existing status.",
152 									"itracker.web.error.existingstatus");
153 						}
154 					}
155 
156 					String version = configurationService
157 							.getProperty("version");
158 					configItem = new Configuration(
159 							SystemConfigurationUtilities.TYPE_STATUS,
160 							formValue, version, value);
161 				} catch (NumberFormatException nfe) {
162 					throw new SystemConfigurationException("Invalid value "
163 							+ formValue + " for status.",
164 							"itracker.web.error.invalidstatus");
165 				}
166 			} else if ("update".equals(action)) {
167 				Integer id = (Integer) PropertyUtils.getSimpleProperty(form,
168 						"id");
169 				configItem = configurationService.getConfigurationItem(id);
170 
171 				if (configItem == null) {
172 					throw new SystemConfigurationException(
173 							"Invalid configuration item id " + id);
174 				}
175 				formValue = configItem.getValue();
176 
177 				initialLanguageKey = SystemConfigurationUtilities
178 						.getLanguageKey(configItem);
179 
180 				if (configItem.getType() == SystemConfigurationUtilities.TYPE_STATUS
181 						&& formValue != null && !formValue.equals("")) {
182 					if (!configItem.getValue().equalsIgnoreCase(formValue)) {
183 						try {
184 							int currStatus = Integer.parseInt(configItem
185 									.getValue());
186 							int newStatus = Integer.parseInt(formValue);
187 
188 							List<Configuration> statuses = configurationService
189 									.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_STATUS);
190 							for (int i = 0; i < statuses.size(); i++) {
191 								if (newStatus == Integer.parseInt(statuses.get(
192 										i).getValue())) {
193 									throw new SystemConfigurationException(
194 											"Supplied status value already equals existing status.",
195 											"itracker.web.error.existingstatus");
196 								}
197 							}
198 							// set new value
199 							configItem.setValue(formValue.trim());
200 
201 							log.debug("Changing issue status values from "
202 									+ configItem.getValue() + " to "
203 									+ formValue);
204 
205 							User currUser = (User) session
206 									.getAttribute(Constants.USER_KEY);
207 							Integer currUserId = (currUser == null ? Integer
208 									.valueOf(-1) : currUser.getId());
209 
210 							IssueService issueService = getITrackerServices()
211 									.getIssueService();
212 
213 							List<Issue> issues = issueService
214 									.getIssuesWithStatus(currStatus);
215 							for (int i = 0; i < issues.size(); i++) {
216 								if (issues.get(i) != null) {
217 									issues.get(i).setStatus(newStatus);
218 									// IssueActivity activity = new
219 									// IssueActivity();
220 									// activity.setActivityType(IssueActivityType.SYSTEM_UPDATE);
221 									// activity.setDescription(ITrackerResources.getString("itracker.activity.system.status"));
222 									// issues.get(i).getActivities().add(activity);
223 									// TODO: is this inteded?
224 									issues.add(issueService.updateIssue(issues
225 											.get(i), currUserId));
226 
227 									// TODO: need to fix this RJST
228 									// activity.setIssue(issues.get(i));
229 								}
230 							}
231 						} catch (NumberFormatException nfe) {
232 							throw new SystemConfigurationException(
233 									"Invalid value " + formValue
234 											+ " for updated status.",
235 									"itracker.web.error.invalidstatus");
236 						}
237 					}
238 				}
239 			} else {
240 				throw new SystemConfigurationException("Invalid action "
241 						+ action + " while editing configuration item.");
242 			}
243 
244 			if (configItem == null) {
245 				throw new SystemConfigurationException(
246 						"Unable to create new configuration item model.");
247 			}
248 			if ("update".equals(action)) {
249 				configItem = configurationService
250 						.updateConfigurationItem(configItem);
251 			} else {
252 				configItem = configurationService
253 						.createConfigurationItem(configItem);
254 			}
255 
256 			if (configItem == null) {
257 				throw new SystemConfigurationException(
258 						"Unable to create new configuration item.");
259 			}
260 
261 			String key = SystemConfigurationUtilities
262 					.getLanguageKey(configItem);
263 			log.debug("Processing translations for configuration item "
264 					+ configItem.getId() + " with key " + key);
265 			if (translations != null && key != null && !key.equals("")) {
266 				String locale, translation;
267 				Iterator<String> iter = translations.keySet().iterator();
268 				configurationService.removeLanguageKey(key);
269 				while (iter.hasNext()) {
270 					locale = iter.next();
271 					if (locale != null) {
272 						translation = translations.get(locale);
273 						if (translation != null && !translation.equals("")) {
274 							log.debug("Adding new translation for locale "
275 									+ locale + " for " + configItem);
276 							configurationService
277 									.updateLanguageItem(new Language(locale,
278 											key, translation));
279 						}
280 					}
281 				}
282 				String baseValue = translations
283 						.get(ITrackerResources.BASE_LOCALE);
284 				configurationService.updateLanguageItem(new Language(
285 						ITrackerResources.BASE_LOCALE, key, baseValue));
286 				// remove old languageItems if resource key has changed
287 				if (initialLanguageKey != null
288 						&& !initialLanguageKey.equals(key)) {
289 					configurationService.removeLanguageKey(initialLanguageKey);
290 				}
291 				ITrackerResources.clearKeyFromBundles(key, true);
292 				ITrackerResources.clearKeyFromBundles(initialLanguageKey, true);
293 			}
294 
295 			// Now reset the cached versions in IssueUtilities
296 			configurationService.resetConfigurationCache(configItem.getType());
297 
298 			request.setAttribute("action", action);
299 			request.setAttribute("value", formValue);
300 			String pageTitleKey = "";
301 			String pageTitleArg = "";
302 			boolean isUpdate = false;
303 
304 			if ("update".equals(request.getAttribute("action"))) {
305 				isUpdate = true;
306 				pageTitleKey = "itracker.web.admin.editconfiguration.title.update";
307 			} else {
308 				Locale locale = getLocale(request);
309 				pageTitleKey = "itracker.web.admin.editconfiguration.title.create";
310 				if ("createseverity".equals(request.getAttribute("action"))) {
311 					pageTitleArg = ITrackerResources.getString(
312 							"itracker.web.attr.severity", locale);
313 				} else if ("createstatus"
314 						.equals(request.getAttribute("action"))) {
315 					pageTitleArg = ITrackerResources.getString(
316 							"itracker.web.attr.status", locale);
317 				} else if ("createresolution".equals(request
318 						.getAttribute("action"))) {
319 					pageTitleArg = ITrackerResources.getString(
320 							"itracker.web.attr.resolution", locale);
321 				} else {
322 					return mapping.findForward("unauthorized");
323 				}
324 			}
325 			request.setAttribute("isUpdate", Boolean.valueOf(isUpdate));
326 			request.setAttribute("pageTitleKey", pageTitleKey);
327 			request.setAttribute("pageTitleArg", pageTitleArg);
328 			return mapping.findForward("listconfiguration");
329 		} catch (SystemConfigurationException sce) {
330 			log.error("Exception processing form data: " + sce.getMessage(),
331 					sce);
332 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(sce
333 					.getKey()));
334 		} catch (Exception e) {
335 			log.error("Exception processing form data", e);
336 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
337 					"itracker.web.error.system"));
338 		}
339 
340 		if (!errors.isEmpty()) {
341 			saveErrors(request, errors);
342 			saveToken(request);
343 			return mapping.getInputForward();
344 		}
345 
346 		return mapping.findForward("error");
347 	}
348 }