Skip to content

Commit

Permalink
Add replaced by information
Browse files Browse the repository at this point in the history
  • Loading branch information
vietnguyengit committed Oct 15, 2024
1 parent b98b96d commit 30b009d
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class VocabModel {
protected List<String> altLabels;
@JsonProperty("is_latest_label")
protected Boolean isLatestLabel;
@JsonProperty("replaced_by")
protected String replacedBy;
protected String definition;
protected String about;
protected List<VocabModel> broader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.function.Function;
import java.util.stream.StreamSupport;
import java.util.stream.Collectors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ArdcVocabServiceImpl implements ArdcVocabService {

Expand All @@ -28,7 +30,7 @@ public class ArdcVocabServiceImpl implements ArdcVocabService {
protected RestTemplate restTemplate;
protected RetryTemplate retryTemplate;

protected Function<JsonNode, String> extractValueField(String key) {
protected Function<JsonNode, String> extractSingleText(String key) {
return (node) -> {
JsonNode labelNode = node.get(key);
if (labelNode != null) {
Expand All @@ -43,7 +45,7 @@ protected Function<JsonNode, String> extractValueField(String key) {
return null;
};
}
protected Function<JsonNode, List<String>> extractMultipleValueFields(String key) {
protected Function<JsonNode, List<String>> extractMultipleTexts(String key) {
return (node) -> {
JsonNode labelNode = node.get(key);
if (labelNode != null && labelNode.isArray()) {
Expand All @@ -57,13 +59,31 @@ protected Function<JsonNode, List<String>> extractMultipleValueFields(String key
}

// Reusing the utility methods for specific labels
protected Function<JsonNode, String> label = extractValueField("prefLabel");
protected Function<JsonNode, String> displayLabel = extractValueField("displayLabel");
protected Function<JsonNode, List<String>> hiddenLabels = extractMultipleValueFields("hiddenLabel");
protected Function<JsonNode, List<String>> altLabels = extractMultipleValueFields("altLabel");
protected Function<JsonNode, String> about = extractValueField("_about");
protected Function<JsonNode, String> definition = extractValueField("definition");
protected Function<JsonNode, Boolean> isLatestLabel = (node) -> !(node.has("isReplacedBy") || (node.has("scopeNote") && extractValueField("scopeNote").apply(node).contains("no longer exists")));
protected Function<JsonNode, String> label = extractSingleText("prefLabel");
protected Function<JsonNode, String> displayLabel = extractSingleText("displayLabel");
protected Function<JsonNode, List<String>> hiddenLabels = extractMultipleTexts("hiddenLabel");
protected Function<JsonNode, List<String>> altLabels = extractMultipleTexts("altLabel");
protected Function<JsonNode, String> about = extractSingleText("_about");
protected Function<JsonNode, String> definition = extractSingleText("definition");
protected Function<JsonNode, Boolean> isLatestLabel = (node) -> !(node.has("isReplacedBy") || (node.has("scopeNote") && extractSingleText("scopeNote").apply(node).toLowerCase().contains("no longer exists")));
protected Function<JsonNode, Boolean> isReplacedBy = (node) -> node.has("isReplacedBy") && node.has("scopeNote") && extractSingleText("scopeNote").apply(node).toLowerCase().contains("replaced by");

private String extractReplacedVocabUri(String scopeNote) {
String regex = "Replaced by (https?://[\\w./-]+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(scopeNote);

if (matcher.find()) {
String result = matcher.group(1);
if (result.endsWith(".")) {
result = result.substring(0, result.length() - 1);
}
return result;
}

return null;
}

protected BiFunction<JsonNode, String, Boolean> isNodeValid = (node, item) -> node != null && !node.isEmpty() && node.has(item) && !node.get(item).isEmpty();

public ArdcVocabServiceImpl(RestTemplate restTemplate, RetryTemplate retryTemplate) {
Expand Down Expand Up @@ -95,6 +115,10 @@ protected VocabModel buildVocabByResourceUri(String vocabUri, String vocabApiBas
.isLatestLabel(isLatestLabel.apply(target))
.build();

if (!vocab.getIsLatestLabel() && isReplacedBy.apply(target)) {
vocab.setReplacedBy(extractReplacedVocabUri(extractSingleText("scopeNote").apply(target)));
}

List<VocabModel> narrowerNodes = new ArrayList<>();
if (isNodeValid.apply(target, "narrower")) {
for (JsonNode j : target.get("narrower")) {
Expand Down Expand Up @@ -175,6 +199,10 @@ protected Map<String, List<VocabModel>> getVocabLeafNodes(String vocabApiBase, V
.isLatestLabel(isLatestLabel.apply(target))
.build();

if (!vocab.getIsLatestLabel() && isReplacedBy.apply(target)) {
vocab.setReplacedBy(extractReplacedVocabUri(extractSingleText("scopeNote").apply(target)));
}

List<VocabModel> vocabNarrower = new ArrayList<>();
if(target.has("narrower") && !target.get("narrower").isEmpty()) {
for(JsonNode currentNode : target.get("narrower")) {
Expand Down
Loading

0 comments on commit 30b009d

Please sign in to comment.