📄 group.java
字号:
* DSpace context * @param g * Group object * @return Set of Integer epersonIDs * @throws SQLException */ public static Set allMemberIDs(Context c, Group g) throws SQLException { // two queries - first to get all groups which are a member of this group // second query gets all members of each group in the first query Set epeopleIDs = new HashSet(); // Get all groups which are a member of this group TableRowIterator tri = DatabaseManager.query(c, "group2groupcache", "SELECT * FROM group2groupcache WHERE parent_id=" + g.getID()); Set groupIDs = new HashSet(); while (tri.hasNext()) { TableRow row = tri.next(); int childID = row.getIntColumn("child_id"); groupIDs.add(new Integer(childID)); } // now we have all the groups (including this one) // it is time to find all the EPeople who belong to those groups // and filter out all duplicates Iterator i = groupIDs.iterator(); // don't forget to add the current group to this query! String epersonQuery = "eperson_group_id=" + g.getID(); if (i.hasNext()) epersonQuery += " OR "; while (i.hasNext()) { int groupID = ((Integer) i.next()).intValue(); epersonQuery += "eperson_group_id=" + groupID; if (i.hasNext()) epersonQuery += " OR "; } //get all the EPerson IDs tri = DatabaseManager.query(c, "epersongroup2eperson", "SELECT * FROM epersongroup2eperson WHERE " + epersonQuery); while (tri.hasNext()) { TableRow row = tri.next(); int epersonID = row.getIntColumn("eperson_id"); epeopleIDs.add(new Integer(epersonID)); } return epeopleIDs; } private static boolean epersonInGroup(Context c, int groupID, EPerson e) throws SQLException { Set groupIDs = Group.allMemberGroupIDs(c, e); return groupIDs.contains(new Integer(groupID)); } /** * find the group by its ID * * @param context * @param id */ public static Group find(Context context, int id) throws SQLException { // First check the cache Group fromCache = (Group) context.fromCache(Group.class, id); if (fromCache != null) { return fromCache; } TableRow row = DatabaseManager.find(context, "epersongroup", id); if (row == null) { return null; } else { return new Group(context, row); } } /** * Find the group by its name - assumes name is unique * * @param context * @param name * * @return Group */ public static Group findByName(Context context, String name) throws SQLException { TableRow row = DatabaseManager.findByUnique(context, "epersongroup", "name", name); if (row == null) { return null; } else { // First check the cache Group fromCache = (Group) context.fromCache(Group.class, row .getIntColumn("eperson_group_id")); if (fromCache != null) { return fromCache; } else { return new Group(context, row); } } } /** * Finds all groups in the site * * @param context * DSpace context * @param sortField * field to sort by -- Group.ID or Group.NAME * * @return array of all groups in the site */ public static Group[] findAll(Context context, int sortField) throws SQLException { String s; switch (sortField) { case ID: s = "eperson_group_id"; break; case NAME: s = "name"; break; default: s = "name"; } TableRowIterator rows = DatabaseManager.query(context, "epersongroup", "SELECT * FROM epersongroup ORDER BY " + s); List gRows = rows.toList(); Group[] groups = new Group[gRows.size()]; for (int i = 0; i < gRows.size(); i++) { TableRow row = (TableRow) gRows.get(i); // First check the cache Group fromCache = (Group) context.fromCache(Group.class, row .getIntColumn("eperson_group_id")); if (fromCache != null) { groups[i] = fromCache; } else { groups[i] = new Group(context, row); } } return groups; } /** * Delete a group * */ public void delete() throws SQLException { // FIXME: authorizations // Remove from cache myContext.removeCached(this, getID()); // Remove any ResourcePolicies that reference this group AuthorizeManager.removeGroupPolicies(myContext, getID()); // Remove any group memberships first DatabaseManager.updateQuery(myContext, "DELETE FROM EPersonGroup2EPerson WHERE eperson_group_id=" + getID()); // remove any group2groupcache entries DatabaseManager.updateQuery(myContext, "DELETE FROM group2groupcache WHERE parent_id=" + getID() + " OR child_id=" + getID()); // Now remove any group2group assignments DatabaseManager.updateQuery(myContext, "DELETE FROM group2group WHERE parent_id=" + getID() + " OR child_id=" + getID()); // don't forget the new table deleteEpersonGroup2WorkspaceItem(); // Remove ourself DatabaseManager.delete(myContext, myRow); epeople.clear(); log.info(LogManager.getHeader(myContext, "delete_group", "group_id=" + getID())); } /** * @throws SQLException */ private void deleteEpersonGroup2WorkspaceItem() throws SQLException { DatabaseManager.updateQuery(myContext, "DELETE FROM EPersonGroup2WorkspaceItem WHERE eperson_group_id=" + getID()); } /** * Return EPerson members of a Group */ public EPerson[] getMembers() { loadData(); // make sure all data is loaded EPerson[] myArray = new EPerson[epeople.size()]; myArray = (EPerson[]) epeople.toArray(myArray); return myArray; } /** * Return Group members of a Group * * @return */ public Group[] getMemberGroups() { loadData(); // make sure all data is loaded Group[] myArray = new Group[groups.size()]; myArray = (Group[]) groups.toArray(myArray); return myArray; } /** * Return true if group has no members */ public boolean isEmpty() { loadData(); // make sure all data is loaded if ((epeople.size() == 0) && (groups.size() == 0)) { return true; } else { return false; } } /** * Update the group - writing out group object and EPerson list if necessary */ public void update() throws SQLException, AuthorizeException { // FIXME: Check authorisation DatabaseManager.update(myContext, myRow); // Redo eperson mappings if they've changed if (epeopleChanged) { // Remove any existing mappings DatabaseManager.updateQuery(myContext, "delete from epersongroup2eperson where eperson_group_id=" + getID()); // Add new mappings Iterator i = epeople.iterator(); while (i.hasNext()) { EPerson e = (EPerson) i.next(); TableRow mappingRow = DatabaseManager.create(myContext, "epersongroup2eperson"); mappingRow.setColumn("eperson_id", e.getID()); mappingRow.setColumn("eperson_group_id", getID()); DatabaseManager.update(myContext, mappingRow); } epeopleChanged = false; } // Redo Group mappings if they've changed if (groupsChanged) { // Remove any existing mappings DatabaseManager.updateQuery(myContext, "delete from group2group where parent_id=" + getID()); // Add new mappings Iterator i = groups.iterator(); while (i.hasNext()) { Group g = (Group) i.next(); TableRow mappingRow = DatabaseManager.create(myContext, "group2group"); mappingRow.setColumn("parent_id", getID()); mappingRow.setColumn("child_id", g.getID()); DatabaseManager.update(myContext, mappingRow); } // groups changed, now change group cache rethinkGroupCache(); groupsChanged = false; } log.info(LogManager.getHeader(myContext, "update_group", "group_id=" + getID())); } /** * Return <code>true</code> if <code>other</code> is the same Group as * this object, <code>false</code> otherwise * * @param other * object to compare to * * @return <code>true</code> if object passed in represents the same group * as this object */ public boolean equals(Object other) { if (!(other instanceof Group)) { return false; } return (getID() == ((Group) other).getID()); } public int getType() { return Constants.GROUP; } public String getHandle() { return null; } /** * Regenerate the group cache AKA the group2groupcache table in the database - * meant to be called when a group is added or removed from another group * */ private void rethinkGroupCache() throws SQLException { // read in the group2group table TableRowIterator tri = DatabaseManager.query(myContext, "group2group", "SELECT * FROM group2group"); Map parents = new HashMap(); while (tri.hasNext()) { TableRow row = (TableRow) tri.next(); Integer parentID = new Integer(row.getIntColumn("parent_id")); Integer childID = new Integer(row.getIntColumn("child_id")); // if parent doesn't have an entry, create one if (!parents.containsKey(parentID)) { Set children = new HashSet(); // add child id to the list children.add(childID); parents.put(parentID, children); } else { // parent has an entry, now add the child to the parent's record // of children Set children = (Set) parents.get(parentID); children.add(childID); } } // now parents is a hash of all of the IDs of groups that are parents // and each hash entry is a hash of all of the IDs of children of those // parent groups // so now to establish all parent,child relationships we can iterate // through the parents hash Iterator i = parents.keySet().iterator(); while (i.hasNext()) { Integer parentID = (Integer) i.next(); Set myChildren = getChildren(parents, parentID); Iterator j = myChildren.iterator(); while (j.hasNext()) { // child of a parent Integer childID = (Integer) j.next(); ((Set) parents.get(parentID)).add(childID); } } // empty out group2groupcache table DatabaseManager.updateQuery(myContext, "DELETE FROM group2groupcache WHERE id >= 0"); // write out new one Iterator pi = parents.keySet().iterator(); // parent iterator while (pi.hasNext()) { Integer parent = (Integer) pi.next(); Set children = (Set) parents.get(parent); Iterator ci = children.iterator(); // child iterator while (ci.hasNext()) { Integer child = (Integer) ci.next(); TableRow row = DatabaseManager.create(myContext, "group2groupcache"); int parentID = parent.intValue(); int childID = child.intValue(); row.setColumn("parent_id", parentID); row.setColumn("child_id", childID); DatabaseManager.update(myContext, row); } } } /** * Used recursively to generate a map of ALL of the children of the given * parent * * @param parents * Map of parent,child relationships * @param parent * the parent you're interested in * @return Map whose keys are all of the children of a parent */ private Set getChildren(Map parents, Integer parent) { Set myChildren = new HashSet(); // degenerate case, this parent has no children if (!parents.containsKey(parent)) return myChildren; // got this far, so we must have children Set children = (Set) parents.get(parent); // now iterate over all of the children Iterator i = children.iterator(); while (i.hasNext()) { Integer childID = (Integer) i.next(); // add this child's ID to our return set myChildren.add(childID); // and now its children myChildren.addAll(getChildren(parents, childID)); } return myChildren; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -