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.log4j.Logger;
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  import org.apache.struts.action.ActionMessage;
37  import org.apache.struts.action.ActionMessages;
38  import org.itracker.model.Issue;
39  import org.itracker.model.PermissionType;
40  import org.itracker.model.User;
41  import org.itracker.services.IssueService;
42  import org.itracker.services.UserService;
43  import org.itracker.services.util.AuthenticationConstants;
44  import org.itracker.services.util.IssueUtilities;
45  import org.itracker.web.actions.base.ItrackerBaseAction;
46  import org.itracker.web.util.Constants;
47  
48  
49  
50  public class AddIssueRelationAction extends ItrackerBaseAction {
51  
52  	private static final Logger log = Logger.getLogger(AddIssueRelationAction.class);
53  
54      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
55  
56      	ActionMessages errors = new ActionMessages();
57  
58          Integer issueId = null;
59          String caller = "index";
60  
61          UserService userService = getITrackerServices().getUserService();
62          
63          try {
64  
65              IssueService issueService = getITrackerServices().getIssueService();
66              
67              caller = (String) PropertyUtils.getSimpleProperty(form, "caller");
68              issueId = (Integer) PropertyUtils.getSimpleProperty(form, "issueId");
69              Integer relatedIssueId = (Integer) PropertyUtils.getSimpleProperty(form, "relatedIssueId");
70              Integer relationType = (Integer)PropertyUtils.getSimpleProperty(form, "relationType");
71  
72              HttpSession session = request.getSession(true);
73              User currUser = (User) session.getAttribute(Constants.USER_KEY);
74  
75              Map<Integer, Set<PermissionType>> usersMapOfProjectIdsAndSetOfPermissionTypes = 
76                      userService.getUsersMapOfProjectIdsAndSetOfPermissionTypes(currUser, AuthenticationConstants.REQ_SOURCE_WEB);
77              
78              Integer currUserId = currUser.getId();
79  
80              Issue issue = issueService.getIssue(issueId);
81              if(issue == null || issue.getProject() == null || ! IssueUtilities.canEditIssue(issue, currUserId, usersMapOfProjectIdsAndSetOfPermissionTypes)) {
82                  return mapping.findForward("unauthorized");
83              }
84  
85              Issue relatedIssue = issueService.getIssue(relatedIssueId);
86              if(relatedIssue == null) {
87              	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.relation.invalidissue"));
88              } else if(relatedIssue.getProject() == null || ! IssueUtilities.canEditIssue(relatedIssue, currUserId, usersMapOfProjectIdsAndSetOfPermissionTypes)) {
89                  return mapping.findForward("unauthorized");
90              } else {
91                  if(IssueUtilities.hasIssueRelation(issue, relatedIssueId)) {
92                  	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.relation.exists", relatedIssueId));
93                  }
94                  if(! issueService.addIssueRelation(issueId, relatedIssueId, relationType, currUser.getId())) {
95                  	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.relation.adderror"));
96                  }
97              }
98          } catch(RuntimeException e) {
99          	log.info("execute: caught exception ", e);
100         	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
101         } catch (IllegalAccessException e) {
102         	log.info("execute: caught exception ", e);
103         	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
104 		} catch (InvocationTargetException e) {
105         	log.info("execute: caught exception ", e);
106         	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
107 		} catch (NoSuchMethodException e) {
108         	log.info("execute: caught exception ", e);
109         	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
110 		}
111 
112         if(! errors.isEmpty()) {
113         	saveErrors(request, errors);
114         }
115 
116         return new ActionForward(mapping.findForward(caller).getPath() + (issueId != null ? "?id=" + issueId : ""));
117     }
118 
119 }
120