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.model.*;
24  import org.itracker.model.Notification.Role;
25  import org.itracker.model.util.UserUtilities;
26  import org.itracker.services.IssueService;
27  import org.itracker.services.NotificationService;
28  import org.itracker.web.actions.base.ItrackerBaseAction;
29  import org.itracker.web.util.Constants;
30  import org.itracker.web.util.RequestHelper;
31  import org.itracker.web.util.ServletContextUtils;
32  
33  import javax.servlet.ServletException;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import javax.servlet.http.HttpSession;
37  import java.io.IOException;
38  import java.util.Iterator;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.Set;
42  
43  
44  public class WatchIssueAction extends ItrackerBaseAction {
45  
46  
47      private static final Logger log = Logger.getLogger(WatchIssueAction.class);
48  
49      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50  
51          ActionMessages errors = new ActionMessages();
52  
53          try {
54              IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
55              NotificationService notificationService = ServletContextUtils.getItrackerServices().getNotificationService();
56              Integer issueId = new Integer((request.getParameter("id") == null ? "-1" : (request.getParameter("id"))));
57              Issue issue = issueService.getIssue(issueId);
58              Project project = issueService.getIssueProject(issueId);
59  
60              if (project == null) {
61                  return mapping.findForward("unauthorized");
62              }
63  
64              HttpSession session = request.getSession(true);
65              User currUser = (User) session.getAttribute(Constants.USER_KEY);
66              Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);
67  
68              if (!UserUtilities.hasPermission(userPermissions, project.getId(), UserUtilities.PERMISSION_VIEW_ALL)) {
69                  return mapping.findForward("unauthorized");
70              }
71  
72              Notification notification = new Notification();
73  
74  
75              boolean userHasIssueNotification = false;
76              List<Notification> notifications = issue.getNotifications();
77  
78              for (Iterator<Notification> nIterator = notifications.iterator(); nIterator.hasNext(); ) {
79                  Notification issue_notification = nIterator.next();
80                  if (issue_notification.getUser().getId().equals(currUser.getId())) {
81                      userHasIssueNotification = true;
82                      if (issue_notification.getRole() == Role.IP) {
83                          notification = issue_notification;
84                          nIterator.remove();
85                          break;
86                      }
87                  }
88              }
89              if (userHasIssueNotification) {
90                  if (null!=notification.getId()) {
91                      issue.setNotifications(notifications);
92                      notificationService.removeIssueNotification(notification.getId());
93                  }
94              } else {
95                  notification.setUser(currUser);
96                  notification.setIssue(issue);
97                  notification.setRole(Role.IP);
98                  notificationService.addIssueNotification(notification);
99              }
100             String caller = request.getParameter("caller");
101             if ("editissue".equals(caller)) {
102                 return new ActionForward(mapping.findForward("editissue").getPath() + "?id=" + issueId);
103             } else if ("viewissue".equals(caller)) {
104                 return new ActionForward(mapping.findForward("viewissue").getPath() + "?id=" + issueId);
105                 //index was the old name for portalhome, we have to clean the naming in this area... 
106             } else if ("index".equals(caller)) {
107                 return mapping.findForward("index");
108             } else {
109                 return new ActionForward(mapping.findForward("listissues").getPath() + "?projectId=" + project.getId());
110             }
111         } catch (Exception e) {
112             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.login.system"));
113             log.error("System Error.", e);
114         }
115         if (!errors.isEmpty()) {
116             saveErrors(request, errors);
117         }
118         return mapping.findForward("error");
119     }
120 
121 }
122