Fix count of items in a Community by adding new methods to ItemDAO, ItemService. Also cleanup other count methods which are duplicative / badly named.

This commit is contained in:
Tim Donohue
2016-02-04 15:31:05 -06:00
parent 094820c12d
commit dcf14a42bf
4 changed files with 68 additions and 23 deletions

View File

@@ -1116,12 +1116,11 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
@Override @Override
public int countItems(Context context, Community community) throws SQLException { public int countItems(Context context, Community community) throws SQLException {
// First we need a list of all collections under this community in the hierarchy
List<Collection> collections = communityService.getAllCollections(context, community); List<Collection> collections = communityService.getAllCollections(context, community);
int itemCount = 0;
for(Collection collection : collections) { // Now, lets count unique items across that list of collections
itemCount += countItems(context, collection); return itemDAO.countItems(context, collections, true, false);
}
return itemCount;
} }
@Override @Override
@@ -1161,12 +1160,14 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
} }
@Override @Override
public int getNotArchivedItemsCount(Context context) throws SQLException { public int countNotArchivedItems(Context context) throws SQLException {
return itemDAO.countNotArchived(context); // return count of items not in archive and also not withdrawn
return itemDAO.countItems(context, false, false);
} }
@Override @Override
public int countWithdrawnItems(Context context) throws SQLException { public int countWithdrawnItems(Context context) throws SQLException {
return itemDAO.countWithdrawn(context); // return count of items that are in archive and withdrawn
return itemDAO.countItems(context, true, true);
} }
} }

View File

@@ -56,7 +56,31 @@ public interface ItemDAO extends DSpaceObjectLegacySupportDAO<Item>
public Iterator<Item> findAllByCollection(Context context, Collection collection) throws SQLException; public Iterator<Item> findAllByCollection(Context context, Collection collection) throws SQLException;
/**
* Count number of items in a given collection
* @param context
* @param collection the collection
* @param includeArchived whether to include archived items in count
* @param includeWithdrawn whether to include withdrawn items in count
* @return item count
* @throws SQLException
*/
public int countItems(Context context, Collection collection, boolean includeArchived, boolean includeWithdrawn) throws SQLException; public int countItems(Context context, Collection collection, boolean includeArchived, boolean includeWithdrawn) throws SQLException;
/**
* Count number of unique items across several collections at once.
* This method can be used with
* {@link org.dspace.content.service.CommunityService#getAllCollections(Context,Community)}
* to determine the unique number of items in a Community.
*
* @param context
* @param collections the list of collections
* @param includeArchived whether to include archived items in count
* @param includeWithdrawn whether to include withdrawn items in count
* @return item count
* @throws SQLException
*/
public int countItems(Context context, List<Collection> collections, boolean includeArchived, boolean includeWithdrawn) throws SQLException;
/** /**
* Get all Items installed or withdrawn, discoverable, and modified since a Date. * Get all Items installed or withdrawn, discoverable, and modified since a Date.
@@ -71,9 +95,22 @@ public interface ItemDAO extends DSpaceObjectLegacySupportDAO<Item>
boolean withdrawn, boolean discoverable, Date lastModified) boolean withdrawn, boolean discoverable, Date lastModified)
throws SQLException; throws SQLException;
/**
* Count total number of items (rows in item table)
* @param context
* @return total count
* @throws SQLException
*/
int countRows(Context context) throws SQLException; int countRows(Context context) throws SQLException;
int countNotArchived(Context context) throws SQLException; /**
* Count number of items based on specific status flags
int countWithdrawn(Context context) throws SQLException; * @param context
* @param includeArchived whether to include archived items in count
* @param includeWithdrawn whether to include withdrawn items in count
* @return count of items
* @throws SQLException
*/
int countItems(Context context, boolean includeArchived, boolean includeWithdrawn) throws SQLException;
} }

View File

@@ -11,7 +11,6 @@ import org.apache.log4j.Logger;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.content.MetadataField; import org.dspace.content.MetadataField;
import org.dspace.content.MetadataSchema;
import org.dspace.content.MetadataValue; import org.dspace.content.MetadataValue;
import org.dspace.content.dao.ItemDAO; import org.dspace.content.dao.ItemDAO;
import org.dspace.core.Context; import org.dspace.core.Context;
@@ -25,10 +24,8 @@ import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions; import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries; import org.hibernate.criterion.Subqueries;
import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
@@ -237,6 +234,18 @@ public class ItemDAOImpl extends AbstractHibernateDSODAO<Item> implements ItemDA
return count(query); return count(query);
} }
@Override
public int countItems(Context context, List<Collection> collections, boolean includeArchived, boolean includeWithdrawn) throws SQLException {
Query query = createQuery(context, "select count(distinct i) from Item i " +
"join i.collections collection " +
"WHERE collection IN (:collections) AND i.inArchive=:in_archive AND i.withdrawn=:withdrawn");
query.setParameterList("collections", collections);
query.setParameter("in_archive", includeArchived);
query.setParameter("withdrawn", includeWithdrawn);
return count(query);
}
@Override @Override
public Iterator<Item> findByLastModifiedSince(Context context, Date since) public Iterator<Item> findByLastModifiedSince(Context context, Date since)
@@ -253,12 +262,10 @@ public class ItemDAOImpl extends AbstractHibernateDSODAO<Item> implements ItemDA
} }
@Override @Override
public int countNotArchived(Context context) throws SQLException { public int countItems(Context context, boolean includeArchived, boolean includeWithdrawn) throws SQLException {
return count(createQuery(context, "SELECT count(*) FROM Item i WHERE i.inArchive=false AND i.withdrawn=false")); Query query = createQuery(context, "SELECT count(*) FROM Item i WHERE i.inArchive=:in_archive AND i.withdrawn=:withdrawn");
} query.setParameter("in_archive", includeArchived);
query.setParameter("withdrawn", includeWithdrawn);
@Override return count(query);
public int countWithdrawn(Context context) throws SQLException {
return count(createQuery(context, "SELECT count(*) FROM Item i WHERE i.withdrawn=true"));
} }
} }

View File

@@ -453,7 +453,7 @@ public interface ItemService extends DSpaceObjectService<Item>, DSpaceObjectLega
public Iterator<Item> findByLastModifiedSince(Context context, Date last) public Iterator<Item> findByLastModifiedSince(Context context, Date last)
throws SQLException; throws SQLException;
/** /**
* counts items in the given community * counts items in the given community
* *
* @return total items * @return total items
@@ -462,7 +462,7 @@ public interface ItemService extends DSpaceObjectService<Item>, DSpaceObjectLega
int countTotal(Context context) throws SQLException; int countTotal(Context context) throws SQLException;
int getNotArchivedItemsCount(Context context) throws SQLException; int countNotArchivedItems(Context context) throws SQLException;
int countWithdrawnItems(Context context) throws SQLException; int countWithdrawnItems(Context context) throws SQLException;
} }