-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MCRDNBURN> { | ||
//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() + ".")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/test/java/de/urmel_dl/dbt/pi/DBTMapObjectIDURNGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> getTestProperties() { | ||
Map<String, String> 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<String, String> 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()); | ||
} | ||
} |