View Javadoc
1   package org.itracker.web.actions.project;
2   
3   import org.apache.log4j.Logger;
4   import org.apache.struts.action.*;
5   import org.itracker.core.resources.ITrackerResources;
6   import org.itracker.model.*;
7   import org.itracker.model.util.IssueUtilities;
8   import org.itracker.model.util.ProjectUtilities;
9   import org.itracker.model.util.UserUtilities;
10  import org.itracker.services.IssueService;
11  import org.itracker.services.NotificationService;
12  import org.itracker.web.actions.base.ItrackerBaseAction;
13  import org.itracker.web.forms.IssueForm;
14  import org.itracker.web.util.IssueNavigationUtil;
15  import org.itracker.web.util.LoginUtilities;
16  import org.itracker.web.util.RequestHelper;
17  import org.itracker.web.util.ServletContextUtils;
18  import org.springframework.security.core.GrantedAuthority;
19  import org.springframework.security.core.context.SecurityContextHolder;
20  import org.springframework.security.core.userdetails.UserDetails;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  import javax.servlet.http.HttpSession;
25  import java.util.*;
26  
27  public class ViewIssueAction extends ItrackerBaseAction {
28  	private static final Logger log = Logger.getLogger(ViewIssueAction.class);
29  
30  	public ActionForward execute(ActionMapping mapping, ActionForm form,
31  			HttpServletRequest request, HttpServletResponse response)
32  			throws Exception {
33  
34  		IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
35  
36  		ActionMessages errors = new ActionMessages();
37  		Locale locale = getLocale(request);
38  
39  		String pageTitleKey = "itracker.web.viewissue.title";
40  		String pageTitleArg = request.getParameter("id");
41  		request.setAttribute("pageTitleKey", pageTitleKey);
42  		request.setAttribute("pageTitleArg", pageTitleArg);
43  		Integer issueId;
44  		try {
45  			issueId = Integer.valueOf(request.getParameter("id"));
46  		} catch (RuntimeException re) {
47  			errors.add(ActionMessages.GLOBAL_MESSAGE,
48  					new ActionMessage("itracker.web.error.noissue"));
49  			saveErrors(request, errors);
50  			return mapping.findForward("error");
51  		}
52  
53  		HttpSession session = request.getSession();
54  		User um = RequestHelper.getCurrentUser(session);
55  
56  		NotificationService notificationService = ServletContextUtils.getItrackerServices().getNotificationService();
57  
58  		Issue issue;
59  
60  		try {
61  			issue = issueService.getIssue(issueId);
62  		} catch (Exception ex) {
63  			issue = null;
64  		}
65  		if (issue == null) {
66  			errors.add(ActionMessages.GLOBAL_MESSAGE,
67  					new ActionMessage("itracker.web.error.invalidissue"));
68  			saveErrors(request, errors);
69  			log.info("ViewIssueAction: Forward: error");
70  			return mapping.findForward("error");
71  		}
72  
73  		Project project = issue.getProject();
74  		if (project != null && project.getStatus() != Status.ACTIVE
75  				&& project.getStatus() != Status.VIEWABLE) {
76  			errors.add(ActionMessages.GLOBAL_MESSAGE,
77  					new ActionMessage("itracker.web.error.projectlocked"));
78  			saveErrors(request, errors);
79  			log.info("ViewIssueAction: Forward: error");
80  			return mapping.findForward("error");
81  		} else {
82  
83  			if (project == null || !LoginUtilities.canViewIssue(issue)) {
84  				log.info("ViewIssueAction: Forward: unauthorized");
85  				return mapping.findForward("unauthorized");
86  			}
87  		}
88          IssueForm.setupNotificationsInRequest(request, issueService
89                        .getIssue(issueId),
90                        ServletContextUtils.getItrackerServices().getNotificationService());
91  
92          IssueNavigationUtil.setupNextPreviousIssueInRequest(request, issue, issueService);
93  		/*
94  		 * Get issue history, sort on create date.
95  		 */
96  		List<IssueHistory> issueHistories = issue.getHistory();
97  		List<IssueHistory> histories = new ArrayList<>();
98  		Collections.sort(issueHistories, IssueHistory.CREATE_DATE_COMPARATOR);
99  		for (IssueHistory history : issueHistories) {
100 			if (history.getStatus() == IssueUtilities.HISTORY_STATUS_AVAILABLE) {
101 				histories.add(history);
102 			}
103 		}
104 		if (project.getVersions() != null) {
105 			Collections.sort(project.getVersions(), Version.VERSION_COMPARATOR);
106 		}
107 		if (issue.getComponents() != null && issue.getComponents().size() > 0) {
108 			Collections.sort(issue.getComponents(), Component.NAME_COMPARATOR);
109 		}
110 		if (issue.getVersions() != null && issue.getVersions().size() > 0) {
111 			Collections.sort(issue.getVersions(), new Version.VersionComparator());
112 		}
113 		/*
114 		 * Get attachments of issue, and sort attachments on created date
115 		 */
116 		List<IssueAttachment> attachments = issue.getAttachments();
117 		Collections.sort(attachments, IssueAttachment.CREATE_DATE_COMPARATOR);
118 		/*
119 		 * Get the issue status name to display.
120 		 */
121 		String issueStatusName = IssueUtilities.getStatusName(issue.getStatus(), locale);
122 		/* Get issue severity name
123 		 * 
124 		 */
125 		String issueSeverityName = IssueUtilities.getSeverityName(issue.getSeverity(), locale);
126 		/*
127 		 * Create Project field map
128 		 */
129 		IssueForm.setupProjectFieldsMapJspEnv(project.getCustomFields(), issue.getFields(), request);
130 
131 		/*
132 		 * Set the objects in request that are required for ui render
133 		 */
134 		request.setAttribute("issueId", issueId);
135 		request.setAttribute("issue", issue);
136 		request.setAttribute("attachments", attachments);
137 		request.setAttribute("hasAttachmentOption", !ProjectUtilities
138 				.hasOption(ProjectUtilities.OPTION_NO_ATTACHMENTS, project
139 						.getOptions()));
140 		request.setAttribute("histories", histories);
141 		request.setAttribute("project", project);
142         request.setAttribute("rssFeed", "/servlets/issues/p" + project.getId() + "/i" + issue.getId());
143 		request.setAttribute("hasIssueNotification", notificationService.hasIssueNotification(
144 				issue, request.getRemoteUser()));
145 		request.setAttribute("hasHardIssueNotification", IssueUtilities.hasHardNotification(
146 				issue, project, um.getId()));
147 		request.setAttribute("canEditIssue", LoginUtilities.canEditIssue(issue));
148 		request.setAttribute("canCreateIssue",
149 				(project.getStatus() == Status.ACTIVE && LoginUtilities
150 						.hasPermission(project,
151 								PermissionType.ISSUE_CREATE)));
152 		request.setAttribute("issueStatusName",issueStatusName);
153 		request.setAttribute("issueSeverityName",issueSeverityName);
154 		request.setAttribute("issueOwnerName",(issue.getOwner() == null ? ITrackerResources.getString("itracker.web.generic.unassigned", locale) : issue.getOwner().getFirstName() + " " + issue.getOwner().getLastName()) );
155 
156 		log.info("ViewIssueAction: Forward: viewissue");
157 		return mapping.findForward("viewissue");
158 
159 	}
160 }