implemented Solr query for CommunityCollectionLinkRepository

This commit is contained in:
Mykhaylo Boychuk
2020-02-28 16:07:27 +01:00
parent 38b89d85ae
commit 67a48dbd71

View File

@@ -8,6 +8,7 @@
package org.dspace.app.rest.repository; package org.dspace.app.rest.repository;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@@ -17,12 +18,17 @@ import javax.servlet.http.HttpServletRequest;
import org.dspace.app.rest.model.CollectionRest; import org.dspace.app.rest.model.CollectionRest;
import org.dspace.app.rest.model.CommunityRest; import org.dspace.app.rest.model.CommunityRest;
import org.dspace.app.rest.projection.Projection; import org.dspace.app.rest.projection.Projection;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.content.Community; import org.dspace.content.Community;
import org.dspace.content.service.CommunityService; import org.dspace.content.service.CommunityService;
import org.dspace.core.Constants;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.discovery.DiscoverQuery;
import org.dspace.discovery.DiscoverResult;
import org.dspace.discovery.IndexableObject;
import org.dspace.discovery.SearchService;
import org.dspace.discovery.SearchServiceException;
import org.dspace.discovery.indexobject.IndexableCollection;
import org.dspace.discovery.indexobject.factory.IndexObjectFactoryFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@@ -41,7 +47,7 @@ public class CommunityCollectionLinkRepository extends AbstractDSpaceRestReposit
CommunityService communityService; CommunityService communityService;
@Autowired @Autowired
AuthorizeService authorizeService; SearchService searchService;
@PreAuthorize("hasPermission(#communityId, 'COMMUNITY', 'READ')") @PreAuthorize("hasPermission(#communityId, 'COMMUNITY', 'READ')")
public Page<CollectionRest> getCollections(@Nullable HttpServletRequest request, public Page<CollectionRest> getCollections(@Nullable HttpServletRequest request,
@@ -54,16 +60,25 @@ public class CommunityCollectionLinkRepository extends AbstractDSpaceRestReposit
if (community == null) { if (community == null) {
throw new ResourceNotFoundException("No such community: " + communityId); throw new ResourceNotFoundException("No such community: " + communityId);
} }
List<Collection> collections = community.getCollections(); Pageable pageable = utils.getPageable(optionalPageable);
List<Collection> publicCollection = new LinkedList<Collection>(); List<Collection> collections = new LinkedList<Collection>();
for (Collection c : collections ) { IndexObjectFactoryFactory indexObjectFactory = IndexObjectFactoryFactory.getInstance();
if (authorizeService.authorizeActionBoolean(context, c, Constants.READ)) { IndexableObject scopeObject = indexObjectFactory.getIndexableObjects(context, community).get(0);
publicCollection.add(c); DiscoverQuery discoverQuery = new DiscoverQuery();
discoverQuery.setQuery("*:*");
discoverQuery.setDSpaceObjectFilter(IndexableCollection.TYPE);
discoverQuery.addFilterQueries("location.parent:" + communityId);
discoverQuery.setStart(Math.toIntExact(pageable.getOffset()));
discoverQuery.setMaxResults(pageable.getPageSize());
DiscoverResult resp = searchService.search(context, scopeObject, discoverQuery);
long tot = resp.getTotalSearchResults();
for (IndexableObject solrCol : resp.getIndexableObjects()) {
Collection c = ((IndexableCollection) solrCol).getIndexedObject();
collections.add(c);
} }
} return converter.toRestPage(collections, pageable, tot, utils.obtainProjection());
return converter.toRestPage(utils.getPage(publicCollection, optionalPageable), projection); } catch (SQLException | SearchServiceException e) {
} catch (SQLException e) { throw new RuntimeException(e.getMessage(), e);
throw new RuntimeException(e);
} }
} }
} }