-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/add identifier meta #71
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9ea14e
Add identifier metadata
samleeflang 9ba31d3
Merge branch 'feature/iiif-er' into feature/add-identifier-meta
samleeflang d695ab1
Switch to Pattern match
samleeflang ed287b4
Add pattern match for UUID
samleeflang a4722f4
Remove unused method
samleeflang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
108 changes: 108 additions & 0 deletions
108
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,108 @@ | ||
package eu.dissco.core.translator.terms.utils; | ||
|
||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.DOI; | ||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.HANDLE; | ||
import static eu.dissco.core.translator.schema.Identifier.DctermsType.URL; | ||
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 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.UUID; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.tuple.Triple; | ||
|
||
@Slf4j | ||
public class IdentifierUtils { | ||
|
||
private static final Map<List<String>, Triple<DctermsType, String, OdsGupriLevel>> map = getPrefixMap(); | ||
|
||
private IdentifierUtils() { | ||
// This is a Utility class | ||
} | ||
|
||
private static Map<List<String>, Triple<DctermsType, String, OdsGupriLevel>> getPrefixMap() { | ||
var linkedMap = new LinkedHashMap<List<String>, Triple<DctermsType, String, OdsGupriLevel>>(); | ||
linkedMap.put(List.of("https://doi.org"), | ||
Triple.of(DOI, "DOI", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT)); | ||
linkedMap.put(List.of("https://hdl.handle.net"), | ||
Triple.of(HANDLE, "Handle", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT)); | ||
linkedMap.put(List.of("http://www.wikidata.org", "https://www.wikidata.org"), | ||
Triple.of(URL, "Wikidata", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE)); | ||
linkedMap.put(List.of("http://orcid.org", "https://orcid.org"), | ||
Triple.of(URL, "ORCID", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE)); | ||
linkedMap.put(List.of("http", "https"), | ||
Triple.of(URL, "URL", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE)); | ||
linkedMap.put(List.of("urn:uuid"), | ||
Triple.of(DctermsType.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 : map.entrySet()) { | ||
for (var prefix : entry.getKey()) { | ||
if (identifierString.startsWith(prefix)) { | ||
identifier.setDctermsType(entry.getValue().getLeft()); | ||
identifier.setDctermsTitle(getDcTermsTitle(identifierName, entry.getValue().getMiddle())); | ||
identifier.setOdsGupriLevel(entry.getValue().getRight()); | ||
return identifier; | ||
} | ||
} | ||
} | ||
if (isValidUUID(identifierString)) { | ||
identifier.setDctermsType(DctermsType.UUID); | ||
identifier.setDctermsTitle(getDcTermsTitle(identifierName, "UUID")); | ||
identifier.setOdsGupriLevel(GLOBALLY_UNIQUE_STABLE); | ||
return identifier; | ||
} else { | ||
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; | ||
} | ||
} | ||
|
||
private static boolean isValidUUID(String identifierString) { | ||
try { | ||
UUID.fromString(identifierString); | ||
return true; | ||
} catch (IllegalArgumentException e) { | ||
log.debug("Identifier {} is not a valid UUID", identifierString); | ||
return false; | ||
} | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
package eu.dissco.core.translator.terms.utils; | ||
|
||
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.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("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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
naming convention?
MAP
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah lacked imagination, now call it PATTERN_MAP