/** * get Set of Integers all of the group memberships for an eperson * * @param c * @param e * @return Set of Integer groupIDs * @throws SQLException */ public static Set<Integer> allMemberGroupIDs(Context c, EPerson e) throws SQLException { Set<Integer> groupIDs = new HashSet<Integer>(); if (e != null) { // two queries - first to get groups eperson is a member of // second query gets parent groups for groups eperson is a member of TableRowIterator tri = DatabaseManager.queryTable( c, "epersongroup2eperson", "SELECT * FROM epersongroup2eperson WHERE eperson_id= ?", e.getID()); try { while (tri.hasNext()) { TableRow row = tri.next(); int childID = row.getIntColumn("eperson_group_id"); groupIDs.add(Integer.valueOf(childID)); } } finally { // close the TableRowIterator to free up resources if (tri != null) { tri.close(); } } } // Also need to get all "Special Groups" user is a member of! // Otherwise, you're ignoring the user's membership to these groups! // However, we only do this is we are looking up the special groups // of the current user, as we cannot look up the special groups // of a user who is not logged in. if ((c.getCurrentUser() == null) || (((c.getCurrentUser() != null) && (c.getCurrentUser().getID() == e.getID())))) { Group[] specialGroups = c.getSpecialGroups(); for (Group special : specialGroups) { groupIDs.add(Integer.valueOf(special.getID())); } } // all the users are members of the anonymous group groupIDs.add(Integer.valueOf(0)); // now we have all owning groups, also grab all parents of owning groups // yes, I know this could have been done as one big query and a union, // but doing the Oracle port taught me to keep to simple SQL! StringBuilder groupQuery = new StringBuilder(); groupQuery.append("SELECT * FROM group2groupcache WHERE "); Iterator<Integer> i = groupIDs.iterator(); // Build a list of query parameters Object[] parameters = new Object[groupIDs.size()]; int idx = 0; while (i.hasNext()) { int groupID = (i.next()).intValue(); parameters[idx++] = Integer.valueOf(groupID); groupQuery.append("child_id= ? "); if (i.hasNext()) { groupQuery.append(" OR "); } } // was member of at least one group // NOTE: even through the query is built dynamically, all data is // separated into the parameters array. TableRowIterator tri = DatabaseManager.queryTable(c, "group2groupcache", groupQuery.toString(), parameters); try { while (tri.hasNext()) { TableRow row = tri.next(); int parentID = row.getIntColumn("parent_id"); groupIDs.add(Integer.valueOf(parentID)); } } finally { // close the TableRowIterator to free up resources if (tri != null) { tri.close(); } } return groupIDs; }