[CSTPER-221] Handle Providers enhanced for Communities and Collections

This commit is contained in:
Alessandro Martelli
2020-10-27 16:39:26 +01:00
parent 36e6a44000
commit b27d2ba322
4 changed files with 70 additions and 20 deletions

View File

@@ -55,6 +55,8 @@ import org.dspace.eperson.service.SubscribeService;
import org.dspace.event.Event;
import org.dspace.harvest.HarvestedCollection;
import org.dspace.harvest.service.HarvestedCollectionService;
import org.dspace.identifier.IdentifierException;
import org.dspace.identifier.service.IdentifierService;
import org.dspace.workflow.factory.WorkflowServiceFactory;
import org.dspace.xmlworkflow.WorkflowConfigurationException;
import org.dspace.xmlworkflow.factory.XmlWorkflowFactory;
@@ -92,6 +94,8 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
protected CommunityService communityService;
@Autowired(required = true)
protected GroupService groupService;
@Autowired(required = true)
protected IdentifierService identifierService;
@Autowired(required = true)
protected LicenseService licenseService;
@@ -131,11 +135,15 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
//Add our newly created collection to our community, authorization checks occur in THIS method
communityService.addCollection(context, community, newCollection);
//Update our community so we have a collection identifier
//Update our collection so we have a collection identifier
try {
if (handle == null) {
handleService.createHandle(context, newCollection);
identifierService.register(context, newCollection);
} else {
handleService.createHandle(context, newCollection, handle);
identifierService.register(context, newCollection, handle);
}
} catch (IdentifierException e) {
throw new RuntimeException("Can't create an Identifier!");
}
// create the default authorization policy for collections

View File

@@ -37,6 +37,8 @@ import org.dspace.core.LogManager;
import org.dspace.eperson.Group;
import org.dspace.eperson.service.GroupService;
import org.dspace.event.Event;
import org.dspace.identifier.IdentifierException;
import org.dspace.identifier.service.IdentifierService;
import org.springframework.beans.factory.annotation.Autowired;
/**
@@ -69,6 +71,8 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
protected BitstreamService bitstreamService;
@Autowired(required = true)
protected SiteService siteService;
@Autowired(required = true)
protected IdentifierService identifierService;
protected CommunityServiceImpl() {
super();
@@ -92,13 +96,12 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
try {
if (handle == null) {
handleService.createHandle(context, newCommunity);
identifierService.register(context, newCommunity);
} else {
handleService.createHandle(context, newCommunity, handle);
identifierService.register(context, newCommunity, handle);
}
} catch (IllegalStateException ie) {
//If an IllegalStateException is thrown, then an existing object is already using this handle
throw ie;
} catch (IdentifierException e) {
throw new RuntimeException("Can't create an Identifier!");
}
if (parent != null) {

View File

@@ -13,11 +13,14 @@ import java.util.List;
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.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.core.LogManager;
@@ -48,7 +51,7 @@ public class HandleIdentifierProvider extends IdentifierProvider {
@Autowired(required = true)
protected HandleService handleService;
@Autowired(required = true)
protected ItemService itemService;
protected ContentServiceFactory contentServiceFactory;
@Override
public boolean supports(Class<? extends Identifier> identifier) {
@@ -91,7 +94,7 @@ public class HandleIdentifierProvider extends IdentifierProvider {
String id = mint(context, dso);
// move canonical to point the latest version
if (dso instanceof Item) {
if (dso instanceof Item || dso instanceof Collection || dso instanceof Community) {
Item item = (Item) dso;
populateHandleMetadata(context, item, id);
}
@@ -108,7 +111,7 @@ public class HandleIdentifierProvider extends IdentifierProvider {
public void register(Context context, DSpaceObject dso, String identifier) {
try {
handleService.createHandle(context, dso, identifier);
if (dso instanceof Item) {
if (dso instanceof Item || dso instanceof Collection || dso instanceof Community) {
Item item = (Item) dso;
populateHandleMetadata(context, item, identifier);
}
@@ -220,22 +223,24 @@ public class HandleIdentifierProvider extends IdentifierProvider {
return prefix;
}
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);
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 = itemService
.getMetadata(item, MetadataSchemaEnum.DC.getName(), "identifier", "uri", Item.ANY);
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) {
itemService.addMetadata(context, item, MetadataSchemaEnum.DC.getName(),
dsoService.addMetadata(context, dso, MetadataSchemaEnum.DC.getName(),
"identifier", "uri", null, handleref);
}
}

View File

@@ -17,10 +17,14 @@ import java.util.regex.Pattern;
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.factory.ContentServiceFactory;
import org.dspace.content.service.DSpaceObjectService;
import org.dspace.content.service.ItemService;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
@@ -67,6 +71,9 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
@Autowired(required = true)
private ItemService itemService;
@Autowired(required = true)
protected ContentServiceFactory contentServiceFactory;
@Override
public boolean supports(Class<? extends Identifier> identifier) {
return Handle.class.isAssignableFrom(identifier);
@@ -110,6 +117,9 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
if (dso instanceof Item) {
populateHandleMetadata(context, (Item) dso, id);
}
if (dso instanceof Collection || dso instanceof Community) {
populateNotVersionedHandleMetadata(context, dso, id);
}
} catch (Exception e) {
log.error(LogManager.getHeader(context, "Error while attempting to create handle",
"Item id: " + (dso != null ? dso.getID() : "")), e);
@@ -122,6 +132,7 @@ 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;
@@ -444,4 +455,27 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
}
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);
}
}
}