[Task 71502] added forceDelete method to the RelationshipService that won't check the cardinality of the relationships. Used this in the ItemService delete and added tests

This commit is contained in:
Raf Ponsaerts
2020-06-26 10:54:34 +02:00
parent 645f6b66c9
commit 2a63f0ff22
4 changed files with 82 additions and 39 deletions

View File

@@ -679,7 +679,7 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
// Remove relationships // Remove relationships
for (Relationship relationship : relationshipService.findByItem(context, item)) { for (Relationship relationship : relationshipService.findByItem(context, item)) {
relationshipService.delete(context, relationship, false, false); relationshipService.forceDelete(context, relationship, false, false);
} }
// Remove bundles // Remove bundles

View File

@@ -331,21 +331,43 @@ public class RelationshipServiceImpl implements RelationshipService {
if (isRelationshipValidToDelete(context, relationship) && if (isRelationshipValidToDelete(context, relationship) &&
copyToItemPermissionCheck(context, relationship, copyToLeftItem, copyToRightItem)) { copyToItemPermissionCheck(context, relationship, copyToLeftItem, copyToRightItem)) {
// To delete a relationship, a user must have WRITE permissions on one of the related Items // To delete a relationship, a user must have WRITE permissions on one of the related Items
copyMetadataValues(context, relationship, copyToLeftItem, copyToRightItem); deleteRelationshipAndCopyToItem(context, relationship, copyToLeftItem, copyToRightItem);
if (authorizeService.authorizeActionBoolean(context, relationship.getLeftItem(), Constants.WRITE) ||
authorizeService.authorizeActionBoolean(context, relationship.getRightItem(), Constants.WRITE)) {
relationshipDAO.delete(context, relationship);
updatePlaceInRelationship(context, relationship);
} else {
throw new AuthorizeException(
"You do not have write rights on this relationship's items");
}
} else { } else {
throw new IllegalArgumentException("The relationship given was not valid"); throw new IllegalArgumentException("The relationship given was not valid");
} }
} }
@Override
public void forceDelete(Context context, Relationship relationship, boolean copyToLeftItem, boolean copyToRightItem)
throws SQLException, AuthorizeException {
log.info(org.dspace.core.LogManager.getHeader(context, "delete_relationship",
"relationship_id=" + relationship.getID() + "&" +
"copyMetadataValuesToLeftItem=" + copyToLeftItem + "&" +
"copyMetadataValuesToRightItem=" + copyToRightItem));
if (copyToItemPermissionCheck(context, relationship, copyToLeftItem, copyToRightItem)) {
// To delete a relationship, a user must have WRITE permissions on one of the related Items
deleteRelationshipAndCopyToItem(context, relationship, copyToLeftItem, copyToRightItem);
} else {
throw new IllegalArgumentException("The relationship given was not valid");
}
}
private void deleteRelationshipAndCopyToItem(Context context, Relationship relationship, boolean copyToLeftItem,
boolean copyToRightItem) throws SQLException, AuthorizeException {
copyMetadataValues(context, relationship, copyToLeftItem, copyToRightItem);
if (authorizeService.authorizeActionBoolean(context, relationship.getLeftItem(), Constants.WRITE) ||
authorizeService.authorizeActionBoolean(context, relationship.getRightItem(), Constants.WRITE)) {
relationshipDAO.delete(context, relationship);
updatePlaceInRelationship(context, relationship);
} else {
throw new AuthorizeException(
"You do not have write rights on this relationship's items");
}
}
/** /**
* Converts virtual metadata from RelationshipMetadataValue objects to actual item metadata. * Converts virtual metadata from RelationshipMetadataValue objects to actual item metadata.
* *

View File

@@ -315,4 +315,17 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
*/ */
void delete(Context context, Relationship relationship, boolean copyToLeftItem, boolean copyToRightItem) void delete(Context context, Relationship relationship, boolean copyToLeftItem, boolean copyToRightItem)
throws SQLException, AuthorizeException; throws SQLException, AuthorizeException;
/**
* This method is used to delete a Relationship whilst given the possibility to copy the Virtual Metadata created
* by this relationship to the left and/or right item.
* This method will bypass the cardinality checks on the {@link RelationshipType} for the given {@link Relationship}
* This should only be used during the deletion of items so that the min cardinality check can't disallow items
* to be deleted
* @param context The relevant DSpace context
* @param relationship The relationship to be deleted
* @param copyToLeftItem A boolean indicating whether we should copy metadata to the left item or not
* @param copyToRightItem A boolean indicating whether we should copy metadata to the right item or not
*/
void forceDelete(Context context, Relationship relationship, boolean copyToLeftItem, boolean copyToRightItem)
throws SQLException, AuthorizeException;
} }

View File

@@ -2717,38 +2717,41 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
.createCollection(context, parentCommunity).withName("Collection 1").build(); .createCollection(context, parentCommunity).withName("Collection 1").build();
Item author1 = ItemBuilder.createItem(context, col1) Item author1 = ItemBuilder.createItem(context, col1)
.withTitle("Author1") .withTitle("Author1")
.withIssueDate("2017-10-17") .withIssueDate("2017-10-17")
.withAuthor("Smith, Donald") .withAuthor("Smith, Donald")
.withPersonIdentifierLastName("Smith") .withPersonIdentifierLastName("Smith")
.withPersonIdentifierFirstName("Donald") .withPersonIdentifierFirstName("Donald")
.withRelationshipType("Person") .withRelationshipType("Person")
.build(); .build();
Item author2 = ItemBuilder.createItem(context, col1) Item author2 = ItemBuilder.createItem(context, col1)
.withTitle("Author2") .withTitle("Author2")
.withIssueDate("2016-02-13") .withIssueDate("2016-02-13")
.withAuthor("Smith, Maria") .withAuthor("Smith, Maria")
.withRelationshipType("Person") .withRelationshipType("Person")
.build(); .build();
Item publication1 = ItemBuilder.createItem(context, col1) Item publication1 = ItemBuilder.createItem(context, col1)
.withTitle("Publication1") .withTitle("Publication1")
.withAuthor("Testy, TEst") .withAuthor("Testy, TEst")
.withIssueDate("2015-01-01") .withIssueDate("2015-01-01")
.withRelationshipType("Publication") .withRelationshipType("Publication")
.build(); .build();
EntityType publication = EntityTypeBuilder.createEntityTypeBuilder(context, "Publication").build(); EntityType publication = EntityTypeBuilder.createEntityTypeBuilder(context, "Publication").build();
EntityType person = EntityTypeBuilder.createEntityTypeBuilder(context, "Person").build(); EntityType person = EntityTypeBuilder.createEntityTypeBuilder(context, "Person").build();
RelationshipType isAuthorOfPublication = RelationshipTypeBuilder.createRelationshipTypeBuilder(context, publication, person, "isAuthorOfPublication", RelationshipType isAuthorOfPublication = RelationshipTypeBuilder
"isPublicationOfAuthor", 2, null, 0, .createRelationshipTypeBuilder(context, publication, person, "isAuthorOfPublication",
null).withCopyToLeft(false).withCopyToRight(true).build(); "isPublicationOfAuthor", 2, null, 0,
null).withCopyToLeft(false).withCopyToRight(true).build();
Relationship relationship1 = RelationshipBuilder.createRelationshipBuilder(context, publication1, author1, isAuthorOfPublication).build(); Relationship relationship1 = RelationshipBuilder
Relationship relationship2 = RelationshipBuilder.createRelationshipBuilder(context, publication1, author2, isAuthorOfPublication).build(); .createRelationshipBuilder(context, publication1, author1, isAuthorOfPublication).build();
Relationship relationship2 = RelationshipBuilder
.createRelationshipBuilder(context, publication1, author2, isAuthorOfPublication).build();
context.restoreAuthSystemState(); context.restoreAuthSystemState();
@@ -2759,8 +2762,8 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
//Delete public item //Delete public item
getClient(token).perform(delete("/api/core/items/" + publication1.getID())) getClient(token).perform(delete("/api/core/items/" + publication1.getID()))
.andExpect(status().is(204)); .andExpect(status().is(204));
getClient(token).perform(get("/api/core/items/" + publication1.getID()))
.andExpect(status().is(404));
} }
@@ -2801,12 +2804,15 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
EntityType person = EntityTypeBuilder.createEntityTypeBuilder(context, "Person").build(); EntityType person = EntityTypeBuilder.createEntityTypeBuilder(context, "Person").build();
RelationshipType isAuthorOfPublication = RelationshipTypeBuilder.createRelationshipTypeBuilder(context, publication, person, "isAuthorOfPublication", RelationshipType isAuthorOfPublication = RelationshipTypeBuilder
"isPublicationOfAuthor", 2, null, 0, .createRelationshipTypeBuilder(context, publication, person, "isAuthorOfPublication",
null).withCopyToLeft(false).withCopyToRight(true).build(); "isPublicationOfAuthor", 2, null, 0,
null).withCopyToLeft(false).withCopyToRight(true).build();
Relationship relationship1 = RelationshipBuilder.createRelationshipBuilder(context, workspaceItem.getItem(), author1, isAuthorOfPublication).build(); Relationship relationship1 = RelationshipBuilder
Relationship relationship2 = RelationshipBuilder.createRelationshipBuilder(context, workspaceItem.getItem(), author2, isAuthorOfPublication).build(); .createRelationshipBuilder(context, workspaceItem.getItem(), author1, isAuthorOfPublication).build();
Relationship relationship2 = RelationshipBuilder
.createRelationshipBuilder(context, workspaceItem.getItem(), author2, isAuthorOfPublication).build();
context.restoreAuthSystemState(); context.restoreAuthSystemState();
@@ -2818,6 +2824,8 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
//Delete the workspaceitem //Delete the workspaceitem
getClient(token).perform(delete("/api/submission/workspaceitems/" + workspaceItem.getID())) getClient(token).perform(delete("/api/submission/workspaceitems/" + workspaceItem.getID()))
.andExpect(status().is(204)); .andExpect(status().is(204));
getClient(token).perform(get("/api/submission/workspaceitems/" + workspaceItem.getID()))
.andExpect(status().is(404));
} }