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.PropertyUtils;
22 import org.apache.log4j.Logger;
23 import org.apache.struts.action.*;
24 import org.itracker.core.resources.ITrackerResources;
25 import org.itracker.model.Configuration;
26 import org.itracker.model.Issue;
27 import org.itracker.SystemConfigurationException;
28 import org.itracker.model.User;
29 import org.itracker.model.util.IssueUtilities;
30 import org.itracker.services.ConfigurationService;
31 import org.itracker.services.IssueService;
32 import org.itracker.model.util.UserUtilities;
33 import org.itracker.web.actions.base.ItrackerBaseAction;
34 import org.itracker.web.util.Constants;
35
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import javax.servlet.http.HttpSession;
40 import java.io.IOException;
41 import java.util.List;
42
43
44 public class RemoveConfigurationItemAction extends ItrackerBaseAction {
45 private static final Logger log = Logger.getLogger(RemoveConfigurationItemAction.class);
46
47
48 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49 ActionMessages errors = new ActionMessages();
50
51
52 if (!hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
53 return mapping.findForward("unauthorized");
54 }
55
56 try {
57 ConfigurationService configurationService = getITrackerServices().getConfigurationService();
58
59 Integer configId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
60 if (configId == null || configId <= 0) {
61 throw new SystemConfigurationException("Invalid configuration id.");
62 }
63
64 Configuration configItem = configurationService.getConfigurationItem(configId);
65 if (configItem == null) {
66 throw new SystemConfigurationException("Invalid configuration id.");
67 }
68
69 String key;
70 if (configItem.getType() == Configuration.Type.severity) {
71 key = ITrackerResources.KEY_BASE_SEVERITY + configItem.getValue();
72
73
74
75 try {
76 String currConfigValue = configItem.getValue();
77 String newConfigValue = null;
78
79 List<Configuration> configItems = configurationService.getConfigurationItemsByType(Configuration.Type.severity);
80 for (int i = 0; i < configItems.size(); i++) {
81 if (configItems.get(i) != null && configId.equals(configItems.get(i).getId())) {
82 if (i == 0 && (i + 1) < configItems.size()) {
83 newConfigValue = configItems.get(i + 1).getValue();
84 break;
85 } else if (i > 0) {
86 newConfigValue = configItems.get(i - 1).getValue();
87 break;
88 }
89 }
90 }
91
92 int currSeverity = Integer.parseInt(currConfigValue);
93 int newSeverity = Integer.parseInt(newConfigValue);
94 log.debug("Promoting issues in severity " + IssueUtilities.getSeverityName(currSeverity) + " to " + IssueUtilities.getSeverityName(newSeverity));
95
96 HttpSession session = request.getSession(true);
97 User currUser = (User) session.getAttribute(Constants.USER_KEY);
98 Integer currUserId = (currUser == null ? -1 : currUser.getId());
99
100 IssueService issueService = getITrackerServices().getIssueService();
101 List<Issue> issues = issueService.getIssuesWithSeverity(currSeverity);
102 for (int i = 0; i < issues.size(); i++) {
103 if (issues.get(i) != null) {
104 issues.get(i).setSeverity(newSeverity);
105
106 issues.set(i, issueService.systemUpdateIssue(issues.get(i), currUserId));
107 }
108 }
109 } catch (Exception e) {
110 log.debug("Exception while promoting issues with severity " + configItem.getValue(), e);
111 }
112 } else if (configItem.getType() == Configuration.Type.status) {
113 key = ITrackerResources.KEY_BASE_STATUS + configItem.getValue();
114
115
116
117
118
119 try {
120 String currConfigValue = configItem.getValue();
121 String newConfigValue = null;
122
123 List<Configuration> configItems = configurationService.getConfigurationItemsByType(Configuration.Type.status);
124 for (int i = 0; i < configItems.size(); i++) {
125 if (configItems.get(i) != null && configId.equals(configItems.get(i).getId())) {
126 if (i == 0 && (i + 1) < configItems.size()) {
127 newConfigValue = configItems.get(i + 1).getValue();
128 break;
129 } else if (i > 0) {
130 newConfigValue = configItems.get(i - 1).getValue();
131 break;
132 }
133 }
134 }
135
136 int currStatus = Integer.parseInt(currConfigValue);
137 int newStatus = Integer.parseInt(newConfigValue);
138 log.debug("Promoting issues in status " + IssueUtilities.getStatusName(currStatus) + " to " + IssueUtilities.getStatusName(newStatus));
139
140 HttpSession session = request.getSession(true);
141 User currUser = (User) session.getAttribute(Constants.USER_KEY);
142 Integer currUserId = (currUser == null ? -1 : currUser.getId());
143
144 IssueService issueService = getITrackerServices().getIssueService();
145 List<Issue> issues = issueService.getIssuesWithStatus(currStatus);
146 for (int i = 0; i < issues.size(); i++) {
147 if (issues.get(i) != null) {
148 issues.get(i).setStatus(newStatus);
149
150 issues.set(i, issueService.systemUpdateIssue(issues.get(i), currUserId));
151 }
152 }
153 } catch (Exception e) {
154 log.debug("Exception while promoting issues with status " + configItem.getValue(), e);
155 }
156 } else if (configItem.getType() == Configuration.Type.resolution) {
157 key = ITrackerResources.KEY_BASE_RESOLUTION + configItem.getValue();
158
159
160 } else {
161 throw new SystemConfigurationException("Unsupported configuration item type " + configItem.getType().name() + " found.");
162 }
163
164 configurationService.removeConfigurationItem(configItem.getId());
165
166 configurationService.resetConfigurationCache(configItem.getType());
167 configurationService.removeLanguageKey(key);
168 ITrackerResources.clearKeyFromBundles(key, false);
169
170
171 return mapping.findForward("listconfiguration");
172 } catch (SystemConfigurationException sce) {
173 log.debug(sce.getMessage(), sce);
174 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
175 } catch (NumberFormatException nfe) {
176 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
177 log.debug("Invalid configuration item id " + request.getParameter("id") + " specified.");
178 } catch (Exception e) {
179 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
180 log.error("System Error.", e);
181 }
182 if (!errors.isEmpty()) {
183 saveErrors(request, errors);
184 }
185 return mapping.findForward("error");
186 }
187
188 }
189