/*
* EPerson.java
*
* Version: $Revision: 1.28 $
*
* Date: $Date: 2005/08/25 17:20:28 $
*
* 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.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.28 $
*/
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 null
*/
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
*
ID
LASTNAME
EMAIL
NETID
ID
LASTNAME
EMAIL
NETID
true
if other
is the same EPerson as
* this object, false
otherwise
*
* @param other
* object to compare to
*
* @return true
if object passed in represents the same
* eperson as this object
*/
public boolean obsolete_equals(Object other)
{
if (!(other instanceof EPerson))
{
return false;
}
return (getID() == ((EPerson) other).getID());
}
/**
* return type found in Constants
*/
public int getType()
{
return Constants.EPERSON;
}
/**
* Check for presence of EPerson in tables that have constraints on
* EPersons. Called by delete() to determine whether the eperson can
* actually be deleted.
*
* An EPerson cannot be deleted if it exists in the item, workflowitem, or
* tasklistitem tables.
*
* @return Vector of tables that contain a reference to the eperson.
*/
public Vector getDeleteConstraints() throws SQLException
{
Vector tableList = new Vector();
//check for eperson in item table
TableRowIterator tri = DatabaseManager.query(myContext,
"SELECT * from item where submitter_id=" + getID());
if (tri.hasNext())
{
tableList.add("item");
}
//check for eperson in workflowitem table
tri = DatabaseManager.query(myContext,
"SELECT * from workflowitem where owner=" + getID());
if (tri.hasNext())
{
tableList.add("workflowitem");
}
//check for eperson in tasklistitem table
tri = DatabaseManager.query(myContext,
"SELECT * from tasklistitem where eperson_id=" + getID());
if (tri.hasNext())
{
tableList.add("tasklistitem");
}
//the list of tables can be used to construct an error message
//explaining to the user why the eperson cannot be deleted.
return tableList;
}
}