1 package org.itracker.web.util;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.apache.commons.lang.StringUtils;
5 import org.itracker.model.Issue;
6 import org.itracker.model.IssueSearchQuery;
7 import org.itracker.model.PermissionType;
8 import org.itracker.model.User;
9 import org.itracker.model.util.IssueUtilities;
10 import org.itracker.model.util.UserUtilities;
11 import org.itracker.services.IssueService;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import javax.servlet.http.HttpServletRequest;
16 import java.util.*;
17
18
19
20
21 public class IssueNavigationUtil {
22 private static final Logger log = LoggerFactory.getLogger(IssueNavigationUtil.class);
23
24 public static void setupNextPreviousIssueInRequest(HttpServletRequest request,
25 Issue issue,
26 IssueService issueService) {
27 String caller = request.getParameter("caller");
28
29 User user = LoginUtilities.getCurrentUser(request);
30 Map<Integer, Set<PermissionType>> permissions = RequestHelper.getUserPermissions(request.getSession());
31
32 if (StringUtils.equals(caller, "searchissue")) {
33 IssueSearchQuery isqm = (IssueSearchQuery)request.getSession().getAttribute(Constants.SEARCH_QUERY_KEY);
34 if (null != isqm && !CollectionUtils.isEmpty(isqm.getResults())) {
35 log.debug("setupNextPreviousIssueInRequest: found search results from session");
36 setupPrevNextByListing(request, issue, isqm.getResults(), user, permissions);
37 }
38 } else if (StringUtils.equals(caller, "index")) {
39 setupPrevNextByListing(request, issue,
40 findAllIssuesForPortalhomeSections(issueService, user),
41 user, permissions);
42 } else {
43 setupPrevNextByProject(request, issue, issueService, user, permissions);
44 }
45 }
46
47 private static List<Issue> findAllIssuesForPortalhomeSections(IssueService issueService, User user) {
48 Set<Issue> issues = new TreeSet<>(Issue.ID_COMPARATOR);
49 final int hiddenSections = user.getPreferences().getHiddenIndexSections();
50 if(!UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_CREATED,
51 hiddenSections)) {
52 issues.addAll(issueService.getIssuesCreatedByUser(user.getId()));
53 }
54 if(!UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_ASSIGNED,
55 hiddenSections)) {
56 issues.addAll(issueService.getIssuesOwnedByUser(user.getId()));
57 }
58 if(!UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_UNASSIGNED,
59 hiddenSections)) {
60 issues.addAll(issueService.getUnassignedIssues());
61 }
62 if(!UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_WATCHED,
63 hiddenSections)) {
64 issues.addAll(issueService.getIssuesWatchedByUser(user.getId()));
65 }
66 return new ArrayList<>(issues);
67 }
68
69 private static void setupPrevNextByProject(HttpServletRequest request, Issue issue, IssueService issueService,
70 User user, Map<Integer, Set<PermissionType>> permissions) {
71 List<Issue> nextIssues = issueService.getNextIssues(issue.getId());
72 List<Issue> prevIssues = issueService.getPreviousIssues(issue.getId());
73
74 if (log.isDebugEnabled()) {
75 log.debug("setupPrevNextByProject: found {} next and () previous issues",
76 new Object[]{nextIssues.size(), prevIssues.size()});
77 }
78 request.setAttribute("nextIssue", firstAvailableIssue(user, permissions, nextIssues));
79 request.setAttribute("previousIssue", firstAvailableIssue(user, permissions, prevIssues));
80 }
81
82 private static Issue firstAvailableIssue(User user, Map<Integer, Set<PermissionType>> permissions, List<Issue> nextIssues) {
83 for (Issue next : nextIssues) {
84 if (IssueUtilities.canViewIssue(next, user, permissions)) {
85 if (log.isDebugEnabled()) {
86 log.debug("firstAvailableIssue: found issue {}", next.getId());
87 }
88 return next;
89 }
90 }
91 return null;
92 }
93
94 private static void setupPrevNextByListing(HttpServletRequest request, Issue issue, List<Issue> issues,
95 User user, Map<Integer, Set<PermissionType>> permissions) {
96 if (CollectionUtils.isEmpty(issues) || !issues.contains(issue)) {
97 return;
98 }
99 for (Issue i: issues) {
100 if (i.equals(issue)) {
101 Integer x = issues.indexOf(i);
102
103 if (x > 0) {
104 List<Issue> sub = new ArrayList<>(issues.subList(0, x));
105 Collections.reverse(sub);
106 request.setAttribute("previousIssue",
107 firstAvailableIssue(user, permissions, sub));
108 }
109 if (x+1 <= issues.size())
110 request.setAttribute("nextIssue",
111 firstAvailableIssue(user, permissions, issues.subList(x+1, issues.size())));
112 return;
113 }
114 }
115 }
116 }