124362: Fix issue with the VersionedHandleIdentifierProviderWithCanonicalHandles and creating communities / collections

This commit is contained in:
Yana De Pauw
2025-01-16 12:01:13 +01:00
parent 636a6ab78c
commit 6bbee7f27b
2 changed files with 29 additions and 18 deletions

View File

@@ -15,11 +15,14 @@ import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.MetadataSchemaEnum;
import org.dspace.content.MetadataValue;
import org.dspace.content.service.ItemService;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.DSpaceObjectService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.LogHelper;
@@ -62,9 +65,6 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
@Autowired(required = true)
private HandleService handleService;
@Autowired(required = true)
private ItemService itemService;
/**
* After all the properties are set check that the versioning is enabled
*
@@ -171,6 +171,16 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
throw new RuntimeException("The current user is not authorized to change this item.", ex);
}
}
if (dso instanceof Collection || dso instanceof Community) {
try {
// Update the metadata with the handle for collections and communities.
modifyHandleMetadata(context, dso, getCanonical(id));
} catch (SQLException ex) {
throw new RuntimeException("A problem with the database connection occured.", ex);
} catch (AuthorizeException ex) {
throw new RuntimeException("The current user is not authorized to change this item.", ex);
}
}
return id;
}
@@ -489,27 +499,29 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
* Remove all handles from an item's metadata and add the supplied handle instead.
*
* @param context The relevant DSpace Context.
* @param item which item to modify
* @param dso which dso to modify
* @param handle which handle to add
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
protected void modifyHandleMetadata(Context context, Item item, String handle)
protected void modifyHandleMetadata(Context context, DSpaceObject dso, String handle)
throws SQLException, AuthorizeException {
// we want to exchange the old handle against the new one. To do so, we
// load all identifiers, clear the metadata field, re add all
// identifiers which are not from type handle and add the new handle.
String handleref = handleService.getCanonicalForm(handle);
List<MetadataValue> identifiers = itemService
.getMetadata(item, MetadataSchemaEnum.DC.getName(), "identifier", "uri", Item.ANY);
itemService.clearMetadata(context, item, MetadataSchemaEnum.DC.getName(), "identifier", "uri", Item.ANY);
DSpaceObjectService<DSpaceObject> dSpaceObjectService =
ContentServiceFactory.getInstance().getDSpaceObjectService(dso);
List<MetadataValue> identifiers = dSpaceObjectService
.getMetadata(dso, MetadataSchemaEnum.DC.getName(), "identifier", "uri", Item.ANY);
dSpaceObjectService.clearMetadata(context, dso, MetadataSchemaEnum.DC.getName(), "identifier", "uri", Item.ANY);
for (MetadataValue identifier : identifiers) {
if (this.supports(identifier.getValue())) {
// ignore handles
continue;
}
itemService.addMetadata(context,
item,
dSpaceObjectService.addMetadata(context,
dso,
identifier.getMetadataField(),
identifier.getLanguage(),
identifier.getValue(),
@@ -517,9 +529,9 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
identifier.getConfidence());
}
if (!StringUtils.isEmpty(handleref)) {
itemService.addMetadata(context, item, MetadataSchemaEnum.DC.getName(),
dSpaceObjectService.addMetadata(context, dso, MetadataSchemaEnum.DC.getName(),
"identifier", "uri", null, handleref);
}
itemService.update(context, item);
dSpaceObjectService.update(context, dso);
}
}

View File

@@ -24,9 +24,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>
<!-- <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
@@ -35,11 +35,10 @@
newest version, but there is no permanent handle, that
will always keep pointing to the actual newest one.
-->
<!--
<bean id="org.dspace.identifier.HandleIdentifierProvider" class="org.dspace.identifier.VersionedHandleIdentifierProviderWithCanonicalHandles" scope="singleton">
<property name="configurationService" ref="org.dspace.services.ConfigurationService"/>
</bean>
-->
<!-- DOIIdentifierProvider mints and registers DOIs with DSpace.
The DOIIdentifierProvider maintains the doi database table and handling