DS-2940: fixing metadata problems with canonical versioned handles.

This commit is contained in:
Pascal-Nicolas Becker
2015-12-08 16:55:37 +01:00
parent bff93d6686
commit 46c223d172
4 changed files with 91 additions and 72 deletions

View File

@@ -37,8 +37,6 @@ public class HandleIdentifierProvider extends IdentifierProvider {
/** Prefix registered to no one */ /** Prefix registered to no one */
protected static final String EXAMPLE_PREFIX = "123456789"; protected static final String EXAMPLE_PREFIX = "123456789";
protected String[] supportedPrefixes = new String[]{"info:hdl", "hdl", "http://"};
@Autowired(required = true) @Autowired(required = true)
protected HandleService handleService; protected HandleService handleService;
@Autowired(required = true) @Autowired(required = true)
@@ -52,22 +50,20 @@ public class HandleIdentifierProvider extends IdentifierProvider {
@Override @Override
public boolean supports(String identifier) public boolean supports(String identifier)
{ {
for(String prefix : supportedPrefixes){ String prefix = handleService.getPrefix();
if(identifier.startsWith(prefix)) String handleResolver = ConfigurationManager.getProperty("handle.canonical.prefix");
if (identifier == null)
{
return false;
}
if (identifier.startsWith(prefix)
|| identifier.startsWith(handleResolver)
|| identifier.startsWith("http://hdl.handle.net/")
|| identifier.startsWith("hdl:")
|| identifier.startsWith("info:hdl"))
{ {
return true; return true;
} }
}
try {
String outOfUrl = retrieveHandleOutOfUrl(identifier);
if(outOfUrl != null)
{
return true;
}
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
return false; return false;
} }

View File

@@ -48,8 +48,6 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
private static final char DOT = '.'; private static final char DOT = '.';
private String[] supportedPrefixes = new String[]{"info:hdl", "hdl", "http://"};
@Autowired(required = true) @Autowired(required = true)
private VersioningService versionService; private VersioningService versionService;
@@ -71,23 +69,20 @@ public class VersionedHandleIdentifierProvider extends IdentifierProvider {
@Override @Override
public boolean supports(String identifier) public boolean supports(String identifier)
{ {
for(String prefix : supportedPrefixes) String prefix = handleService.getPrefix();
String handleResolver = ConfigurationManager.getProperty("handle.canonical.prefix");
if (identifier == null)
{ {
if(identifier.startsWith(prefix)) return false;
}
if (identifier.startsWith(prefix)
|| identifier.startsWith(handleResolver)
|| identifier.startsWith("http://hdl.handle.net/")
|| identifier.startsWith("hdl:")
|| identifier.startsWith("info:hdl"))
{ {
return true; return true;
} }
}
try {
String outOfUrl = retrieveHandleOutOfUrl(identifier);
if(outOfUrl != null)
{
return true;
}
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
return false; return false;
} }

View File

@@ -26,6 +26,7 @@ import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils;
/** /**
* *
@@ -44,8 +45,6 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
private static final char DOT = '.'; private static final char DOT = '.';
private String[] supportedPrefixes = new String[]{"info:hdl", "hdl", "http://"};
@Autowired(required = true) @Autowired(required = true)
private VersioningService versionService; private VersioningService versionService;
@@ -67,23 +66,20 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
@Override @Override
public boolean supports(String identifier) public boolean supports(String identifier)
{ {
for(String prefix : supportedPrefixes) String prefix = handleService.getPrefix();
String handleResolver = ConfigurationManager.getProperty("handle.canonical.prefix");
if (identifier == null)
{ {
if(identifier.startsWith(prefix)) return false;
}
if (identifier.startsWith(prefix)
|| identifier.startsWith(handleResolver)
|| identifier.startsWith("http://hdl.handle.net/")
|| identifier.startsWith("hdl:")
|| identifier.startsWith("info:hdl"))
{ {
return true; return true;
} }
}
try {
String outOfUrl = retrieveHandleOutOfUrl(identifier);
if(outOfUrl != null)
{
return true;
}
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
return false; return false;
} }
@@ -106,18 +102,34 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
// Modify Canonical: 12345/100 will point to the new item // Modify Canonical: 12345/100 will point to the new item
handleService.modifyHandleDSpaceObject(context, canonical, item); handleService.modifyHandleDSpaceObject(context, canonical, item);
// in case of first version we have to modify the previous metadata to be xxxx.1 // we have to replace the canonical handle within the
// metadata of the previous item
Version version = versionService.getVersion(context, item); Version version = versionService.getVersion(context, item);
Version previous; Version previous;
boolean isFirstVersion; String previousItemHandle = null;
try { try {
previous = versionHistoryService.getPrevious(context, history, version); previous = versionHistoryService.getPrevious(context, history, version);
isFirstVersion = versionHistoryService.isFirstVersion(context, history, previous); if (previous != null)
{
previousItemHandle = handleService.findHandle(context, previous.getItem());
}
} catch (SQLException ex) { } catch (SQLException ex) {
throw new RuntimeException("A problem with the database connection occured.", ex); throw new RuntimeException("A problem with the database connection occured.", ex);
} }
if (isFirstVersion) if (previous != null)
{ {
// Check if our previous item doesn't have a handle anymore.
// This occurs only when a switch has been made from
// the standard handle identifier provider to the
// versioned one, in this case no "versioned handle" is
// reserved so we need to create one
if (previousItemHandle == null)
{
previousItemHandle = makeIdentifierBasedOnHistory(context, previous.getItem(), canonical, history);
}
// remove the canonical handle from the metadata of the
// previous item an add the versioned handle
try{ try{
//If we have a reviewer he/she might not have the rights to edit the metadata of this item, so temporarly grant them. //If we have a reviewer he/she might not have the rights to edit the metadata of this item, so temporarly grant them.
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
@@ -125,14 +137,6 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
}finally { }finally {
context.restoreAuthSystemState(); context.restoreAuthSystemState();
} }
}
// Check if our previous item hasn't got a handle anymore.
// This only occurs when a switch has been made from the standard handle identifier provider
// to the versioned one, in this case no "versioned handle" is reserved so we need to create one
if(previous != null && handleService.findHandle(context, previous.getItem()) == null){
makeIdentifierBasedOnHistory(context, previous.getItem(), canonical, history);
} }
} }
populateHandleMetadata(context, item); populateHandleMetadata(context, item);
@@ -351,7 +355,7 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
} }
public static String retrieveHandleOutOfUrl(String url) throws SQLException public static String retrieveHandleOutOfUrl(String url)
{ {
// We can do nothing with this, return null // We can do nothing with this, return null
if (!url.contains("/")) return null; if (!url.contains("/")) return null;
@@ -489,9 +493,33 @@ public class VersionedHandleIdentifierProviderWithCanonicalHandles extends Ident
protected void modifyHandleMetadata(Context context, Item item, String handle) protected void modifyHandleMetadata(Context context, Item item, String handle)
throws SQLException, IOException, AuthorizeException throws SQLException, IOException, 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 = getCanonicalForm(handle); String handleref = getCanonicalForm(handle);
List<MetadataValue> identifiers = itemService.getMetadata(item, MetadataSchema.DC_SCHEMA, "identifier", "uri", Item.ANY);
itemService.clearMetadata(context, item, MetadataSchema.DC_SCHEMA, "identifier", "uri", Item.ANY); itemService.clearMetadata(context, item, MetadataSchema.DC_SCHEMA, "identifier", "uri", Item.ANY);
for (MetadataValue identifier : identifiers)
{
if (this.supports(identifier.getValue()))
{
// ignore handles
log.warn("Removing identifier " + identifier.getValue());
continue;
}
log.warn("Preserving identifier " + identifier.getValue());
itemService.addMetadata(context,
item,
identifier.getMetadataField(),
identifier.getLanguage(),
identifier.getValue(),
identifier.getAuthority(),
identifier.getConfidence());
}
if (!StringUtils.isEmpty(handleref))
{
itemService.addMetadata(context, item, MetadataSchema.DC_SCHEMA, "identifier", "uri", null, handleref); itemService.addMetadata(context, item, MetadataSchema.DC_SCHEMA, "identifier", "uri", null, handleref);
}
itemService.update(context, item); itemService.update(context, item);
} }
} }

View File

@@ -118,7 +118,7 @@ public class InstallItemTest extends AbstractUnitTest
public void testInstallItem_validHandle() throws Exception public void testInstallItem_validHandle() throws Exception
{ {
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
String handle = "1345/567"; String handle = "123456789/567";
WorkspaceItem is = workspaceItemService.create(context, collection, false); WorkspaceItem is = workspaceItemService.create(context, collection, false);
//Test assigning a specified handle to an item //Test assigning a specified handle to an item
@@ -145,7 +145,7 @@ public class InstallItemTest extends AbstractUnitTest
authorizeService.isAdmin((Context) any); result = true; authorizeService.isAdmin((Context) any); result = true;
}}; }};
String handle = "1345/567"; String handle = "123456789/567";
WorkspaceItem is = workspaceItemService.create(context, collection, false); WorkspaceItem is = workspaceItemService.create(context, collection, false);
WorkspaceItem is2 = workspaceItemService.create(context, collection, false); WorkspaceItem is2 = workspaceItemService.create(context, collection, false);
@@ -166,7 +166,7 @@ public class InstallItemTest extends AbstractUnitTest
public void testRestoreItem() throws Exception public void testRestoreItem() throws Exception
{ {
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
String handle = "1345/567"; String handle = "123456789/567";
WorkspaceItem is = workspaceItemService.create(context, collection, false); WorkspaceItem is = workspaceItemService.create(context, collection, false);
//get current date //get current date
@@ -236,7 +236,7 @@ public class InstallItemTest extends AbstractUnitTest
{ {
//create a dummy WorkspaceItem //create a dummy WorkspaceItem
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
String handle = "1345/567"; String handle = "123456789/567";
WorkspaceItem is = workspaceItemService.create(context, collection, false); WorkspaceItem is = workspaceItemService.create(context, collection, false);
// Set "today" as "dc.date.issued" // Set "today" as "dc.date.issued"
@@ -267,7 +267,7 @@ public class InstallItemTest extends AbstractUnitTest
{ {
//create a dummy WorkspaceItem with no dc.date.issued //create a dummy WorkspaceItem with no dc.date.issued
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
String handle = "1345/567"; String handle = "123456789/567";
WorkspaceItem is = workspaceItemService.create(context, collection, false); WorkspaceItem is = workspaceItemService.create(context, collection, false);
Item result = installItemService.installItem(context, is, handle); Item result = installItemService.installItem(context, is, handle);
@@ -286,7 +286,7 @@ public class InstallItemTest extends AbstractUnitTest
{ {
//create a dummy WorkspaceItem //create a dummy WorkspaceItem
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
String handle = "1345/567"; String handle = "123456789/567";
WorkspaceItem is = workspaceItemService.create(context, collection, false); WorkspaceItem is = workspaceItemService.create(context, collection, false);
// Set "today" as "dc.date.issued" // Set "today" as "dc.date.issued"