Skip to content

Commit

Permalink
Add routes, controller, and view template for dataset description
Browse files Browse the repository at this point in the history
See #134
  • Loading branch information
fsteeg committed Sep 19, 2018
1 parent 99cbf9f commit 804fed9
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 5 deletions.
31 changes: 26 additions & 5 deletions app/controllers/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,36 @@ private Result result(String content, String contentType) {

public Result context() {
response().setHeader("Access-Control-Allow-Origin", "*");
return ok(readFile(config("context.file"))).as(config("context.content"));
}

/**
* See https://www.w3.org/TR/dwbp/#metadata
*
* @param format
* The format ("jsonld" or "")
*
* @return JSON-LD dataset metadata
*/
public Result dataset(String format) {
response().setHeader("Access-Control-Allow-Origin", "*");
Format responseFormat = Accept.formatFor(format, request().acceptedTypes());
String content = readFile(config("dataset.file"));
return responseFormat.queryParamString.startsWith("json") ? //
ok(content).as(config("dataset.content")) : //
ok(views.html.dataset.render(Json.parse(content)));
}

private String readFile(String name) {
try {
File file = env.getFile(config("context.file"));
File file = env.getFile(name);
Path path = Paths.get(file.getAbsolutePath());
return ok(Files.readAllLines(path).stream().collect(Collectors.joining("\n")))
.as(config("context.content"));
String content = Files.readAllLines(path).stream().collect(Collectors.joining("\n"));
return content;
} catch (IOException e) {
e.printStackTrace();
Logger.error("Couldn't get: " + name, e);
return null;
}
return null;
}

public Result gnd(String id) {
Expand Down
50 changes: 50 additions & 0 deletions app/views/dataset.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@* Copyright 2014-2018 Fabian Steeg, hbz. Licensed under the GPLv2 *@

@(json: com.fasterxml.jackson.databind.JsonNode)

@import helper._

@main("", "Dataset: lobid-gnd") {
<h3>Datenbeschreibung<small><a title="Beschreibung als JSON-LD anzeigen" href='@routes.HomeController.dataset("json")'><img class='json-ld-icon' src='@routes.Assets.versioned("images/json-ld.png")'></a></small></h3>
<table class="table table-striped">
<tr><th width="20%"/><th width="80%"/></tr>
<tr>
<td>Titel</td>
<td><a href='@json.get("id").asText()'>
@json.get("name").get("de").asText() &mdash; @json.get("alternateName").get("de").asText()
</a></td>
</tr>
<tr><td>Beschreibung</td><td>@Html(json.get("description").get("de").asText())</td></tr>
<tr>
<td>Schlagwörter</td>
<td>@json.get("keywords").toString().replaceAll("\",\"", ", ").replaceAll("[\"\\[\\]]", "")</td>
</tr>
@defining(json.get("publisher")) { pub =>
<tr>
<td>Veröffentlicht von</td>
<td><a href='@pub.get("id").asText()'>
@pub.get("name").get("de").asText() &mdash; @pub.get("alternateName").elements().next().asText()
</a></td>
</tr>
}
<tr><td>Basiert auf</td><td><a href='@json.get("isBasedOn").get("url").asText()'>@json.get("isBasedOn").get("name").get("de").asText()</a></td></tr>
<tr><td>Veröffentlicht am</td><td>@json.get("datePublished").asText()</td></tr>
<tr><td>Sprache</td><td>@json.get("inLanguage").elements().next().asText()</td></tr>
<tr><td>Ergänzungsfrequenz</td><td>@json.get("accrualPeriodicity").get("name").get("de").asText()</td></tr>
<tr><td>Kontakt</td><td><a href='@json.get("contactPoint").asText()'>@json.get("contactPoint").asText()</a></td></tr>
</table>
<h3>Datendistribution</h3>
@defining(json.get("distribution").elements().next()) { dist =>
<table class="table table-striped">
<tr><th width="20%"/><th width="80%"/></tr>
<tr><td>Titel</td><td>@dist.get("name").get("de").asText()</td></tr>
<tr><td>Beschreibung</td><td>@Html(dist.get("description").get("de").asText())</td></tr>
<tr><td>URL</td><td><a href='@dist.get("url").asText()'>@dist.get("url").asText()</a></td></tr>
<tr><td>Lizenz</td><td><a href='@dist.get("license").asText()'>@dist.get("license").asText()</a></td></tr>
<tr><td>Medientypen</td><td>@dist.get("encodingFormat").toString().replaceAll("\",\"", ", ").replaceAll("[\"\\[\\]]", "")</td></tr>
</table>
}
<script type="application/ld+json">
@Html(json.toString)
</script>
}
3 changes: 3 additions & 0 deletions app/views/main.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
<li @if(title=="lobid-gnd - API"){class="active"}>
<a href="@routes.HomeController.api()" title="Programmierschnittstelle">Programmierschnittstelle (API)</a>
</li>
<li @if(title=="Dataset: lobid-gnd"){class="active"}>
<a href='@routes.HomeController.dataset("")'>Datenbeschreibung</a>
</li>
<li class="divider"></li>
<li class="dropdown-header">Kontakt</li>
<li><a href="mailto:semweb@@hbz-nrw.de?subject=Feedback%20zu%20lobid-gnd,%20aktuelle%20URL%[email protected]()">Feedback zur aktuellen Seite</a></li>
Expand Down
5 changes: 5 additions & 0 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ context {
prod: "http://lobid.org/gnd/context.jsonld"
}

dataset {
file: "conf/dataset.jsonld"
content: "application/ld+json; charset=utf-8"
}

topLevelTypes : [
AuthorityResource,
CorporateBody,
Expand Down
3 changes: 3 additions & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ GET /gnd/api controllers.HomeController.api

GET /gnd/context.jsonld controllers.HomeController.context()

GET /gnd/dataset.jsonld controllers.HomeController.dataset(format="json")
GET /gnd/dataset controllers.HomeController.dataset(format?="")

GET /gnd/search controllers.HomeController.search(q ?= "", filter ?= "", from: Int ?= 0, size: Int ?= 10, format ?= null)

GET /gnd/:id.:format controllers.HomeController.authorityDotFormat(id, format)
Expand Down
6 changes: 6 additions & 0 deletions public/stylesheets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,10 @@ figcaption {
max-width: 100%;
max-height: 300px;
float: right;
}

.json-ld-icon {
margin-left: 1px;
float:right;
height: 1.4em
}
7 changes: 7 additions & 0 deletions test/controllers/AcceptIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public class AcceptIntegrationTest extends IndexTest {
public static Collection<Object[]> data() {
// @formatter:off
return Arrays.asList(new Object[][] {
// service meta data: context and dataset description
{ fakeRequest(GET, "/gnd/context.jsonld"), /*->*/ "application/ld+json" },
{ fakeRequest(GET, "/gnd/dataset.jsonld"), /*->*/ "application/ld+json" },
{ fakeRequest(GET, "/gnd/dataset"), /*->*/ "application/ld+json" },
{ fakeRequest(GET, "/gnd/dataset").header("Accept", "text/html"), /*->*/ "text/html" },
{ fakeRequest(GET, "/gnd/dataset").header("Accept", "application/ld+json"), /*->*/ "application/ld+json" },
{ fakeRequest(GET, "/gnd/dataset").header("Accept", "application/json"), /*->*/ "application/ld+json" },
// search, default format: JSON
{ fakeRequest(GET, "/gnd/search?q=*"), /*->*/ "application/json" },
{ fakeRequest(GET, "/gnd/search?q=*").header("Accept", "*/*"), /*->*/ "application/json" },
Expand Down

0 comments on commit 804fed9

Please sign in to comment.