matthew016
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.U SER)) {
Object[] memberGroups = ((IUser) userAllProp).getGroups().t oArray();
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(group Descr);
groups.add(group);
}
}
}
return groups;
}
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
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.
.getValue();
if (uProgID.equals(CeProgID.U
Object[] memberGroups = ((IUser) userAllProp).getGroups().t
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(group
groups.add(group);
}
}
}
return groups;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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