We help IT Professionals succeed at work.

Need help with recursive code

matthew016 asked
Last Modified: 2008-02-07
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
            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 =
                              "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();

      return groups;
Watch Question

Senior Software Developer
This one is on us!
(Get your first solution completely free - no credit card required)
Richard QuadlingSenior Software Developer

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
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.