Skip to content
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

Handle external records with hbzId in PUT route (RPB-184) #118

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions app/controllers/nwbib/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.Streams;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

Expand Down Expand Up @@ -976,16 +980,59 @@ private static Promise<Result> deleteFromIndex(String id) throws UnsupportedEnco

private static Promise<Result> transformAndIndex(String id, JsonNode jsonBody)
throws IOException, FileNotFoundException, RecognitionException, UnsupportedEncodingException {
JsonNode transformedJson = transform(jsonBody);
Promise<JsonNode> dataPromise = id.startsWith("f") && transformedJson.has("hbzId") ? // hbz-Fremddaten
addToLobidData(transformedJson) : Promise.pure(transformedJson);
return dataPromise.flatMap(result -> {
Cache.remove(String.format("/%s", id));
WSRequest request = WS.url(elasticsearchUrl(id)).setHeader("Content-Type", "application/json");
return request.put(result).map(response -> status(response.getStatus(), response.getBody()));
});
}

private static JsonNode transform(JsonNode jsonBody)
throws IOException, FileNotFoundException, RecognitionException {
File input = new File("conf/output/test-output-strapi.json");
File output = new File("conf/output/test-output-0.json");
Files.write(Paths.get(input.getAbsolutePath()), jsonBody.toString().getBytes(Charsets.UTF_8));
ETL.main(new String[] {"conf/rpb-test-titel-to-lobid.flux"});
String result = Files.readAllLines(Paths.get(output.getAbsolutePath())).stream().collect(Collectors.joining("\n"));
Cache.remove(String.format("/%s", id));
WSRequest request = WS.url(elasticsearchUrl(id)).setHeader("Content-Type", "application/json");
return request.put(result).map(response -> status(response.getStatus(), response.getBody()));
ETL.main(new String[] { "conf/rpb-test-titel-to-lobid.flux" });
String result = Files.readAllLines(Paths.get(output.getAbsolutePath())).stream()
.collect(Collectors.joining("\n"));
return Json.parse(result);
}

private static Promise<JsonNode> addToLobidData(JsonNode transformedJson) {
String lobidUrl = transformedJson.get("hbzId").textValue();
WSRequest lobidRequest = WS.url(lobidUrl).setHeader("Content-Type", "application/json");
Promise<JsonNode> lobidPromise = lobidRequest.get().map(WSResponse::asJson);
Promise<JsonNode> merged = lobidPromise.map(lobidJson -> mergeRecords(transformedJson, lobidJson));
return merged;
}


private static JsonNode mergeRecords(JsonNode transformedJson, JsonNode lobidJson)
throws JsonMappingException, JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
MapType mapType = TypeFactory.defaultInstance().constructMapType(Map.class, String.class, Object.class);
Map<String, Object> transformedMap = objectMapper.readValue(transformedJson.toString(), mapType);
Map<String, Object> lobidMap = objectMapper.readValue(lobidJson.toString(), mapType);
transformedMap.remove("type");
transformedMap.keySet().forEach(key -> {
Object transformedObject = transformedMap.get(key);
Object lobidObject = lobidMap.getOrDefault(key, new ArrayList<Object>());
Object values = transformedObject instanceof List ? mergeValues(transformedObject, lobidObject)
: transformedObject;
lobidMap.put(key, values);
});
return Json.toJson(lobidMap);
}

private static Object mergeValues(Object transformedObject, Object lobidObject) {
List<Object> mergedValues = lobidObject instanceof List ? new ArrayList<>((List<?>) lobidObject)
: Arrays.asList(lobidObject);
mergedValues.addAll((List<?>) transformedObject);
return mergedValues;
}

private static String elasticsearchUrl(String id) throws UnsupportedEncodingException {
return "http://weywot3:9200/resources-rpb-test/resource/"
+ URLEncoder.encode("https://lobid.org/resources/" + id, "UTF-8");
Expand Down
13 changes: 13 additions & 0 deletions app/views/TableRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import controllers.nwbib.Classification;
import controllers.nwbib.Lobid;
import play.Logger;
import play.libs.F.Promise;
import play.libs.ws.WS;
import play.libs.ws.WSRequest;
import play.libs.ws.WSResponse;

/**
* Different ways of serializing a table row
Expand Down Expand Up @@ -214,6 +218,9 @@ private static String lifeDates(JsonNode node) {

String[] refAndLabel(String property, String value,
Optional<List<String>> labels) {
if (value.contains("lobid.org/resources/")) {
value = rpbUrlIfInRpb(value);
}
if ((property.equals("containedIn") || property.equals("hasPart")
|| property.equals("isPartOf") || property.equals("hasSuperordinate")
|| property.equals("bibliographicCitation")) && value.contains("lobid.org")) {
Expand All @@ -227,6 +234,12 @@ String[] refAndLabel(String property, String value,
return new String[] { value, label };
}

String rpbUrlIfInRpb(String value) {
WSRequest lobidRequest = WS.url(value).setHeader("Content-Type", "application/json");
JsonNode lobidJson = lobidRequest.get().map(WSResponse::asJson).get(Lobid.API_TIMEOUT);
return lobidJson.has("rpbId") ? "https://rpb.lobid.org/" + lobidJson.get("rpbId").textValue() : value;
}

public abstract String process(JsonNode doc, String property, String param,
String label, List<String> values, Optional<List<String>> labels);
}
2 changes: 1 addition & 1 deletion conf/nwbib.conf
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ medium.labels={
"http://rdaregistry.info/termList/RDAMediaType/1002" : ["Mikroform","octicon octicon-versions", 4],
"http://purl.org/ontology/bibo/AudioDocument" : ["Audio","glyphicon glyphicon-headphones", 5],
"http://purl.org/ontology/bibo/AudioVisualDocument" : ["Audio-Visuell","glyphicon glyphicon-film", 6],
"http://rdaregistry.info/termList/RDAMediaType/1008" : ["Kombination","glyphicon glyphicon-asterisk", 7],
"http://rdaregistry.info/termList/RDAMediaType/1008" : ["Video","glyphicon glyphicon-asterisk", 7],
"http://purl.org/library/BrailleBook" : ["Braille","glyphicon glyphicon-eye-close", 8],
"http://purl.org/ontology/mo/Vinyl" : ["Schallplatte","glyphicon glyphicon-record", 9],
"http://id.loc.gov/vocabulary/carriers/ss" : ["Tonband", "octicon octicon-link", 10],
Expand Down