From c6b2c4033da9a540001914355ebe539d9c1e8afe Mon Sep 17 00:00:00 2001 From: Thomas Scheffler Date: Wed, 19 Jun 2024 13:44:59 +0200 Subject: [PATCH] DBT-373 use objectID for DNBURN --- pom.xml | 4 ++ .../dbt/pi/DBTMapObjectIDURNGenerator.java | 39 +++++++++++++++ .../resources/config/dbt/mycore.properties | 17 +++---- .../pi/DBTMapObjectIDURNGeneratorTest.java | 49 +++++++++++++++++++ 4 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 src/main/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGenerator.java create mode 100644 src/test/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGeneratorTest.java diff --git a/pom.xml b/pom.xml index 27826ba6..f0078768 100644 --- a/pom.xml +++ b/pom.xml @@ -568,6 +568,10 @@ + + org.mycore + mycore-pi + org.mycore mycore-solr diff --git a/src/main/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGenerator.java b/src/main/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGenerator.java new file mode 100644 index 00000000..5cb6f1ed --- /dev/null +++ b/src/main/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGenerator.java @@ -0,0 +1,39 @@ +package de.urmel_dl.dbt.pi; + +import java.util.Optional; + +import org.mycore.datamodel.metadata.MCRBase; +import org.mycore.datamodel.metadata.MCRObjectID; +import org.mycore.pi.MCRPIGenerator; +import org.mycore.pi.exceptions.MCRPersistentIdentifierException; +import org.mycore.pi.urn.MCRDNBURN; +import org.mycore.pi.urn.MCRDNBURNParser; + +public class DBTMapObjectIDURNGenerator extends MCRPIGenerator { + //last char will be replaced by checksum + private static final String CHECKSUM_PART = "-$"; + private static final MCRDNBURNParser DNBURN_PARSER = new MCRDNBURNParser(); + private static final String URN_NBN_DE = "urn:nbn:de:"; + + public MCRDNBURN generate(MCRBase mcrObj, String additional) throws MCRPersistentIdentifierException { + return buildURN(mcrObj.getId(), additional); + } + + public String getNamespace(String prefix) { + if (prefix.startsWith(URN_NBN_DE)) { + return prefix; + } + return "urn:nbn:de:" + prefix; + } + + protected MCRDNBURN buildURN(MCRObjectID mcrObjectID, String s) + throws MCRPersistentIdentifierException { + return Optional.ofNullable(getProperties().get("Prefix." + mcrObjectID.getBase())) + .map(this::getNamespace) + .map(prefix -> prefix + mcrObjectID.getNumberAsInteger() + CHECKSUM_PART) + .flatMap(DNBURN_PARSER::parse).map(MCRDNBURN.class::cast) + .orElseThrow(() -> new MCRPersistentIdentifierException("Prefix." + mcrObjectID.getBase() + + " is not defined in " + getGeneratorID() + ".")); + } + +} diff --git a/src/main/resources/config/dbt/mycore.properties b/src/main/resources/config/dbt/mycore.properties index 23dcc8a9..845edb49 100644 --- a/src/main/resources/config/dbt/mycore.properties +++ b/src/main/resources/config/dbt/mycore.properties @@ -25,10 +25,11 @@ MIR.EditorForms.CustomIncludes=webapp:editor/editor-dbt.xed # URN/DOI configuration ###################################################################### -MCR.PI.Generator.TypeYearCountURN.Namespace=urn:nbn:de:gbv:27-dbt- -MCR.PI.Generator.TypeYearCountURN.GeneralPattern=%MCR.PI.Generator.TypeYearCountURN.Namespace%$ObjectDate-$Count -MCR.PI.Generator.TypeYearCountURN.DateFormat=yyyyMMdd-HHmmss -MCR.PI.Generator.TypeYearCountURN.CountPrecision=2 +MCR.PI.Service.DNBURN.Generator=MapObjectIDURN +MCR.PI.Generator.MapObjectIDURN=de.urmel_dl.dbt.pi.DBTMapObjectIDURNGenerator +MCR.PI.Generator.MapObjectIDURN.Prefix.dbt_mods=%MCR.PI.Generator.MapObjectIDURN.Namespace%dbt- +MCR.PI.Generator.MapObjectIDURN.Namespace=urn:nbn:de:gbv:27- +MCR.PI.Generator.MapObjectIDURN.Type=dnbUrn MIR.registerDOI=true MCR.DOI.Prefix=10.22032 @@ -71,11 +72,9 @@ MCR.PI.Service.Uni-Weimar-Datacite.HostingInstitution=Bauhaus-Universit\u00E4t W MCR.PI.Service.Uni-Weimar-Datacite.Namespace=http://datacite.org/schema/kernel-4 MCR.PI.Service.Uni-Weimar-Datacite.Schema=xsd/datacite/v4.3/metadata.xsd -MCR.PI.Generator.Uni-Weimar-URN=org.mycore.pi.MCRGenericPIGenerator -MCR.PI.Generator.Uni-Weimar-URN.CountPrecision=2 -MCR.PI.Generator.Uni-Weimar-URN.DateFormat=yyyyMMdd-HHmmss -MCR.PI.Generator.Uni-Weimar-URN.GeneralPattern=%MCR.PI.Generator.Uni-Weimar-URN.Namespace%$ObjectDate-$Count -MCR.PI.Generator.Uni-Weimar-URN.Namespace=%MCR.PI.MetadataService.Uni-Weimar-MODSURN.Prefix%dbt- +MCR.PI.Generator.Uni-Weimar-URN=de.urmel_dl.dbt.pi.DBTMapObjectIDURNGenerator +MCR.PI.Generator.Uni-Weimar-URN.Prefix.dbt_mods=%MCR.PI.Generator.Uni-Weimar-URN.Namespace%dbt- +MCR.PI.Generator.Uni-Weimar-URN.Namespace=%MCR.PI.MetadataService.Uni-Weimar-MODSURN.Prefix% MCR.PI.Generator.Uni-Weimar-URN.Type=dnbUrn MCR.PI.Service.Uni-Weimar-DNBURN=org.mycore.pi.urn.MCRURNOAIService diff --git a/src/test/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGeneratorTest.java b/src/test/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGeneratorTest.java new file mode 100644 index 00000000..c932542c --- /dev/null +++ b/src/test/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGeneratorTest.java @@ -0,0 +1,49 @@ +package de.urmel_dl.dbt.pi; + +import java.util.Map; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mycore.common.MCRTestCase; +import org.mycore.common.config.MCRConfiguration2; +import org.mycore.datamodel.metadata.MCRObjectID; +import org.mycore.pi.exceptions.MCRPersistentIdentifierException; +import org.mycore.pi.urn.MCRDNBURN; + +public class DBTMapObjectIDURNGeneratorTest extends MCRTestCase { + + private static final String CONFIG_PREFIX = "MCR.PI.Generator.Test"; + DBTMapObjectIDURNGenerator generator; + + @Override + protected Map getTestProperties() { + Map testProperties = super.getTestProperties(); + testProperties.put(CONFIG_PREFIX, ""); + testProperties.put(CONFIG_PREFIX+".Prefix.dbt_mods", "urn:nbn:de:test-dbt-"); + return testProperties; + } + + @Before + public void setUp() throws Exception { + super.setUp(); + generator = new DBTMapObjectIDURNGenerator(); + generator.init(CONFIG_PREFIX); + Map subPropertiesMap = MCRConfiguration2.getSubPropertiesMap(CONFIG_PREFIX+"."); + System.out.println(subPropertiesMap); + generator.setProperties(subPropertiesMap); + } + + @Test + public void getNamespace() { + Assert.assertEquals("urn:nbn:de:test",generator.getNamespace("test")); + Assert.assertEquals("urn:nbn:de:test",generator.getNamespace("urn:nbn:de:test")); + } + + @Test + public void buildURN() throws MCRPersistentIdentifierException { + MCRObjectID mcrObjectID=MCRObjectID.getInstance("dbt_mods_4711"); + MCRDNBURN urn = generator.buildURN(mcrObjectID, ""); + Assert.assertEquals("urn:nbn:de:test-dbt-4711-2",urn.asString()); + } +}