KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > RemoveConfigurationItemAction


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 cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.commons.beanutils.*;
31 import org.apache.struts.action.*;
32 import org.apache.struts.util.*;
33
34 import cowsultants.itracker.ejb.client.exceptions.*;
35 import cowsultants.itracker.ejb.client.interfaces.*;
36 import cowsultants.itracker.ejb.client.models.*;
37 import cowsultants.itracker.ejb.client.resources.*;
38 import cowsultants.itracker.ejb.client.util.*;
39 import cowsultants.itracker.web.scheduler.*;
40 import cowsultants.itracker.web.util.*;
41
42
43 public class RemoveConfigurationItemAction extends ITrackerAction {
44
45     public RemoveConfigurationItemAction() {
46     }
47
48     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49         ActionErrors errors = new ActionErrors();
50
51         if(! isLoggedIn(request, response)) {
52             return mapping.findForward("login");
53         }
54
55         if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
56             return mapping.findForward("unauthorized");
57         }
58
59         try {
60             InitialContext ic = new InitialContext();
61
62             Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
63             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
64             SystemConfiguration sc = scHome.create();
65
66             Integer JavaDoc configId = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "id");
67             if(configId == null || configId.intValue() <= 0) {
68                 throw new SystemConfigurationException("Invalid configuration id.");
69             }
70
71             ConfigurationModel configItem = sc.getConfigurationItem(configId);
72             if(configItem == null) {
73                 throw new SystemConfigurationException("Invalid configuration id.");
74             }
75
76             String JavaDoc key = null;
77             if(configItem.getType() == SystemConfigurationUtilities.TYPE_SEVERITY) {
78                 key = ITrackerResources.KEY_BASE_SEVERITY + configItem.getValue();
79
80                 // Need to promote all issues with the deleted severity. The safest thing to do is
81
// promote them to the next higher severity.
82
try {
83                     String JavaDoc currConfigValue = configItem.getValue();
84                     String JavaDoc newConfigValue = null;
85
86                     ConfigurationModel[] configItems = sc.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_SEVERITY);
87                     for(int i = 0; i < configItems.length; i++) {
88                         if(configItems[i] != null && configId.equals(configItems[i].getId())) {
89                             if(i == 0 && (i + 1) < configItems.length) {
90                                 newConfigValue = configItems[i + 1].getValue();
91                                 break;
92                             } else if(i > 0) {
93                                 newConfigValue = configItems[i - 1].getValue();
94                                 break;
95                             }
96                         }
97                     }
98
99                     int currSeverity = Integer.parseInt(currConfigValue);
100                     int newSeverity = Integer.parseInt(newConfigValue);
101                     Logger.logDebug("Promoting issues in severity " + IssueUtilities.getSeverityName(currSeverity) + " to " + IssueUtilities.getSeverityName(newSeverity));
102
103                     HttpSession session = request.getSession(true);
104                     UserModel currUser = (UserModel) session.getAttribute(Constants.USER_KEY);
105                     Integer JavaDoc currUserId = (currUser == null ? new Integer JavaDoc(-1) : currUser.getId());
106
107                     Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
108                     IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
109                     IssueHandler ih = ihHome.create();
110                     IssueModel[] issues = ih.getIssuesWithSeverity(currSeverity);
111                     for(int i = 0; i < issues.length; i++) {
112                         if(issues[i] != null) {
113                             issues[i].setSeverity(newSeverity);
114                             issues[i] = ih.updateIssue(issues[i], currUserId);
115                             IssueActivityModel activity = new IssueActivityModel(IssueUtilities.ACTIVITY_SYSTEM_UPDATE,
116                                 ITrackerResources.getString("itracker.activity.system.severity"), issues[i].getId(), currUserId);
117                             ih.addIssueActivity(activity);
118                         }
119                     }
120                 } catch(Exception JavaDoc e) {
121                     Logger.logDebug("Exception while promoting issues with severity " + configItem.getValue(), e);
122                 }
123             } else if(configItem.getType() == SystemConfigurationUtilities.TYPE_STATUS) {
124                 key = ITrackerResources.KEY_BASE_STATUS + configItem.getValue();
125
126                 // Need to demote all issues with the deleted severity. The safest thing to do is
127
// move them down one status to make sure they don't skip something important in any
128
// workflow.
129

130                 try {
131                     String JavaDoc currConfigValue = configItem.getValue();
132                     String JavaDoc newConfigValue = null;
133
134                     ConfigurationModel[] configItems = sc.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_STATUS);
135                     for(int i = 0; i < configItems.length; i++) {
136                         if(configItems[i] != null && configId.equals(configItems[i].getId())) {
137                             if(i == 0 && (i + 1) < configItems.length) {
138                                 newConfigValue = configItems[i + 1].getValue();
139                                 break;
140                             } else if(i > 0) {
141                                 newConfigValue = configItems[i - 1].getValue();
142                                 break;
143                             }
144                         }
145                     }
146
147                     int currStatus = Integer.parseInt(currConfigValue);
148                     int newStatus = Integer.parseInt(newConfigValue);
149                     Logger.logDebug("Promoting issues in status " + IssueUtilities.getStatusName(currStatus) + " to " + IssueUtilities.getStatusName(newStatus));
150
151                     HttpSession session = request.getSession(true);
152                     UserModel currUser = (UserModel) session.getAttribute(Constants.USER_KEY);
153                     Integer JavaDoc currUserId = (currUser == null ? new Integer JavaDoc(-1) : currUser.getId());
154
155                     Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
156                     IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
157                     IssueHandler ih = ihHome.create();
158                     IssueModel[] issues = ih.getIssuesWithStatus(currStatus);
159                     for(int i = 0; i < issues.length; i++) {
160                         if(issues[i] != null) {
161                             issues[i].setStatus(newStatus);
162                             issues[i] = ih.updateIssue(issues[i], currUserId);
163                             IssueActivityModel activity = new IssueActivityModel(IssueUtilities.ACTIVITY_SYSTEM_UPDATE,
164                                 ITrackerResources.getString("itracker.activity.system.status"), issues[i].getId(), currUserId);
165                             ih.addIssueActivity(activity);
166                         }
167                     }
168                 } catch(Exception JavaDoc e) {
169                     Logger.logDebug("Exception while promoting issues with status " + configItem.getValue(), e);
170                 }
171             } else if(configItem.getType() == SystemConfigurationUtilities.TYPE_RESOLUTION) {
172                 key = ITrackerResources.KEY_BASE_RESOLUTION + configItem.getValue();
173
174                 // No need to edit any issues since the resolutions are stored as text in the issue
175
} else {
176                 throw new SystemConfigurationException("Unsupported configuration item type " + configItem.getType() + " found.");
177             }
178
179             sc.removeConfigurationItem(configItem.getId());
180             if(key != null) {
181                 sc.removeLanguageKey(key);
182                 ITrackerResources.clearKeyFromBundles(key, false);
183             }
184
185             return mapping.findForward("listconfiguration");
186         } catch(SystemConfigurationException sce) {
187             Logger.logDebug(sce.getMessage(), sce);
188             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidconfiguration"));
189         } catch(NumberFormatException JavaDoc nfe) {
190             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidconfiguration"));
191             Logger.logDebug("Invalid configuration item id " + request.getParameter("id") + " specified.");
192         } catch(Exception JavaDoc e) {
193             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
194             Logger.logError("System Error.", e);
195         }
196         if(! errors.isEmpty()) {
197             saveErrors(request, errors);
198         }
199         return mapping.findForward("error");
200     }
201
202 }
203   
Popular Tags