mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-18 07:23:08 +00:00
[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:
@@ -679,7 +679,7 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
|
||||
|
||||
// Remove relationships
|
||||
for (Relationship relationship : relationshipService.findByItem(context, item)) {
|
||||
relationshipService.delete(context, relationship, false, false);
|
||||
relationshipService.forceDelete(context, relationship, false, false);
|
||||
}
|
||||
|
||||
// Remove bundles
|
||||
|
@@ -331,6 +331,31 @@ public class RelationshipServiceImpl implements RelationshipService {
|
||||
if (isRelationshipValidToDelete(context, relationship) &&
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
@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)) {
|
||||
@@ -340,11 +365,8 @@ public class RelationshipServiceImpl implements RelationshipService {
|
||||
throw new AuthorizeException(
|
||||
"You do not have write rights on this relationship's items");
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("The relationship given was not valid");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts virtual metadata from RelationshipMetadataValue objects to actual item metadata.
|
||||
|
@@ -315,4 +315,17 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
|
||||
*/
|
||||
void delete(Context context, Relationship relationship, boolean copyToLeftItem, boolean copyToRightItem)
|
||||
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;
|
||||
}
|
@@ -2743,12 +2743,15 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
EntityType person = EntityTypeBuilder.createEntityTypeBuilder(context, "Person").build();
|
||||
|
||||
|
||||
RelationshipType isAuthorOfPublication = RelationshipTypeBuilder.createRelationshipTypeBuilder(context, publication, person, "isAuthorOfPublication",
|
||||
RelationshipType isAuthorOfPublication = RelationshipTypeBuilder
|
||||
.createRelationshipTypeBuilder(context, publication, person, "isAuthorOfPublication",
|
||||
"isPublicationOfAuthor", 2, null, 0,
|
||||
null).withCopyToLeft(false).withCopyToRight(true).build();
|
||||
|
||||
Relationship relationship1 = RelationshipBuilder.createRelationshipBuilder(context, publication1, author1, isAuthorOfPublication).build();
|
||||
Relationship relationship2 = RelationshipBuilder.createRelationshipBuilder(context, publication1, author2, isAuthorOfPublication).build();
|
||||
Relationship relationship1 = RelationshipBuilder
|
||||
.createRelationshipBuilder(context, publication1, author1, isAuthorOfPublication).build();
|
||||
Relationship relationship2 = RelationshipBuilder
|
||||
.createRelationshipBuilder(context, publication1, author2, isAuthorOfPublication).build();
|
||||
|
||||
context.restoreAuthSystemState();
|
||||
|
||||
@@ -2759,8 +2762,8 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
//Delete public item
|
||||
getClient(token).perform(delete("/api/core/items/" + publication1.getID()))
|
||||
.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();
|
||||
|
||||
|
||||
RelationshipType isAuthorOfPublication = RelationshipTypeBuilder.createRelationshipTypeBuilder(context, publication, person, "isAuthorOfPublication",
|
||||
RelationshipType isAuthorOfPublication = RelationshipTypeBuilder
|
||||
.createRelationshipTypeBuilder(context, publication, person, "isAuthorOfPublication",
|
||||
"isPublicationOfAuthor", 2, null, 0,
|
||||
null).withCopyToLeft(false).withCopyToRight(true).build();
|
||||
|
||||
Relationship relationship1 = RelationshipBuilder.createRelationshipBuilder(context, workspaceItem.getItem(), author1, isAuthorOfPublication).build();
|
||||
Relationship relationship2 = RelationshipBuilder.createRelationshipBuilder(context, workspaceItem.getItem(), author2, isAuthorOfPublication).build();
|
||||
Relationship relationship1 = RelationshipBuilder
|
||||
.createRelationshipBuilder(context, workspaceItem.getItem(), author1, isAuthorOfPublication).build();
|
||||
Relationship relationship2 = RelationshipBuilder
|
||||
.createRelationshipBuilder(context, workspaceItem.getItem(), author2, isAuthorOfPublication).build();
|
||||
|
||||
context.restoreAuthSystemState();
|
||||
|
||||
@@ -2818,6 +2824,8 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
//Delete the workspaceitem
|
||||
getClient(token).perform(delete("/api/submission/workspaceitems/" + workspaceItem.getID()))
|
||||
.andExpect(status().is(204));
|
||||
getClient(token).perform(get("/api/submission/workspaceitems/" + workspaceItem.getID()))
|
||||
.andExpect(status().is(404));
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user