[CSTPER-221] Versioned handle populate metadata cleanup

This commit is contained in:
Alessandro Martelli
2020-10-29 17:28:40 +01:00
parent e959d70cc7
commit d3c3d30c6c

View File

@@ -25,7 +25,6 @@ import org.dspace.content.MetadataSchemaEnum;
import org.dspace.content.MetadataValue; import org.dspace.content.MetadataValue;
import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.DSpaceObjectService; import org.dspace.content.service.DSpaceObjectService;
import org.dspace.content.service.ItemService;
import org.dspace.core.ConfigurationManager; import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants; import org.dspace.core.Constants;
import org.dspace.core.Context; import org.dspace.core.Context;
@@ -68,9 +67,6 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
@Autowired(required = true) @Autowired(required = true)
private HandleService handleService; private HandleService handleService;
@Autowired(required = true)
private ItemService itemService;
@Autowired(required = true) @Autowired(required = true)
protected ContentServiceFactory contentServiceFactory; protected ContentServiceFactory contentServiceFactory;
@@ -114,11 +110,8 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
public String register(Context context, DSpaceObject dso) { public String register(Context context, DSpaceObject dso) {
String id = mint(context, dso); String id = mint(context, dso);
try { try {
if (dso instanceof Item) { if (dso instanceof Item || dso instanceof Collection || dso instanceof Community) {
populateHandleMetadata(context, (Item) dso, id); populateHandleMetadata(context, dso, id);
}
if (dso instanceof Collection || dso instanceof Community) {
populateNotVersionedHandleMetadata(context, dso, id);
} }
} catch (Exception e) { } catch (Exception e) {
log.error(LogManager.getHeader(context, "Error while attempting to create handle", log.error(LogManager.getHeader(context, "Error while attempting to create handle",
@@ -132,7 +125,6 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
@Override @Override
public void register(Context context, DSpaceObject dso, String identifier) public void register(Context context, DSpaceObject dso, String identifier)
throws IdentifierException { throws IdentifierException {
// FIXME are Collections and Communities to be handled here?
if (dso instanceof Item && identifier != null) { if (dso instanceof Item && identifier != null) {
Item item = (Item) dso; Item item = (Item) dso;
@@ -421,16 +413,17 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
return identifier; return identifier;
} }
protected void populateHandleMetadata(Context context, Item item, String handle) protected void populateHandleMetadata(Context context, DSpaceObject dso, String handle)
throws SQLException, IOException, AuthorizeException { throws SQLException, IOException, AuthorizeException {
String handleref = handleService.getCanonicalForm(handle); String handleref = handleService.getCanonicalForm(handle);
// we want to remove the old handle and insert the new. To do so, we // we want to remove the old handle and insert the new. 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.
List<MetadataValue> identifiers = itemService.getMetadata(item, DSpaceObjectService<DSpaceObject> dsoService = contentServiceFactory.getDSpaceObjectService(dso);
List<MetadataValue> identifiers = dsoService.getMetadata(dso,
MetadataSchemaEnum.DC.getName(), "identifier", "uri", MetadataSchemaEnum.DC.getName(), "identifier", "uri",
Item.ANY); Item.ANY);
itemService.clearMetadata(context, item, MetadataSchemaEnum.DC.getName(), dsoService.clearMetadata(context, dso, MetadataSchemaEnum.DC.getName(),
"identifier", "uri", Item.ANY); "identifier", "uri", Item.ANY);
for (MetadataValue identifier : identifiers) { for (MetadataValue identifier : identifiers) {
if (this.supports(identifier.getValue())) { if (this.supports(identifier.getValue())) {
@@ -439,8 +432,8 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
continue; continue;
} }
log.debug("Preserving identifier " + identifier.getValue()); log.debug("Preserving identifier " + identifier.getValue());
itemService.addMetadata(context, dsoService.addMetadata(context,
item, dso,
identifier.getMetadataField(), identifier.getMetadataField(),
identifier.getLanguage(), identifier.getLanguage(),
identifier.getValue(), identifier.getValue(),
@@ -450,32 +443,9 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
// Add handle as identifier.uri DC value. // Add handle as identifier.uri DC value.
if (StringUtils.isNotBlank(handleref)) { if (StringUtils.isNotBlank(handleref)) {
itemService.addMetadata(context, item, MetadataSchemaEnum.DC.getName(),
"identifier", "uri", null, handleref);
}
itemService.update(context, item);
}
protected void populateNotVersionedHandleMetadata(Context context, DSpaceObject dso, String handle)
throws SQLException, IOException, AuthorizeException {
String handleref = handleService.getCanonicalForm(handle);
DSpaceObjectService<DSpaceObject> dsoService = contentServiceFactory.getDSpaceObjectService(dso);
// Add handle as identifier.uri DC value.
// First check that identifier doesn't already exist.
boolean identifierExists = false;
// List<MetadataValue> identifiers = dsoObjectService
List<MetadataValue> identifiers = dsoService
.getMetadata(dso, MetadataSchemaEnum.DC.getName(), "identifier", "uri", Item.ANY);
for (MetadataValue identifier : identifiers) {
if (handleref.equals(identifier.getValue())) {
identifierExists = true;
}
}
if (!identifierExists) {
dsoService.addMetadata(context, dso, MetadataSchemaEnum.DC.getName(), dsoService.addMetadata(context, dso, MetadataSchemaEnum.DC.getName(),
"identifier", "uri", null, handleref); "identifier", "uri", null, handleref);
} }
dsoService.update(context, dso);
} }
} }