/**
   * Serializes list of ldap users matching filter.
   *
   * @param context the current request context
   * @param filter the user search filter for ldap
   * @return the list of users as json
   * @throws IdentityException if a system error occurs preventing the action
   * @throws NamingException if an LDAP naming exception occurs
   * @throws SQLException
   */
  protected String serializeUsersAsJson(
      RequestContext context, String filter, String attributeName, boolean isMemberSearch)
      throws IdentityException, NamingException, SQLException {
    Users users = new Users();
    int totalMatches = 0;
    if (!isMemberSearch) {
      HashMap<String, Object> resultsMap = buildUsersList(context, filter, null);
      users = (Users) resultsMap.get("topUserMatches");
      totalMatches = (Integer) resultsMap.get("totalMatches");
    } else if (isMemberSearch && attributeName != null) {
      Roles configuredRoles = context.getIdentityConfiguration().getConfiguredRoles();
      Role role = configuredRoles.get(attributeName);
      String sDn = role.getDistinguishedName();
      IdentityAdapter idAdapter = context.newIdentityAdapter();
      users = idAdapter.readGroupMembers(sDn);
      totalMatches = users.size();
      users.sort();
    } else {
      IdentityAdapter idAdapter = context.newIdentityAdapter();
      Users members = idAdapter.readGroupMembers(filter);
      for (User u : members.values()) {
        users.add(u);
      }
      users.sort();
      totalMatches = users.size();
    }

    String usersJson =
        "{ \"totalUsers\" : \""
            + totalMatches
            + "\" ,\"topUsers\" : \""
            + users.size()
            + "\" , \"users\": [";
    boolean firstUser = true;
    for (User user : users.values()) {
      String userName = user.getName();
      String dn = user.getKey();
      if (!firstUser) {
        usersJson += ",";
      } else {
        firstUser = false;
      }
      usersJson +=
          " { \"dn\" : \""
              + dn
              + "\" , \"userName\" : \""
              + Val.escapeStrForJson(userName)
              + "\" }";
    }
    usersJson += " ] }";
    return usersJson;
  }
  /**
   * Builds list of ldap users matching filter.
   *
   * @param context the current request context (contains the active user)
   * @param filter the user search filter for ldap
   * @return the list of users matching filter
   * @throws IdentityException if a system error occurs preventing the action
   * @throws NamingException if an LDAP naming exception occurs
   */
  protected HashMap<String, Object> buildUsersList(
      RequestContext context, String filter, String attributeName)
      throws IdentityException, NamingException {
    HashMap<String, Object> resultsMap = new HashMap<String, Object>();
    IdentityAdapter idAdapter = context.newIdentityAdapter();
    String searchLimit =
        Val.chkStr(
            context
                .getCatalogConfiguration()
                .getParameters()
                .getValue("ldap.identity.search.maxResults"));
    int srchLimit = -1;
    if (searchLimit.length() > 0) {
      srchLimit = Integer.parseInt(searchLimit);
    }
    Users users = idAdapter.readUsers(filter, attributeName);
    users.sort();
    int totalMatches = users.size();
    resultsMap.put("totalMatches", totalMatches);
    if (srchLimit == -1) {
      resultsMap.put("topUserMatches", users);
      return resultsMap;
    }

    if (attributeName != null) {
      resultsMap.put("topUserMatches", users);
      return resultsMap;
    }
    Users topUserMatches = new Users();
    int count = 0;
    for (User user : users.values()) {
      count++;
      if (count <= srchLimit) {
        topUserMatches.add(user);
      } else {
        break;
      }
    }
    resultsMap.put("topUserMatches", topUserMatches);
    return resultsMap;
  }