1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web.actions.admin.configuration;
20
21 import org.apache.commons.beanutils.BeanUtils;
22 import org.apache.commons.beanutils.PropertyUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.log4j.Logger;
25 import org.apache.struts.action.*;
26 import org.itracker.SystemConfigurationException;
27 import org.itracker.core.resources.ITrackerResources;
28 import org.itracker.model.*;
29 import org.itracker.model.util.SystemConfigurationUtilities;
30 import org.itracker.services.ConfigurationService;
31 import org.itracker.services.IssueService;
32 import org.itracker.web.actions.base.ItrackerBaseAction;
33 import org.itracker.web.util.LoginUtilities;
34 import org.itracker.web.util.ServletContextUtils;
35
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import java.io.IOException;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.Map;
44
45 public class EditConfigurationAction extends ItrackerBaseAction {
46
47 private static final Logger log = Logger
48 .getLogger(EditConfigurationAction.class);
49
50 @SuppressWarnings("unchecked")
51 public ActionForward execute(ActionMapping mapping, ActionForm form,
52 HttpServletRequest request, HttpServletResponse response)
53 throws ServletException, IOException {
54 ActionMessages errors = new ActionMessages();
55
56
57 User currUser = LoginUtilities.getCurrentUser(request);
58 if (!isTokenValid(request)) {
59 log.debug("Invalid request token while editing configuration.");
60 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
61 "itracker.web.error.transaction"));
62 saveErrors(request, errors);
63 return mapping.getInputForward();
64 }
65 resetToken(request);
66
67 try {
68 final ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
69 .getConfigurationService();
70
71 final String action = (String) PropertyUtils.getSimpleProperty(form,
72 "action");
73 String formValue = (String) PropertyUtils.getSimpleProperty(form,
74 "value");
75
76 String initialLanguageKey = null;
77 Map<String, String> translations = (Map<String, String>) PropertyUtils
78 .getSimpleProperty(form, "translations");
79
80 if (action == null) {
81 return mapping.findForward("listconfiguration");
82 }
83
84 Configuration configItem = null;
85 if ("createresolution".equals(action)) {
86 int value = -1;
87 int order = 0;
88
89 try {
90 List<Configuration> resolutions = configurationService
91 .getConfigurationItemsByType(Configuration.Type.resolution);
92 if (resolutions.size() < 1) {
93
94 value = Math.max(value, 0);
95 }
96 for (Configuration resolution : resolutions) {
97 value = Math.max(value, Integer.parseInt(resolution.getValue()));
98 order = resolution.getOrder();
99 }
100 if (value > -1) {
101 String version = configurationService
102 .getProperty("version");
103 configItem = new Configuration(
104 Configuration.Type.resolution,
105 Integer.toString(++value), version, ++order);
106 }
107 } catch (NumberFormatException nfe) {
108 log.debug("Found invalid value or order for a resolution.",
109 nfe);
110 throw new SystemConfigurationException(
111 "Found invalid value or order for a resolution.");
112 }
113 } else if ("createseverity".equals(action)) {
114 int value = -1;
115 int order = 0;
116
117 try {
118 List<Configuration> severities = configurationService
119 .getConfigurationItemsByType(Configuration.Type.severity);
120 if (severities.size() < 1) {
121
122 value = Math.max(value, 0);
123 }
124 for (Configuration severity : severities) {
125 value = Math.max(value, Integer.parseInt(severity.getValue()));
126 order = severity.getOrder();
127 }
128 if (value > -1) {
129 String version = configurationService
130 .getProperty("version");
131 configItem = new Configuration(
132 Configuration.Type.severity,
133 Integer.toString(++value), version, ++order);
134 }
135 } catch (NumberFormatException nfe) {
136 log.debug("Found invalid value or order for a severity.",
137 nfe);
138 throw new SystemConfigurationException(
139 "Found invalid value or order for a severity.");
140 }
141 } else if ("createstatus".equals(action)) {
142 try {
143 if (null == formValue) {
144
145 throw new SystemConfigurationException(
146 "Supplied status value is null.",
147 "itracker.web.error.validate.required");
148 }
149 int value = Integer.parseInt(formValue);
150 List<Configuration> statuses = configurationService
151 .getConfigurationItemsByType(Configuration.Type.status);
152 for (Configuration status : statuses) {
153 if (value == Integer.parseInt(status
154 .getValue())) {
155 throw new SystemConfigurationException(
156 "Supplied status value already equals existing status.",
157 "itracker.web.error.existingstatus");
158 }
159 }
160
161 String version = configurationService
162 .getProperty("version");
163 configItem = new Configuration(
164 Configuration.Type.status,
165 formValue, version, value);
166 } catch (NumberFormatException nfe) {
167 throw new SystemConfigurationException("Invalid value "
168 + formValue + " for status.",
169 "itracker.web.error.invalidstatus");
170 }
171 } else if ("update".equals(action)) {
172 Integer id = (Integer) PropertyUtils.getSimpleProperty(form,
173 "id");
174 configItem = configurationService.getConfigurationItem(id);
175
176 if (configItem == null) {
177 throw new SystemConfigurationException(
178 "Invalid configuration item id " + id);
179 }
180 formValue = configItem.getValue();
181
182 initialLanguageKey = SystemConfigurationUtilities
183 .getLanguageKey(configItem);
184
185 if (configItem.getType() == Configuration.Type.status
186 && formValue != null && !formValue.equals("")) {
187 if (!configItem.getValue().equalsIgnoreCase(formValue)) {
188 try {
189 int currStatus = Integer.parseInt(configItem
190 .getValue());
191 int newStatus = Integer.parseInt(formValue);
192
193 List<Configuration> statuses = configurationService
194 .getConfigurationItemsByType(Configuration.Type.status);
195 for (Configuration statuse : statuses) {
196 if (newStatus == Integer.parseInt(statuse.getValue())) {
197 throw new SystemConfigurationException(
198 "Supplied status value already equals existing status.",
199 "itracker.web.error.existingstatus");
200 }
201 }
202
203 configItem.setValue(formValue.trim());
204
205 log.debug("Changing issue status values from "
206 + configItem.getValue() + " to "
207 + formValue);
208
209 IssueService issueService = ServletContextUtils.getItrackerServices()
210 .getIssueService();
211
212 List<Issue> issues = issueService
213 .getIssuesWithStatus(currStatus);
214 Issue issue;
215 for (Issue i : issues) {
216 if (i != null) {
217 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 i.getActivities().add(activity);
223 activity.setIssue(i);
224 issue = issueService.systemUpdateIssue(i, currUser.getId());
225 issues.add(issue);
226 }
227 }
228 } catch (NumberFormatException nfe) {
229 throw new SystemConfigurationException(
230 "Invalid value " + formValue
231 + " for updated status.",
232 "itracker.web.error.invalidstatus");
233 }
234 }
235 }
236 } else {
237 throw new SystemConfigurationException("Invalid action "
238 + action + " while editing configuration item.");
239 }
240
241 if (configItem == null) {
242 throw new SystemConfigurationException(
243 "Unable to create new configuration item model.");
244 }
245 if ("update".equals(action)) {
246 configItem = configurationService
247 .updateConfigurationItem(configItem);
248 } else {
249 configItem = configurationService
250 .createConfigurationItem(configItem);
251 }
252
253 if (configItem == null) {
254 throw new SystemConfigurationException(
255 "Unable to create new configuration item.");
256 }
257
258 String key = SystemConfigurationUtilities
259 .getLanguageKey(configItem);
260 log.debug("Processing translations for configuration item "
261 + configItem.getId() + " with key " + key);
262 if (translations != null && StringUtils.isNotBlank(key)) {
263 String locale, translation;
264 Iterator<String> iter = translations.keySet().iterator();
265 configurationService.removeLanguageKey(key);
266 while (iter.hasNext()) {
267 locale = iter.next();
268 if (locale != null) {
269 translation = translations.get(locale);
270 if (StringUtils.isNotBlank(translation)) {
271 log.debug("Adding new translation for locale "
272 + locale + " for " + configItem);
273 configurationService
274 .updateLanguageItem(new Language(locale,
275 key, translation));
276 }
277 }
278 }
279 String baseValue = translations
280 .get(ITrackerResources.BASE_LOCALE);
281 if (StringUtils.isNotBlank(baseValue)) {
282 configurationService.updateLanguageItem(new Language(
283 ITrackerResources.BASE_LOCALE, key, baseValue));
284 }
285
286 if (initialLanguageKey != null
287 && !initialLanguageKey.equals(key)) {
288 configurationService.removeLanguageKey(initialLanguageKey);
289 }
290 ITrackerResources.clearKeyFromBundles(key, true);
291 ITrackerResources.clearKeyFromBundles(initialLanguageKey, true);
292 }
293
294
295 configurationService.resetConfigurationCache(configItem.getType());
296
297 PropertyUtils.setSimpleProperty(form, "value", formValue);
298
299 String pageTitleKey = "";
300 String pageTitleArg = "";
301
302 if ("update".equals(action)) {
303 pageTitleKey = "itracker.web.admin.editconfiguration.title.update";
304 } else {
305 Locale locale = getLocale(request);
306 pageTitleKey = "itracker.web.admin.editconfiguration.title.create";
307 if ("createseverity".equals(BeanUtils.getSimpleProperty(form, "action"))) {
308 pageTitleArg = ITrackerResources.getString(
309 "itracker.web.attr.severity", locale);
310 } else if ("createstatus"
311 .equals(BeanUtils.getSimpleProperty(form, "action"))) {
312 pageTitleArg = ITrackerResources.getString(
313 "itracker.web.attr.status", locale);
314 } else if ("createresolution".equals(BeanUtils.getSimpleProperty(form, "action"))) {
315 pageTitleArg = ITrackerResources.getString(
316 "itracker.web.attr.resolution", locale);
317 } else {
318 return mapping.findForward("unauthorized");
319 }
320 }
321 request.setAttribute("pageTitleKey", pageTitleKey);
322 request.setAttribute("pageTitleArg", pageTitleArg);
323 return mapping.findForward("listconfiguration");
324 } catch (SystemConfigurationException sce) {
325 log.error("Exception processing form data: " + sce.getMessage(),
326 sce);
327 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(sce
328 .getKey()));
329 } catch (Exception e) {
330 log.error("Exception processing form data", e);
331 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
332 "itracker.web.error.system"));
333 }
334
335 if (!errors.isEmpty()) {
336 saveErrors(request, errors);
337 saveToken(request);
338 return mapping.getInputForward();
339 }
340
341 return mapping.findForward("error");
342 }
343 }