Skip to content

Commit 4a8c98f

Browse files
committed
Get hebis data and call transformation in PUT route (RPB-225)
To merge external hebis data with strapi record for indexing
1 parent 530275c commit 4a8c98f

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

app/controllers/nwbib/Application.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -995,17 +995,23 @@ private static Promise<Result> deleteFromIndex(String id) throws UnsupportedEnco
995995

996996
private static Promise<Result> transformAndIndex(String id, JsonNode jsonBody)
997997
throws IOException, FileNotFoundException, RecognitionException, UnsupportedEncodingException {
998-
JsonNode transformedJson = transform(jsonBody);
999-
Promise<JsonNode> dataPromise = id.startsWith("f") && transformedJson.has("hbzId") ? // hbz-Fremddaten
1000-
addToLobidData(transformedJson) : Promise.pure(transformedJson);
998+
JsonNode transformedJson = transformStrapiToLobid(jsonBody);
999+
Promise<JsonNode> dataPromise = Promise.pure(transformedJson);
1000+
if (id.startsWith("f")) { // Fremddaten
1001+
if (transformedJson.has("hbzId")) {
1002+
dataPromise = addToLobidData(transformedJson);
1003+
} else if (transformedJson.has("hebisId")) {
1004+
dataPromise = addToHebisData(transformedJson);
1005+
}
1006+
}
10011007
return dataPromise.flatMap(result -> {
10021008
Cache.remove(String.format("/%s", id));
10031009
WSRequest request = WS.url(elasticsearchUrl(id)).setHeader("Content-Type", "application/json");
10041010
return request.put(result).map(response -> status(response.getStatus(), response.getBody()));
10051011
});
10061012
}
10071013

1008-
private static JsonNode transform(JsonNode jsonBody)
1014+
private static JsonNode transformStrapiToLobid(JsonNode jsonBody)
10091015
throws IOException, FileNotFoundException, RecognitionException {
10101016
File input = new File("conf/output/test-output-strapi.json");
10111017
File output = new File("conf/output/test-output-0.json");
@@ -1023,6 +1029,33 @@ private static Promise<JsonNode> addToLobidData(JsonNode transformedJson) {
10231029
return merged;
10241030
}
10251031

1032+
private static Promise<JsonNode> addToHebisData(JsonNode transformedJson) {
1033+
WSRequest hebisRequest = WS.url("http://sru.hebis.de/sru/DB=2.1").setHeader("Accept", "application/xml")
1034+
.setQueryParameter("query", "pica.ppn = \"" + transformedJson.get("hebisId").textValue() + "\"")//
1035+
.setQueryParameter("version", "1.1")//
1036+
.setQueryParameter("operation", "searchRetrieve")//
1037+
.setQueryParameter("stylesheet", "http://sru.hebis.de/sru/?xsl=searchRetrieveResponse")//
1038+
.setQueryParameter("recordSchema", "marc21")//
1039+
.setQueryParameter("maximumRecords", "1")//
1040+
.setQueryParameter("startRecord", "1")//
1041+
.setQueryParameter("recordPacking", "xml")//
1042+
.setQueryParameter("sortKeys", "LST_Y,pica,0,,");
1043+
Promise<String> hebisXmlPromise = hebisRequest.get().map(WSResponse::asByteArray).map(byteArray -> new String(byteArray, "UTF-8"));
1044+
Promise<JsonNode> hebisJsonPromise = hebisXmlPromise.map(hebisXml -> transformHebisToLobid(hebisXml));
1045+
Promise<JsonNode> merged = hebisJsonPromise.map(hebisJson -> mergeRecords(transformedJson, hebisJson));
1046+
return merged;
1047+
}
1048+
1049+
private static JsonNode transformHebisToLobid(String xmlBody)
1050+
throws IOException, FileNotFoundException, RecognitionException {
1051+
File input = new File("conf/output/test-output-hebis.xml");
1052+
File output = new File("conf/output/test-output-0.json");
1053+
Files.write(Paths.get(input.getAbsolutePath()), xmlBody.getBytes());
1054+
ETL.main(new String[] {"conf/rpb-test-titel-hebis-to-lobid.flux"});
1055+
String result = Files.readAllLines(Paths.get(output.getAbsolutePath())).stream().collect(Collectors.joining("\n"));
1056+
return Json.parse(result);
1057+
}
1058+
10261059
private static JsonNode mergeRecords(JsonNode transformedJson, JsonNode lobidJson)
10271060
throws JsonMappingException, JsonProcessingException {
10281061
ObjectMapper objectMapper = new ObjectMapper();
@@ -1039,6 +1072,9 @@ private static JsonNode mergeRecords(JsonNode transformedJson, JsonNode lobidJso
10391072
: transformedObject;
10401073
lobidMap.put(key, values);
10411074
});
1075+
if(transformedJson.has("hebisId")) {
1076+
lobidMap.remove("hasItem"); // temp: https://github.com/hbz/rpb/pull/105#discussion_r2022907781
1077+
}
10421078
return Json.toJson(lobidMap);
10431079
}
10441080

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FLUX_DIR + "output/test-output-hebis.xml"
2+
| open-file
3+
| decode-xml
4+
| handle-marcxml
5+
| fix(FLUX_DIR + "hebisMarc2lobid-transformation/marcToLobid.fix", *)
6+
| batch-reset(batchsize="1")
7+
| encode-json(prettyPrinting="true")
8+
| write(FLUX_DIR + "output/test-output-${i}.json")
9+
;

conf/rpb-titel-to-lobid.fix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,6 @@ end
388388

389389
move_field("edition", "edition[]")
390390

391-
retain( "type[]", "contribution[]", "edition[]", "extent", "hasItem[]", "responsibilityStatement[]", "language[]", "medium[]", "subject[]", "title", "hbzId", "oclcNumber[]", "otherTitleInformation[]", "alternativeTitle[]", "titleKeyword[]", "natureOfContent[]", "publication[]", "sameAs[]", "describedBy", "@context", "id", "zdbId", "spatial[]", "inCollection[]", "rpbId", "schoeneNummer", "bibliographicCitation", "isPartOf[]", "note[]", "fulltextOnline[]", "description[]", "isbn[]", "containedIn[]")
391+
retain( "type[]", "contribution[]", "edition[]", "extent", "hasItem[]", "responsibilityStatement[]", "language[]", "medium[]", "subject[]", "title", "hbzId", "hebisId", "oclcNumber[]", "otherTitleInformation[]", "alternativeTitle[]", "titleKeyword[]", "natureOfContent[]", "publication[]", "sameAs[]", "describedBy", "@context", "id", "zdbId", "spatial[]", "inCollection[]", "rpbId", "schoeneNummer", "bibliographicCitation", "isPartOf[]", "note[]", "fulltextOnline[]", "description[]", "isbn[]", "containedIn[]")
392392

393393
vacuum()

0 commit comments

Comments
 (0)