[CST-5294] FIxed NBEventRestRepositoryIT test

This commit is contained in:
Luca Giamminonni
2022-02-16 15:48:28 +01:00
parent 75d9d6747e
commit 2340a44e96
3 changed files with 85 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ import org.dspace.content.Item;
import org.dspace.content.Relationship;
import org.dspace.content.RelationshipType;
import org.dspace.content.WorkspaceItem;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.EntityTypeService;
import org.dspace.content.service.InstallItemService;
import org.dspace.content.service.ItemService;
@@ -51,6 +52,9 @@ public class NBEntityMetadataAction implements NBAction {
@Autowired
private WorkspaceItemService workspaceItemService;
@Autowired
private CollectionService collectionService;
public void setItemService(ItemService itemService) {
this.itemService = itemService;
}
@@ -96,11 +100,11 @@ public class NBEntityMetadataAction implements NBAction {
if (relatedItem != null) {
link(context, item, relatedItem);
} else {
Collection collection = item.getOwningCollection();
Collection collection = collectionService.retrieveCollectionByEntityType(context, item, entityType);
WorkspaceItem workspaceItem = workspaceItemService.create(context, collection, false);
relatedItem = workspaceItem.getItem();
if (StringUtils.isNotBlank(entityType)) {
itemService.addMetadata(context, relatedItem, "relationship", "type", null, null, entityType);
itemService.addMetadata(context, relatedItem, "dspace", "entity", "type", null, entityType);
}
for (String key : entityMetadata.keySet()) {
String value = getValue(message, key);

View File

@@ -1017,6 +1017,58 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
return resp;
}
@Override
public Collection retrieveCollectionByEntityType(Context context, Item item, String entityType)
throws SQLException {
Collection ownCollection = item.getOwningCollection();
return retrieveCollectionByEntityType(context, ownCollection.getCommunities(), entityType);
}
private Collection retrieveCollectionByEntityType(Context context, List<Community> communities, String entityType) {
for (Community community : communities) {
Collection collection = retrieveCollectionByCommunityAndEntityType(context, community, entityType);
if (collection != null) {
return collection;
}
}
for (Community community : communities) {
List<Community> parentCommunities = community.getParentCommunities();
Collection collection = retrieveCollectionByEntityType(context, parentCommunities, entityType);
if (collection != null) {
return collection;
}
}
return retrieveCollectionByCommunityAndEntityType(context, null, entityType);
}
@Override
public Collection retrieveCollectionByCommunityAndEntityType(Context context, Community community,
String entityType) {
context.turnOffAuthorisationSystem();
List<Collection> collections;
try {
collections = findCollectionsWithSubmit(null, context, community, entityType, 0, 1);
} catch (SQLException | SearchServiceException e) {
throw new RuntimeException(e);
}
context.restoreAuthSystemState();
if (collections != null && collections.size() > 0) {
return collections.get(0);
}
if (community != null) {
for (Community subCommunity : community.getSubcommunities()) {
Collection collection = retrieveCollectionByCommunityAndEntityType(context, subCommunity, entityType);
if (collection != null) {
return collection;
}
}
}
return null;
}
@Override
public List<Collection> findCollectionsWithSubmit(String q, Context context, Community community, String entityType,
int offset, int limit) throws SQLException, SearchServiceException {

View File

@@ -412,6 +412,33 @@ public interface CollectionService
public List<Collection> findCollectionsWithSubmit(String q, Context context, Community community,
int offset, int limit) throws SQLException, SearchServiceException;
/**
* Retrieve the first collection in the community or its descending that support
* the provided entityType
*
* @param context the DSpace context
* @param community the root from where the search start
* @param entityType the requested entity type
* @return the first collection in the community or its descending
* that support the provided entityType
*/
public Collection retrieveCollectionByCommunityAndEntityType(Context context, Community community,
String entityType);
/**
* Retrieve the close collection to the item that support the provided
* entityType. Close mean the collection that can be reach with the minimum
* steps starting from the item (owningCollection, brothers collections, etc)
*
* @param context the DSpace context
* @param item the item from where the search start
* @param entityType the requested entity type
* @return the first collection in the community or its descending
* that support the provided entityType
*/
public Collection retrieveCollectionByEntityType(Context context, Item item, String entityType)
throws SQLException;
/**
* 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)