Option to generate community and collection "strength" as a batch job

git-svn-id: http://scm.dspace.org/svn/repo/branches/dspace-1_5_x@2328 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Richard Jones
2007-11-12 11:59:47 +00:00
parent 8a7e41a5fe
commit 8d7b59f45d
17 changed files with 1315 additions and 12 deletions

View File

@@ -0,0 +1,98 @@
/*
* ItemCountDAO.java
*
* Copyright (c) 2002-2007, 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.browse;
import org.dspace.content.Community;
import org.dspace.content.Collection;
import org.dspace.core.Context;
import org.dspace.content.DSpaceObject;
/**
* Interface for data access of cached community and collection item count
* information
*
* @author Richard Jones
*
*/
public interface ItemCountDAO
{
/**
* Set the DSpace Context to use during data access
*
* @param context
* @throws ItemCountException
*/
public void setContext(Context context) throws ItemCountException;
/**
* Set the given count as the number of items in the given community
*
* @param community
* @param count
* @throws ItemCountException
*/
public void communityCount(Community community, int count) throws ItemCountException;
/**
* Set the given count as the number of items in the given collection
*
* @param collection
* @param count
* @throws ItemCountException
*/
public void collectionCount(Collection collection, int count) throws ItemCountException;
/**
* Get the number of items in the given DSpaceObject container. This method will
* only succeed if the DSpaceObject is an instance of either a Community or a
* Collection. Otherwise it will throw an exception
*
* @param dso
* @return
* @throws ItemCountException
*/
public int getCount(DSpaceObject dso) throws ItemCountException;
/**
* Remove any cached data regarding the given DSpaceObject container. This method will
* only succeed if the DSpaceObject is an instance of either a Community or a
* Collection. Otherwise it will throw an exception
*
* @param dso
* @throws ItemCountException
*/
public void remove(DSpaceObject dso) throws ItemCountException;
}

View File

@@ -0,0 +1,79 @@
/*
* ItemCountDAOFactory.java
*
* Copyright (c) 2002-2007, 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.browse;
import org.dspace.core.Context;
import org.dspace.core.ConfigurationManager;
/**
* Factory class to allow us to load the correct DAO for registering
* item count information
*
* @author Richard Jones
*
*/
public class ItemCountDAOFactory
{
/**
* Get an instance of ItemCountDAO which supports the correct database
* for the specific DSpace instance.
*
* @param context
* @return
* @throws ItemCountException
*/
public static ItemCountDAO getInstance(Context context)
throws ItemCountException
{
String db = ConfigurationManager.getProperty("db.name");
ItemCountDAO dao;
if ("postgres".equals(db))
{
dao = new ItemCountDAOPostgres();
}
else if ("oracle".equals(db))
{
dao = new ItemCountDAOOracle();
}
else
{
throw new ItemCountException("Database type: " + db + " is not currently supported");
}
dao.setContext(context);
return dao;
}
}

View File

@@ -0,0 +1,336 @@
/*
* ItemCountDAOOracle.java
*
* Copyright (c) 2002-2007, 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.browse;
import org.apache.log4j.Logger;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.core.Context;
import org.dspace.storage.rdbms.TableRowIterator;
import org.dspace.storage.rdbms.TableRow;
import org.dspace.storage.rdbms.DatabaseManager;
import java.sql.SQLException;
/**
* Oracle driver implementing ItemCountDAO interface to cache item
* count information in communities and collections
*
* @author Richard Jones
*
*/
public class ItemCountDAOOracle implements ItemCountDAO
{
/** Log4j logger */
private static Logger log = Logger.getLogger(ItemCountDAOOracle.class);
/** DSpace context */
private Context context;
/** SQL to select on a collection id */
private String collectionSelect = "SELECT * FROM collection_item_count WHERE collection_id = ?";
/** SQL to insert a new collection record */
private String collectionInsert = "INSERT INTO collection_item_count (collection_id, number) VALUES (?, ?)";
/** SQL to update an existing collection record */
private String collectionUpdate = "UPDATE collection_item_count SET number = ? WHERE collection_id = ?";
/** SQL to remove a collection record */
private String collectionRemove = "DELETE FROM collection_item_count WHERE collection_id = ?";
/** SQL to select on a community id */
private String communitySelect = "SELECT * FROM community_item_count WHERE community_id = ?";
/** SQL to insert a new community record */
private String communityInsert = "INSERT INTO community_item_count (community_id, number) VALUES (?, ?)";
/** SQL to update an existing community record */
private String communityUpdate = "UPDATE community_item_count SET number = ? WHERE community_id = ?";
/** SQL to remove a community record */
private String communityRemove = "DELETE FROM community_item_count WHERE community_id = ?";
/**
* Store the count of the given collection
*
* @param collection
* @param count
* @throws ItemCountException
*/
public void collectionCount(Collection collection, int count)
throws ItemCountException
{
try
{
// first find out if we have a record
Object[] sparams = { new Integer(collection.getID()) };
TableRowIterator tri = DatabaseManager.query(context, collectionSelect, sparams);
if (tri.hasNext())
{
Object[] params = { new Integer(count), new Integer(collection.getID()) };
DatabaseManager.updateQuery(context, collectionUpdate, params);
}
else
{
Object[] params = { new Integer(collection.getID()), new Integer(count) };
DatabaseManager.updateQuery(context, collectionInsert, params);
}
tri.close();
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Store the count of the given community
*
* @param community
* @param count
* @throws ItemCountException
*/
public void communityCount(Community community, int count)
throws ItemCountException
{
try
{
// first find out if we have a record
Object[] sparams = { new Integer(community.getID()) };
TableRowIterator tri = DatabaseManager.query(context, communitySelect, sparams);
if (tri.hasNext())
{
Object[] params = { new Integer(count), new Integer(community.getID()) };
DatabaseManager.updateQuery(context, communityUpdate, params);
}
else
{
Object[] params = { new Integer(community.getID()), new Integer(count) };
DatabaseManager.updateQuery(context, communityInsert, params);
}
tri.close();
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Set the dspace context to use
*
* @param context
* @throws ItemCountException
*/
public void setContext(Context context)
throws ItemCountException
{
this.context = context;
}
/**
* get the count of the items in the given container
*
* @param dso
* @return
* @throws ItemCountException
*/
public int getCount(DSpaceObject dso)
throws ItemCountException
{
if (dso instanceof Collection)
{
return getCollectionCount((Collection) dso);
}
else if (dso instanceof Community)
{
return getCommunityCount((Community) dso);
}
else
{
throw new ItemCountException("We can only count items in Communities or Collections");
}
}
/**
* remove the cache for the given container
*
* @param dso
* @throws ItemCountException
*/
public void remove(DSpaceObject dso) throws ItemCountException
{
if (dso instanceof Collection)
{
removeCollection((Collection) dso);
}
else if (dso instanceof Community)
{
removeCommunity((Community) dso);
}
else
{
throw new ItemCountException("We can only delete count of items from Communities or Collections");
}
}
/**
* remove the cache for the given collection
*
* @param collection
* @throws ItemCountException
*/
private void removeCollection(Collection collection)
throws ItemCountException
{
try
{
Object[] params = { new Integer(collection.getID()) };
DatabaseManager.updateQuery(context, collectionRemove, params);
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Remove the cache for the given community
*
* @param community
* @throws ItemCountException
*/
private void removeCommunity(Community community)
throws ItemCountException
{
try
{
Object[] params = { new Integer(community.getID()) };
DatabaseManager.updateQuery(context, communityRemove, params);
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Get the count for the given collection
*
* @param collection
* @return
* @throws ItemCountException
*/
private int getCollectionCount(Collection collection)
throws ItemCountException
{
try
{
Object[] params = { new Integer(collection.getID()) };
TableRowIterator tri = DatabaseManager.query(context, collectionSelect, params);
if (!tri.hasNext())
{
return 0;
}
TableRow tr = tri.next();
if (tri.hasNext())
{
throw new ItemCountException("More than one count row in the database");
}
tri.close();
return tr.getIntColumn("number");
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* get the count for the given community
*
* @param community
* @return
* @throws ItemCountException
*/
private int getCommunityCount(Community community)
throws ItemCountException
{
try
{
Object[] params = { new Integer(community.getID()) };
TableRowIterator tri = DatabaseManager.query(context, communitySelect, params);
if (!tri.hasNext())
{
return 0;
}
TableRow tr = tri.next();
if (tri.hasNext())
{
throw new ItemCountException("More than one count row in the database");
}
tri.close();
return tr.getIntColumn("number");
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
}

View File

@@ -0,0 +1,336 @@
/*
* ItemCountDAOPostgres.java
*
* Copyright (c) 2002-2007, 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.browse;
import org.apache.log4j.Logger;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.core.Context;
import org.dspace.storage.rdbms.TableRowIterator;
import org.dspace.storage.rdbms.TableRow;
import org.dspace.storage.rdbms.DatabaseManager;
import java.sql.SQLException;
/**
* Postgres driver implementing ItemCountDAO interface to cache item
* count information in communities and collections
*
* @author Richard Jones
*
*/
public class ItemCountDAOPostgres implements ItemCountDAO
{
/** Log4j logger */
private static Logger log = Logger.getLogger(ItemCountDAOPostgres.class);
/** DSpace context */
private Context context;
/** SQL to select on a collection id */
private String collectionSelect = "SELECT * FROM collection_item_count WHERE collection_id = ?";
/** SQL to insert a new collection record */
private String collectionInsert = "INSERT INTO collection_item_count (collection_id, number) VALUES (?, ?)";
/** SQL to update an existing collection record */
private String collectionUpdate = "UPDATE collection_item_count SET number = ? WHERE collection_id = ?";
/** SQL to remove a collection record */
private String collectionRemove = "DELETE FROM collection_item_count WHERE collection_id = ?";
/** SQL to select on a community id */
private String communitySelect = "SELECT * FROM community_item_count WHERE community_id = ?";
/** SQL to insert a new community record */
private String communityInsert = "INSERT INTO community_item_count (community_id, number) VALUES (?, ?)";
/** SQL to update an existing community record */
private String communityUpdate = "UPDATE community_item_count SET number = ? WHERE community_id = ?";
/** SQL to remove a community record */
private String communityRemove = "DELETE FROM community_item_count WHERE community_id = ?";
/**
* Store the count of the given collection
*
* @param collection
* @param count
* @throws ItemCountException
*/
public void collectionCount(Collection collection, int count)
throws ItemCountException
{
try
{
// first find out if we have a record
Object[] sparams = { new Integer(collection.getID()) };
TableRowIterator tri = DatabaseManager.query(context, collectionSelect, sparams);
if (tri.hasNext())
{
Object[] params = { new Integer(count), new Integer(collection.getID()) };
DatabaseManager.updateQuery(context, collectionUpdate, params);
}
else
{
Object[] params = { new Integer(collection.getID()), new Integer(count) };
DatabaseManager.updateQuery(context, collectionInsert, params);
}
tri.close();
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Store the count of the given community
*
* @param community
* @param count
* @throws ItemCountException
*/
public void communityCount(Community community, int count)
throws ItemCountException
{
try
{
// first find out if we have a record
Object[] sparams = { new Integer(community.getID()) };
TableRowIterator tri = DatabaseManager.query(context, communitySelect, sparams);
if (tri.hasNext())
{
Object[] params = { new Integer(count), new Integer(community.getID()) };
DatabaseManager.updateQuery(context, communityUpdate, params);
}
else
{
Object[] params = { new Integer(community.getID()), new Integer(count) };
DatabaseManager.updateQuery(context, communityInsert, params);
}
tri.close();
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Set the dspace context to use
*
* @param context
* @throws ItemCountException
*/
public void setContext(Context context)
throws ItemCountException
{
this.context = context;
}
/**
* get the count of the items in the given container
*
* @param dso
* @return
* @throws ItemCountException
*/
public int getCount(DSpaceObject dso)
throws ItemCountException
{
if (dso instanceof Collection)
{
return getCollectionCount((Collection) dso);
}
else if (dso instanceof Community)
{
return getCommunityCount((Community) dso);
}
else
{
throw new ItemCountException("We can only count items in Communities or Collections");
}
}
/**
* remove the cache for the given container
*
* @param dso
* @throws ItemCountException
*/
public void remove(DSpaceObject dso) throws ItemCountException
{
if (dso instanceof Collection)
{
removeCollection((Collection) dso);
}
else if (dso instanceof Community)
{
removeCommunity((Community) dso);
}
else
{
throw new ItemCountException("We can only delete count of items from Communities or Collections");
}
}
/**
* remove the cache for the given collection
*
* @param collection
* @throws ItemCountException
*/
private void removeCollection(Collection collection)
throws ItemCountException
{
try
{
Object[] params = { new Integer(collection.getID()) };
DatabaseManager.updateQuery(context, collectionRemove, params);
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Remove the cache for the given community
*
* @param community
* @throws ItemCountException
*/
private void removeCommunity(Community community)
throws ItemCountException
{
try
{
Object[] params = { new Integer(community.getID()) };
DatabaseManager.updateQuery(context, communityRemove, params);
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Get the count for the given collection
*
* @param collection
* @return
* @throws ItemCountException
*/
private int getCollectionCount(Collection collection)
throws ItemCountException
{
try
{
Object[] params = { new Integer(collection.getID()) };
TableRowIterator tri = DatabaseManager.query(context, collectionSelect, params);
if (!tri.hasNext())
{
return 0;
}
TableRow tr = tri.next();
if (tri.hasNext())
{
throw new ItemCountException("More than one count row in the database");
}
tri.close();
return tr.getIntColumn("number");
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* get the count for the given community
*
* @param community
* @return
* @throws ItemCountException
*/
private int getCommunityCount(Community community)
throws ItemCountException
{
try
{
Object[] params = { new Integer(community.getID()) };
TableRowIterator tri = DatabaseManager.query(context, communitySelect, params);
if (!tri.hasNext())
{
return 0;
}
TableRow tr = tri.next();
if (tri.hasNext())
{
throw new ItemCountException("More than one count row in the database");
}
tri.close();
return tr.getIntColumn("number");
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* ItemCountException.java
*
* Copyright (c) 2002-2007, 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.browse;
/**
* Exception type to handle item count specific problems
*
* @author Richard Jones
*
*/
public class ItemCountException extends Exception
{
public ItemCountException()
{
}
public ItemCountException(String message)
{
super(message);
}
public ItemCountException(Throwable cause)
{
super(cause);
}
public ItemCountException(String message, Throwable cause)
{
super(message, cause);
}
}

View File

@@ -0,0 +1,280 @@
/*
* ItemCounter.java
*
* Copyright (c) 2002-2007, 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.browse;
import org.apache.log4j.Logger;
import org.dspace.content.Community;
import org.dspace.content.Collection;
import org.dspace.core.Context;
import org.dspace.content.DSpaceObject;
import org.dspace.core.ConfigurationManager;
import java.sql.SQLException;
/**
* This class provides a standard interface to all item counting
* operations for communities and collections. It can be run from the
* command line to prepare the cached data if desired, simply by
* running:
*
* java org.dspace.browse.ItemCounter
*
* It can also be invoked via its standard API. In the event that
* the data cache is not being used, this class will return direct
* real time counts of content.
*
* @author Richard Jones
*
*/
public class ItemCounter
{
/** Log4j logger */
private static Logger log = Logger.getLogger(ItemCounter.class);
/** DAO to use to store and retrieve data */
private ItemCountDAO dao;
/** DSpace Context */
private Context context;
/**
* method invoked by CLI which will result in the number of items
* in each community and collection being cached. These counts will
* not update themselves until this is run again.
*
* @param args
*/
public static void main(String[] args)
throws ItemCountException
{
ItemCounter ic = new ItemCounter();
ic.buildItemCounts();
ic.completeContext();
}
/**
* Construct a new item counter which will create its own
* DSpace Context
*
* @throws ItemCountException
*/
public ItemCounter()
throws ItemCountException
{
try
{
this.context = new Context();
this.dao = ItemCountDAOFactory.getInstance(this.context);
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Construct a new item counter which will use the give DSpace Context
*
* @param context
* @throws ItemCountException
*/
public ItemCounter(Context context)
throws ItemCountException
{
this.context = context;
this.dao = ItemCountDAOFactory.getInstance(this.context);
}
/**
* Complete the context being used by this class. This exists so that
* instances of this class which created their own instance of the
* DSpace Context can also terminate it. For Context object which were
* passed in by the constructor, the caller is responsible for
* either calling this method themselves or completing the context
* when they need to.
*
* @throws ItemCountException
*/
public void completeContext()
throws ItemCountException
{
try
{
this.context.complete();
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* This method does the grunt work of drilling through and iterating
* over all of the communities and collections in the system and
* obtaining and caching the item counts for each one.
*
* @throws ItemCountException
*/
public void buildItemCounts()
throws ItemCountException
{
try
{
Community[] tlc = Community.findAllTop(context);
for (int i = 0; i < tlc.length; i++)
{
count(tlc[i]);
}
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* Get the count of the items in the given container. If the configuration
* value webui.strengths.cache is equal to 'true' this will return the
* cached value if it exists. If it is equal to 'false' it will count
* the number of items in the container in real time
*
* @param dso
* @return
* @throws ItemCountException
*/
public int getCount(DSpaceObject dso)
throws ItemCountException
{
boolean useCache = ConfigurationManager.getBooleanProperty("webui.strengths.cache");
if (useCache)
{
return dao.getCount(dso);
}
// if we make it this far, we need to manually count
if (dso instanceof Collection)
{
return ((Collection) dso).countItems();
}
if (dso instanceof Community)
{
return ((Collection) dso).countItems();
}
return 0;
}
/**
* Remove any cached data for the given container
*
* @param dso
* @throws ItemCountException
*/
public void remove(DSpaceObject dso)
throws ItemCountException
{
dao.remove(dso);
}
/**
* count and cache the number of items in the community. This
* will include all sub-communities and collections in the
* community. It will also recurse into sub-communities and
* collections and call count() on them also.
*
* Therefore, the count the contents of the entire system, it is
* necessary just to call this method on each top level community
*
* @param community
* @throws ItemCountException
*/
private void count(Community community)
throws ItemCountException
{
try
{
// first count the community we are in
int count = community.countItems();
dao.communityCount(community, count);
// now get the sub-communities
Community[] scs = community.getSubcommunities();
for (int i = 0; i < scs.length; i++)
{
count(scs[i]);
}
// now get the collections
Collection[] cols = community.getCollections();
for (int i = 0; i < cols.length; i++)
{
count(cols[i]);
}
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
/**
* count and cache the number of items in the given collection
*
* @param collection
* @throws ItemCountException
*/
private void count(Collection collection)
throws ItemCountException
{
try
{
int ccount = collection.countItems();
dao.collectionCount(collection, ccount);
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new ItemCountException(e);
}
}
}

View File

@@ -1053,6 +1053,19 @@ public class Collection extends DSpaceObject
wsarray[x].deleteAll();
}
// get rid of the content count cache if it exists
try
{
ItemCounter ic = new ItemCounter(ourContext);
ic.remove(this);
}
catch (ItemCountException e)
{
// FIXME: upside down exception handling due to lack of good
// exception framework
throw new SQLException(e);
}
// Delete collection row
DatabaseManager.delete(ourContext, collectionRow);

View File

@@ -906,6 +906,19 @@ public class Community extends DSpaceObject
// Remove all authorization policies
AuthorizeManager.removeAllPolicies(ourContext, this);
// get rid of the content count cache if it exists
try
{
ItemCounter ic = new ItemCounter(ourContext);
ic.remove(this);
}
catch (ItemCountException e)
{
// FIXME: upside down exception handling due to lack of good
// exception framework
throw new SQLException(e);
}
// Delete community row
DatabaseManager.delete(ourContext, communityRow);
}