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.issuesearch;
20  
21  import java.io.IOException;
22  import java.lang.reflect.InvocationTargetException;
23  import java.util.Arrays;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpSession;
32  
33  import org.apache.commons.beanutils.PropertyUtils;
34  import org.apache.log4j.Logger;
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  import org.apache.struts.action.ActionMessage;
39  import org.apache.struts.action.ActionMessages;
40  import org.apache.struts.validator.ValidatorForm;
41  import org.itracker.model.Issue;
42  import org.itracker.model.IssueSearchQuery;
43  import org.itracker.model.PermissionType;
44  import org.itracker.model.User;
45  import org.itracker.services.ReportService;
46  import org.itracker.services.UserService;
47  import org.itracker.services.exceptions.IssueSearchException;
48  import org.itracker.web.actions.base.ItrackerBaseAction;
49  import org.itracker.web.util.Constants;
50  
51  public class SearchIssuesAction extends ItrackerBaseAction {
52  	private static final Logger log = Logger
53  			.getLogger(SearchIssuesAction.class);
54  
55  	public ActionForward execute(ActionMapping mapping, ActionForm form,
56  			HttpServletRequest request, HttpServletResponse response)
57  			throws ServletException, IOException {
58  		ActionMessages errors = new ActionMessages();
59  
60  		String pageTitleKey = "itracker.web.search.title";
61  		String pageTitleArg = "";
62  		request.setAttribute("pageTitleKey", pageTitleKey);
63  		request.setAttribute("pageTitleArg", pageTitleArg);
64  
65  		HttpSession session = request.getSession();
66  
67  		User user = (User) session.getAttribute(Constants.USER_KEY);
68  		Map<Integer, Set<PermissionType>> userPermissions = getUserPermissions(session);
69  
70  		try {
71  
72  			ReportService reportService = getITrackerServices()
73  					.getReportService();
74  			UserService userService = getITrackerServices().getUserService();
75  			request.setAttribute("rh", reportService);
76  			request.setAttribute("uh", userService);
77  
78  			IssueSearchQuery isqm = (IssueSearchQuery) session
79  					.getAttribute(Constants.SEARCH_QUERY_KEY);
80  			if (isqm == null) {
81  				return mapping.findForward("searchissues");
82  			}
83  			processQueryParameters(isqm, (ValidatorForm) form, errors);
84  
85  			if (errors.isEmpty()) {
86  				List<Issue> results = getITrackerServices().getIssueService()
87  						.searchIssues(isqm, user, userPermissions);
88  				if (log.isDebugEnabled()) {
89  					log.debug("SearchIssuesAction received " + results.size()
90  							+ " results to query.");
91  				}
92  
93  				isqm.setResults(results);
94  				log.debug("Setting search results with "
95  						+ isqm.getResults().size() + " results");
96  				session.setAttribute(Constants.SEARCH_QUERY_KEY, isqm);
97  			}
98  		} catch (IssueSearchException ise) {
99  			if (ise.getType() == IssueSearchException.ERROR_NULL_QUERY) {
100 				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
101 						"itracker.web.error.nullsearch"));
102 			} else {
103 				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
104 						"itracker.web.error.system"));
105 			}
106 		} catch (Exception e) {
107 			log.error(e.getMessage(), e);
108 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
109 					"itracker.web.error.system"));
110 		}
111 
112 		if (!errors.isEmpty()) {
113 			saveErrors(request, errors);
114 		}
115 
116 		return mapping.getInputForward();
117 	}
118 
119 	private IssueSearchQuery processQueryParameters(IssueSearchQuery isqm,
120 			ValidatorForm form, ActionMessages errors) {
121 		if (isqm == null) {
122 			isqm = new IssueSearchQuery();
123 		}
124 
125 		try {
126 			Integer creatorValue = (Integer) PropertyUtils.getSimpleProperty(
127 					form, "creator");
128 			if (creatorValue != null && creatorValue.intValue() != -1) {
129 				isqm.setCreator(getITrackerServices().getUserService().getUser(
130 						creatorValue));
131 			} else {
132 				isqm.setCreator(null);
133 			}
134 
135 			Integer ownerValue = (Integer) PropertyUtils.getSimpleProperty(
136 					form, "owner");
137 			if (ownerValue != null && ownerValue.intValue() != -1) {
138 				isqm.setOwner(getITrackerServices().getUserService().getUser(
139 						ownerValue));
140 			} else {
141 				isqm.setOwner(null);
142 			}
143 
144 			String textValue = (String) PropertyUtils.getSimpleProperty(form,
145 					"textphrase");
146 			if (textValue != null && textValue.trim().length() > 0) {
147 				isqm.setText(textValue.trim());
148 			} else {
149 				isqm.setText(null);
150 			}
151 
152 			String resolutionValue = (String) PropertyUtils.getSimpleProperty(
153 					form, "resolution");
154 			if (resolutionValue != null && !resolutionValue.equals("")) {
155 				isqm.setResolution(resolutionValue);
156 			} else {
157 				isqm.setResolution(null);
158 			}
159 
160 			Integer[] projectsArray = (Integer[]) PropertyUtils
161 					.getSimpleProperty(form, "projects");
162 			List<Integer> projects = Arrays.asList(projectsArray);
163 			if (projects == null || projects.size() == 0) {
164 				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
165 						"itracker.web.error.projectrequired"));
166 			} else {
167 				isqm.setProjects(projects);
168 			}
169 
170 			Integer[] severitiesArray = (Integer[]) PropertyUtils
171 					.getSimpleProperty(form, "severities");
172 			if (severitiesArray != null && severitiesArray.length > 0) {
173 				List<Integer> severities = Arrays.asList(severitiesArray);
174 				isqm.setSeverities(severities);
175 			} else {
176 				isqm.setSeverities(null);
177 			}
178 
179 			Integer[] statusesArray = (Integer[]) PropertyUtils
180 					.getSimpleProperty(form, "statuses");
181 			if (statusesArray != null && statusesArray.length > 0) {
182 				List<Integer> statuses = Arrays.asList(statusesArray);
183 				isqm.setStatuses(statuses);
184 			} else {
185 				isqm.setStatuses(null);
186 			}
187 
188 			Integer[] componentsArray = (Integer[]) PropertyUtils
189 					.getSimpleProperty(form, "components");
190 			if (componentsArray != null && componentsArray.length > 0) {
191 				List<Integer> components = Arrays.asList(componentsArray);
192 				isqm.setComponents(components);
193 			} else {
194 				isqm.setComponents(null);
195 			}
196 
197 			Integer[] versionsArray = (Integer[]) PropertyUtils
198 					.getSimpleProperty(form, "versions");
199 			if (versionsArray != null && versionsArray.length > 0) {
200 				List<Integer> versions = Arrays.asList(versionsArray);
201 				isqm.setVersions(versions);
202 			} else {
203 				isqm.setVersions(null);
204 			}
205 
206 			Integer targetVersion = (Integer) PropertyUtils.getSimpleProperty(
207 					form, "targetVersion");
208 			if (targetVersion != null && targetVersion > 0) {
209 				isqm.setTargetVersion(targetVersion);
210 			} else {
211 				isqm.setTargetVersion(null);
212 			}
213 
214 			String orderBy = (String) PropertyUtils.getSimpleProperty(form,
215 					"orderBy");
216 			if (orderBy != null && !orderBy.equals("")) {
217 				if (log.isDebugEnabled()) {
218 					log
219 							.debug("processQueryParameters: set orderBy: "
220 									+ orderBy);
221 				}
222 				isqm.setOrderBy(orderBy);
223 			}
224 
225 			Integer type = (Integer) PropertyUtils.getSimpleProperty(form,
226 					"type");
227 			if (type != null) {
228 				if (log.isDebugEnabled()) {
229 					log.debug("processQueryParameters: set type: " + type);
230 				}
231 				isqm.setType(type);
232 			}
233 		} catch (RuntimeException e) {
234 			log.error(
235 					"processQueryParameters: Unable to parse search query parameters: "
236 							+ e.getMessage(), e);
237 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
238 					"itracker.web.error.invalidsearchquery"));
239 		} catch (IllegalAccessException e) {
240 			log.error(
241 					"processQueryParameters: Unable to parse search query parameters: "
242 							+ e.getMessage(), e);
243 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
244 					"itracker.web.error.invalidsearchquery"));
245 		} catch (InvocationTargetException e) {
246 			log.error(
247 					"processQueryParameters: Unable to parse search query parameters: "
248 							+ e.getMessage(), e);
249 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
250 					"itracker.web.error.invalidsearchquery"));
251 		} catch (NoSuchMethodException e) {
252 			log.error(
253 					"processQueryParameters: Unable to parse search query parameters: "
254 							+ e.getMessage(), e);
255 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
256 					"itracker.web.error.invalidsearchquery"));
257 		}
258 
259 		return isqm;
260 	}
261 }