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.commons.beanutils.PropertyUtils;
22  import org.apache.commons.beanutils.converters.IntegerConverter;
23  import org.apache.log4j.Logger;
24  import org.apache.struts.action.*;
25  import org.itracker.model.PermissionType;
26  import org.itracker.model.Project;
27  import org.itracker.model.Status;
28  import org.itracker.model.User;
29  import org.itracker.model.util.UserUtilities;
30  import org.itracker.services.IssueService;
31  import org.itracker.services.ProjectService;
32  import org.itracker.web.actions.base.ItrackerBaseAction;
33  import org.itracker.web.util.Constants;
34  import org.itracker.web.util.RequestHelper;
35  import org.itracker.web.util.ServletContextUtils;
36  
37  import javax.servlet.ServletException;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  import javax.servlet.http.HttpSession;
41  import java.io.IOException;
42  import java.lang.reflect.InvocationTargetException;
43  import java.util.Map;
44  import java.util.Set;
45  
46  
47  public class AssignIssueAction extends ItrackerBaseAction {
48  
49      private static final Logger log = Logger.getLogger(AssignIssueAction.class);
50  
51  
52      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
53  
54          ActionMessages errors = new ActionMessages();
55  
56          try {
57              IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
58              ProjectService projectService = ServletContextUtils.getItrackerServices().getProjectService();
59  
60              Integer defaultValue = -1;
61              IntegerConverter converter = new IntegerConverter(defaultValue);
62              Integer issueId = (Integer) converter.convert(Integer.class, (String) PropertyUtils.getSimpleProperty(form, "issueId"));
63              Integer projectId = (Integer) converter.convert(Integer.class, (String) PropertyUtils.getSimpleProperty(form, "projectId"));
64              Integer userId = (Integer) converter.convert(Integer.class, (String) PropertyUtils.getSimpleProperty(form, "userId"));
65  
66              HttpSession session = request.getSession(true);
67              User currUser = (User) session.getAttribute(Constants.USER_KEY);
68              Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);
69              Integer currUserId = currUser.getId();
70  
71              Project project = projectService.getProject(projectId);
72              if (project == null) {
73                  return mapping.findForward("unauthorized");
74              }
75  
76              if (!userId.equals(currUserId) && !UserUtilities.hasPermission(userPermissions, projectId, UserUtilities.PERMISSION_ASSIGN_OTHERS)) {
77                  return mapping.findForward("unauthorized");
78              } else if (!UserUtilities.hasPermission(userPermissions, projectId, UserUtilities.PERMISSION_ASSIGN_SELF)) {
79                  return mapping.findForward("unauthorized");
80              }
81  
82              if (project.getStatus() != Status.ACTIVE) {
83                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.projectlocked"));
84              } else {
85                  issueService.assignIssue(issueId, userId, currUserId);
86              }
87          } catch (RuntimeException e) {
88              log.warn("execute: caught exception", e);
89              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
90          } catch (IllegalAccessException e) {
91              log.warn("execute: caught exception", e);
92              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
93          } catch (InvocationTargetException e) {
94              log.warn("execute: caught exception", e);
95              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
96          } catch (NoSuchMethodException e) {
97              log.warn("execute: caught exception", e);
98              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
99          }
100 
101         if (!errors.isEmpty()) {
102             saveErrors(request, errors);
103             saveToken(request);
104         }
105         return mapping.findForward("index");
106     }
107 
108 }
109