public static List<Issue> executeJQLQuery(String jqlQuery) {
    List<Issue> result = null;

    User user = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();
    SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlQuery);

    if (parseResult.isValid()) {
      Query query = parseResult.getQuery();
      try {
        SearchResults results = searchService.search(user, query, PagerFilter.getUnlimitedFilter());
        result = results.getIssues();
      } catch (SearchException e) {
        log.error("Utils::search exception during executing JQL", e);
      }
    }

    return result;
  }
 private String createIssueNavigatorUrl(
     final SearchRequest searchRequest,
     final VelocityRequestContext velocityRequestContext,
     final User remoteUser) {
   if (searchRequest == null) {
     return null;
   } else {
     final Query optimizedQuery =
         new RedundantClausesQueryOptimizer().optimizeQuery(searchRequest.getQuery());
     return velocityRequestContext.getCanonicalBaseUrl()
         + "/secure/IssueNavigator.jspa?reset=true"
         + searchService.getQueryString(remoteUser, optimizedQuery);
   }
 }
  /**
   * Test that the manager executes the correct search to find the recent comments.
   *
   * @param issueIds the issue ids to limit the comment search by.
   * @param request the search request being used to find the comments. This is the request that is
   *     normally used to find all the issue ids to form the scope of the comment search. The
   *     request may also be used to add extra "update date" criteria to the comment search.
   * @param user the user performing the search.
   * @param includeDates true if and only if the passed search request should be used to create
   *     extra "update date" criteria.
   * @param expectedQuery the query that we expect to be generated by the manager.
   * @throws Exception this is a test, just rethrow for a failure.
   */
  private void _testGetRecentComments(
      final List<Long> issueIds,
      final SearchRequest request,
      final User user,
      final boolean includeDates,
      final org.apache.lucene.search.Query expectedQuery)
      throws Exception {
    // the issue searcher is never called, but we need it there.
    final MockControl mockIssueSearcherControl =
        MockClassControl.createControl(IndexSearcher.class);
    final IndexSearcher mockIssueSearcher = (IndexSearcher) mockIssueSearcherControl.getMock();
    mockIssueSearcherControl.replay();

    // this is where we check to make sure search lucene with the correct comment.
    final MockControl mockCommentSearcherControl =
        MockClassControl.createControl(IndexSearcher.class);
    final IndexSearcher mockCommentSearcher = (IndexSearcher) mockCommentSearcherControl.getMock();

    final Sort sort =
        new Sort(
            new SortField[] {
              new SortField(DocumentConstants.COMMENT_UPDATED, SortField.STRING, true)
            });
    final ArgumentsMatcher compositeArgumentsMatcher =
        ArgumentsMatcherBuilder.newNaturalBuilder()
            .addDefaultMatcher()
            .addDefaultMatcher()
            .addArgumentMatcher(Sort.class, new LuceneSortMatcher())
            .asArgumentsMatcher();

    mockCommentSearcher.search(expectedQuery, Integer.MAX_VALUE, sort);
    mockCommentSearcherControl.setMatcher(compositeArgumentsMatcher);
    mockCommentSearcherControl.setReturnValue(null);
    mockCommentSearcherControl.replay();

    final MockSearchProviderFactory factory = new MockSearchProviderFactory();
    factory.addRegistration(SearchProviderFactory.ISSUE_INDEX, mockIssueSearcher);
    factory.addRegistration(SearchProviderFactory.COMMENT_INDEX, mockCommentSearcher);

    // create a IssueSearchProvider that will return our constant list of ids.
    MockIssueSearchProvider mockIssueSearchProvider =
        new MockIssueSearchProvider(issueIds, user, request.getQuery());
    final SearchService service = EasyMock.createMock(SearchService.class);
    EasyMock.expect(service.doesQueryFitFilterForm(user, request.getQuery()))
        .andReturn(includeDates);
    EasyMock.replay(service);

    MyLuceneQueryModifier myLuceneQueryModifier = new MyLuceneQueryModifier();

    CommentService commentService = EasyMock.createMock(CommentService.class);
    JqlOperandResolver jqlOperandResolver = MockJqlOperandResolver.createSimpleSupport();
    final DefaultRecentCommentManager defaultCommentManager =
        new DefaultRecentCommentManager(
            commentService,
            factory,
            mockIssueSearchProvider,
            dateSupport,
            jqlOperandResolver,
            service,
            myLuceneQueryModifier);
    defaultCommentManager.getRecentComments(request, user);

    mockIssueSearcherControl.verify();
    mockCommentSearcherControl.verify();
  }
  @Override
  public Map<String, Object> getVelocityParameters(
      Issue issue, CustomField field, FieldLayoutItem fieldLayoutItem) {
    Map<String, Object> params = super.getVelocityParameters(issue, field, fieldLayoutItem);
    params.put("i18n", getI18nBean());
    params.put("baseUrl", applicationProperties.getBaseUrl());

    Long prId;
    if (field.isAllProjects()) {
      prId = Consts.PROJECT_ID_FOR_GLOBAL_CF;
    } else {
      if (issue == null) {
        return params;
      }
      prId = issue.getProjectObject().getId();
    }

    String jqlData = qfMgr.getQueryFieldData(field.getIdAsLong(), prId);
    boolean addNull = qfMgr.getAddNull(field.getIdAsLong(), prId);
    boolean isAutocompleteView = qfMgr.isAutocompleteView(field.getIdAsLong(), prId);
    List<String> options = qfMgr.getLinkeFieldsOptions(field.getIdAsLong(), prId);

    params.put("isAutocompleteView", isAutocompleteView);
    params.put("prId", prId.toString());

    String cfValue = field.getValueFromIssue(issue);
    if (Utils.isValidStr(cfValue)) {
      MutableIssue mi = issueMgr.getIssueObject(cfValue);
      if (mi != null && Utils.isValidStr(mi.getSummary())) {
        StringBuilder sb = new StringBuilder();
        if (options.contains("status")) {
          sb.append(getI18nBean().getText("queryfields.opt.status"))
              .append(": ")
              .append(mi.getStatusObject().getName());
        }
        if (options.contains("assignee") && mi.getAssigneeUser() != null) {
          if (sb.length() > 0) {
            sb.append(", ");
          }
          User aUser = mi.getAssigneeUser();
          String encodedUser;
          try {
            encodedUser = URLEncoder.encode(aUser.getName(), "UTF-8");
          } catch (UnsupportedEncodingException e) {
            // --> impossible
            encodedUser = aUser.getName();
          }

          sb.append(getI18nBean().getText("queryfields.opt.assignee"))
              .append(": ")
              .append("<a class='user-hover' rel='")
              .append(aUser.getName())
              .append("' id='issue_summary_assignee_'")
              .append(aUser.getName())
              .append("' href='/secure/ViewProfile.jspa?name='")
              .append(encodedUser)
              .append("'>")
              .append(aUser.getDisplayName())
              .append("</a>");
        }
        if (options.contains("priority") && mi.getPriorityObject() != null) {
          if (sb.length() > 0) {
            sb.append(", ");
          }
          sb.append(getI18nBean().getText("queryfields.opt.priority"))
              .append(": ")
              .append(mi.getPriorityObject().getName());
        }
        if (options.contains("due") && mi.getDueDate() != null) {
          if (sb.length() > 0) {
            sb.append(", ");
          }
          sb.append(getI18nBean().getText("queryfields.opt.due"))
              .append(": ")
              .append(
                  ComponentAccessor.getJiraAuthenticationContext()
                      .getOutlookDate()
                      .format(mi.getDueDate()));
        }

        if (sb.length() > 0) {
          sb.insert(0, " (");
          sb.append(")");
        }

        IssueData issueData;
        if (options.contains("justDesc")) {
          String descr = mi.getDescription();
          if (Utils.isValidStr(descr)) {
            issueData = new IssueData(descr, sb.toString());
          } else {
            issueData = new IssueData(mi.getSummary(), sb.toString());
          }
        } else if (options.contains("key")) {
          issueData = new IssueData(mi.getKey().concat(":").concat(mi.getSummary()), sb.toString());
        } else {
          issueData = new IssueData(mi.getSummary(), sb.toString());
        }
        params.put("fullValue", issueData);
      }
    }

    if (!Utils.isValidStr(jqlData)) {
      params.put("jqlNotSet", Boolean.TRUE);
      return params;
    }
    params.put("jqlNotSet", Boolean.FALSE);
    params.put("options", options);

    if (options.contains("editKey")) {
      params.put("hasKey", Boolean.TRUE);
    }

    User user = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();
    SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlData);
    if (parseResult.isValid()) {
      params.put("jqlNotValid", Boolean.FALSE);
      Query query = parseResult.getQuery();
      try {
        Map<String, String> cfVals = new LinkedHashMap<String, String>();
        SearchResults results = searchService.search(user, query, PagerFilter.getUnlimitedFilter());
        List<Issue> issues = results.getIssues();
        for (Issue i : issues) {
          String summary;
          if (options.contains("justDesc")) {
            String descr = i.getDescription();
            if (Utils.isValidStr(descr)) {
              summary = descr;
            } else {
              summary = i.getSummary();
            }
          } else if (options.contains("editKey")) {
            summary = i.getKey().concat(":").concat(i.getSummary());
          } else {
            summary = i.getSummary();
          }
          cfVals.put(i.getKey(), summary);
        }

        if (addNull) {
          cfVals.put("Empty", Consts.EMPTY_VALUE);
        }

        String selected = Consts.EMPTY_VALUE;
        String value = (String) issue.getCustomFieldValue(field);
        for (Map.Entry<String, String> cf : cfVals.entrySet()) {
          if (value != null && cf.getKey().equals(value)) {
            selected = value;
            break;
          }
        }

        if (isAutocompleteView) {
          Issue selectedIssue = issueMgr.getIssueObject(selected);
          if (selectedIssue != null) {
            params.put("selIssue", selectedIssue);
          }
        } else {
          if (selected.equals("")) {
            String defaultValue = (String) field.getDefaultValue(issue);
            if (defaultValue != null
                && defaultValue.length() > 0
                && cfVals.keySet().contains(defaultValue)) {
              selected = defaultValue;
            }
          }

          if (cfVals != null && !cfVals.isEmpty() && selected.equals("")) {
            selected = cfVals.keySet().iterator().next();
          }
        }

        params.put("selected", selected);
        params.put("isError", Boolean.FALSE);
        params.put("cfVals", cfVals);
      } catch (SearchException e) {
        params.put("isError", Boolean.TRUE);
      }
    } else {
      params.put("jqlNotValid", Boolean.TRUE);
      return params;
    }

    return params;
  }