implemented findByEntityType collection end point

This commit is contained in:
Mykhaylo
2021-09-20 10:00:30 +02:00
parent ef07aabbe1
commit 25cfa7c750
6 changed files with 162 additions and 0 deletions

View File

@@ -992,4 +992,89 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
DiscoverResult resp = searchService.search(context, discoverQuery);
return resp;
}
/**
* Finds all Indexed Collections where the current user has submit rights. If the user is an Admin,
* this is all Indexed Collections. Otherwise, it includes those collections where
* an indexed "submit" policy lists either the eperson or one of the eperson's groups
*
* @param context DSpace context
* @param discoverQuery
* @param entityType limit the returned collection to those related to given entity type
* @param community parent community, could be null
* @param q limit the returned collection to those with metadata values matching the query
* terms. The terms are used to make also a prefix query on SOLR
* so it can be used to implement an autosuggest feature over the collection name
* @return discovery search result objects
* @throws SQLException if something goes wrong
* @throws SearchServiceException if search error
*/
private DiscoverResult retrieveCollectionsWithSubmit(Context context, DiscoverQuery discoverQuery,
String entityType, Community community, String q)
throws SQLException, SearchServiceException {
StringBuilder query = new StringBuilder();
EPerson currentUser = context.getCurrentUser();
if (!authorizeService.isAdmin(context)) {
String userId = "";
if (currentUser != null) {
userId = currentUser.getID().toString();
}
query.append("submit:(e").append(userId);
Set<Group> groups = groupService.allMemberGroupsSet(context, currentUser);
for (Group group : groups) {
query.append(" OR g").append(group.getID());
}
query.append(")");
discoverQuery.addFilterQueries(query.toString());
}
StringBuilder buildFilter = new StringBuilder();
if (community != null) {
buildFilter.append("location.comm:").append(community.getID().toString());
}
if (StringUtils.isNotBlank(entityType)) {
if (buildFilter.length() > 0) {
buildFilter.append(" AND ");
}
buildFilter.append("dspace.entity.type:").append(entityType);
}
if (StringUtils.isNotBlank(q)) {
StringBuilder buildQuery = new StringBuilder();
String escapedQuery = ClientUtils.escapeQueryChars(q);
buildQuery.append(escapedQuery).append(" OR ").append(escapedQuery).append("*");
discoverQuery.setQuery(buildQuery.toString());
}
discoverQuery.addFilterQueries(buildFilter.toString());
DiscoverResult resp = searchService.search(context, discoverQuery);
return resp;
}
@Override
public List<Collection> findCollectionsWithSubmit(String q, Context context, Community community, String entityType,
int offset, int limit) throws SQLException, SearchServiceException {
List<Collection> collections = new ArrayList<>();
DiscoverQuery discoverQuery = new DiscoverQuery();
discoverQuery.setDSpaceObjectFilter(IndexableCollection.TYPE);
discoverQuery.setStart(offset);
discoverQuery.setMaxResults(limit);
DiscoverResult resp = retrieveCollectionsWithSubmit(context, discoverQuery,
entityType, community, q);
for (IndexableObject solrCollections : resp.getIndexableObjects()) {
Collection c = ((IndexableCollection) solrCollections).getIndexedObject();
collections.add(c);
}
return collections;
}
@Override
public int countCollectionsWithSubmit(String q, Context context, Community community, String entityType)
throws SQLException, SearchServiceException {
DiscoverQuery discoverQuery = new DiscoverQuery();
discoverQuery.setMaxResults(0);
discoverQuery.setDSpaceObjectFilter(IndexableCollection.TYPE);
DiscoverResult resp = retrieveCollectionsWithSubmit(context, discoverQuery, entityType, community, q);
return (int) resp.getTotalSearchResults();
}
}

View File

@@ -344,6 +344,27 @@ public interface CollectionService
Group createDefaultReadGroup(Context context, Collection collection, String typeOfGroupString, int defaultRead)
throws SQLException, AuthorizeException;
/**
* Returns Collections for which the current user has 'submit' privileges.
* NOTE: for better performance, this method retrieves its results from an
* index (cache) and does not query the database directly.
* This means that results may be stale or outdated until DS-4524 is resolved"
*
* @param q limit the returned collection to those with metadata values matching the query terms.
* The terms are used to make also a prefix query on SOLR so it can be used to implement
* an autosuggest feature over the collection name
* @param context DSpace Context
* @param community parent community
* @param entityType limit the returned collection to those related to given entity type
* @param offset the position of the first result to return
* @param limit paging limit
* @return discovery search result objects
* @throws SQLException if something goes wrong
* @throws SearchServiceException if search error
*/
public List<Collection> findCollectionsWithSubmit(String q, Context context, Community community,
String entityType, int offset, int limit) throws SQLException, SearchServiceException;
/**
* Returns Collections for which the current user has 'submit' privileges.
* NOTE: for better performance, this method retrieves its results from an
@@ -381,4 +402,24 @@ public interface CollectionService
*/
public int countCollectionsWithSubmit(String q, Context context, Community community)
throws SQLException, SearchServiceException;
/**
* Counts the number of Collection for which the current user has 'submit' privileges.
* NOTE: for better performance, this method retrieves its results from an index (cache)
* and does not query the database directly.
* This means that results may be stale or outdated until DS-4524 is resolved."
*
* @param q limit the returned collection to those with metadata values matching the query terms.
* The terms are used to make also a prefix query on SOLR so it can be used to implement
* an autosuggest feature over the collection name
* @param context DSpace Context
* @param community parent community
* @param entityType limit the returned collection to those related to given entity type
* @return total collections found
* @throws SQLException if something goes wrong
* @throws SearchServiceException if search error
*/
public int countCollectionsWithSubmit(String q, Context context, Community community, String entityType)
throws SQLException, SearchServiceException;
}

View File

@@ -111,6 +111,7 @@ public class CollectionIndexFactoryImpl extends DSpaceObjectIndexFactoryImpl<Ind
CollectionService.MD_LICENSE, Item.ANY);
String title = collectionService.getMetadataFirstValue(collection,
CollectionService.MD_NAME, Item.ANY);
String relation = collectionService.getMetadataFirstValue(collection, "dspace", "entity", "type", Item.ANY);
List<String> toIgnoreMetadataFields = SearchUtils.getIgnoredMetadataFields(collection.getType());
addContainerMetadataField(doc, highlightedMetadataFields, toIgnoreMetadataFields, "dc.description",
@@ -125,6 +126,7 @@ public class CollectionIndexFactoryImpl extends DSpaceObjectIndexFactoryImpl<Ind
rights_license);
addContainerMetadataField(doc, highlightedMetadataFields, toIgnoreMetadataFields, "dc.title", title);
doc.addField("dc.title_sort", title);
addContainerMetadataField(doc, highlightedMetadataFields, toIgnoreMetadataFields,"dspace.entity.type",relation);
return doc;
}

View File

@@ -100,6 +100,10 @@ public class CollectionBuilder extends AbstractDSpaceObjectBuilder<Collection> {
return setMetadataSingleValue(collection, MetadataSchemaEnum.DC.getName(), "title", null, name);
}
public CollectionBuilder withEntityType(final String entityType) {
return setMetadataSingleValue(collection, "dspace", "entity", "type", entityType);
}
/**
* Set the name of the Collection in the given language.
*

View File

@@ -41,10 +41,12 @@ import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.Bitstream;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.EntityType;
import org.dspace.content.Item;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.EntityTypeService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
@@ -112,6 +114,9 @@ public class CollectionRestRepository extends DSpaceObjectRestRepository<Collect
@Autowired
private CollectionRoleService collectionRoleService;
@Autowired
private EntityTypeService entityTypeService;
@Autowired
SearchService searchService;
@@ -216,6 +221,28 @@ public class CollectionRestRepository extends DSpaceObjectRestRepository<Collect
}
}
@SearchRestMethod(name = "findSubmitAuthorizedByEntityType")
public Page<CollectionRest> findSubmitAuthorizedByEntityType(
@Parameter(value = "query") String query,
@Parameter(value = "entityType", required = true) String entityTypeLabel,
Pageable pageable)
throws SearchServiceException {
try {
Context context = obtainContext();
EntityType entityType = this.entityTypeService.findByEntityType(context, entityTypeLabel);
if (entityType == null) {
throw new ResourceNotFoundException("There was no entityType found with label: " + entityTypeLabel);
}
List<Collection> collections = cs.findCollectionsWithSubmit(query, context, null, entityTypeLabel,
Math.toIntExact(pageable.getOffset()),
Math.toIntExact(pageable.getOffset() + pageable.getPageSize()));
int tot = cs.countCollectionsWithSubmit(query, context, null, entityTypeLabel);
return converter.toRestPage(collections, pageable, tot, utils.obtainProjection());
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@Override
@PreAuthorize("hasPermission(#id, 'COLLECTION', 'WRITE')")
protected void patch(Context context, HttpServletRequest request, String apiCategory, String model, UUID id,

View File

@@ -263,6 +263,9 @@
<!-- used to track which group(s) have submit permissions -->
<field name="submit" type="string" indexed="true" stored="true" omitNorms="true" multiValued="true" docValues="true" />
<!-- used to track entity type of collections -->
<field name="dspace.entity.type" type="string" indexed="true" stored="true" required="false" />
<!-- Community and collection hierarchy of the Item of interest (candidate for hierarchical facetting ) -->
<field name="location" type="lowerCaseSort" indexed="true" stored="true" multiValued="true" required="false" omitNorms="true" />