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