Item counters refactor. Removing unnecessary code.

This commit is contained in:
damian
2023-05-12 12:29:22 +02:00
parent 0231af5867
commit 1cc6fb2f58
9 changed files with 21 additions and 27 deletions

View File

@@ -18,6 +18,7 @@ import org.dspace.content.service.ItemService;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.services.ConfigurationService; import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory; import org.dspace.services.factory.DSpaceServicesFactory;
import org.dspace.web.ContextUtil;
/** /**
* This class provides a standard interface to all item counting * This class provides a standard interface to all item counting
@@ -49,6 +50,8 @@ public class ItemCounter {
*/ */
private Context context; private Context context;
private static ItemCounter instance;
protected ItemService itemService; protected ItemService itemService;
protected ConfigurationService configurationService; protected ConfigurationService configurationService;
@@ -65,6 +68,13 @@ public class ItemCounter {
this.configurationService = DSpaceServicesFactory.getInstance().getConfigurationService(); this.configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
} }
public static ItemCounter getInstance() throws ItemCountException {
if (instance == null) {
instance = new ItemCounter(ContextUtil.obtainCurrentRequestContext());
}
return instance;
}
/** /**
* Get the count of the items in the given container. If the configuration * 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 * value webui.strengths.cache is equal to 'true' this will return the

View File

@@ -337,7 +337,7 @@ public class Collection extends DSpaceObject implements DSpaceObjectLegacySuppor
return collectionService; return collectionService;
} }
public int getAllItems() { public int countArchivedItem() {
try { try {
return collectionService.countArchivedItem(this); return collectionService.countArchivedItem(this);
} catch (ItemCountException e) { } catch (ItemCountException e) {

View File

@@ -1051,10 +1051,6 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
@Override @Override
public int countArchivedItem(Collection collection) throws ItemCountException { public int countArchivedItem(Collection collection) throws ItemCountException {
//TODO load ItemCounter on bean creation return ItemCounter.getInstance().getCount(collection);
Context context = new Context();
return new ItemCounter(context).getCount(collection);
} }
} }

View File

@@ -265,9 +265,9 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport
return communityService; return communityService;
} }
public int getAllItems() { public int countArchivedItem() {
try { try {
return communityService.getArchivedItems(this); return communityService.getArchivedItems(this);
} catch (ItemCountException e) { } catch (ItemCountException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -713,8 +713,6 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
@Override @Override
public int getArchivedItems(Community community) throws ItemCountException { public int getArchivedItems(Community community) throws ItemCountException {
//TODO load ItemCounter on bean creation return ItemCounter.getInstance().getCount(community);
Context context = new Context();
return new ItemCounter(context).getCount(community);
} }
} }

View File

@@ -10,7 +10,6 @@ package org.dspace.content.dao;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.content.Item; import org.dspace.content.Item;
@@ -54,6 +53,4 @@ public interface CollectionDAO extends DSpaceObjectLegacySupportDAO<Collection>
int countRows(Context context) throws SQLException; int countRows(Context context) throws SQLException;
List<Map.Entry<Collection, Long>> getCollectionsWithBitstreamSizesTotal(Context context) throws SQLException; List<Map.Entry<Collection, Long>> getCollectionsWithBitstreamSizesTotal(Context context) throws SQLException;
int countArchivedItems(Context context, UUID id) throws SQLException;
} }

View File

@@ -7,9 +7,11 @@
*/ */
package org.dspace.content.dao.impl; package org.dspace.content.dao.impl;
import java.math.BigInteger;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.Query; import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
@@ -180,12 +182,4 @@ public class CollectionDAOImpl extends AbstractHibernateDSODAO<Collection> imple
} }
return returnList; return returnList;
} }
@Override
public int countArchivedItems(Context context, UUID collectionId) throws SQLException {
return ((BigInteger) getHibernateSession(context)
.createNativeQuery("select count(*) from collection2item ci left join item i on i.uuid = ci.item_id where i.in_archive and uuid(ci.collection_id)=uuid(:collectionId)")
.setParameter("collectionId", collectionId.toString())
.getSingleResult()).intValue();
}
} }

View File

@@ -10,7 +10,6 @@ package org.dspace.app.rest.converter;
import org.dspace.app.rest.model.CollectionRest; import org.dspace.app.rest.model.CollectionRest;
import org.dspace.app.rest.projection.Projection; import org.dspace.app.rest.projection.Projection;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.core.Context;
import org.dspace.discovery.IndexableObject; import org.dspace.discovery.IndexableObject;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -27,7 +26,7 @@ public class CollectionConverter extends DSpaceObjectConverter<Collection, Colle
@Override @Override
public CollectionRest convert(Collection collection, Projection projection) { public CollectionRest convert(Collection collection, Projection projection) {
CollectionRest resource = super.convert(collection, projection); CollectionRest resource = super.convert(collection, projection);
resource.setArchivedItems(collection.getAllItems()); resource.setArchivedItems(collection.countArchivedItem());
return resource; return resource;
} }
@Override @Override

View File

@@ -26,7 +26,7 @@ public class CommunityConverter
public CommunityRest convert(Community community, Projection projection) { public CommunityRest convert(Community community, Projection projection) {
CommunityRest resource = super.convert(community, projection); CommunityRest resource = super.convert(community, projection);
resource.setArchivedItems(community.getAllItems()); resource.setArchivedItems(community.countArchivedItem());
return resource; return resource;
} }