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.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject; import org.dspace.content.DSpaceObject;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.content.MetadataSchemaEnum; import org.dspace.content.MetadataSchemaEnum;
import org.dspace.content.MetadataValue; 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.Constants;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.core.LogHelper; import org.dspace.core.LogHelper;
@@ -62,9 +65,6 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
@Autowired(required = true) @Autowired(required = true)
private HandleService handleService; private HandleService handleService;
@Autowired(required = true)
private ItemService itemService;
/** /**
* After all the properties are set check that the versioning is enabled * 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); 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; 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. * Remove all handles from an item's metadata and add the supplied handle instead.
* *
* @param context The relevant DSpace Context. * @param context The relevant DSpace Context.
* @param item which item to modify * @param dso which dso to modify
* @param handle which handle to add * @param handle which handle to add
* @throws SQLException if database error * @throws SQLException if database error
* @throws AuthorizeException if authorization 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 { throws SQLException, AuthorizeException {
// we want to exchange the old handle against the new one. To do so, we // 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 // load all identifiers, clear the metadata field, re add all
// identifiers which are not from type handle and add the new handle. // identifiers which are not from type handle and add the new handle.
String handleref = handleService.getCanonicalForm(handle); String handleref = handleService.getCanonicalForm(handle);
List<MetadataValue> identifiers = itemService DSpaceObjectService<DSpaceObject> dSpaceObjectService =
.getMetadata(item, MetadataSchemaEnum.DC.getName(), "identifier", "uri", Item.ANY); ContentServiceFactory.getInstance().getDSpaceObjectService(dso);
itemService.clearMetadata(context, item, MetadataSchemaEnum.DC.getName(), "identifier", "uri", Item.ANY); 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) { for (MetadataValue identifier : identifiers) {
if (this.supports(identifier.getValue())) { if (this.supports(identifier.getValue())) {
// ignore handles // ignore handles
continue; continue;
} }
itemService.addMetadata(context, dSpaceObjectService.addMetadata(context,
item, dso,
identifier.getMetadataField(), identifier.getMetadataField(),
identifier.getLanguage(), identifier.getLanguage(),
identifier.getValue(), identifier.getValue(),
@@ -517,9 +529,9 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
identifier.getConfidence()); identifier.getConfidence());
} }
if (!StringUtils.isEmpty(handleref)) { if (!StringUtils.isEmpty(handleref)) {
itemService.addMetadata(context, item, MetadataSchemaEnum.DC.getName(), dSpaceObjectService.addMetadata(context, dso, MetadataSchemaEnum.DC.getName(),
"identifier", "uri", null, handleref); "identifier", "uri", null, handleref);
} }
itemService.update(context, item); dSpaceObjectService.update(context, dso);
} }
} }

View File

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