📄 group.java
字号:
/* * Group.java * * Version: $Revision: 1.26 $ * * Date: $Date: 2005/10/13 05:48:02 $ * * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts * Institute of Technology. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the Hewlett-Packard Company nor the name of the * Massachusetts Institute of Technology nor the names of their * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. */package org.dspace.eperson;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.log4j.Logger;import org.dspace.authorize.AuthorizeException;import org.dspace.authorize.AuthorizeManager;import org.dspace.content.DSpaceObject;import org.dspace.core.Constants;import org.dspace.core.Context;import org.dspace.core.LogManager;import org.dspace.storage.rdbms.DatabaseManager;import org.dspace.storage.rdbms.TableRow;import org.dspace.storage.rdbms.TableRowIterator;/** * Class representing a group of e-people. * * @author David Stuve * @version $Revision: 1.26 $ */public class Group extends DSpaceObject{ // findAll sortby types public static final int ID = 0; // sort by ID public static final int NAME = 1; // sort by NAME (default) /** log4j logger */ private static Logger log = Logger.getLogger(Group.class); /** Our context */ private Context myContext; /** The row in the table representing this object */ private TableRow myRow; /** lists of epeople and groups in the group */ private List epeople = new ArrayList(); private List groups = new ArrayList(); /** lists that need to be written out again */ private boolean epeopleChanged = false; private boolean groupsChanged = false; /** is this just a stub, or is all data loaded? */ private boolean isDataLoaded = false; /** * Construct a Group from a given context and tablerow * * @param context * @param row */ Group(Context context, TableRow row) throws SQLException { myContext = context; myRow = row; // Cache ourselves context.cache(this, row.getIntColumn("eperson_group_id")); } /** * Populate Group with eperson and group objects * * @param context * @throws SQLException */ public void loadData() { // only populate if not already populated if (!isDataLoaded) { // naughty thing to do - swallowing SQL exception and throwing it as // a RuntimeException - a hack to avoid changing the API all over // the place try { // get epeople objects TableRowIterator tri = DatabaseManager .query( myContext, "eperson", "SELECT eperson.* FROM eperson, epersongroup2eperson WHERE " + "epersongroup2eperson.eperson_id=eperson.eperson_id AND " + "epersongroup2eperson.eperson_group_id=" + myRow .getIntColumn("eperson_group_id")); while (tri.hasNext()) { TableRow r = (TableRow) tri.next(); // First check the cache EPerson fromCache = (EPerson) myContext.fromCache( EPerson.class, r.getIntColumn("eperson_id")); if (fromCache != null) { epeople.add(fromCache); } else { epeople.add(new EPerson(myContext, r)); } } // now get Group objects tri = DatabaseManager .query( myContext, "epersongroup", "SELECT epersongroup.* FROM epersongroup, group2group WHERE " + "group2group.child_id=epersongroup.eperson_group_id AND " + "group2group.parent_id=" + myRow .getIntColumn("eperson_group_id")); while (tri.hasNext()) { TableRow r = (TableRow) tri.next(); // First check the cache Group fromCache = (Group) myContext.fromCache(Group.class, r.getIntColumn("eperson_group_id")); if (fromCache != null) { groups.add(fromCache); } else { groups.add(new Group(myContext, r)); } } } catch (Exception e) { throw new RuntimeException(e); } isDataLoaded = true; } } /** * Create a new group * * @param context * DSpace context object */ public static Group create(Context context) throws SQLException, AuthorizeException { // FIXME - authorization? if (!AuthorizeManager.isAdmin(context)) { throw new AuthorizeException( "You must be an admin to create an EPerson Group"); } // Create a table row TableRow row = DatabaseManager.create(context, "epersongroup"); Group g = new Group(context, row); log.info(LogManager.getHeader(context, "create_group", "group_id=" + g.getID())); return g; } /** * get the ID of the group object * * @return id */ public int getID() { return myRow.getIntColumn("eperson_group_id"); } /** * get name of group * * @return name */ public String getName() { return myRow.getStringColumn("name"); } /** * set name of group * * @param name * new group name */ public void setName(String name) { myRow.setColumn("name", name); } /** * add an eperson member * * @param e * eperson */ public void addMember(EPerson e) { loadData(); // make sure Group has data loaded if (isMember(e)) { return; } epeople.add(e); epeopleChanged = true; } /** * add group to this group * * @param g */ public void addMember(Group g) { loadData(); // make sure Group has data loaded // don't add if it's already a member if (isMember(g)) { return; } groups.add(g); groupsChanged = true; } /** * remove an eperson from a group * * @param e * eperson */ public void removeMember(EPerson e) { loadData(); // make sure Group has data loaded if (epeople.remove(e)) { epeopleChanged = true; } } /** * remove group from this group * * @param g */ public void removeMember(Group g) { loadData(); // make sure Group has data loaded if (groups.remove(g)) { groupsChanged = true; } } /** * check to see if an eperson is a member * * @param e * eperson to check membership */ public boolean isMember(EPerson e) { // special, group 0 is anonymous if (getID() == 0) { return true; } loadData(); // make sure Group has data loaded return epeople.contains(e); } /** * check to see if group is a member * * @param g * group to check * @return */ public boolean isMember(Group g) { loadData(); // make sure Group has data loaded return groups.contains(g); } /** * fast check to see if an eperson is a member called with eperson id, does * database lookup without instantiating all of the epeople objects and is * thus a static method * * @param c * context * @param groupid * group ID to check */ public static boolean isMember(Context c, int groupid) throws SQLException { // special, everyone is member of group 0 (anonymous) if (groupid == 0) { return true; } // first, check for membership if it's a special group // (special groups can be set even if person isn't authenticated) if (c.inSpecialGroup(groupid)) { return true; } EPerson currentuser = c.getCurrentUser(); // only test for membership if context contains a user if (currentuser != null) { return epersonInGroup(c, groupid, currentuser); } // currentuser not set, return FALSE return false; } /** * Get all of the groups that an eperson is a member of * * @param c * @param e * @return * @throws SQLException */ public static Group[] allMemberGroups(Context c, EPerson e) throws SQLException { List groupList = new ArrayList(); Set myGroups = allMemberGroupIDs(c, e); // now convert those Integers to Groups Iterator i = myGroups.iterator(); while (i.hasNext()) { groupList.add(Group.find(c, ((Integer) i.next()).intValue())); } return (Group[]) groupList.toArray(new Group[0]); } /** * 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 allMemberGroupIDs(Context c, EPerson e) throws SQLException { // 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.query(c, "epersongroup2eperson", "SELECT * FROM epersongroup2eperson WHERE eperson_id=" + e.getID()); Set groupIDs = new HashSet(); while (tri.hasNext()) { TableRow row = tri.next(); int childID = row.getIntColumn("eperson_group_id"); groupIDs.add(new Integer(childID)); } // 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! String groupQuery = ""; Iterator i = groupIDs.iterator(); while (i.hasNext()) { int groupID = ((Integer) i.next()).intValue(); groupQuery += "child_id=" + groupID; if (i.hasNext()) groupQuery += " OR "; } if ("".equals(groupQuery)) { // don't do query, isn't member of any groups return groupIDs; } // was member of at least one group tri = DatabaseManager.query(c, "group2groupcache", "SELECT * FROM group2groupcache WHERE " + groupQuery); while (tri.hasNext()) { TableRow row = tri.next(); int parentID = row.getIntColumn("parent_id"); groupIDs.add(new Integer(parentID)); } return groupIDs; } /** * Get all of the epeople who are a member of the * specified group, or a member of a sub-group of the * specified group, etc. * * @param c * DSpace context * @param g * Group object * @return Array of EPerson objects * @throws SQLException */ public static EPerson[] allMembers(Context c, Group g) throws SQLException { List epersonList = new ArrayList(); Set myEpeople = allMemberIDs(c, g); // now convert those Integers to EPerson objects Iterator i = myEpeople.iterator(); while (i.hasNext()) { epersonList.add(EPerson.find(c, ((Integer) i.next()).intValue())); } return (EPerson[]) epersonList.toArray(new EPerson[0]); } /** * Get Set of all Integers all of the epeople * members for a group * * @param c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -