/**
   * Gets the groups that match the specified criteria.
   *
   * @param criteria the criteria in searching of user groups.
   * @return an array of user groups matching the criteria or an empty array of no ones are found.
   * @throws AdminException if an error occurs while getting the user groups.
   */
  public Group[] getGroupsMatchingCriteria(final GroupSearchCriteriaForDAO criteria)
      throws AdminException {
    Connection connection = null;
    try {
      connection = DBUtil.makeConnection(JNDINames.ADMIN_DATASOURCE);

      List<Group> groups =
          groupDao.getGroupsByCriteria(connection, (GroupSearchCriteriaForDAO) criteria);

      String domainIdConstraint = null;
      List<String> domainIds = criteria.getCriterionOnDomainIds();
      for (String domainId : domainIds) {
        if (!domainId.equals(Domain.MIXED_DOMAIN_ID)) {
          domainIdConstraint = domainId;
          break;
        }
      }

      SearchCriteriaDAOFactory factory = SearchCriteriaDAOFactory.getFactory();
      for (Group group : groups) {
        List<String> groupIds = getAllSubGroupIdsRecursively(group.getId());
        groupIds.add(group.getId());
        UserSearchCriteriaForDAO criteriaOnUsers = factory.getUserSearchCriteriaDAO();
        List<UserDetail> users =
            userDao.getUsersByCriteria(
                connection,
                criteriaOnUsers
                    .onDomainId(domainIdConstraint)
                    .and()
                    .onGroupIds(groupIds.toArray(new String[groupIds.size()])));
        group.setTotalNbUsers(users.size());
      }
      return groups.toArray(new Group[groups.size()]);
    } catch (Exception e) {
      throw new AdminException(
          "GroupManager.getGroupsMatchingCriteria",
          SilverpeasException.ERROR,
          "admin.EX_ERR_GET_USER_GROUPS",
          e);
    } finally {
      DBUtil.close(connection);
    }
  }