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

Set up PUT route for indexing single records (RPB-223) #100

Merged
merged 10 commits into from
Nov 15, 2024
22 changes: 22 additions & 0 deletions app/controllers/nwbib/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
package controllers.nwbib;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -28,6 +31,7 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.antlr.runtime.RecognitionException;
import org.apache.commons.lang3.tuple.Pair;
import org.elasticsearch.common.base.Charsets;
import org.elasticsearch.common.geo.GeoPoint;
Expand All @@ -54,6 +58,7 @@
import play.mvc.Result;
import play.mvc.Results;
import play.twirl.api.HtmlFormat;
import rpb.ETL;
import views.html.browse_classification;
import views.html.browse_register;
import views.html.classification;
Expand Down Expand Up @@ -943,4 +948,21 @@ private static List<String> starredIds() {
return new ArrayList<>(Arrays.asList(currentlyStarred().split(" ")).stream()
.filter(s -> !s.trim().isEmpty()).collect(Collectors.toList()));
}

public static Promise<Result> put(String id, String secret) throws FileNotFoundException, RecognitionException, IOException {
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()), request().body().asJson().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"));
boolean authorized = !secret.trim().isEmpty() && secret.equals(CONFIG.getString("secret"));
if (authorized) {
String url = "http://weywot3:9200/resources-rpb-test/resource/"
+ URLEncoder.encode("https://lobid.org/resources/" + id, "UTF-8");
WSRequest request = WS.url(url).setHeader("Content-Type", "application/json");
return request.put(result).map(response -> status(response.getStatus(), response.getBody()));
} else {
return Promise.pure(unauthorized());
}
}
}
2 changes: 1 addition & 1 deletion app/controllers/nwbib/Lobid.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public static String resourceLabel(String id) {
Callable<String> getLabel = () -> {
// e.g. take TT000086525 from http://lobid.org/resources/TT000086525#!
String simpleId =
id.replaceAll("https?://[^/]+/resources?/(?:search\\?q=rpbId:)?(.+?)[^A-Z0-9]*$", "$1");
id.replaceAll("https?://[^/]+/(?:resources?/)?(?:search\\?q=rpbId:)?(.+?)[^A-Z0-9]*$", "$1");
JsonNode json = getResource(simpleId).findValue("title");
String label =
json == null ? "" : HtmlEscapers.htmlEscaper().escape(json.asText());
Expand Down
13 changes: 7 additions & 6 deletions app/views/TableRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public String process(JsonNode doc, String property, String param,
return filtered.isEmpty() ? ""
: String.format("<tr><td>%s</td><td>%s</td></tr>", label,
filtered.stream()
.flatMap(s -> Arrays.asList(s.split("; ")).stream())
.map(val -> label(doc, property, param, val, keys))
.collect(Collectors.joining(
property.equals("subjectChain") ? " <br/> " : " | ")));
Expand All @@ -48,7 +49,9 @@ private String label(JsonNode doc, String property, String param,
String value = property.equals("subjectChain")
? val.replaceAll("\\([\\d,]+\\)$", "").trim() : val;
if (!labels.isPresent()) {
return refAndLabel(property, value, labels)[0];
String[] refAndLabel = refAndLabel(property, value, labels);
return value.startsWith("http") ? String.format("<a title='%s' href='%s'>%s</a>",
refAndLabel[0], refAndLabel[0], refAndLabel[1]) : refAndLabel[0];
}
String term = value;
if (param.equals("q")) {
Expand Down Expand Up @@ -210,11 +213,9 @@ private static String lifeDates(JsonNode node) {
String[] refAndLabel(String property, String value,
Optional<List<String>> labels) {
if ((property.equals("containedIn") || property.equals("hasPart")
|| property.equals("isPartOf") || property.equals("hasSuperordinate"))
&& value.contains("lobid.org")) {
return new String[] {
value.replaceAll("https://lobid.org/resources?/", "http://rpb.lobid.org/"),
Lobid.resourceLabel(value) };
|| property.equals("isPartOf") || property.equals("hasSuperordinate")
|| property.equals("bibliographicCitation")) && value.contains("lobid.org")) {
return new String[] { value, Lobid.resourceLabel(value) };
}
String label =
labels.isPresent() && labels.get().size() > 0 ? labels.get().get(0)
Expand Down
2 changes: 2 additions & 0 deletions app/views/tags/result_doc.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
}

@subjects(subjects: Seq[JsValue]) = {
@if(!subjects.isEmpty) {
<tr>
<td>Schlagwörter</td>
<td>
Expand All @@ -157,6 +158,7 @@
} else { | }
}</td>
</tr>
}
}

@sortedPublications(seq: Seq[JsValue]) = @{
Expand Down
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ lazy val root = (project in file(".")).enablePlugins(PlayJava)

javacOptions ++= Seq("-source", "1.8", "-target", "1.8")

javaOptions += "-Dnet.sf.ehcache.disabled=true"

import com.typesafe.sbteclipse.core.EclipsePlugin.EclipseKeys

EclipseKeys.projectFlavor := EclipseProjectFlavor.Java // Java project. Don't expect Scala IDE
Expand Down
1 change: 1 addition & 0 deletions conf/nwbib.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ item.api="http://lobid.org/items"
hbz01.api="http://lobid.org/hbz01"
orgs.api="http://lobid.org/organisations"
nwbib.filter="inCollection.id:\"http://lobid.org/resources/HT013494180#!\""
secret=""

# Embedded Elasticsearch index for classification data
index {
Expand Down
3 changes: 2 additions & 1 deletion conf/nwbib.routes
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ GET /robots.txt controllers.Assets.at(path="/public",file="robots.
GET /cgi-bin/wwwalleg/:name.pl controllers.nwbib.Application.showPl(name, db ?= "rnam", index: Int ?= 1, zeilen: Int ?= 1, s1)
GET /sw/:rpbId controllers.nwbib.Application.showSw(rpbId)
GET /o:id controllers.nwbib.Application.searchSpatial(id, from:Int?=0, size:Int?=25, format?="html")
GET /:id controllers.nwbib.Application.show(id, format ?= "")
GET /:id controllers.nwbib.Application.show(id, format ?= "")
PUT /:id controllers.nwbib.Application.put(id, secret ?= "")
40 changes: 28 additions & 12 deletions conf/rpb-titel-to-lobid.fix
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ copy_field("rpbId", "_id")
# ------- Set "type" -------

set_array("type[]", "BibliographicResource")
unless exists("type")
set_field("type[].$append", "Article")
end
if all_equal("type", "u")
set_field("type[].$append", "Article")
end
Expand All @@ -40,7 +43,10 @@ end
if any_equal("type", "Band")
set_field("type[].$append", "Book")
call_macro("move_here", field: "isPartOf[]")
copy_field("isPartOf[].value", "superordinateLabel")
copy_field("isPartOf[].label", "superordinateLabel")
unless exists("superordinateLabel")
copy_field("isPartOf[].value", "superordinateLabel")
end
set_array("isPartOf[]")
set_array("isPartOf[].$append.type[]", "IsPartOfRelation")
set_array("isPartOf[].$last.hasSuperordinate[]")
Expand Down Expand Up @@ -120,9 +126,14 @@ if exists("isPartOf[]")
set_array("temp")
set_array("temp.$append.type[]", "IsPartOfRelation")
set_array("temp.$last.hasSuperordinate[]")
split_field("isPartOf[].1.value", " ; ")
copy_field("isPartOf[].1.value.1", "temp.$last.hasSuperordinate[].$append.label")
copy_field("isPartOf[].1.value.2", "temp.$last.numbering")
if exists("isPartOf[].1.label")
copy_field("isPartOf[].1.value", "temp.$last.hasSuperordinate[].$append.id")
copy_field("isPartOf[].1.label", "temp.$last.hasSuperordinate[].$last.label")
else
split_field("isPartOf[].1.value", " ; ")
copy_field("isPartOf[].1.value.1", "temp.$last.hasSuperordinate[].$append.label")
copy_field("isPartOf[].1.value.2", "temp.$last.numbering")
end
move_field("temp", "isPartOf[]")
end
end
Expand Down Expand Up @@ -204,10 +215,12 @@ end
do
list (path:"temp_subject.*.componentList[]", "var": "$i")
move_field("$i.value", "$i.id")
copy_field("$i.id", "$i.label")
replace_all("$i.label","^http.+[/#](.+)$","$1")
replace_all("$i.label","^(\\d+)-(\\d+)$","$1n$2")
lookup("$i.label", "./RPB-Export_HBZ_SW.tsv","sep_char":"\t")
unless exists("$i.label")
copy_field("$i.id", "$i.label")
replace_all("$i.label","^http.+[/#](.+)$","$1")
replace_all("$i.label","^(\\d+)-(\\d+)$","$1n$2")
lookup("$i.label", "./RPB-Export_HBZ_SW.tsv","sep_char":"\t")
end
if all_match("$i.id","^http://rpb.lobid.org/sw/.*$")
add_field("$i.source.id", "http://rpb.lobid.org/sw")
add_field("$i.source.label", "RPB-Sachsystematik")
Expand Down Expand Up @@ -283,11 +296,14 @@ do put_macro("contributions")
set_array("_temp")
copy_field("$[from][].*.value", "_temp.$append.agent.id")
do list_as(f: "$[from][]", c: "_temp")
copy_field("c.agent.id", "c.agent.label")
copy_field("f.label", "c.agent.label")
unless exists("c.agent.label")
copy_field("c.agent.id", "c.agent.label")
replace_all("c.agent.label","^http.+[/#](.+)$","$1")
replace_all("c.agent.label","^(\\d+)-(\\d+)$","$1n$2")
lookup("c.agent.label", "./RPB-Export_HBZ_SW.tsv","sep_char":"\t")
end
set_array("c.agent.type[]", "Person")
replace_all("c.agent.label","^http.+[/#](.+)$","$1")
replace_all("c.agent.label","^(\\d+)-(\\d+)$","$1n$2")
lookup("c.agent.label", "./RPB-Export_HBZ_SW.tsv","sep_char":"\t")
copy_field("f.type", "c.role.id")
copy_field("f.type", "c.role.label")
lookup("c.role.id", "role-to-uri")
Expand Down
Loading