From feebea46bcc77a98fd1bafc933c0b8c875e7b6f4 Mon Sep 17 00:00:00 2001 From: Tim Donohue Date: Wed, 7 Oct 2015 16:29:08 -0500 Subject: [PATCH] Restore SetRepository classes (accidentally deleted in refactor). Add a new limit/offet option to CommunityService.findAll() --- .../dspace/content/CommunityServiceImpl.java | 6 + .../org/dspace/content/dao/CommunityDAO.java | 2 + .../content/dao/impl/CommunityDAOImpl.java | 17 +- .../content/service/CommunityService.java | 10 + .../dspace/xoai/app/BasicConfiguration.java | 8 + .../impl/xoai/DSpaceSetRepository.java | 220 ++++++++++++++++++ .../xoai/DSpaceSetRepositoryResolver.java | 24 ++ 7 files changed, 284 insertions(+), 3 deletions(-) create mode 100644 dspace-oai/src/main/java/org/dspace/xoai/services/impl/xoai/DSpaceSetRepository.java create mode 100644 dspace-oai/src/main/java/org/dspace/xoai/services/impl/xoai/DSpaceSetRepositoryResolver.java diff --git a/dspace-api/src/main/java/org/dspace/content/CommunityServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/CommunityServiceImpl.java index 94b1ab965b..553acf5ca8 100644 --- a/dspace-api/src/main/java/org/dspace/content/CommunityServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/CommunityServiceImpl.java @@ -137,6 +137,12 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl imp return communityDAO.findAll(context, sortField); } + @Override + public List findAll(Context context, Integer limit, Integer offset) throws SQLException { + MetadataField nameField = metadataFieldService.findByElement(context, MetadataSchema.DC_SCHEMA, "title", null); + return communityDAO.findAll(context, nameField, limit, offset); + } + @Override public List findAllTop(Context context) throws SQLException { diff --git a/dspace-api/src/main/java/org/dspace/content/dao/CommunityDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/CommunityDAO.java index fc20ba4b29..b8342bc47f 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/CommunityDAO.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/CommunityDAO.java @@ -27,6 +27,8 @@ public interface CommunityDAO extends DSpaceObjectLegacySupportDAO { public List findAll(Context context, MetadataField sortField) throws SQLException; + public List findAll(Context context, MetadataField sortField, Integer limit, Integer offset) throws SQLException; + public Community findByAdminGroup(Context context, Group group) throws SQLException; public List findAllNoParent(Context context, MetadataField sortField) throws SQLException; diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/CommunityDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/CommunityDAOImpl.java index b7b0ce1dc1..a535b191de 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/CommunityDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/CommunityDAOImpl.java @@ -46,13 +46,24 @@ public class CommunityDAOImpl extends AbstractHibernateDSODAO impleme @Override public List findAll(Context context, MetadataField sortField) throws SQLException { + return findAll(context, sortField, null, null); + } + + @Override + public List findAll(Context context, MetadataField sortField, Integer limit, Integer offset) throws SQLException { StringBuilder queryBuilder = new StringBuilder(); - queryBuilder.append("SELECT community FROM Community as community "); - addMetadataLeftJoin(queryBuilder, Community.class.getSimpleName().toLowerCase(), Arrays.asList(sortField)); + queryBuilder.append("SELECT ").append(Community.class.getSimpleName()).append(" FROM Community as ").append(Community.class.getSimpleName()).append(" "); + addMetadataLeftJoin(queryBuilder, Community.class.getSimpleName(), Arrays.asList(sortField)); addMetadataSortQuery(queryBuilder, Arrays.asList(sortField), ListUtils.EMPTY_LIST); - Query query = createQuery(context, queryBuilder.toString()); + if(offset != null) + { + query.setFirstResult(offset); + } + if(limit != null){ + query.setMaxResults(limit); + } query.setParameter(sortField.toString(), sortField.getFieldID()); return list(query); } diff --git a/dspace-api/src/main/java/org/dspace/content/service/CommunityService.java b/dspace-api/src/main/java/org/dspace/content/service/CommunityService.java index 7931175de3..2da17936a1 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/CommunityService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/CommunityService.java @@ -65,6 +65,16 @@ public interface CommunityService extends DSpaceObjectService, DSpace */ public List findAll(Context context) throws SQLException; + /** + * Get all communities in the system. Adds support for limit and offset. + * @param context + * @param limit + * @param offset + * @return + * @throws SQLException + */ + public List findAll(Context context, Integer limit, Integer offset) throws SQLException; + /** * Get a list of all top-level communities in the system. These are * alphabetically sorted by community name. A top-level community is one diff --git a/dspace-oai/src/main/java/org/dspace/xoai/app/BasicConfiguration.java b/dspace-oai/src/main/java/org/dspace/xoai/app/BasicConfiguration.java index a397c905a2..7454b9490a 100644 --- a/dspace-oai/src/main/java/org/dspace/xoai/app/BasicConfiguration.java +++ b/dspace-oai/src/main/java/org/dspace/xoai/app/BasicConfiguration.java @@ -29,6 +29,7 @@ import org.dspace.xoai.services.api.solr.SolrServerResolver; import org.dspace.xoai.services.api.xoai.DSpaceFilterResolver; import org.dspace.xoai.services.api.xoai.IdentifyResolver; import org.dspace.xoai.services.api.xoai.ItemRepositoryResolver; +import org.dspace.xoai.services.api.xoai.SetRepositoryResolver; import org.dspace.xoai.services.impl.cache.DSpaceEmptyCacheService; import org.dspace.xoai.services.impl.cache.DSpaceXOAICacheService; import org.dspace.xoai.services.impl.cache.DSpaceXOAIItemCacheService; @@ -42,6 +43,7 @@ import org.dspace.xoai.services.impl.solr.DSpaceSolrServerResolver; import org.dspace.xoai.services.impl.xoai.BaseDSpaceFilterResolver; import org.dspace.xoai.services.impl.xoai.DSpaceIdentifyResolver; import org.dspace.xoai.services.impl.xoai.DSpaceItemRepositoryResolver; +import org.dspace.xoai.services.impl.xoai.DSpaceSetRepositoryResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -114,6 +116,12 @@ public class BasicConfiguration { public ItemRepositoryResolver itemRepositoryResolver () { return new DSpaceItemRepositoryResolver(); } + + @Bean + public SetRepositoryResolver setRepositoryResolver () { + return new DSpaceSetRepositoryResolver(); + } + @Bean public IdentifyResolver identifyResolver () { return new DSpaceIdentifyResolver(); diff --git a/dspace-oai/src/main/java/org/dspace/xoai/services/impl/xoai/DSpaceSetRepository.java b/dspace-oai/src/main/java/org/dspace/xoai/services/impl/xoai/DSpaceSetRepository.java new file mode 100644 index 0000000000..c6e707b4df --- /dev/null +++ b/dspace-oai/src/main/java/org/dspace/xoai/services/impl/xoai/DSpaceSetRepository.java @@ -0,0 +1,220 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.xoai.services.impl.xoai; + +import com.lyncode.xoai.dataprovider.core.ListSetsResult; +import com.lyncode.xoai.dataprovider.core.Set; +import com.lyncode.xoai.dataprovider.services.api.SetRepository; +import org.apache.log4j.LogManager; +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.xoai.data.DSpaceSet; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import org.dspace.content.factory.ContentServiceFactory; +import org.dspace.content.service.CollectionService; +import org.dspace.content.service.CommunityService; +import org.dspace.handle.factory.HandleServiceFactory; +import org.dspace.handle.service.HandleService; + +/** + * + * @author Lyncode Development Team + */ +public class DSpaceSetRepository implements SetRepository +{ + private static final Logger log = LogManager.getLogger(DSpaceSetRepository.class); + + private final Context _context; + + private final HandleService handleService; + private final CommunityService communityService; + private final CollectionService collectionService; + + public DSpaceSetRepository(Context context) + { + _context = context; + handleService = HandleServiceFactory.getInstance().getHandleService(); + communityService = ContentServiceFactory.getInstance().getCommunityService(); + collectionService = ContentServiceFactory.getInstance().getCollectionService(); + } + + private int getCommunityCount() + { + try + { + List communityList = communityService.findAll(_context); + + return communityList.size(); + } + catch (SQLException e) + { + log.error(e.getMessage(), e); + } + return 0; + } + + private int getCollectionCount() + { + try + { + List collectionList = collectionService.findAll(_context); + + return collectionList.size(); + } + catch (SQLException e) + { + log.error(e.getMessage(), e); + } + return 0; + } + + /** + * Produce a list of DSpaceCommunitySet. The list is a segment of the full + * list of Community ordered alphabetically by name. + * + * @param offset start this far down the list of Community. + * @param length return up to this many Sets. + * @return some Sets representing the Community list segment. + */ + private List community(int offset, int length) + { + List array = new ArrayList(); + + try + { + List communityList = communityService.findAll(_context, length, offset); + + for(Community community : communityList) + { + array.add(DSpaceSet.newDSpaceCommunitySet( + community.getHandle(), + community.getName())); + } + } + catch (SQLException e) + { + log.error(e.getMessage(), e); + } + return array; + } + + /** + * Produce a list of DSpaceCollectionSet. The list is a segment of the full + * list of Collection ordered alphabetically by name. + * + * @param offset start this far down the list of Collection. + * @param length return up to this many Sets. + * @return some Sets representing the Collection list segment. + */ + private List collection(int offset, int length) + { + List array = new ArrayList(); + + try + { + List collectionList = collectionService.findAll(_context, length, offset); + + for(Collection collection : collectionList) + { + array.add(DSpaceSet.newDSpaceCollectionSet( + collection.getHandle(), + collection.getName())); + } + } + catch (SQLException e) + { + log.error(e.getMessage(), e); + } + return array; + } + + @Override + public ListSetsResult retrieveSets(int offset, int length) + { + // Only database sets (virtual sets are added by lyncode common library) + log.debug("Querying sets. Offset: " + offset + " - Length: " + length); + List array = new ArrayList(); + int communityCount = this.getCommunityCount(); + log.debug("Communities: " + communityCount); + int collectionCount = this.getCollectionCount(); + log.debug("Collections: " + collectionCount); + + if (offset < communityCount) + { + if (offset + length > communityCount) + { + // Add some collections + List tmp = community(offset, length); + array.addAll(tmp); + array.addAll(collection(0, length - tmp.size())); + } + else + array.addAll(community(offset, length)); + } + else if (offset < communityCount + collectionCount) + { + array.addAll(collection(offset - communityCount, length)); + } + log.debug("Has More Results: " + + ((offset + length < communityCount + collectionCount) ? "Yes" + : "No")); + return new ListSetsResult(offset + length < communityCount + + collectionCount, array, communityCount + collectionCount); + } + + @Override + public boolean supportSets() + { + return true; + } + + @Override + public boolean exists(String setSpec) + { + if (setSpec.startsWith("col_")) + { + try + { + + DSpaceObject dso = handleService.resolveToObject(_context, + setSpec.replace("col_", "").replace("_", "/")); + if (dso == null || !(dso instanceof Collection)) + return false; + return true; + } + catch (SQLException ex) + { + log.error(ex.getMessage(), ex); + } + } + else if (setSpec.startsWith("com_")) + { + try + { + DSpaceObject dso = handleService.resolveToObject(_context, + setSpec.replace("com_", "").replace("_", "/")); + if (dso == null || !(dso instanceof Community)) + return false; + return true; + } + catch (SQLException ex) + { + log.error(ex.getMessage(), ex); + } + } + return false; + } + +} + diff --git a/dspace-oai/src/main/java/org/dspace/xoai/services/impl/xoai/DSpaceSetRepositoryResolver.java b/dspace-oai/src/main/java/org/dspace/xoai/services/impl/xoai/DSpaceSetRepositoryResolver.java new file mode 100644 index 0000000000..c5551d0f93 --- /dev/null +++ b/dspace-oai/src/main/java/org/dspace/xoai/services/impl/xoai/DSpaceSetRepositoryResolver.java @@ -0,0 +1,24 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.xoai.services.impl.xoai; + +import com.lyncode.xoai.dataprovider.services.api.SetRepository; +import org.dspace.xoai.services.api.context.ContextService; +import org.dspace.xoai.services.api.context.ContextServiceException; +import org.dspace.xoai.services.api.xoai.SetRepositoryResolver; +import org.springframework.beans.factory.annotation.Autowired; + +public class DSpaceSetRepositoryResolver implements SetRepositoryResolver { + @Autowired + private ContextService contextService; + + @Override + public SetRepository getSetRepository() throws ContextServiceException { + return new DSpaceSetRepository(contextService.getContext()); + } +}