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