Skip to content

Commit

Permalink
Merge pull request DSpace#9643 from DSpace/backport-9621-to-dspace-7_x
Browse files Browse the repository at this point in the history
[Port dspace-7_x] Fixed item edit relationships with same type name but different entities all being shown under same label
  • Loading branch information
tdonohue committed Jun 10, 2024
2 parents 2bf663e + 7789304 commit 370550c
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.DSpaceObject;
import org.dspace.content.EntityType;
import org.dspace.content.Item;
import org.dspace.content.Relationship;
import org.dspace.content.RelationshipType;
import org.dspace.content.service.EntityTypeService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.RelationshipService;
import org.dspace.content.service.RelationshipTypeService;
Expand Down Expand Up @@ -60,6 +62,9 @@ public class RelationshipRestRepository extends DSpaceRestRepository<Relationshi
private static final String RIGHT = "right";
private static final String CONFIGURED = "configured";

@Autowired
private EntityTypeService entityTypeService;

@Autowired
private ItemService itemService;

Expand Down Expand Up @@ -331,13 +336,15 @@ protected void delete(Context context, Integer id) throws AuthorizeException {
*
* @param label The label of a RelationshipType which the Relationships must have if they're to be returned
* @param dsoId The dsoId of the object that has to be a leftItem or rightItem if this parameter is present
* @param relatedEntityType The entity type that the items who have a relationship with the given dso should have
* @param pageable The page object
* @return A page with all the RelationshipRest objects that correspond to the constraints
* @throws SQLException If something goes wrong
*/
@SearchRestMethod(name = "byLabel")
public Page<RelationshipRest> findByLabel(@Parameter(value = "label", required = true) String label,
@Parameter(value = "dso", required = false) UUID dsoId,
@Parameter(value = "relatedEntityType") String relatedEntityType,
Pageable pageable) throws SQLException {
Context context = obtainContext();

Expand All @@ -352,14 +359,28 @@ public Page<RelationshipRest> findByLabel(@Parameter(value = "label", required =
if (item == null) {
throw new ResourceNotFoundException("The request DSO with id: " + dsoId + " was not found");
}

EntityType dsoEntityType = itemService.getEntityType(context, item);

if (dsoEntityType == null) {
throw new UnprocessableEntityException(String.format(
"The request DSO with id: %s doesn't have an entity type", dsoId));
}

for (RelationshipType relationshipType : relationshipTypeList) {
boolean isLeft = false;
if (relationshipType.getLeftwardType().equalsIgnoreCase(label)) {
isLeft = true;
if (relatedEntityType == null ||
relationshipType.getRightType().getLabel().equals(dsoEntityType.getLabel()) &&
relationshipType.getLeftType().getLabel().equals(relatedEntityType) ||
relationshipType.getRightType().getLabel().equals(relatedEntityType) &&
relationshipType.getLeftType().getLabel().equals(dsoEntityType.getLabel())) {
boolean isLeft = relationshipType.getLeftwardType().equalsIgnoreCase(label);
total +=
relationshipService.countByItemAndRelationshipType(context, item, relationshipType, isLeft);
relationships.addAll(
relationshipService.findByItemAndRelationshipType(context, item, relationshipType,
isLeft, pageable.getPageSize(),
Math.toIntExact(pageable.getOffset())));
}
total += relationshipService.countByItemAndRelationshipType(context, item, relationshipType, isLeft);
relationships.addAll(relationshipService.findByItemAndRelationshipType(context, item, relationshipType,
isLeft, pageable.getPageSize(), Math.toIntExact(pageable.getOffset())));
}
} else {
for (RelationshipType relationshipType : relationshipTypeList) {
Expand All @@ -377,7 +398,7 @@ public Page<RelationshipRest> findByLabel(@Parameter(value = "label", required =
* of potentially related items we need to know which of these other items
* are already in a specific relationship with the focus item and,
* by exclusion which ones are not yet related.
*
*
* @param typeId The relationship type id to apply as a filter to the returned relationships
* @param label The name of the relation as defined from the side of the 'focusItem'
* @param focusUUID The uuid of the item to be checked on the side defined by 'relationshipLabel'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,77 @@ public void findRelationshipByLabelTest() throws Exception {
;
}

@Test
public void findRelationshipByLabelWithRelatedEntityTypeTest() throws Exception {
context.turnOffAuthorisationSystem();
RelationshipType isAuthorOfPublicationRelationshipTypePublication = relationshipTypeService
.findbyTypesAndTypeName(context, entityTypeService.findByEntityType(context, "Publication"),
entityTypeService.findByEntityType(context, "Person"),
"isAuthorOfPublication", "isPublicationOfAuthor");
RelationshipType isAuthorOfPublicationRelationshipTypeOrgUnit = relationshipTypeService
.findbyTypesAndTypeName(context, entityTypeService.findByEntityType(context, "Publication"),
entityTypeService.findByEntityType(context, "OrgUnit"),
"isAuthorOfPublication", "isPublicationOfAuthor");

// We're creating a Relationship of type isAuthorOfPublication between a Publication and a Person
Relationship relationship1 = RelationshipBuilder
.createRelationshipBuilder(context, publication1, author1, isAuthorOfPublicationRelationshipTypePublication)
.build();

// We're creating a Relationship of type isAuthorOfPublication between a Publication and an OrgUnit
Relationship relationship2 = RelationshipBuilder
.createRelationshipBuilder(context, publication1, orgUnit1, isAuthorOfPublicationRelationshipTypeOrgUnit)
.build();
context.restoreAuthSystemState();

// Perform a GET request to the searchByLabel endpoint, asking for Relationships of type isAuthorOfPublication
// With an extra parameter namely DSO which resolves to the publication used by both relationships.
// Both relationships should be returned if we don't specify the DSO's related entity type
getClient().perform(get("/api/core/relationships/search/byLabel")
.param("label", "isAuthorOfPublication")
.param("dso", publication1.getID().toString())
.param("projection", "full"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.page", is(PageMatcher.pageEntryWithTotalPagesAndElements(0, 20, 1, 2))))
.andExpect(jsonPath("$._embedded.relationships", containsInAnyOrder(
RelationshipMatcher.matchRelationship(relationship1),
RelationshipMatcher.matchRelationship(relationship2)
)))
;

// Perform a GET request to the searchByLabel endpoint, asking for Relationships of type isAuthorOfPublication
// With an extra parameter namely DSO which resolves to the publication used by both relationships.
// Only the Person relationship should be returned if we specify the DSO's related entity type
getClient().perform(get("/api/core/relationships/search/byLabel")
.param("label", "isAuthorOfPublication")
.param("dso", publication1.getID().toString())
.param("relatedEntityType", "Person")
.param("projection", "full"))

.andExpect(status().isOk())
.andExpect(jsonPath("$.page", is(PageMatcher.pageEntryWithTotalPagesAndElements(0, 20, 1, 1))))
.andExpect(jsonPath("$._embedded.relationships", containsInAnyOrder(
RelationshipMatcher.matchRelationship(relationship1)
)))
;

// Perform a GET request to the searchByLabel endpoint, asking for Relationships of type isAuthorOfPublication
// With an extra parameter namely DSO which resolves to the publication used by both relationships.
// Only the OrgUnit relationship should be returned if we specify the DSO's related entity type
getClient().perform(get("/api/core/relationships/search/byLabel")
.param("label", "isAuthorOfPublication")
.param("dso", publication1.getID().toString())
.param("relatedEntityType", "OrgUnit")
.param("projection", "full"))

.andExpect(status().isOk())
.andExpect(jsonPath("$.page", is(PageMatcher.pageEntryWithTotalPagesAndElements(0, 20, 1, 1))))
.andExpect(jsonPath("$._embedded.relationships", containsInAnyOrder(
RelationshipMatcher.matchRelationship(relationship2)
)))
;
}

@Test
public void putRelationshipWithNonexistentID() throws Exception {
context.turnOffAuthorisationSystem();
Expand Down

0 comments on commit 370550c

Please sign in to comment.