private void checkSearchForUnsupportedTerms(
     String searchString, AdvancedUserQueryResults results, ServiceContext context) {
   if (searchString != null && results != null) {
     // assume non-alphanumeric covers operators, conditions, multiple terms, quotes, ...
     if (!isAlphanumeric(searchString)) {
       results.setMessage(context.getText("csum.manageusersadvanced.warning.unsupportedterm"));
     }
   }
 }
  public AdvancedUserQueryResults findUsers(
      AdvancedUserQuery advancedUserQuery, ServiceContext context) throws FindException {
    log.debug("findUsers() called.");
    AdvancedUserQueryResults results = new AdvancedUserQueryResults();

    // TODO: this is really slow with osuser search. must use
    // http://confluence.atlassian.com/display/DOC/How+to+Improve+User+Search+Performance

    checkSearchForUnsupportedTerms(advancedUserQuery.getPartialSearchTerm(), results, context);

    Pager pager = new DefaultPager(new ArrayList());
    if (advancedUserQuery.isUsernameSearchDefined()) {
      try {
        UserNameTermQuery query =
            new UserNameTermQuery(
                advancedUserQuery.getPartialSearchTerm(),
                advancedUserQuery.getSubstringMatchType());
        SearchResult result = userAccessor.findUsers(query);
        pager = result.pager();
        // results.setMessage("" + PagerUtils.count(pager) + " returned");
      } catch (EntityException e) {
        LogUtil.warnWithRemoteUserInfo(log, "query by username failed due to EntityException", e);
        results.setMessage("" + e);
      } catch (IllegalArgumentException e) {
        // if search type is not allowed
        LogUtil.warnWithRemoteUserInfo(
            log,
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'",
            e);
        results.setMessage(
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'");
      }
    }

    if (advancedUserQuery.isFullnameSearchDefined()) {
      try {
        FullNameTermQuery query =
            new FullNameTermQuery(
                advancedUserQuery.getPartialSearchTerm(),
                advancedUserQuery.getSubstringMatchType());
        SearchResult result = userAccessor.findUsers(query);
        pager = result.pager();
        // results.setMessage("" + PagerUtils.count(pager) + " returned");
      } catch (EntityException e) {
        LogUtil.warnWithRemoteUserInfo(
            log, "query by user fullname failed due to EntityException", e);
        results.setMessage("" + e);
      } catch (IllegalArgumentException e) {
        // if search type is not allowed
        LogUtil.warnWithRemoteUserInfo(
            log,
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'",
            e);
        results.setMessage(
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'");
      }
    }

    if (advancedUserQuery.isEmailSearchDefined()) {
      try {
        EmailTermQuery query =
            new EmailTermQuery(
                advancedUserQuery.getPartialSearchTerm(),
                advancedUserQuery.getSubstringMatchType());
        SearchResult result = userAccessor.findUsers(query);
        pager = result.pager();
        // results.setMessage("" + PagerUtils.count(pager) + " returned");
      } catch (EntityException e) {
        LogUtil.warnWithRemoteUserInfo(log, "query by user email failed due to EntityException", e);
        results.setMessage("" + e);
      } catch (IllegalArgumentException e) {
        // if search type is not allowed
        LogUtil.warnWithRemoteUserInfo(
            log,
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'",
            e);
        results.setMessage(
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'");
      }
    }

    results.setUsers(pager);

    return results;
  }