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