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