Skip to content

Commit

Permalink
Merge pull request DSpace#9160 from DSpace/backport-9106-to-dspace-7_x
Browse files Browse the repository at this point in the history
[Port dspace-7_x] Clear primary bistream when it's deleted- Fix issue DSpace#9099
  • Loading branch information
alanorth authored Oct 31, 2023
2 parents c0f4d1e + c601ac3 commit 1ad9a09
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ public void delete(Context context, Bitstream bitstream) throws SQLException, Au
//Remove our bitstream from all our bundles
final List<Bundle> bundles = bitstream.getBundles();
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);
}

Expand Down
2 changes: 1 addition & 1 deletion dspace-api/src/main/java/org/dspace/content/Bundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void setPrimaryBitstreamID(Bitstream bitstream) {
* Unset the primary bitstream ID of the bundle
*/
public void unsetPrimaryBitstreamID() {
primaryBitstream = null;
setPrimaryBitstreamID(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
45 changes: 45 additions & 0 deletions dspace-api/src/test/java/org/dspace/content/BitstreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,51 @@ public void testDeleteAndExpunge() throws IOException, SQLException, AuthorizeEx
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.
*/
Expand Down
35 changes: 35 additions & 0 deletions dspace-api/src/test/java/org/dspace/content/BundleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,41 @@ public void testRemoveBitstreamAuth() throws SQLException, AuthorizeException, I
}


/**
* 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.
*/
Expand Down

0 comments on commit 1ad9a09

Please sign in to comment.