Merge pull request #9106 from paulo-graca/bugfix/issue-9099

Clear primary bistream when it's deleted- Fix issue #9099
This commit is contained in:
Alan Orth
2023-10-31 09:54:49 +03:00
committed by GitHub
5 changed files with 120 additions and 1 deletions

View File

@@ -276,6 +276,11 @@ public class BitstreamServiceImpl extends DSpaceObjectServiceImpl<Bitstream> imp
//Remove our bitstream from all our bundles //Remove our bitstream from all our bundles
final List<Bundle> bundles = bitstream.getBundles(); final List<Bundle> bundles = bitstream.getBundles();
for (Bundle bundle : bundles) { for (Bundle bundle : bundles) {
authorizeService.authorizeAction(context, bundle, Constants.REMOVE);
//We also need to remove the bitstream id when it's set as bundle's primary bitstream
if (bitstream.equals(bundle.getPrimaryBitstream())) {
bundle.unsetPrimaryBitstreamID();
}
bundle.removeBitstream(bitstream); bundle.removeBitstream(bitstream);
} }

View File

@@ -126,7 +126,7 @@ public class Bundle extends DSpaceObject implements DSpaceObjectLegacySupport {
* Unset the primary bitstream ID of the bundle * Unset the primary bitstream ID of the bundle
*/ */
public void unsetPrimaryBitstreamID() { public void unsetPrimaryBitstreamID() {
primaryBitstream = null; setPrimaryBitstreamID(null);
} }
/** /**

View File

@@ -0,0 +1,34 @@
--
-- The contents of this file are subject to the license and copyright
-- detailed in the LICENSE and NOTICE files at the root of the source
-- tree and available online at
--
-- http://www.dspace.org/license/
--
BEGIN;
-- Unset any primary bitstream that is marked as deleted
UPDATE bundle
SET primary_bitstream_id = NULL
WHERE primary_bitstream_id IN
( SELECT bs.uuid
FROM bitstream AS bs
INNER JOIN bundle as bl ON bs.uuid = bl.primary_bitstream_id
WHERE bs.deleted IS TRUE );
-- Unset any primary bitstream that don't belong to bundle's bitstream list
UPDATE bundle
SET primary_bitstream_id = NULL
WHERE primary_bitstream_id IN
( SELECT bl.primary_bitstream_id
FROM bundle as bl
WHERE bl.primary_bitstream_id IS NOT NULL
AND bl.primary_bitstream_id NOT IN
( SELECT bitstream_id
FROM bundle2bitstream AS b2b
WHERE b2b.bundle_id = bl.uuid
)
);
COMMIT;

View File

@@ -432,6 +432,51 @@ public class BitstreamTest extends AbstractDSpaceObjectTest {
assertThat("testExpunge 0", bitstreamService.find(context, bitstreamId), nullValue()); assertThat("testExpunge 0", bitstreamService.find(context, bitstreamId), nullValue());
} }
/**
* Test of delete method, of class Bitstream.
*/
@Test
public void testDeleteBitstreamAndUnsetPrimaryBitstreamID()
throws IOException, SQLException, AuthorizeException {
context.turnOffAuthorisationSystem();
Community owningCommunity = communityService.create(null, context);
Collection collection = collectionService.create(context, owningCommunity);
WorkspaceItem workspaceItem = workspaceItemService.create(context, collection, false);
Item item = installItemService.installItem(context, workspaceItem);
Bundle b = bundleService.create(context, item, "TESTBUNDLE");
// Allow Bundle REMOVE permissions
doNothing().when(authorizeServiceSpy).authorizeAction(context, b, Constants.REMOVE);
// Allow Bitstream WRITE permissions
doNothing().when(authorizeServiceSpy)
.authorizeAction(any(Context.class), any(Bitstream.class), eq(Constants.WRITE));
// Allow Bitstream DELETE permissions
doNothing().when(authorizeServiceSpy)
.authorizeAction(any(Context.class), any(Bitstream.class), eq(Constants.DELETE));
//set a value different than default
File f = new File(testProps.get("test.bitstream").toString());
// Create a new bitstream, which we can delete.
Bitstream delBS = bitstreamService.create(context, new FileInputStream(f));
bundleService.addBitstream(context, b, delBS);
// set primary bitstream
b.setPrimaryBitstreamID(delBS);
context.restoreAuthSystemState();
// Test that delete will flag the bitstream as deleted
assertFalse("testDeleteBitstreamAndUnsetPrimaryBitstreamID 0", delBS.isDeleted());
assertThat("testDeleteBitstreamAndUnsetPrimaryBitstreamID 1", b.getPrimaryBitstream(), equalTo(delBS));
// Delete bitstream
bitstreamService.delete(context, delBS);
assertTrue("testDeleteBitstreamAndUnsetPrimaryBitstreamID 2", delBS.isDeleted());
// Now test if the primary bitstream was unset from bundle
assertThat("testDeleteBitstreamAndUnsetPrimaryBitstreamID 3", b.getPrimaryBitstream(), equalTo(null));
}
/** /**
* Test of retrieve method, of class Bitstream. * Test of retrieve method, of class Bitstream.
*/ */

View File

@@ -513,6 +513,41 @@ public class BundleTest extends AbstractDSpaceObjectTest {
} }
/**
* Test removeBitstream method and also the unsetPrimaryBitstreamID method, of class Bundle.
*/
@Test
public void testRemoveBitstreamAuthAndUnsetPrimaryBitstreamID()
throws IOException, SQLException, AuthorizeException {
// Allow Item WRITE permissions
doNothing().when(authorizeServiceSpy).authorizeAction(context, item, Constants.WRITE);
// Allow Bundle ADD permissions
doNothing().when(authorizeServiceSpy).authorizeAction(context, b, Constants.ADD);
// Allow Bundle REMOVE permissions
doNothing().when(authorizeServiceSpy).authorizeAction(context, b, Constants.REMOVE);
// Allow Bitstream WRITE permissions
doNothing().when(authorizeServiceSpy)
.authorizeAction(any(Context.class), any(Bitstream.class), eq(Constants.WRITE));
// Allow Bitstream DELETE permissions
doNothing().when(authorizeServiceSpy)
.authorizeAction(any(Context.class), any(Bitstream.class), eq(Constants.DELETE));
context.turnOffAuthorisationSystem();
//set a value different than default
File f = new File(testProps.get("test.bitstream").toString());
Bitstream bs = bitstreamService.create(context, new FileInputStream(f));
bundleService.addBitstream(context, b, bs);
b.setPrimaryBitstreamID(bs);
context.restoreAuthSystemState();
assertThat("testRemoveBitstreamAuthAndUnsetPrimaryBitstreamID 0", b.getPrimaryBitstream(), equalTo(bs));
//remove bitstream
bundleService.removeBitstream(context, b, bs);
//is -1 when not set
assertThat("testRemoveBitstreamAuthAndUnsetPrimaryBitstreamID 1", b.getPrimaryBitstream(), equalTo(null));
}
/** /**
* Test of update method, of class Bundle. * Test of update method, of class Bundle.
*/ */