/**
  * Called from getChildren if in flat mode
  *
  * @param start
  * @param pageSize
  * @param totalCount
  * @param context
  * @param request
  * @return
  * @throws Exception
  */
 protected Set getFlatChildren(
     int start, int pageSize, StringBuffer totalCount, String context, HttpServletRequest request)
     throws Exception {
   if ("stem".equals(flatType)) {
     List l =
         GrouperHelper.stems2Maps(
             s,
             LowLevelGrouperCapableAction.sort(
                 GrouperHelper.getStemsForPrivileges(s, flatPrivs, start, pageSize, totalCount),
                 request,
                 context,
                 -1,
                 null));
     return new LinkedHashSet(l);
   }
   if (flatPrivs.length == 1 && flatPrivs[0].equals("member")) {
     /*Set tmp = GrouperHelper.getMembershipsSet(getGrouperSession(),start, pageSize, totalCount);*/
     Set tmp = GrouperHelper.getMembershipsSet(getGrouperSession());
     totalCount.append("" + tmp.size());
     List tmpList = LowLevelGrouperCapableAction.sort(tmp, request, context, -1, null);
     int end = start + pageSize;
     if (end > tmpList.size()) end = tmpList.size();
     tmpList = GrouperHelper.groups2Maps(getGrouperSession(), tmpList.subList(start, end));
     return new LinkedHashSet(tmpList);
   }
   Set groupsForPrivileges =
       GrouperHelper.getGroupsForPrivileges(s, flatPrivs, start, pageSize, totalCount);
   List l =
       GrouperHelper.groups2Maps(
           s, LowLevelGrouperCapableAction.sort(groupsForPrivileges, request, context, -1, null));
   return new LinkedHashSet(l);
 }
  /**
   * @see edu.internet2.middleware.grouper.ui.RepositoryBrowser#getChildren(java.lang.String, int,
   *     int, java.lang.StringBuffer, boolean, boolean)
   */
  public Set getChildren(
      String node,
      String listField,
      int start,
      int pageSize,
      StringBuffer totalCount,
      boolean isFlat,
      boolean isForAssignment,
      String omitForAssignment,
      String context,
      HttpServletRequest request)
      throws Exception {

    if (isFlat) return getFlatChildren(start, pageSize, totalCount, "flat", request);

    Set results = new LinkedHashSet();
    GroupOrStem groupOrStem = GroupOrStem.findByID(s, node);
    Group group = groupOrStem.getGroup();
    Stem stem = groupOrStem.getStem();
    if (listField == null || "".equals(listField)) listField = "members";
    Field field = FieldFinder.find(listField, true);
    List sortedChildren = null;
    int[] resultSizeArray = new int[1];
    int resultSize = 0;
    if (isForAssignment) {
      if (group != null) { // display immediate members

        Set<Membership> allChildren = new LinkedHashSet<Membership>();
        ResourceBundle resourceBundle = GrouperUiFilter.retrieveSessionMediaResourceBundle();
        String sortLimitString = resourceBundle.getString("comparator.sort.limit");
        int sortLimit = Integer.parseInt(sortLimitString);

        allChildren =
            MembershipFinder.internal_findAllImmediateByGroupAndFieldAndPage(
                group, field, start, pageSize, sortLimit, resultSizeArray);
        resultSize = resultSizeArray[0];
        sortedChildren =
            LowLevelGrouperCapableAction.sort(allChildren, request, context, resultSize, null);

        int groupList2SubjectStart = (start >= sortedChildren.size()) ? 0 : start;

        results.addAll(
            GrouperHelper.groupList2SubjectsMaps(
                s, sortedChildren, groupList2SubjectStart, pageSize));
        if (totalCount != null) {
          totalCount.setLength(0);
          totalCount.append(resultSize);
        }
        return results;
      }
    } else if (group != null) return results;
    Set<GroupAsMap> allChildren = new LinkedHashSet<GroupAsMap>();

    // must be stem
    String stemName = null;
    if (stem != null) {
      stemName = stem.getName();
    } else if (GrouperHelper.NS_ROOT.equals(node)) {
      stemName = node;
    } else {
      throw new RuntimeException(node + " is not recognised");
    }
    List<GroupAsMap> listOfMaps = getChildrenAsMaps(s, stemName, start, pageSize, resultSizeArray);

    if (this.pagedQuery()) {
      resultSize = resultSizeArray[0];
    }

    if (sortedQuery()) {
      listOfMaps = LowLevelGrouperCapableAction.sort(listOfMaps, request, context, -1, null);
    }

    allChildren.addAll(listOfMaps);
    // Map validStems  = GrouperHelper.getValidStems(s,browseMode);
    boolean addChild = false;
    int end = start + pageSize;

    Map child;
    String name;
    Iterator it = allChildren.iterator();
    int count = 0;
    while (it.hasNext()) {
      addChild = false;

      child = (Map) it.next();
      if (isForAssignment) {
        // Do not try to exclude current group - so what if someone tries to add an existing member?
        // Also becomes complicated if there are custom fields
        // if(omitForAssignment!=null && omitForAssignment.equals(child.get("id"))) {
        // addChild=false;
        // }else{
        addChild = true;
        // }
      } else {
        addChild = isValidChild(child);
      }
      if (addChild) {
        if (!this.pagedQuery()) {
          resultSize++;
        }

        if (this.pagedQuery() || (resultSize >= start && resultSize < end)) {
          results.add(child);
        }
      } else if (this.pagedQuery()) {
        resultSize--;
      }
    }
    if (totalCount != null) {
      totalCount.setLength(0);
      totalCount.append(resultSize);
    }
    return results;
  }