Skip to content

Commit

Permalink
Merge pull request DSpace#8784 from atmire/w2p-89779_VersionedHandleI…
Browse files Browse the repository at this point in the history
…dentifierProviderWithCanonicalHandles-fix

Fix for the optional VersionedHandleIdentifierProviderWithCanonicalHandles identifier system
  • Loading branch information
tdonohue committed May 1, 2023
2 parents 9bbfb8d + 090994a commit 7cebc65
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ public String mint(Context context, DSpaceObject dso) {
public DSpaceObject resolve(Context context, String identifier, String... attributes) {
// We can do nothing with this, return null
try {
identifier = handleService.parseHandle(identifier);
return handleService.resolveToObject(context, identifier);
} catch (IllegalStateException | SQLException e) {
log.error(LogHelper.getHeader(context, "Error while resolving handle to item", "handle: " + identifier),
Expand Down Expand Up @@ -426,6 +427,19 @@ protected String makeIdentifierBasedOnHistory(Context context, DSpaceObject dso,
}
}

DSpaceObject itemWithCanonicalHandle = handleService.resolveToObject(context, canonical);
if (itemWithCanonicalHandle != null) {
if (itemWithCanonicalHandle.getID() != previous.getItem().getID()) {
log.warn("The previous version's item (" + previous.getItem().getID() +
") does not match with the item containing handle " + canonical +
" (" + itemWithCanonicalHandle.getID() + ")");
}
// Move the original handle from whatever item it's on to the newest version
handleService.modifyHandleDSpaceObject(context, canonical, dso);
} else {
handleService.createHandle(context, dso, canonical);
}

// add a new Identifier for this item: 12345/100.x
String idNew = canonical + DOT + version.getVersionNumber();
//Make sure we don't have an old handle hanging around (if our previous version was deleted in the workspace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


<!-- Identifier Service Application Interface. Will be autowired with
any Identifier Providers present in Spring context.
-->
<bean id="org.dspace.identifier.service.IdentifierService"
class="org.dspace.identifier.IdentifierServiceImpl"
autowire="byType"
scope="singleton"/>

<bean id="org.dspace.services.ConfigurationService"
class="org.dspace.servicemanager.config.DSpaceConfigurationService" scope="singleton"/>

Expand All @@ -31,12 +22,6 @@
<property name="configurationService" ref="org.dspace.services.ConfigurationService"/>
</bean-->

<bean id="org.dspace.identifier.VersionedHandleIdentifierProvider"
class="org.dspace.identifier.VersionedHandleIdentifierProvider"
scope="singleton">
<property name="configurationService" ref="org.dspace.services.ConfigurationService"/>
</bean>

<bean name="org.dspace.core.DBConnection" class="org.dspace.core.HibernateDBConnection" lazy-init="true" scope="prototype"/>

<!-- Register all our Flyway callback classes (which run before/after database migrations) -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@
<bean id="org.dspace.identifier.service.IdentifierService"
class="org.dspace.identifier.IdentifierServiceImpl"
autowire="byType"
scope="singleton"/>
scope="singleton">
<property name="providers">
<list>
<ref bean="org.dspace.identifier.HandleIdentifierProvider"/>
<ref bean="org.dspace.identifier.DOIIdentifierProvider"/>
</list>
</property>
</bean>

<bean id="org.dspace.identifier.HandleIdentifierProvider" class="org.dspace.identifier.VersionedHandleIdentifierProvider" scope="singleton">
<property name="configurationService" ref="org.dspace.services.ConfigurationService"/>
</bean>

<!-- provider to mint and register DOIs with DSpace.
To mint DOIs you need a registration agency. The DOIIdentifierProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* 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/
*/
package org.dspace.identifier;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.authorize.AuthorizeException;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.ItemBuilder;
import org.dspace.builder.VersionBuilder;
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.kernel.ServiceManager;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.Before;
import org.junit.Test;

public class VersionedHandleIdentifierProviderTest extends AbstractIntegrationTestWithDatabase {
private ServiceManager serviceManager;
private IdentifierServiceImpl identifierService;

private String firstHandle;

private Collection collection;
private Item itemV1;
private Item itemV2;
private Item itemV3;

@Before
@Override
public void setUp() throws Exception {
super.setUp();
context.turnOffAuthorisationSystem();

serviceManager = DSpaceServicesFactory.getInstance().getServiceManager();
identifierService = serviceManager.getServicesByType(IdentifierServiceImpl.class).get(0);
// Clean out providers to avoid any being used for creation of community and collection
identifierService.setProviders(new ArrayList<>());

parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
collection = CollectionBuilder.createCollection(context, parentCommunity)
.withName("Collection")
.build();
}

private void registerProvider(Class type) {
// Register our new provider
serviceManager.registerServiceClass(type.getName(), type);
IdentifierProvider identifierProvider =
(IdentifierProvider) serviceManager.getServiceByName(type.getName(), type);

// Overwrite the identifier-service's providers with the new one to ensure only this provider is used
identifierService.setProviders(List.of(identifierProvider));
}

private void createVersions() throws SQLException, AuthorizeException {
itemV1 = ItemBuilder.createItem(context, collection)
.withTitle("First version")
.build();
firstHandle = itemV1.getHandle();
itemV2 = VersionBuilder.createVersion(context, itemV1, "Second version").build().getItem();
itemV3 = VersionBuilder.createVersion(context, itemV1, "Third version").build().getItem();
}

@Test
public void testDefaultVersionedHandleProvider() throws Exception {
registerProvider(VersionedHandleIdentifierProvider.class);
createVersions();

// Confirm the original item only has its original handle
assertEquals(firstHandle, itemV1.getHandle());
assertEquals(1, itemV1.getHandles().size());
// Confirm the second item has the correct version handle
assertEquals(firstHandle + ".2", itemV2.getHandle());
assertEquals(1, itemV2.getHandles().size());
// Confirm the last item has the correct version handle
assertEquals(firstHandle + ".3", itemV3.getHandle());
assertEquals(1, itemV3.getHandles().size());
}

@Test
public void testCanonicalVersionedHandleProvider() throws Exception {
registerProvider(VersionedHandleIdentifierProviderWithCanonicalHandles.class);
createVersions();

// Confirm the original item only has a version handle
assertEquals(firstHandle + ".1", itemV1.getHandle());
assertEquals(1, itemV1.getHandles().size());
// Confirm the second item has the correct version handle
assertEquals(firstHandle + ".2", itemV2.getHandle());
assertEquals(1, itemV2.getHandles().size());
// Confirm the last item has both the correct version handle and the original handle
assertEquals(firstHandle, itemV3.getHandle());
assertEquals(2, itemV3.getHandles().size());
containsHandle(itemV3, firstHandle + ".3");
}

private void containsHandle(Item item, String handle) {
assertTrue(item.getHandles().stream().anyMatch(h -> handle.equals(h.getHandle())));
}
}
2 changes: 0 additions & 2 deletions dspace/config/spring/api/identifier-service.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
The VersionedHandleIdentifierProvider creates a new versioned
handle for every new version.
-->
<!--
<bean id="org.dspace.identifier.HandleIdentifierProvider" class="org.dspace.identifier.VersionedHandleIdentifierProvider" scope="singleton">
<property name="configurationService" ref="org.dspace.services.ConfigurationService"/>
</bean>
-->
<!--
The VersionedHandleIdentifierProviderWithCanonicalHandles
preserves the first handle for every new version. Whenever
Expand Down

0 comments on commit 7cebc65

Please sign in to comment.