1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web.actions.issuesearch;
20
21 import org.apache.commons.beanutils.PropertyUtils;
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24 import org.apache.struts.action.*;
25 import org.itracker.model.IssueSearchQuery;
26 import org.itracker.model.PermissionType;
27 import org.itracker.model.Project;
28 import org.itracker.model.UserPreferences;
29 import org.itracker.model.util.IssueUtilities;
30 import org.itracker.model.util.UserUtilities;
31 import org.itracker.services.ProjectService;
32 import org.itracker.services.ReportService;
33 import org.itracker.services.UserService;
34 import org.itracker.web.actions.base.ItrackerBaseAction;
35 import org.itracker.web.forms.SearchForm;
36 import org.itracker.web.util.Constants;
37 import org.itracker.web.util.RequestHelper;
38 import org.itracker.web.util.ServletContextUtils;
39
40 import javax.servlet.ServletException;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.servlet.http.HttpSession;
44 import java.io.IOException;
45 import java.util.*;
46
47 public class SearchIssuesFormAction extends ItrackerBaseAction {
48
49 private static final Logger log = Logger.getLogger(SearchIssuesFormAction.class);
50
51 public ActionForward execute(ActionMapping mapping,
52 ActionForm form,
53 HttpServletRequest request,
54 HttpServletResponse response)
55 throws ServletException, IOException {
56
57 ActionMessages errors = new ActionMessages();
58
59 HttpSession session = request.getSession();
60
61 try {
62 ProjectService projectService = ServletContextUtils.getItrackerServices().getProjectService();
63
64 ReportService reportService = ServletContextUtils.getItrackerServices().getReportService();
65 UserService userService = ServletContextUtils.getItrackerServices().getUserService();
66 request.setAttribute("rh", reportService);
67 request.setAttribute("uh", userService);
68
69 String projectId = request.getParameter("projectId");
70
71 UserPreferences userPrefs = (UserPreferences) session.getAttribute(Constants.PREFERENCES_KEY);
72 Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);
73
74 String action = (String) PropertyUtils.getSimpleProperty(form, "action");
75
76 SearchForm/../../org/itracker/web/forms/SearchForm.html#SearchForm">SearchForm searchForm = (SearchForm) form;
77 if (searchForm == null) {
78 searchForm = new SearchForm();
79 }
80
81 boolean newQuery = false;
82 IssueSearchQuery query = (IssueSearchQuery) session.getAttribute(Constants.SEARCH_QUERY_KEY);
83
84 log.debug("projectid = " + projectId);
85 log.debug("query type = " + (query == null ? "NULL" : query.getType().toString()));
86 log.debug("query projectid = " + (query == null ? "NULL" : query.getProjectId().toString()));
87
88 if (query == null || query.getType() == null || "reset".equalsIgnoreCase(action) || (userPrefs != null && !userPrefs.getRememberLastSearch())) {
89 log.debug("New search query. No existing query, reset forced, or saved querys not allowed.");
90 query = new IssueSearchQuery();
91 query.setType(IssueSearchQuery.TYPE_FULL);
92 newQuery = true;
93 } else if (query.getType().intValue() == IssueSearchQuery.TYPE_FULL.intValue() && projectId != null) {
94 log.debug("New search query. Previous query FULL, new query PROJECT.");
95 query = new IssueSearchQuery();
96 query.setType(IssueSearchQuery.TYPE_PROJECT);
97 newQuery = true;
98 } else if (query.getType().intValue() == IssueSearchQuery.TYPE_PROJECT.intValue()) {
99 if (projectId == null || projectId.equals("")) {
100 log.debug("New search query. Previous query PROJECT, new query FULL.");
101 query = new IssueSearchQuery();
102 query.setType(IssueSearchQuery.TYPE_FULL);
103 newQuery = true;
104 } else if (!projectId.equals(query.getProjectId().toString())) {
105 log.debug("New search query. Requested project (" + projectId + ") different from previous query (" + query.getProjectId().toString() + ")");
106 query = new IssueSearchQuery();
107 query.setType(IssueSearchQuery.TYPE_PROJECT);
108 newQuery = true;
109 }
110 }
111
112 query.setAvailableProjects(null);
113
114 List<Project> projects = projectService.getAllAvailableProjects();
115
116 List<Project> availableProjectsList = new ArrayList<Project>();
117 List<Integer> selectedProjectsList = new ArrayList<Integer>();
118
119 for (Project project : projects) {
120 if (!UserUtilities.hasPermission(userPermissions, project.getId(), PermissionType.ISSUE_VIEW_ALL) &&
121 !UserUtilities.hasPermission(userPermissions, project.getId(), PermissionType.ISSUE_VIEW_USERS)) {
122 continue;
123 }
124
125 log.debug("Adding project " + project.getId() + " to list of available projects.");
126 availableProjectsList.add(project);
127
128 if (projectId != null && StringUtils.equals(String.valueOf(project.getId()), projectId)) {
129 query.setType(IssueSearchQuery.TYPE_PROJECT);
130 query.setProject(project);
131 String pageTitleKey = "itracker.web.search.project.title";
132 String pageTitleArg = project.getName();
133 request.setAttribute("pageTitleKey", pageTitleKey);
134 request.setAttribute("pageTitleArg", pageTitleArg);
135 break;
136 } else {
137 if (query.getProjects().contains(project.getId())) {
138 selectedProjectsList.add(project.getId());
139 }
140
141 }
142 }
143
144 if (!availableProjectsList.isEmpty()) {
145 log.debug("Issue Search has " + availableProjectsList.size() + " available projects.");
146
147 Collections.sort(availableProjectsList, new Project.ProjectComparator());
148 query.setAvailableProjects(availableProjectsList);
149 if (query.getType().equals(IssueSearchQuery.TYPE_PROJECT)) {
150 searchForm.setProject(query.getProjectId());
151 }
152
153 if (newQuery) {
154 log.debug("New search query. Clearing results and setting defaults.");
155 query.setResults(null);
156 List<Integer> selectedStatusesIntegerList = new ArrayList<Integer>();
157 for (int i = 0; i < IssueUtilities.getStatuses().size(); i++) {
158 try {
159 int statusNumber = Integer.parseInt(IssueUtilities.getStatuses().get(i).getValue());
160 if (statusNumber < IssueUtilities.STATUS_CLOSED) {
161 selectedStatusesIntegerList.add(statusNumber);
162 }
163 } catch (Exception e) {
164 log.debug("Invalid status entry: " + IssueUtilities.getStatuses().get(i));
165 }
166 }
167
168 Integer[] statusesArray = new Integer[selectedStatusesIntegerList.size()];
169 selectedStatusesIntegerList.toArray(statusesArray);
170 searchForm.setStatuses(statusesArray);
171
172 List<Integer> selectedSeverities = new ArrayList<Integer>();
173 for (int i = 1; i <= IssueUtilities.getNumberSeverities(); i++) {
174 selectedSeverities.add(i);
175 }
176
177 Integer[] severitiesArray = new Integer[selectedSeverities.size()];
178 selectedSeverities.toArray(severitiesArray);
179 searchForm.setSeverities(severitiesArray);
180 } else {
181 List<Integer> selectedProjects;
182 selectedProjects = selectedProjectsList;
183 query.setProjects(selectedProjects);
184
185 searchForm.setComponents(null);
186 if (null != query.getComponents() && query.getComponents().size() > 0) {
187 Integer[] componentsArray = new Integer[query.getComponents().size()];
188 query.getComponents().toArray(componentsArray);
189 searchForm.setComponents(componentsArray);
190 }
191
192 searchForm.setCreator(null);
193 if (null != query.getCreator()) {
194 searchForm.setCreator(query.getCreator().getId());
195 }
196 searchForm.setOwner(null);
197 if (null != query.getOwner()) {
198 searchForm.setOwner(query.getOwner().getId());
199 }
200 searchForm.setOrderBy(query.getOrderBy());
201 searchForm.setProject(query.getProjectId());
202
203 searchForm.setProjects(null);
204 if (null != query.getProjects() && query.getProjects().size() > 0) {
205 Integer[] projectsArray = new Integer[query.getProjects().size()];
206 query.getProjects().toArray(projectsArray);
207 searchForm.setProjects(projectsArray);
208 }
209
210 searchForm.setResolution(query.getResolution());
211
212 searchForm.setSeverities(null);
213 if (null != query.getSeverities() && query.getSeverities().size() > 0) {
214 Integer[] severitiesArray = new Integer[query.getSeverities().size()];
215 query.getSeverities().toArray(severitiesArray);
216 searchForm.setSeverities(severitiesArray);
217 }
218
219 searchForm.setStatuses(null);
220 if (null != query.getStatuses() && query.getStatuses().size() > 0) {
221 Integer[] statusesArray = new Integer[query.getStatuses().size()];
222 query.getStatuses().toArray(statusesArray);
223 searchForm.setStatuses(statusesArray);
224 }
225
226 searchForm.setTargetVersion(query.getTargetVersion());
227 searchForm.setTextphrase(query.getText());
228
229 searchForm.setVersions(null);
230 if (query.getVersions() != null && query.getVersions().size() > 0) {
231 Integer[] versionsArray = new Integer[query.getVersions().size()];
232 query.getVersions().toArray(versionsArray);
233 searchForm.setVersions(versionsArray);
234 }
235
236 }
237
238 request.setAttribute("searchForm", searchForm);
239
240 session.setAttribute(Constants.SEARCH_QUERY_KEY, query);
241
242 } else {
243 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.noprojects"));
244 }
245
246 if (errors.isEmpty()) {
247 return mapping.getInputForward();
248 }
249 } catch (Exception e) {
250 log.error("Exception while creating search issues form.", e);
251 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
252 }
253
254 if (!errors.isEmpty()) {
255 saveErrors(request, errors);
256 }
257
258 return mapping.findForward("error");
259 }
260
261 }