Skip to content

Commit

Permalink
Show labels for linked title data URIs in details view (RPB-154)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Apr 15, 2024
1 parent 36b21f9 commit 23f842a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 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
27 changes: 26 additions & 1 deletion 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 @@ -463,6 +473,7 @@ else if (value.startsWith("http")) {
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 ? searchField + ":\"" + value + "\"" : "", "", "", "", "", "",
labelBasedFacet ? field + ".label:\"" + label + "\""
Expand Down Expand Up @@ -491,6 +502,20 @@ else if (value.startsWith("http")) {
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 23f842a

Please sign in to comment.