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