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 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  //TODO: Action Cleanup
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                  // Need to promote all issues with the deleted severity.  The safest thing to do is
74                  // promote them to the next higher severity.
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                 // Need to demote all issues with the deleted severity.  The safest thing to do is
116                 // move them down one status to make sure they don't skip something important in any
117                 // workflow.
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                 // No need to edit any issues since the resolutions are stored as text in the issue
160             } else {
161                 throw new SystemConfigurationException("Unsupported configuration item type " + configItem.getType().name() + " found.");
162             }
163 
164             configurationService.removeConfigurationItem(configItem.getId());
165             // Now reset the cached items of removed item's type
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