Skip to content

Commit

Permalink
Merge branch 'rppd_rpb-154-missingLabels' of https://github.com/hbz/l…
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Apr 17, 2024
2 parents f8f0349 + 23f842a commit 4b896b0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public Result authority(String id, String format) {
}

private AuthorityResource entityWithImage(String jsonLd) {
AuthorityResource entity = new AuthorityResource(Json.parse(jsonLd));
AuthorityResource entity = new AuthorityResource(Json.parse(jsonLd), httpClient);
if (entity.getImage().url.contains("File:"))
entity.imageAttribution = attribution(
entity.getImage().url.substring(entity.getImage().url.indexOf("File:") + 5).split("\\?")[0]);
Expand Down
48 changes: 36 additions & 12 deletions app/models/AuthorityResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.TreeSet;
import java.util.concurrent.ExecutionException;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand All @@ -32,9 +33,11 @@
import controllers.HomeController;
import play.Logger;
import play.libs.Json;
import play.libs.ws.WSClient;
import play.libs.ws.WSResponse;

public class AuthorityResource {

public static final String ID = "AuthorityResource";
private static final int SHORTEN = 5;
public static final String DNB_PREFIX = "https://d-nb.info/";
Expand All @@ -58,7 +61,14 @@ public class AuthorityResource {
public String imageAttribution;
private JsonNode json;

private final WSClient httpClient;

public AuthorityResource(JsonNode json) {
this(json, null);
}

public AuthorityResource(JsonNode json, WSClient httpClient) {
this.httpClient = httpClient;
this.json = json;
this.id = json.get("id").textValue();
this.type = get("type");
Expand Down Expand Up @@ -336,6 +346,7 @@ private void addIds(String field, List<Pair<String, String>> result) {
add(field, list, result, i -> {
String id = list.get(i).get("id").toString();
String label = list.get(i).get("label").toString();
label = label.matches("\\d.+|http.+") ? GndOntology.label(id) : label;
return process(field, id, label, i, list.size());
});
}
Expand Down Expand Up @@ -454,29 +465,28 @@ private String process(String field, String value, String label, int i, int size
result = String.format("<a href='%s'>%s</a>", value, value);
} else if (Arrays.asList("dateOfBirth", "dateOfDeath").contains(field)) {
result = germanDate(value);
} else if (field.equals("source") && value.startsWith("http")) {
result = String.format("<a href='%s'>%s</a> %s", value.split(" ")[0], value.split(" ")[0],
value.replace(value.split(" ")[0] + " ", ""));
} else if (value.startsWith("http")) {
String link = value.startsWith(GND_PREFIX)
? controllers.routes.HomeController.authority(value.replace(GND_PREFIX, ""), null).toString()
: value;
}
else if (value.startsWith("http")) {
List<String> facets = Arrays.asList(HomeController.AGGREGATIONS);
boolean labelBasedFacet = facets.contains(field + ".label");
boolean qBasedSearch = facets.stream().noneMatch(s -> s.startsWith(field));
boolean plainUriField = field.equals("source") || field.equals("publication");
String searchField = (field + (plainUriField ? "" : ".id")).replace("source",
"describedBy.source");
label = plainUriField ? labelFor(value) : label;
String search = controllers.routes.HomeController
.search(qBasedSearch ? field + ".id:\"" + value + "\"" : "", "", "", "", "", "",
.search(qBasedSearch ? searchField + ":\"" + value + "\"" : "", "", "", "", "", "",
labelBasedFacet ? field + ".label:\"" + label + "\""
: field + ".id:\"" + value + "\"", "", 0, 10, "html")
: searchField + ":\"" + value + "\"", "", 0, 10, "html")
.toString();
String searchLink = String.format(
"<a id='%s-%s' title='Weitere Einträge mit %s \"%s\" suchen' href='%s'>%s</a>", //
field, i, GndOntology.label(field), label, search, label);
String entityLink = String.format(
"<a title='Linked-Data-Quelle zu \"%s\" anzeigen' href='%s'>"
+ "<i class='octicon octicon-link text-muted' aria-hidden='true'></i></a>",
label, link);
boolean linkableEntity = field.equals("relatedPerson")
label, value);
boolean linkableEntity = field.equals("relatedPerson") || plainUriField
|| (field.startsWith("place") && value.contains("spatial"));
result = searchLink + "&nbsp;" + (linkableEntity ? entityLink : "");
} else if (field.endsWith("AsLiteral")) {
Expand All @@ -492,6 +502,20 @@ private String process(String field, String value, String label, int i, int size
return withDefaultHidden(field, size, i, result);
}

private String labelFor(String uri) {
try {
JsonNode response = httpClient.url(uri).setFollowRedirects(true)
.addQueryParameter("format", "json").get().thenApply(WSResponse::asJson)
.toCompletableFuture().get();
JsonNode entity = response.has("member") ? response.get("member").elements().next()
: response;
return entity.get("title").asText();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return uri;
}

public static String germanDate(String value) {
try {
return LocalDate
Expand Down
1 change: 1 addition & 0 deletions app/views/details.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
$(function() { $('[data-toggle="tooltip"]').tooltip(); });
</script>
<table class='table table-striped table-condensed'>
<tr><th style="width: 30%"></th><th style="width: 70%"></th></tr>
@*<tr>
<td>URI</td> @defining((resource.getFullId(), "URI in die Zwischenablage kopieren")) { case (uri, title) =>
<td class='field-value'>@uri
Expand Down

0 comments on commit 4b896b0

Please sign in to comment.