Restore SetRepository classes (accidentally deleted in refactor). Add a new limit/offet option to CommunityService.findAll()

This commit is contained in:
Tim Donohue
2015-10-07 16:29:08 -05:00
parent 05fbfeac7a
commit feebea46bc
7 changed files with 284 additions and 3 deletions

View File

@@ -137,6 +137,12 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
return communityDAO.findAll(context, sortField);
}
@Override
public List<Community> 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<Community> findAllTop(Context context) throws SQLException
{

View File

@@ -27,6 +27,8 @@ public interface CommunityDAO extends DSpaceObjectLegacySupportDAO<Community> {
public List<Community> findAll(Context context, MetadataField sortField) throws SQLException;
public List<Community> findAll(Context context, MetadataField sortField, Integer limit, Integer offset) throws SQLException;
public Community findByAdminGroup(Context context, Group group) throws SQLException;
public List<Community> findAllNoParent(Context context, MetadataField sortField) throws SQLException;

View File

@@ -46,13 +46,24 @@ public class CommunityDAOImpl extends AbstractHibernateDSODAO<Community> impleme
@Override
public List<Community> findAll(Context context, MetadataField sortField) throws SQLException
{
return findAll(context, sortField, null, null);
}
@Override
public List<Community> 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);
}

View File

@@ -65,6 +65,16 @@ public interface CommunityService extends DSpaceObjectService<Community>, DSpace
*/
public List<Community> 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<Community> 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

View File

@@ -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();

View File

@@ -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 <dspace@lyncode.com>
*/
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<Community> communityList = communityService.findAll(_context);
return communityList.size();
}
catch (SQLException e)
{
log.error(e.getMessage(), e);
}
return 0;
}
private int getCollectionCount()
{
try
{
List<Collection> 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<Set> community(int offset, int length)
{
List<Set> array = new ArrayList<Set>();
try
{
List<Community> 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<Set> collection(int offset, int length)
{
List<Set> array = new ArrayList<Set>();
try
{
List<Collection> 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<Set> array = new ArrayList<Set>();
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<Set> 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;
}
}

View File

@@ -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());
}
}