KickJava   Java API By Example, From Geeks To Geeks.

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


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.resources.*;
39 import cowsultants.itracker.ejb.client.util.*;
40 import cowsultants.itracker.web.util.*;
41
42
43 public class CreateIssueAction extends ITrackerAction {
44
45     public CreateIssueAction() {
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         if(! isTokenValid(request)) {
55             Logger.logDebug("Invalid request token while creating issue.");
56             return mapping.findForward("listprojects");
57         }
58         resetToken(request);
59
60         try {
61             InitialContext ic = new InitialContext();
62             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
63             IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
64             IssueHandler ih = ihHome.create();
65
66             Object JavaDoc phRef = ic.lookup("java:comp/env/" + ProjectHandler.JNDI_NAME);
67             ProjectHandlerHome phHome = (ProjectHandlerHome) PortableRemoteObject.narrow(phRef, ProjectHandlerHome.class);
68             ProjectHandler ph = phHome.create();
69
70             HttpSession session = request.getSession(true);
71             UserModel currUser = (UserModel) session.getAttribute(Constants.USER_KEY);
72             HashMap currPermissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY);
73             Locale currLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
74             Integer JavaDoc currUserId = currUser.getId();
75
76             IssueModel issue = new IssueModel();
77             issue.setDescription((String JavaDoc) PropertyUtils.getSimpleProperty(form, "description"));
78             issue.setSeverity(((Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "severity")).intValue());
79             issue.setStatus(IssueUtilities.STATUS_NEW);
80
81             ProjectModel project = null;
82             Integer JavaDoc projectId = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "projectId");
83             if(projectId == null) {
84                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidproject"));
85             } else {
86                 project = ph.getProject(projectId);
87             }
88
89             if(errors.isEmpty() && project == null) {
90                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidproject"));
91             } else if(errors.isEmpty() && project.getStatus() != ProjectUtilities.STATUS_ACTIVE) {
92                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.projectlocked"));
93             } else if(! UserUtilities.hasPermission(currPermissions, projectId, UserUtilities.PERMISSION_CREATE)) {
94                 return mapping.findForward("unauthorized");
95             } else {
96                 Integer JavaDoc creator = currUserId;
97                 if(UserUtilities.hasPermission(currPermissions, projectId, UserUtilities.PERMISSION_CREATE_OTHERS)) {
98                     creator = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "creatorId");
99                     Logger.logDebug("New issue creator set to " + creator + ". Issue created by " + currUserId);
100                 }
101                 issue = ih.createIssue(issue, projectId, (creator == null ? currUserId : creator), currUserId);
102
103                 if(issue != null) {
104                     Integer JavaDoc newOwner = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "ownerId");
105                     if(newOwner != null && newOwner.intValue() >= 0) {
106                         if(UserUtilities.hasPermission(currPermissions, UserUtilities.PERMISSION_ASSIGN_OTHERS) ||
107                           (UserUtilities.hasPermission(currPermissions, UserUtilities.PERMISSION_ASSIGN_SELF) &&
108                            currUserId.equals(newOwner))) {
109                             ih.assignIssue(issue.getId(), newOwner, currUserId);
110                         }
111                     }
112
113                     IssueFieldModel[] issueFields = new IssueFieldModel[0];
114                     HashMap customFields = (HashMap) PropertyUtils.getSimpleProperty(form, "customFields");
115                     if(customFields != null && customFields.size() > 0) {
116                         Vector issueFieldsVector = new Vector();
117                         for(Iterator iter = customFields.keySet().iterator(); iter.hasNext(); ) {
118                             try {
119                                 Integer JavaDoc fieldId = new Integer JavaDoc((String JavaDoc) iter.next());
120                                 CustomFieldModel field = IssueUtilities.getCustomField(fieldId);
121                                 String JavaDoc fieldValue = (String JavaDoc) PropertyUtils.getMappedProperty(form, "customFields(" + fieldId + ")");
122                                 if(fieldValue != null && ! fieldValue.equals("")) {
123                                     IssueFieldModel issueField = new IssueFieldModel(field, issue.getId());
124                                     issueField.setValue(fieldValue, currLocale);
125                                     issueFieldsVector.add(issueField);
126                                 }
127                             } catch(Exception JavaDoc e) {
128                             }
129                         }
130                         issueFields = new IssueFieldModel[issueFieldsVector.size()];
131                         issueFieldsVector.copyInto(issueFields);
132                     }
133                     ih.setIssueFields(issue.getId(), issueFields);
134
135                     IssueHistoryModel issueHistory = new IssueHistoryModel((String JavaDoc) PropertyUtils.getSimpleProperty(form, "history"),
136                                                                            IssueUtilities.HISTORY_STATUS_AVAILABLE, issue.getId(), creator);
137                     ih.addIssueHistory(issueHistory);
138
139                     HashSet components = new HashSet();
140                     Integer JavaDoc[] componentIds = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "components");
141                     if(componentIds != null) {
142                         for(int i = 0; i < componentIds.length; i++) {
143                             components.add(componentIds[i]);
144                         }
145                         ih.setIssueComponents(issue.getId(), components, creator);
146                     }
147                     HashSet versions = new HashSet();
148                     Integer JavaDoc[] versionIds = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "versions");
149                     if(versionIds != null) {
150                         for(int i = 0; i < versionIds.length; i++) {
151                             versions.add(versionIds[i]);
152                         }
153                         ih.setIssueVersions(issue.getId(), versions, creator);
154                     }
155
156                     if(! ProjectUtilities.hasOption(ProjectUtilities.OPTION_NO_ATTACHMENTS, project.getOptions())) {
157                         FormFile file = (FormFile) PropertyUtils.getSimpleProperty(form, "attachment");
158                         if(file != null && ! "".equals(file.getFileName())) {
159                             String JavaDoc origFileName = file.getFileName();
160                             if(AttachmentUtilities.checkFile(file)) {
161                                 int lastSlash = Math.max(origFileName.lastIndexOf('/'), origFileName.lastIndexOf('\\'));
162                                 if(lastSlash > -1) {
163                                     origFileName = origFileName.substring(lastSlash + 1);
164                                 }
165
166                                 IssueAttachmentModel attachmentModel = new IssueAttachmentModel(origFileName,
167                                                                                                 file.getContentType(),
168                                                                                                 (String JavaDoc) PropertyUtils.getSimpleProperty(form, "attachmentDescription"),
169                                                                                                 file.getFileSize(),
170                                                                                                 issue.getId(), creator);
171                                 ih.addIssueAttachment(attachmentModel, file.getFileData());
172                             }
173                         }
174                     }
175
176                     ih.sendNotification(issue.getId(), NotificationUtilities.TYPE_CREATED, getBaseURL(request));
177                 }
178                 session.removeAttribute(Constants.PROJECT_KEY);
179                 return new ActionForward(mapping.findForward("listissues").getPath() + "?pid=" + projectId);
180             }
181         } catch(Exception JavaDoc e) {
182             Logger.logError("Exception processing form data", e);
183             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
184         }
185
186         if(! errors.isEmpty()) {
187             saveErrors(request, errors);
188         }
189         return mapping.findForward("error");
190     }
191
192 }
193   
Popular Tags