KickJava   Java API By Example, From Geeks To Geeks.

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


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.upload.*;
33 import org.apache.struts.util.*;
34
35 import cowsultants.itracker.ejb.client.exceptions.*;
36 import cowsultants.itracker.ejb.client.interfaces.*;
37 import cowsultants.itracker.ejb.client.models.*;
38 import cowsultants.itracker.ejb.client.util.*;
39 import cowsultants.itracker.web.util.*;
40
41
42 public class EditIssueAction extends ITrackerAction {
43
44     public EditIssueAction() {
45     }
46
47     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48         ActionErrors errors = new ActionErrors();
49
50         if(! isLoggedIn(request, response)) {
51             return mapping.findForward("login");
52         }
53         if(! isTokenValid(request)) {
54             Logger.logDebug("Invalid request token while editing issue.");
55             return mapping.findForward("listprojects");
56         }
57         resetToken(request);
58
59         try {
60             InitialContext ic = new InitialContext();
61             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
62             IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
63             IssueHandler ih = ihHome.create();
64
65             Object JavaDoc phRef = ic.lookup("java:comp/env/" + ProjectHandler.JNDI_NAME);
66             ProjectHandlerHome phHome = (ProjectHandlerHome) PortableRemoteObject.narrow(phRef, ProjectHandlerHome.class);
67             ProjectHandler ph = phHome.create();
68
69             HttpSession session = request.getSession(true);
70             UserModel currUser = (UserModel) session.getAttribute(Constants.USER_KEY);
71             HashMap currPermissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY);
72             Locale currLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
73             Integer JavaDoc currUserId = currUser.getId();
74
75             Integer JavaDoc issueId = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "id");
76             if(issueId == null) {
77                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidissue"));
78                 return mapping.findForward("error");
79             }
80
81             int previousStatus = -1;
82             IssueModel issue = ih.getIssue(issueId);
83
84             if(issue == null || issue.getId() == null || issue.getId().intValue() < 0) {
85                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidissue"));
86                 return mapping.findForward("error");
87             }
88
89             ProjectModel project = issue.getProject();
90             if(project == null) {
91                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidproject"));
92                 return mapping.findForward("error");
93             } else if(project.getStatus() != ProjectUtilities.STATUS_ACTIVE) {
94                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.projectlocked"));
95                 return mapping.findForward("error");
96             } else if(! IssueUtilities.canEditIssue(issue, currUserId, currPermissions)) {
97                 return mapping.findForward("unauthorized");
98             } else if(UserUtilities.hasPermission(currPermissions, project.getId(), UserUtilities.PERMISSION_EDIT_FULL)) {
99                 previousStatus = issue.getStatus();
100                 processFullEdit(issue, project, currUser, currPermissions, currLocale, form, ih);
101             } else {
102                 previousStatus = issue.getStatus();
103                 processLimitedEdit(issue, project, currUser, currPermissions, currLocale, form, ih);
104             }
105
106             sendNotification(issue, previousStatus, getBaseURL(request), ih);
107             session.removeAttribute(Constants.ISSUE_KEY);
108             return getReturnForward(issue, project, form, mapping);
109         } catch(Exception JavaDoc e) {
110             Logger.logError("Exception processing form data", e);
111             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
112         }
113
114         if(! errors.isEmpty()) {
115             saveErrors(request, errors);
116             saveToken(request);
117             return mapping.getInputForward();
118         }
119
120         return mapping.findForward("error");
121     }
122
123     private void processFullEdit(IssueModel issue, ProjectModel project, UserModel user, HashMap userPermissions, Locale locale, ActionForm form, IssueHandler ih) throws Exception JavaDoc {
124         int previousStatus = issue.getStatus();
125
126         issue.setDescription((String JavaDoc) PropertyUtils.getSimpleProperty(form, "description"));
127         issue.setResolution((String JavaDoc) PropertyUtils.getSimpleProperty(form, "resolution"));
128         issue.setSeverity(((Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "severity")).intValue());
129
130         Integer JavaDoc targetVersionId = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "targetVersion");
131         if(targetVersionId != null && targetVersionId.intValue() != -1) {
132             issue.setTargetVersion(new VersionModel(targetVersionId));
133         }
134
135         Integer JavaDoc formStatus = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "status");
136         if(formStatus != null) {
137             issue.setStatus(formStatus.intValue());
138         }
139
140         if(previousStatus != -1) {
141             // Reopened the issue. Reset the resolution field.
142
if((issue.getStatus() >= IssueUtilities.STATUS_ASSIGNED && issue.getStatus() < IssueUtilities.STATUS_RESOLVED) &&
143                (previousStatus >= IssueUtilities.STATUS_RESOLVED && previousStatus < IssueUtilities.STATUS_END)) {
144                 issue.setResolution("");
145             }
146
147             if(issue.getStatus() >= IssueUtilities.STATUS_CLOSED && ! UserUtilities.hasPermission(userPermissions, project.getId(), UserUtilities.PERMISSION_CLOSE)) {
148                 if(previousStatus < IssueUtilities.STATUS_CLOSED) {
149                     issue.setStatus(previousStatus);
150                 } else {
151                     issue.setStatus(IssueUtilities.STATUS_RESOLVED);
152                 }
153             }
154
155             if(issue.getStatus() < IssueUtilities.STATUS_NEW || issue.getStatus() >= IssueUtilities.STATUS_END) {
156                 issue.setStatus(previousStatus);
157             }
158         } else if(issue.getStatus() >= IssueUtilities.STATUS_CLOSED && ! UserUtilities.hasPermission(userPermissions, project.getId(), UserUtilities.PERMISSION_CLOSE)) {
159             issue.setStatus(IssueUtilities.STATUS_RESOLVED);
160         }
161
162         if(issue.getStatus() < IssueUtilities.STATUS_NEW) {
163             issue.setStatus(IssueUtilities.STATUS_NEW);
164         } else if(issue.getStatus() >= IssueUtilities.STATUS_END) {
165             if(! UserUtilities.hasPermission(userPermissions, project.getId(), UserUtilities.PERMISSION_CLOSE)) {
166                 issue.setStatus(IssueUtilities.STATUS_RESOLVED);
167             } else {
168                 issue.setStatus(IssueUtilities.STATUS_CLOSED);
169             }
170         }
171
172         issue = ih.updateIssue(issue, user.getId());
173         if(issue != null) {
174             setIssueFields(issue, user, locale, form, ih);
175             setOwner(issue, user, userPermissions, form, ih);
176             addHistoryEntry(issue, user, form, ih);
177
178             HashSet components = new HashSet();
179             Integer JavaDoc[] componentIds = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "components");
180             if(componentIds != null) {
181                 for(int i = 0; i < componentIds.length; i++) {
182                     components.add(componentIds[i]);
183                 }
184                 ih.setIssueComponents(issue.getId(), components, user.getId());
185             }
186             HashSet versions = new HashSet();
187             Integer JavaDoc[] versionIds = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "versions");
188             if(versionIds != null) {
189                 for(int i = 0; i < versionIds.length; i++) {
190                     versions.add(versionIds[i]);
191                 }
192                 ih.setIssueVersions(issue.getId(), versions, user.getId());
193             }
194
195             addAttachment(issue, project, user, form, ih);
196         }
197     }
198
199     private void processLimitedEdit(IssueModel issue, ProjectModel project, UserModel user, HashMap userPermissions, Locale locale, ActionForm form, IssueHandler ih) throws Exception JavaDoc {
200         issue.setDescription((String JavaDoc) PropertyUtils.getSimpleProperty(form, "description"));
201
202         Integer JavaDoc formStatus = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "status");
203         if(formStatus != null) {
204             int newStatus = formStatus.intValue();
205             if(issue.getStatus() >= IssueUtilities.STATUS_RESOLVED && newStatus >= IssueUtilities.STATUS_CLOSED &&
206                UserUtilities.hasPermission(userPermissions, UserUtilities.PERMISSION_CLOSE)) {
207                 issue.setStatus(newStatus);
208             }
209         }
210
211         issue = ih.updateIssue(issue, user.getId());
212
213         setIssueFields(issue, user, locale, form, ih);
214         setOwner(issue, user, userPermissions, form, ih);
215         addHistoryEntry(issue, user, form, ih);
216         addAttachment(issue, project, user, form, ih);
217     }
218
219     private void setOwner(IssueModel issue, UserModel user, HashMap userPermissions, ActionForm form, IssueHandler ih) throws Exception JavaDoc {
220         Integer JavaDoc currentOwner = issue.getOwnerId();
221         Integer JavaDoc ownerId = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "ownerId");
222         if(ownerId != null && ! ownerId.equals(currentOwner)) {
223             if(UserUtilities.hasPermission(userPermissions, UserUtilities.PERMISSION_ASSIGN_OTHERS) ||
224                (UserUtilities.hasPermission(userPermissions, UserUtilities.PERMISSION_ASSIGN_SELF) && user.getId().equals(ownerId)) ||
225                (UserUtilities.hasPermission(userPermissions, UserUtilities.PERMISSION_UNASSIGN_SELF) && user.getId().equals(currentOwner) && ownerId.intValue() == -1)
226               ) {
227                 ih.assignIssue(issue.getId(), ownerId, user.getId());
228             }
229         }
230     }
231
232     private void setIssueFields(IssueModel issue, UserModel user, Locale locale, ActionForm form, IssueHandler ih) throws Exception JavaDoc {
233         IssueFieldModel[] issueFields = new IssueFieldModel[0];
234         HashMap customFields = (HashMap) PropertyUtils.getSimpleProperty(form, "customFields");
235         if(customFields != null && customFields.size() > 0) {
236             Vector issueFieldsVector = new Vector();
237             for(Iterator iter = customFields.keySet().iterator(); iter.hasNext(); ) {
238                 try {
239                     Integer JavaDoc fieldId = new Integer JavaDoc((String JavaDoc) iter.next());
240                     CustomFieldModel field = IssueUtilities.getCustomField(fieldId);
241                     String JavaDoc fieldValue = (String JavaDoc) PropertyUtils.getMappedProperty(form, "customFields(" + fieldId + ")");
242                     if(fieldValue != null && ! fieldValue.equals("")) {
243                         IssueFieldModel issueField = new IssueFieldModel(field, issue.getId());
244                         issueField.setValue(fieldValue, locale);
245                         issueFieldsVector.add(issueField);
246                     }
247                 } catch(Exception JavaDoc e) {
248                 }
249             }
250             issueFields = new IssueFieldModel[issueFieldsVector.size()];
251             issueFieldsVector.copyInto(issueFields);
252         }
253         ih.setIssueFields(issue.getId(), issueFields);
254     }
255
256     private void addHistoryEntry(IssueModel issue, UserModel user, ActionForm form, IssueHandler ih) throws Exception JavaDoc {
257         String JavaDoc history = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "history");
258         if(history != null && ! history.equals("")) {
259             IssueHistoryModel issueHistory = new IssueHistoryModel(history, IssueUtilities.HISTORY_STATUS_AVAILABLE, issue.getId(), user.getId());
260             ih.addIssueHistory(issueHistory);
261         }
262     }
263
264     private void addAttachment(IssueModel issue, ProjectModel project, UserModel user, ActionForm form, IssueHandler ih) throws Exception JavaDoc {
265         if(! ProjectUtilities.hasOption(ProjectUtilities.OPTION_NO_ATTACHMENTS, project.getOptions())) {
266             FormFile file = (FormFile) PropertyUtils.getSimpleProperty(form, "attachment");
267             if(file != null && ! "".equals(file.getFileName())) {
268                 String JavaDoc origFileName = file.getFileName();
269 // int numAttachments = ih.getIssueAttachmentCount(issue.getId()) + 1;
270
// String filename = "proj" + project.getId() + "_issue" + issue.getId() + "_attachment" + numAttachments;
271
if(AttachmentUtilities.checkFile(file)) {
272                     int lastSlash = Math.max(origFileName.lastIndexOf('/'), origFileName.lastIndexOf('\\'));
273                     if(lastSlash > -1) {
274                         origFileName = origFileName.substring(lastSlash + 1);
275                     }
276                     IssueAttachmentModel attachmentModel = new IssueAttachmentModel(origFileName,
277                                                                                     file.getContentType(),
278                                                                                     (String JavaDoc) PropertyUtils.getSimpleProperty(form, "attachmentDescription"),
279                                                                                     file.getFileSize(),
280                                                                                     issue.getId(), user.getId());
281                     ih.addIssueAttachment(attachmentModel, file.getFileData());
282                 }
283             }
284         }
285     }
286
287     private void sendNotification(IssueModel issue, int previousStatus, String JavaDoc baseURL, IssueHandler ih) {
288         int notificationType = NotificationUtilities.TYPE_UPDATED;
289         if(previousStatus >= IssueUtilities.STATUS_CLOSED && issue.getStatus() >= IssueUtilities.STATUS_CLOSED) {
290             notificationType = NotificationUtilities.TYPE_CLOSED;
291         }
292         ih.sendNotification(issue.getId(), notificationType, baseURL);
293     }
294
295     private ActionForward getReturnForward(IssueModel issue, ProjectModel project, ActionForm form, ActionMapping mapping) throws Exception JavaDoc {
296         if("index".equals((String JavaDoc) PropertyUtils.getSimpleProperty(form, "caller"))) {
297             return mapping.findForward("index");
298         } else if("viewissue".equals((String JavaDoc) PropertyUtils.getSimpleProperty(form, "caller"))) {
299             return new ActionForward(mapping.findForward("viewissue").getPath() + "?id=" + issue.getId());
300         } else {
301             return new ActionForward(mapping.findForward("listissues").getPath() + "?pid=" + project.getId());
302         }
303     }
304
305 }
306   
Popular Tags