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 org.apache.log4j.Logger;
22  import org.apache.struts.action.*;
23  import org.itracker.WorkflowException;
24  import org.itracker.model.*;
25  import org.itracker.model.util.Convert;
26  import org.itracker.model.util.IssueUtilities;
27  import org.itracker.model.util.UserUtilities;
28  import org.itracker.model.util.WorkflowUtilities;
29  import org.itracker.services.ProjectService;
30  import org.itracker.services.UserService;
31  import org.itracker.web.actions.base.ItrackerBaseAction;
32  import org.itracker.web.forms.IssueForm;
33  import org.itracker.web.util.*;
34  
35  import javax.servlet.ServletException;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import javax.servlet.http.HttpSession;
39  import java.io.IOException;
40  import java.util.*;
41  
42  public class CreateIssueFormAction extends ItrackerBaseAction {
43      private static final Logger log = Logger
44              .getLogger(CreateIssueFormAction.class);
45  
46      public CreateIssueFormAction() {
47      }
48  
49      public ActionForward execute(ActionMapping mapping, ActionForm form,
50                                   HttpServletRequest request, HttpServletResponse response)
51              throws ServletException, IOException {
52          ActionMessages errors = new ActionMessages();
53          //  TODO: Action Cleanup
54  
55          try {
56              ProjectService projectService = ServletContextUtils.getItrackerServices()
57                      .getProjectService();
58              UserService userService = ServletContextUtils.getItrackerServices().getUserService();
59  
60              Integer projectId = new Integer(
61                      (request.getParameter("projectId") == null ? "-1"
62                              : (request.getParameter("projectId"))));
63  
64              HttpSession session = request.getSession(true);
65              User currUser = (User) session.getAttribute(Constants.USER_KEY);
66              Map<Integer, Set<PermissionType>> permissions = RequestHelper.getUserPermissions(session);
67              Locale locale = LoginUtilities.getCurrentLocale(request);
68  
69              if (!UserUtilities.hasPermission(permissions, projectId,
70                      PermissionType.ISSUE_CREATE)) {
71                  log
72                          .debug("Unauthorized user requested access to create issue for project "
73                                  + projectId);
74                  return mapping.findForward("unauthorized");
75              }
76  
77              Project project = projectService.getProject(projectId);
78              if (log.isDebugEnabled() && project != null) {
79                  log.debug("execute: Received request for project " + projectId + "("
80                          + project.getName() + ")");
81              }
82              if (project == null) {
83                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
84                          "itracker.web.error.invalidproject"));
85              } else if (project.getStatus() != Status.ACTIVE) {
86                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
87                          "itracker.web.error.projectlocked"));
88              }
89  
90              if (errors.isEmpty()) {
91                  Map<Integer, List<NameValuePair>> listOptions = new HashMap<Integer, List<NameValuePair>>();
92                  if (UserUtilities.hasPermission(permissions, project.getId(),
93                          UserUtilities.PERMISSION_ASSIGN_OTHERS)) {
94                      List<User> possibleOwners = userService.getPossibleOwners(
95                              null, project.getId(), currUser.getId());
96                      Collections.sort(possibleOwners, User.NAME_COMPARATOR);
97                      listOptions.put(IssueUtilities.FIELD_OWNER, Convert
98                              .usersToNameValuePairs(possibleOwners));
99                  } else if (UserUtilities.hasPermission(permissions, project
100                         .getId(), UserUtilities.PERMISSION_ASSIGN_SELF)) {
101                     NameValuePair myNameValuePair = new NameValuePair(currUser
102                             .getFirstName()
103                             + " " + currUser.getLastName(), currUser.getId()
104                             .toString());
105                     List<NameValuePair> myNameValuePairList = new ArrayList<NameValuePair>();
106                     myNameValuePairList.add(myNameValuePair);
107                     listOptions.put(IssueUtilities.FIELD_OWNER,
108                             myNameValuePairList);
109                 }
110                 
111                 if (UserUtilities.hasPermission(permissions, project.getId(),
112                         UserUtilities.PERMISSION_CREATE_OTHERS)) {
113                     List<User> possibleCreators = userService
114                             .getUsersWithAnyProjectPermission(
115                                     project.getId(),
116                                     PermissionType.valueOf(new int[]{
117                                             UserUtilities.PERMISSION_VIEW_ALL,
118                                             UserUtilities.PERMISSION_VIEW_USERS}));
119                     Collections.sort(possibleCreators, User.NAME_COMPARATOR);
120                     listOptions.put(IssueUtilities.FIELD_CREATOR, Convert
121                             .usersToNameValuePairs(possibleCreators));
122                 }
123 
124                 IssueForm../../../org/itracker/web/forms/IssueForm.html#IssueForm">IssueForm issueForm = (IssueForm) form;
125                 if (issueForm == null) {
126                     issueForm = new IssueForm();
127                 }
128                 issueForm.setCreatorId(currUser.getId());
129                 List<NameValuePair> severities = IssueUtilities
130                                        .getSeverities(locale);
131                // sort by severity code so it will be ascending output.
132                Collections.sort(severities, NameValuePair.VALUE_COMPARATOR);
133                listOptions.put(IssueUtilities.FIELD_SEVERITY, severities);
134 
135                List<Component> components = project.getComponents();
136                Collections.sort(components, Component.NAME_COMPARATOR);
137                listOptions.put(IssueUtilities.FIELD_COMPONENTS, Convert
138                        .componentsToNameValuePairs(components));
139                List<Version> versions = project.getVersions();
140                Collections.sort(versions, new Version.VersionComparator());
141                listOptions.put(IssueUtilities.FIELD_VERSIONS, Convert
142                        .versionsToNameValuePairs(versions));
143 
144                List<CustomField> projectFields = project.getCustomFields();
145                for (int i = 0; i < projectFields.size(); i++) {
146                    if (projectFields.get(i).getFieldType() == CustomField.Type.LIST) {
147                        listOptions
148                                .put(
149                                        projectFields.get(i).getId(),
150                                        Convert
151                                                .customFieldOptionsToNameValuePairs(projectFields
152                                                        .get(i), locale));
153                    }
154                }
155 
156 
157                 if (versions.size() > 0) {
158                     issueForm.setVersions(new Integer[]{versions.get(0)
159                             .getId()});
160                 }
161 
162                 // Severity by configured default value or Major (2)
163                 Integer severity = ServletContextUtils.getItrackerServices()
164                         .getConfigurationService().getIntegerProperty("default_severity", 2);
165                 issueForm.setSeverity(severity);
166 
167                 // populate the possible list options
168                 issueForm.invokeProjectScripts(project, WorkflowUtilities.EVENT_FIELD_ONPOPULATE, listOptions, errors);
169 
170                 issueForm.invokeProjectScripts(project, WorkflowUtilities.EVENT_FIELD_ONSETDEFAULT, errors);
171 
172 
173                 if (errors == null || errors.isEmpty()) {
174                     log.debug("Forwarding to create issue form for project "
175                             + project.getId());
176                     request.setAttribute("issueForm", issueForm);
177                     session.setAttribute(Constants.PROJECT_KEY, project);
178                     session.setAttribute(Constants.LIST_OPTIONS_KEY,
179                             listOptions);
180                     saveToken(request);
181 
182                     if (project == null) {
183                         return mapping.findForward("unauthorized");
184 
185                     } else {
186                         EditIssueActionUtil.setupCreateIssue(request);
187                         return mapping.getInputForward();
188                     }
189                 }
190             }
191         } catch (RuntimeException e) {
192             log.error("Exception while creating create issue form.", e);
193             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
194                     "itracker.web.error.system"));
195         } catch (WorkflowException e) {
196 
197             log.error("Exception while creating create issue form.", e);
198             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
199                     "itracker.web.error.system"));
200         }
201 
202         if (!errors.isEmpty()) {
203             saveErrors(request, errors);
204         }
205         return mapping.findForward("error");
206     }
207 }