📄 eperson.java
字号:
/* * EPerson.java * * Version: $Revision: 1.29 $ * * 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.List;import java.util.Vector;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.core.Utils;import org.dspace.history.HistoryManager;import org.dspace.storage.rdbms.DatabaseManager;import org.dspace.storage.rdbms.TableRow;import org.dspace.storage.rdbms.TableRowIterator;/** * Class representing an e-person. * * @author David Stuve * @version $Revision: 1.29 $ */public class EPerson extends DSpaceObject{ /** The e-mail field (for sorting) */ public static final int EMAIL = 1; /** The last name (for sorting) */ public static final int LASTNAME = 2; /** The e-mail field (for sorting) */ public static final int ID = 3; /** The netid field (for sorting) */ public static final int NETID = 4; /** log4j logger */ private static Logger log = Logger.getLogger(EPerson.class); /** Our context */ private Context myContext; /** The row in the table representing this eperson */ private TableRow myRow; /** * Construct an EPerson * * @param context * the context this object exists in * @param row * the corresponding row in the table */ EPerson(Context context, TableRow row) { myContext = context; myRow = row; // Cache ourselves context.cache(this, row.getIntColumn("eperson_id")); } /** * Get an EPerson from the database. * * @param context * DSpace context object * @param id * ID of the EPerson * * @return the EPerson format, or null if the ID is invalid. */ public static EPerson find(Context context, int id) throws SQLException { // First check the cache EPerson fromCache = (EPerson) context.fromCache(EPerson.class, id); if (fromCache != null) { return fromCache; } TableRow row = DatabaseManager.find(context, "eperson", id); if (row == null) { return null; } else { return new EPerson(context, row); } } /** * Find the eperson by their email address * * @return EPerson */ public static EPerson findByEmail(Context context, String email) throws SQLException, AuthorizeException { TableRow row = DatabaseManager.findByUnique(context, "eperson", "email", email); if (row == null) { return null; } else { // First check the cache EPerson fromCache = (EPerson) context.fromCache(EPerson.class, row .getIntColumn("eperson_id")); if (fromCache != null) { return fromCache; } else { return new EPerson(context, row); } } } /** * Find the eperson by their netid * * @param context * DSpace context * @param netid * Network ID * * @return corresponding EPerson, or <code>null</code> */ public static EPerson findByNetid(Context context, String netid) throws SQLException { if (netid == null) return null; TableRow row = DatabaseManager.findByUnique(context, "eperson", "netid", netid); if (row == null) { return null; } else { // First check the cache EPerson fromCache = (EPerson) context.fromCache(EPerson.class, row .getIntColumn("eperson_id")); if (fromCache != null) { return fromCache; } else { return new EPerson(context, row); } } } /** * Retrieve all e-person records from the database, sorted by a particular * field. Fields are: <<<<<<< EPerson.java * <ul> * <li><code>ID</code></li> * <li><code>LASTNAME</code></li> * <li><code>EMAIL</code></li> * <li><code>NETID</code></li> * </ul> * ======= * <UL> * <LI><code>ID</code></LI> * <LI><code>LASTNAME</code></LI> * <LI><code>EMAIL</code></LI> * <LI><code>NETID</code></LI> * </UL> * * @return array of EPerson objects >>>>>>> 1.27 */ public static EPerson[] findAll(Context context, int sortField) throws SQLException { String s; switch (sortField) { case ID: s = "eperson_id"; break; case EMAIL: s = "email"; break; case NETID: s = "netid"; break; default: s = "lastname"; } TableRowIterator rows = DatabaseManager.query(context, "SELECT * FROM eperson ORDER BY " + s); List epeopleRows = rows.toList(); EPerson[] epeople = new EPerson[epeopleRows.size()]; for (int i = 0; i < epeopleRows.size(); i++) { TableRow row = (TableRow) epeopleRows.get(i); // First check the cache EPerson fromCache = (EPerson) context.fromCache(EPerson.class, row .getIntColumn("eperson_id")); if (fromCache != null) { epeople[i] = fromCache; } else { epeople[i] = new EPerson(context, row); } } return epeople; } /** * Create a new eperson * * @param context * DSpace context object */ public static EPerson create(Context context) throws SQLException, AuthorizeException { // authorized? if (!AuthorizeManager.isAdmin(context)) { throw new AuthorizeException( "You must be an admin to create an EPerson"); } // Create a table row TableRow row = DatabaseManager.create(context, "eperson"); EPerson e = new EPerson(context, row); log.info(LogManager.getHeader(context, "create_eperson", "eperson_id=" + e.getID())); HistoryManager.saveHistory(context, e, HistoryManager.REMOVE, context .getCurrentUser(), context.getExtraLogInfo()); return e; } /** * Delete an eperson * */ public void delete() throws SQLException, AuthorizeException, EPersonDeletionException { // authorized? if (!AuthorizeManager.isAdmin(myContext)) { throw new AuthorizeException( "You must be an admin to delete an EPerson"); } HistoryManager.saveHistory(myContext, this, HistoryManager.REMOVE, myContext.getCurrentUser(), myContext.getExtraLogInfo()); // check for presence of eperson in tables that // have constraints on eperson_id Vector constraintList = getDeleteConstraints(); // if eperson exists in tables that have constraints // on eperson, throw an exception if (constraintList.size() > 0) { throw new EPersonDeletionException(constraintList); } // Remove from cache myContext.removeCached(this, getID()); // Remove any group memberships first DatabaseManager.updateQuery(myContext, "DELETE FROM EPersonGroup2EPerson WHERE eperson_id=" + getID()); // Remove any subscriptions DatabaseManager.updateQuery(myContext, "DELETE FROM subscription WHERE eperson_id=" + getID()); // Remove ourself DatabaseManager.delete(myContext, myRow); log.info(LogManager.getHeader(myContext, "delete_eperson", "eperson_id=" + getID())); } /** * Get the e-person's internal identifier * * @return the internal identifier */ public int getID() { return myRow.getIntColumn("eperson_id"); } public String getHandle() { // No Handles for e-people return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -