SearchIssuesAction.java

  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. package org.itracker.web.actions.issuesearch;

  19. import org.apache.commons.beanutils.PropertyUtils;
  20. import org.apache.log4j.Logger;
  21. import org.apache.struts.action.*;
  22. import org.apache.struts.validator.ValidatorForm;
  23. import org.itracker.IssueSearchException;
  24. import org.itracker.model.Issue;
  25. import org.itracker.model.IssueSearchQuery;
  26. import org.itracker.model.PermissionType;
  27. import org.itracker.model.User;
  28. import org.itracker.services.ReportService;
  29. import org.itracker.services.UserService;
  30. import org.itracker.web.actions.base.ItrackerBaseAction;
  31. import org.itracker.web.util.Constants;
  32. import org.itracker.web.util.RequestHelper;
  33. import org.itracker.web.util.ServletContextUtils;

  34. import javax.servlet.ServletException;
  35. import javax.servlet.http.HttpServletRequest;
  36. import javax.servlet.http.HttpServletResponse;
  37. import javax.servlet.http.HttpSession;
  38. import java.io.IOException;
  39. import java.lang.reflect.InvocationTargetException;
  40. import java.util.Arrays;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.Set;

  44. public class SearchIssuesAction extends ItrackerBaseAction {
  45.     private static final Logger log = Logger
  46.             .getLogger(SearchIssuesAction.class);

  47.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  48.                                  HttpServletRequest request, HttpServletResponse response)
  49.             throws ServletException, IOException {
  50.         ActionMessages errors = new ActionMessages();

  51.         String pageTitleKey = "itracker.web.search.title";
  52.         String pageTitleArg = "";
  53.         request.setAttribute("pageTitleKey", pageTitleKey);
  54.         request.setAttribute("pageTitleArg", pageTitleArg);

  55.         HttpSession session = request.getSession();

  56.         User user = (User) session.getAttribute(Constants.USER_KEY);
  57.         Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);

  58.         try {

  59.             ReportService reportService = ServletContextUtils.getItrackerServices()
  60.                     .getReportService();
  61.             UserService userService = ServletContextUtils.getItrackerServices().getUserService();
  62.             request.setAttribute("rh", reportService);
  63.             request.setAttribute("uh", userService);

  64.             IssueSearchQuery isqm = (IssueSearchQuery) session
  65.                     .getAttribute(Constants.SEARCH_QUERY_KEY);
  66.             if (isqm == null) {
  67.                 return mapping.findForward("searchissues");
  68.             }
  69.             processQueryParameters(isqm, (ValidatorForm) form, errors);

  70.             if (errors.isEmpty()) {
  71.                 List<Issue> results = ServletContextUtils.getItrackerServices().getIssueService()
  72.                         .searchIssues(isqm, user, userPermissions);
  73.                 if (log.isDebugEnabled()) {
  74.                     log.debug("SearchIssuesAction received " + results.size()
  75.                             + " results to query.");
  76.                 }

  77.                 isqm.setResults(results);
  78.                 log.debug("Setting search results with "
  79.                         + isqm.getResults().size() + " results");
  80.                 session.setAttribute(Constants.SEARCH_QUERY_KEY, isqm);
  81.             }
  82.         } catch (IssueSearchException ise) {
  83.             if (ise.getType() == IssueSearchException.ERROR_NULL_QUERY) {
  84.                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  85.                         "itracker.web.error.nullsearch"));
  86.             } else {
  87.                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  88.                         "itracker.web.error.system"));
  89.             }
  90.         } catch (Exception e) {
  91.             log.error(e.getMessage(), e);
  92.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  93.                     "itracker.web.error.system"));
  94.         }

  95.         if (!errors.isEmpty()) {
  96.             saveErrors(request, errors);
  97.         }

  98.         return mapping.getInputForward();
  99.     }

  100.     private IssueSearchQuery processQueryParameters(IssueSearchQuery isqm,
  101.                                                     ValidatorForm form, ActionMessages errors) {
  102.         if (isqm == null) {
  103.             isqm = new IssueSearchQuery();
  104.         }

  105.         try {
  106.             Integer creatorValue = (Integer) PropertyUtils.getSimpleProperty(
  107.                     form, "creator");
  108.             if (creatorValue != null && creatorValue.intValue() != -1) {
  109.                 isqm.setCreator(ServletContextUtils.getItrackerServices().getUserService().getUser(
  110.                         creatorValue));
  111.             } else {
  112.                 isqm.setCreator(null);
  113.             }

  114.             Integer ownerValue = (Integer) PropertyUtils.getSimpleProperty(
  115.                     form, "owner");
  116.             if (ownerValue != null && ownerValue.intValue() != -1) {
  117.                 isqm.setOwner(ServletContextUtils.getItrackerServices().getUserService().getUser(
  118.                         ownerValue));
  119.             } else {
  120.                 isqm.setOwner(null);
  121.             }

  122.             String textValue = (String) PropertyUtils.getSimpleProperty(form,
  123.                     "textphrase");
  124.             if (textValue != null && textValue.trim().length() > 0) {
  125.                 isqm.setText(textValue.trim());
  126.             } else {
  127.                 isqm.setText(null);
  128.             }

  129.             String resolutionValue = (String) PropertyUtils.getSimpleProperty(
  130.                     form, "resolution");
  131.             if (resolutionValue != null && !resolutionValue.equals("")) {
  132.                 isqm.setResolution(resolutionValue);
  133.             } else {
  134.                 isqm.setResolution(null);
  135.             }

  136.             Integer[] projectsArray = (Integer[]) PropertyUtils
  137.                     .getSimpleProperty(form, "projects");
  138.             List<Integer> projects = Arrays.asList(projectsArray);
  139.             if (projects == null || projects.size() == 0) {
  140.                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  141.                         "itracker.web.error.projectrequired"));
  142.             } else {
  143.                 isqm.setProjects(projects);
  144.             }

  145.             Integer[] severitiesArray = (Integer[]) PropertyUtils
  146.                     .getSimpleProperty(form, "severities");
  147.             if (severitiesArray != null && severitiesArray.length > 0) {
  148.                 List<Integer> severities = Arrays.asList(severitiesArray);
  149.                 isqm.setSeverities(severities);
  150.             } else {
  151.                 isqm.setSeverities(null);
  152.             }

  153.             Integer[] statusesArray = (Integer[]) PropertyUtils
  154.                     .getSimpleProperty(form, "statuses");
  155.             if (statusesArray != null && statusesArray.length > 0) {
  156.                 List<Integer> statuses = Arrays.asList(statusesArray);
  157.                 isqm.setStatuses(statuses);
  158.             } else {
  159.                 isqm.setStatuses(null);
  160.             }

  161.             Integer[] componentsArray = (Integer[]) PropertyUtils
  162.                     .getSimpleProperty(form, "components");
  163.             if (componentsArray != null && componentsArray.length > 0) {
  164.                 List<Integer> components = Arrays.asList(componentsArray);
  165.                 isqm.setComponents(components);
  166.             } else {
  167.                 isqm.setComponents(null);
  168.             }

  169.             Integer[] versionsArray = (Integer[]) PropertyUtils
  170.                     .getSimpleProperty(form, "versions");
  171.             if (versionsArray != null && versionsArray.length > 0) {
  172.                 List<Integer> versions = Arrays.asList(versionsArray);
  173.                 isqm.setVersions(versions);
  174.             } else {
  175.                 isqm.setVersions(null);
  176.             }

  177.             Integer targetVersion = (Integer) PropertyUtils.getSimpleProperty(
  178.                     form, "targetVersion");
  179.             if (targetVersion != null && targetVersion > 0) {
  180.                 isqm.setTargetVersion(targetVersion);
  181.             } else {
  182.                 isqm.setTargetVersion(null);
  183.             }

  184.             String orderBy = (String) PropertyUtils.getSimpleProperty(form,
  185.                     "orderBy");
  186.             if (orderBy != null && !orderBy.equals("")) {
  187.                 if (log.isDebugEnabled()) {
  188.                     log
  189.                             .debug("processQueryParameters: set orderBy: "
  190.                                     + orderBy);
  191.                 }
  192.                 isqm.setOrderBy(orderBy);
  193.             }

  194.             Integer type = (Integer) PropertyUtils.getSimpleProperty(form,
  195.                     "type");
  196.             if (type != null) {
  197.                 if (log.isDebugEnabled()) {
  198.                     log.debug("processQueryParameters: set type: " + type);
  199.                 }
  200.                 isqm.setType(type);
  201.             }
  202.         } catch (RuntimeException e) {
  203.             log.error(
  204.                     "processQueryParameters: Unable to parse search query parameters: "
  205.                             + e.getMessage(), e);
  206.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  207.                     "itracker.web.error.invalidsearchquery"));
  208.         } catch (IllegalAccessException e) {
  209.             log.error(
  210.                     "processQueryParameters: Unable to parse search query parameters: "
  211.                             + e.getMessage(), e);
  212.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  213.                     "itracker.web.error.invalidsearchquery"));
  214.         } catch (InvocationTargetException e) {
  215.             log.error(
  216.                     "processQueryParameters: Unable to parse search query parameters: "
  217.                             + e.getMessage(), e);
  218.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  219.                     "itracker.web.error.invalidsearchquery"));
  220.         } catch (NoSuchMethodException e) {
  221.             log.error(
  222.                     "processQueryParameters: Unable to parse search query parameters: "
  223.                             + e.getMessage(), e);
  224.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  225.                     "itracker.web.error.invalidsearchquery"));
  226.         }

  227.         return isqm;
  228.     }
  229. }