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.project;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import javax.servlet.http.HttpSession;
34  
35  import org.apache.log4j.Logger;
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  import org.apache.struts.action.ActionMessage;
40  import org.apache.struts.action.ActionMessages;
41  import org.itracker.model.Component;
42  import org.itracker.model.CustomField;
43  import org.itracker.model.NameValuePair;
44  import org.itracker.model.PermissionType;
45  import org.itracker.model.Project;
46  import org.itracker.model.ProjectScript;
47  import org.itracker.model.Status;
48  import org.itracker.model.User;
49  import org.itracker.model.Version;
50  import org.itracker.services.ProjectService;
51  import org.itracker.services.UserService;
52  import org.itracker.services.exceptions.WorkflowException;
53  import org.itracker.services.util.Convert;
54  import org.itracker.services.util.IssueUtilities;
55  import org.itracker.services.util.UserUtilities;
56  import org.itracker.services.util.WorkflowUtilities;
57  import org.itracker.web.actions.base.ItrackerBaseAction;
58  import org.itracker.web.forms.IssueForm;
59  import org.itracker.web.ptos.CreateIssuePTO;
60  import org.itracker.web.util.Constants;
61  import org.itracker.web.util.LoginUtilities;
62  
63  public class CreateIssueFormAction extends ItrackerBaseAction {
64  	private static final Logger log = Logger
65  			.getLogger(CreateIssueFormAction.class);
66  
67  	public CreateIssueFormAction() {
68  	}
69  
70  	public ActionForward execute(ActionMapping mapping, ActionForm form,
71  			HttpServletRequest request, HttpServletResponse response)
72  			throws ServletException, IOException {
73  		ActionMessages errors = new ActionMessages();
74  		//  TODO: Action Cleanup
75  
76  		try {
77  			ProjectService projectService = getITrackerServices()
78  					.getProjectService();
79  			UserService userService = getITrackerServices().getUserService();
80  
81  			Integer projectId = new Integer(
82  					(request.getParameter("projectId") == null ? "-1"
83  							: (request.getParameter("projectId"))));
84  
85  			HttpSession session = request.getSession(true);
86  			User currUser = (User) session.getAttribute(Constants.USER_KEY);
87  			Map<Integer, Set<PermissionType>> Permissions = getUserPermissions(session);
88  			Locale locale = LoginUtilities.getCurrentLocale(request);
89  
90  			if (!UserUtilities.hasPermission(Permissions, projectId,
91  					UserUtilities.PERMISSION_CREATE)) {
92  				log
93  						.debug("Unauthorized user requested access to create issue for project "
94  								+ projectId);
95  				return mapping.findForward("unauthorized");
96  			}
97  
98  			Project project = projectService.getProject(projectId);
99  			if (log.isDebugEnabled() && project != null) {
100 				log.debug("execute: Received request for project " + projectId + "("
101 					+ project.getName() + ")");
102 			}
103 			if (project == null) {
104 				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
105 						"itracker.web.error.invalidproject"));
106 			} else if (project.getStatus() != Status.ACTIVE) {
107 				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
108 						"itracker.web.error.projectlocked"));
109 			}
110 
111 			if (errors.isEmpty()) {
112 				Map<Integer, List<NameValuePair>> listOptions = new HashMap<Integer, List<NameValuePair>>();
113 				if (UserUtilities.hasPermission(Permissions, project.getId(),
114 						UserUtilities.PERMISSION_ASSIGN_OTHERS)) {
115 					List<User> possibleOwners = userService.getPossibleOwners(
116 							null, project.getId(), currUser.getId());
117 					Collections.sort(possibleOwners, User.NAME_COMPARATOR);
118 					listOptions.put(IssueUtilities.FIELD_OWNER, Convert
119 							.usersToNameValuePairs(possibleOwners));
120 				} else if (UserUtilities.hasPermission(Permissions, project
121 						.getId(), UserUtilities.PERMISSION_ASSIGN_SELF)) {
122 					NameValuePair myNameValuePair = new NameValuePair(currUser
123 							.getFirstName()
124 							+ " " + currUser.getLastName(), currUser.getId()
125 							.toString());
126 					List<NameValuePair> myNameValuePairList = new ArrayList<NameValuePair>();
127 					myNameValuePairList.add(myNameValuePair);
128 					listOptions.put(IssueUtilities.FIELD_OWNER,
129 							myNameValuePairList);
130 				}
131 
132 				if (UserUtilities.hasPermission(Permissions, project.getId(),
133 						UserUtilities.PERMISSION_CREATE_OTHERS)) {
134 					List<User> possibleCreators = userService
135 							.getUsersWithAnyProjectPermission(
136 									project.getId(),
137 									new int[] {
138 											UserUtilities.PERMISSION_VIEW_ALL,
139 											UserUtilities.PERMISSION_VIEW_USERS });
140 					Collections.sort(possibleCreators, User.NAME_COMPARATOR);
141 					listOptions.put(IssueUtilities.FIELD_CREATOR, Convert
142 							.usersToNameValuePairs(possibleCreators));
143 				}
144 
145 				List<NameValuePair> severities = IssueUtilities
146 						.getSeverities(locale);
147 				// sort by severity code so it will be ascending output.
148 				Collections.sort(severities, NameValuePair.VALUE_COMPARATOR);
149 				listOptions.put(IssueUtilities.FIELD_SEVERITY, severities);
150 
151 				List<Component> components = project.getComponents();
152 				Collections.sort(components, Component.NAME_COMPARATOR);
153 				listOptions.put(IssueUtilities.FIELD_COMPONENTS, Convert
154 						.componentsToNameValuePairs(components));
155 				List<Version> versions = project.getVersions();
156 				Collections.sort(versions, new Version.VersionComparator());
157 				listOptions.put(IssueUtilities.FIELD_VERSIONS, Convert
158 						.versionsToNameValuePairs(versions));
159 
160 				List<CustomField> projectFields = project.getCustomFields();
161 				for (int i = 0; i < projectFields.size(); i++) {
162 					if (projectFields.get(i).getFieldType() == CustomField.Type.LIST) {
163 //						projectFields.get(i).setLabels(locale);
164 						listOptions
165 								.put(
166 										projectFields.get(i).getId(),
167 										Convert
168 												.customFieldOptionsToNameValuePairs(projectFields
169 														.get(i).getOptions()));
170 					}
171 				}
172 
173 				IssueForm issueForm = (IssueForm) form;
174 				if (issueForm == null) {
175 					issueForm = new IssueForm();
176 				}
177 				issueForm.setCreatorId(currUser.getId());
178 				if (severities.size() > 0) {
179 					try {
180 						// this sets the selected severity to a medium level
181 						// (middleSeverity). It was argued that this is not
182 						// simple to understand and therefore needs
183 						// simplification or refactoring
184 						int middleSeverity = (severities.size() / 2);
185 						issueForm.setSeverity(Integer.valueOf(severities.get(
186 								middleSeverity).getValue()));
187 					} catch (NumberFormatException nfe) {
188 						log
189 								.debug(
190 										"Invalid status number found while preparing create issue form.",
191 										nfe);
192 					}
193 				}
194 
195 				if (versions.size() > 0) {
196 					issueForm.setVersions(new Integer[] { versions.get(0)
197 							.getId() });
198 				}
199 
200 				String pageTitleKey = "itracker.web.createissue.title";
201 				String pageTitleArg = project.getName();
202 				request.setAttribute("pageTitleKey", pageTitleKey);
203 				request.setAttribute("pageTitleArg", pageTitleArg);
204 
205 				List<ProjectScript> scripts = project.getScripts();
206 				WorkflowUtilities.processFieldScripts(scripts,
207 						WorkflowUtilities.EVENT_FIELD_ONPOPULATE, listOptions,
208 						errors, issueForm);
209 				WorkflowUtilities.processFieldScripts(scripts,
210 						WorkflowUtilities.EVENT_FIELD_ONSETDEFAULT, null,
211 						errors, issueForm);
212 
213 				if (errors == null || errors.isEmpty()) {
214 					log.debug("Forwarding to create issue form for project "
215 							+ project.getId());
216 					request.setAttribute("issueForm", issueForm);
217 					session.setAttribute(Constants.PROJECT_KEY, project);
218 					session.setAttribute(Constants.LIST_OPTIONS_KEY,
219 							listOptions);
220 					saveToken(request);
221 
222 					if (project == null) {
223 						return mapping.findForward("unauthorized");
224 
225 					} else {
226 						CreateIssuePTO.setupCreateIssue(request);
227 						return mapping.getInputForward();
228 					}
229 				}
230 			}
231 		} catch (RuntimeException e) {
232 			log.error("Exception while creating create issue form.", e);
233 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
234 					"itracker.web.error.system"));
235 		} catch (WorkflowException e) {
236 			 
237 			log.error("Exception while creating create issue form.", e);
238 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
239 					"itracker.web.error.system"));
240 		}
241 
242 		if (!errors.isEmpty()) {
243 			saveErrors(request, errors);
244 		}
245 		return mapping.findForward("error");
246 	}
247 }