Link to home
Start Free TrialLog in
Avatar of matthew016
matthew016Flag for Belgium

asked on

Need help with recursive code

Hi,
I need some help with my code :
In my application, each user has a set of groups.
For now on, I can retrieve the groups of a user.
The problem, is that each group can also have a set of groups.
And the groups and the users are in the same table.
So I think I need something recursive ...
I need help with this.


Version which simply returns the direct groups of a user.

getGroups( objectId ) {

   Sql database : get the object

   Retrieve the groups of the object

   Return the groups

}


But need to have the indirect groups :


getGroups( objectId ) {

   Sql database : get the object

   Retrieve the groups of the object

   For each group : call getGroups again ?

   return ... ?

}


Code in Java which returns the groups a user :

public List getMemberGroups(IInfoStore iStore, int objectID) throws RetrieveGroupsException {
      List groups = new ArrayList();

      IInfoObjects usersAllProp =
            iStore.query("Select TOP 1* From CI_SYSTEMOBJECTS WHERE SI_ID=" + objectID);
      if (usersAllProp.size() == 0)
            return groups;
      else {
            IInfoObject userAllProp = (IInfoObject) usersAllProp.get(0);
            // Check that the object has the correct ProgID.
            String uProgID =
                  (String) userAllProp
                        .properties()
                        .getProperty(CePropertyID.SI_PROGID)
                        .getValue();
            if (uProgID.equals(CeProgID.USER)) {
                  Object[] memberGroups = ((IUser) userAllProp).getGroups().toArray();
                  for (int j = 0; j < memberGroups.length; j++) {
                        Group group = new Group();
                        IInfoObjects result =
                        iStore.query(
                              "SELECT SI_NAME FROM "
                              + "CI_SYSTEMOBJECTS WHERE SI_ID="
                              + memberGroups[j]);
                        IInfoObject boGroup = (IInfoObject) result.get(0);

                        int groupID = boGroup.getID();
                        String groupTitle = boGroup.getTitle();
                        String groupDescr = boGroup.getDescription();

                        group.setId(groupID);
                        group.setName(groupTitle);
                        group.setDescription(groupDescr);
                        groups.add(group);
                  }
            }
      }
      return groups;
}
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But, depending upon your DB, you may have recursive functionality built in. (I think MS SQL 2005 has this now - I'm still on SQL 2000)

Also, it __MAY__ be worth restructuring your DB to use a different mechanism of storage for "hierarchical" data ... http://dev.mysql.com/tech-resources/articles/hierarchical-data.html