Skip to content

Instantly share code, notes, and snippets.

@einsty
Created July 14, 2017 16:57
Show Gist options
  • Save einsty/d7450cc6e59fcfc09c72debdcd5445cc to your computer and use it in GitHub Desktop.
Save einsty/d7450cc6e59fcfc09c72debdcd5445cc to your computer and use it in GitHub Desktop.
Utility method that can traverse the Liferay Group hierarchy and build an array of long representing all child groups. Useful in scoping a searchContext for content in an org hierarchy.
public static long[] getCurrentAndChildGroupIds(Group currentGroup){
try {
List<Long> theseChildGroupIds = new ArrayList<>();
theseChildGroupIds.add(currentGroup.getGroupId());
for (Group childGroup : currentGroup.getChildren(true)
) {
theseChildGroupIds.add(childGroup.getGroupId());
if(childGroup.getChildren(true).size() > 0){
for (Group grandChildGroup : childGroup.getChildren(true)
) {
theseChildGroupIds.add(grandChildGroup.getGroupId());
}
}
}
Long[] l1 = theseChildGroupIds.toArray(new Long[theseChildGroupIds.size()]);
long[] l2 = ArrayUtils.toPrimitive(l1);
return l2;
//log.debug("Current and Child GroupIds: " + ArrayUtils.toString(l2));
} catch (SystemException e) {
log.error(e.getMessage());
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment