-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from DiSSCo/feature/add-identifier-meta
Feature/add identifier meta
- Loading branch information
Showing
7 changed files
with
206 additions
and
23 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
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
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
95 changes: 95 additions & 0 deletions
95
src/main/java/eu/dissco/core/translator/terms/utils/IdentifierUtils.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,95 @@ | ||
package eu.dissco.core.translator.terms.utils; | ||
|
||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.*; | ||
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE; | ||
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE; | ||
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT; | ||
import static java.util.regex.Pattern.compile; | ||
|
||
import eu.dissco.core.translator.schema.Identifier; | ||
import eu.dissco.core.translator.schema.Identifier.DctermsType; | ||
import eu.dissco.core.translator.schema.Identifier.OdsGupriLevel; | ||
import eu.dissco.core.translator.schema.Identifier.OdsIdentifierStatus; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.tuple.Triple; | ||
|
||
@Slf4j | ||
public class IdentifierUtils { | ||
|
||
private static final Map<List<Pattern>, Triple<DctermsType, String, OdsGupriLevel>> PATTERN_MAP = patternMap(); | ||
|
||
private IdentifierUtils() { | ||
// This is a Utility class | ||
} | ||
|
||
private static Map<List<Pattern>, Triple<DctermsType, String, OdsGupriLevel>> patternMap() { | ||
var linkedMap = new LinkedHashMap<List<Pattern>, Triple<DctermsType, String, OdsGupriLevel>>(); | ||
linkedMap.put(List.of(compile("^https?://doi.org")), | ||
Triple.of(DOI, "DOI", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT)); | ||
linkedMap.put(List.of(compile("^https?://hdl.handle.net")), | ||
Triple.of(HANDLE, "Handle", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT)); | ||
linkedMap.put(List.of(compile("^https?://www.wikidata.org")), | ||
Triple.of(URL, "Wikidata", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE)); | ||
linkedMap.put(List.of(compile("^https?://orcid.org")), | ||
Triple.of(URL, "ORCID", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE)); | ||
linkedMap.put(List.of(compile("^https?://\\w+.\\w+/ark:/\\w+/\\w+")), | ||
Triple.of(ARK, "ARK", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE)); | ||
linkedMap.put(List.of(compile("https?://purl.org")), | ||
Triple.of(PURL, "PURL", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE)); | ||
linkedMap.put(List.of(compile("^https?")), | ||
Triple.of(URL, "URL", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE)); | ||
linkedMap.put(List.of(compile( | ||
"(uuid:)*[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}")), | ||
Triple.of(UUID, "UUID", GLOBALLY_UNIQUE_STABLE)); | ||
return linkedMap; | ||
} | ||
|
||
public static Identifier addIdentifier(String identifierString) { | ||
return addIdentifier(identifierString, null, null); | ||
} | ||
|
||
public static Identifier addIdentifier(String identifierString, String identifierName) { | ||
return addIdentifier(identifierString, identifierName, null); | ||
} | ||
|
||
public static Identifier addIdentifier(String identifierString, String identifierName, | ||
OdsIdentifierStatus identifierStatus) { | ||
if (identifierString == null) { | ||
return null; | ||
} | ||
var identifier = new Identifier() | ||
.withId(identifierString) | ||
.withType("ods:Identifier") | ||
.withDctermsIdentifier(identifierString) | ||
.withOdsIdentifierStatus(identifierStatus); | ||
for (var entry : PATTERN_MAP.entrySet()) { | ||
for (var prefix : entry.getKey()) { | ||
if (prefix.matcher(identifierString).find()) { | ||
identifier.setDctermsType(entry.getValue().getLeft()); | ||
identifier.setDctermsTitle(getDcTermsTitle(identifierName, entry.getValue().getMiddle())); | ||
identifier.setOdsGupriLevel(entry.getValue().getRight()); | ||
return identifier; | ||
} | ||
} | ||
} | ||
log.debug( | ||
"Unable to recognise the type of identifier: {}. Assuming locally unique identifier", | ||
identifierString); | ||
identifier.setDctermsType(DctermsType.LOCALLY_UNIQUE_IDENTIFIER); | ||
identifier.setDctermsTitle(identifierName); | ||
identifier.setOdsGupriLevel(OdsGupriLevel.LOCALLY_UNIQUE_STABLE); | ||
return identifier; | ||
} | ||
|
||
private static String getDcTermsTitle(String identifierName, String defaultValue) { | ||
if (identifierName != null) { | ||
return identifierName; | ||
} else { | ||
return defaultValue; | ||
} | ||
} | ||
} |
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
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
84 changes: 84 additions & 0 deletions
84
src/test/java/eu/dissco/core/translator/terms/utils/IdentifierUtilsTest.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,84 @@ | ||
package eu.dissco.core.translator.terms.utils; | ||
|
||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.ARK; | ||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.HANDLE; | ||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.LOCALLY_UNIQUE_IDENTIFIER; | ||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.PURL; | ||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.URL; | ||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.UUID; | ||
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE; | ||
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE; | ||
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT; | ||
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.LOCALLY_UNIQUE_STABLE; | ||
import static eu.dissco.core.translator.schema.Identifier.OdsIdentifierStatus.PREFERRED; | ||
import static eu.dissco.core.translator.terms.utils.IdentifierUtils.addIdentifier; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import eu.dissco.core.translator.schema.Identifier; | ||
import eu.dissco.core.translator.schema.Identifier.DctermsType; | ||
import eu.dissco.core.translator.schema.Identifier.OdsGupriLevel; | ||
import eu.dissco.core.translator.schema.Identifier.OdsIdentifierStatus; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class IdentifierUtilsTest { | ||
|
||
|
||
public static Stream<Arguments> identifierProvider() { | ||
return Stream.of( | ||
Arguments.of("https://www.wikidata.org/wiki/Q66581882", null, null, | ||
createIdentifier("https://www.wikidata.org/wiki/Q66581882", URL, "Wikidata", | ||
GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE, null)), | ||
Arguments.of("https://hdl.handle.net/XXX-XXX-XXX", null, PREFERRED, | ||
createIdentifier("https://hdl.handle.net/XXX-XXX-XXX", HANDLE, "Handle", | ||
GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT, PREFERRED)), | ||
Arguments.of("88e320d5-c47a-4288-b265-fa3c93c57440", "dwc:catalogueNumber", null, | ||
createIdentifier("88e320d5-c47a-4288-b265-fa3c93c57440", UUID, "dwc:catalogueNumber", | ||
GLOBALLY_UNIQUE_STABLE, null)), | ||
Arguments.of("urn:uuid:541fd754-17e8-43c8-ba4e-b413a1bf3a2f", "dwca:ID", null, | ||
createIdentifier("urn:uuid:541fd754-17e8-43c8-ba4e-b413a1bf3a2f", UUID, "dwca:ID", | ||
GLOBALLY_UNIQUE_STABLE, null)), | ||
Arguments.of("https://geocollections.info/specimen/126758", "abcd:unitGUID", null, | ||
createIdentifier("https://geocollections.info/specimen/126758", URL, "abcd:unitGUID", | ||
GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE, null)), | ||
Arguments.of("http://n2t.net/ark:/65665/3173bef93-f5c6-4534-bd31-42289606938b", "dwc:catalogueNumber", null, | ||
createIdentifier("http://n2t.net/ark:/65665/3173bef93-f5c6-4534-bd31-42289606938b", ARK, "dwc:catalogueNumber", | ||
GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE, null)), | ||
Arguments.of("http://purl.org/dc/terms/accessRights", null, null, | ||
createIdentifier("http://purl.org/dc/terms/accessRights", PURL, "PURL", | ||
GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE, null)), | ||
Arguments.of("AVES-071259", "dwc:occurrenceID", null, | ||
createIdentifier("AVES-071259", LOCALLY_UNIQUE_IDENTIFIER, "dwc:occurrenceID", | ||
LOCALLY_UNIQUE_STABLE, null)), | ||
Arguments.of(null, null, null, null) | ||
); | ||
} | ||
|
||
private static Identifier createIdentifier(String id, DctermsType type, String title, | ||
OdsGupriLevel gupriLevel, OdsIdentifierStatus status) { | ||
return new Identifier() | ||
.withId(id) | ||
.withType("ods:Identifier") | ||
.withDctermsIdentifier(id) | ||
.withDctermsTitle(title) | ||
.withDctermsType(type) | ||
.withOdsGupriLevel(gupriLevel) | ||
.withOdsIdentifierStatus(status); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("identifierProvider") | ||
void testAddIdentifier(String identifier, String identifierName, OdsIdentifierStatus status, | ||
Identifier expected) { | ||
// When | ||
var result = addIdentifier(identifier, identifierName, status); | ||
|
||
// Then | ||
assertThat(result).isEqualTo(expected); | ||
} | ||
} |