Skip to content

Commit

Permalink
Merge #1810 from remote-tracking branch 'origin/1809-rpb'
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed Jun 29, 2023
2 parents de877b0 + b10f6ec commit d741206
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 21 deletions.
35 changes: 27 additions & 8 deletions web/app/controllers/resources/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,24 @@ public static Promise<Result> resource(final String id, String format) {
if (cachedResult != null)
return cachedResult;
Promise<Result> promise = Promise.promise(() -> {
JsonNode result =
new Search.Builder().build().getResource(id).getResult();
if (result == null) {
JsonNode result = new Search.Builder().build().getResource(id).getResult();
if (result == null) { // direct access failed, try to redirect to almaMmsId
String movedTo = idSearchResult(id);
Logger.debug(
"Could not get resource via index ID, trying to redirect '{}' to almaMmsId: '{}'",
id, movedTo);
if (movedTo != null) {
return movedPermanently(routes.Application.resource(movedTo, format));
}
}
if (result == null) { // no almaMmsId to redirect to, try ID query w/o redirect
QueryBuilder idQuery = new Queries.IdQuery().build(id);
result = new Search.Builder().query(idQuery).build().queryResources()
.getResult().get(0);
Logger.debug(
"Could not get resource via index ID or redirect, trying query '{}', result: '{}'",
idQuery, result);
}
boolean htmlRequested =
responseFormat.equals(Accept.Format.HTML.queryParamString);
if (htmlRequested) {
Expand Down Expand Up @@ -508,8 +518,6 @@ static String idSearchResult(final String id) {
JsonNode result;
String idSearch = String.format("(hbzId:%s OR zdbId:(%s OR %s))", id, id,
id.replace("ZDB-", ""));
Logger.debug("Could not get resource via index ID, trying search: '{}'",
idSearch);
QueryBuilder idQuery = new Queries.Builder().q(idSearch).build();
result = new Search.Builder().query(idQuery).size(1).build()
.queryResources().getResult();
Expand Down Expand Up @@ -832,14 +840,25 @@ public static Promise<Result> showStars(String format, String ids) {
List<JsonNode> json = (List<JsonNode>) cachedJson;
return Promise.pure(ok(stars.render(starredIds, json, format)));
}
List<JsonNode> vals = starredIds.stream()
.map(id -> new Search.Builder().build().getResource(id).getResult())
.collect(Collectors.toList());
List<JsonNode> vals =
starredIds.stream().map(id -> jsonFor(id)).collect(Collectors.toList());
uncache(starredIds);
Cache.set(cacheKey, vals, ONE_DAY);
return Promise.pure(ok(stars.render(starredIds, vals, format)));
}

/**
* @param id The ID to get the JSON data for
* @return The resource JSON for the given ID
*/
public static JsonNode jsonFor(String id) {
JsonNode getResult =
new Search.Builder().build().getResource(id).getResult();
return getResult != null ? getResult
: new Search.Builder().query(new Queries.IdQuery().build(id)).build()
.queryResources().getResult().get(0);
}

/**
* @param ids The ids of the resources to unstar, or empty string to clear all
* @return If ids is empty: an OK result to confirm deletion of all starred
Expand Down
5 changes: 1 addition & 4 deletions web/app/controllers/resources/Queries.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,7 @@ private static String normalizedLobidResourceIdQueryString(
if (normalizedQueryString.matches("\"?\\d.*\"?")) { // thus: isbn
normalizedQueryString = normalizedQueryString.replaceAll("-", "");
}
final String hbzId = "\\p{L}+\\d+(-.+)?";
return normalizedQueryString.matches(hbzId)
? "http://lobid.org/resources/" + normalizedQueryString
: normalizedQueryString;
return normalizedQueryString;
}
}

Expand Down
4 changes: 3 additions & 1 deletion web/app/views/details.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ <h1>@((doc\"title").asOpt[String].getOrElse(q))@for(corporateBodyForTitle <- (do
@defining(!(doc\\"containedIn").isEmpty && (doc\"type").toString.contains("Article")){superordination =>
<div class="col-md-@if(items.isEmpty && !doc.toString.contains("fulltextOnline") && !superordination){12} else {8}">
<dl>
@defining((doc\"almaMmsId").asOpt[String].getOrElse((doc\"hbzId").asOpt[String].getOrElse(q))){ id =>
@defining((doc\"almaMmsId").asOpt[String].orElse((doc\"hbzId").asOpt[String]).orElse((doc\"rpbId").asOpt[String]).getOrElse(q)){ id =>
<dt>@tags.star_button(id) Titeldetails:
<small style='float:right'>
@if((doc\"almaMmsId").asOpt[String].isDefined||(doc\"hbzId").asOpt[String].isDefined){
<a title="Quelldaten anzeigen" href='@if((doc\"almaMmsId").asOpt[String].isDefined){@Application.CONFIG.getString("mrcx.api")@id}else{@Application.CONFIG.getString("hbz01.api")/@id}'><small>&#12296;M&#12297;</small></a>
}
<a title="JSON-LD-Indexdaten anzeigen" href='@resources.routes.Application.resourceDotFormat(id, "json")'><img class='json-ld-icon' src='@routes.Assets.at("images/json-ld.png")'></a>
</small>
</dt>
Expand Down
8 changes: 6 additions & 2 deletions web/app/views/query.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
if(q.contains("hasSuperordinate")) {select(("Band","Bände"))} else if (q.contains("containedIn")) {select(("Beitrag","Beiträge"))} else {""}
}

@idFrom(doc: JsValue) = @{
(doc\"almaMmsId").asOpt[String].orElse((doc\"hbzId").asOpt[String]).orElse((doc\"rpbId").asOpt[String]).getOrElse("")
}

@main(q, "lobid-resources - Ergebnisliste", rss = Some(resources.routes.Application.query(q,agent,name,subject,id,publisher,issued,medium,from,size,owner,t,word=word,format="rss").toString)) {
@if(Seq(name, id, publisher).exists(!_.isEmpty)){ @* advanced search, not shown in facets *@
@tags.search_advanced("Suche aktualisieren", q, agent, name, subject, id, publisher, issued, sortParam)
Expand Down Expand Up @@ -97,7 +101,7 @@
zeige @(from+1) bis @(Math.min(from+hits.size,from+size)):
</div>
<div class="col-md-1" style="text-align: right; padding-right: 4px;">
@defining((Json.parse(result)\\"hbzId").map(_.as[String]).mkString(",")) { ids =>
@defining((for(doc <- Json.parse(result).as[Seq[JsValue]]; id = idFrom(doc)) yield id).mkString(",")) { ids =>
@if(ids.split(",").forall(Application.currentlyStarred().split(" ").contains)) {
<a href='@resources.routes.Application.clearStars(ids)' title="Alle Titel dieser Seite von Merkliste entfernen"><span class="glyphicon glyphicon-star"></span></a>
} else {
Expand All @@ -115,7 +119,7 @@
<th style="width: 5%; text-align: right"></th>
<th style="width: 5%; text-align: right"></th>
</tr>
@for((doc,i) <- hits; id = (doc\"almaMmsId").asOpt[String].getOrElse((doc\"hbzId").as[String])) {
@for((doc,i) <- hits; id = idFrom(doc)) {
@tags.query_result_short(id,doc,i-1)
}
</table>
Expand Down
2 changes: 1 addition & 1 deletion web/app/views/rss.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<link>@controllers.resources.Application.CONFIG.getString("host")@uri</link>
<description>hbz union catalogue query @query</description>
@for((doc, i) <- hits;
ids <- (doc \ "almaMmsId").asOpt[JsValue].getOrElse(doc \ "hbzId").asOpt[JsValue];
ids <- (doc \ "almaMmsId").asOpt[JsValue].orElse((doc \ "hbzId").asOpt[JsValue]).orElse((doc \ "rpbId").asOpt[JsValue]);
id = ids.asOpt[Seq[JsValue]].getOrElse(Seq(ids))(0).as[String];
dateCreated = ((doc \ "describedBy" \ "dateCreated").asOpt[String].getOrElse((doc \ "describedBy" \ "resultOf" \ "object" \ "dateCreated").as[String]))
) {
Expand Down
3 changes: 2 additions & 1 deletion web/app/views/stars.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
@import play.api.Play.current
@import play.api.libs.json.Json
@import controllers.resources.Search
@import controllers.resources.Application

@entries() = {
@for((id,i) <- ids.zipWithIndex; doc = Json.parse(vals(i).toString())) {
@if(f=="details"){@tags.result_doc(Json.parse(new Search.Builder().build().getResource(id).getResult().toString))} else {@tags.query_result_short(id,doc,i,ids)}
@if(f=="details"){@tags.result_doc(Json.parse(Application.jsonFor(id).toString))} else {@tags.query_result_short(id,doc,i,ids)}
}
}

Expand Down
8 changes: 4 additions & 4 deletions web/app/views/tags/result_doc.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
}

@with_icon(label: String, property: String, fullField: String) = {
@if((doc\property).get.asOpt[JsValue].isDefined) {
@if((doc\property).asOpt[JsValue].isDefined) {
@defining(if(property != "type") { nestedIds(property) } else { singleOrMultiString(property) }){ v =>
<tr><td>@label</td><td><span class="@Lobid.facetIcon(v,fullField)"></span> @Lobid.facetLabel(v,fullField,"")</td></tr>
}
Expand All @@ -91,8 +91,8 @@
}}}
}

@lobidUrl(hbzId: String) = @{
"http://lobid.org/resources/" + hbzId + "#!"
@lobidUrl(doc: JsValue) = @{
"http://lobid.org/resources/" + (doc\"almaMmsId").asOpt[String].orElse((doc\"hbzId").asOpt[String]).orElse((doc\"rpbId").asOpt[String]).getOrElse("") + "#!"
}

@lobid2_part_of(field: String, subField: String) = {
Expand Down Expand Up @@ -196,7 +196,7 @@
@result_field("In", "containedIn", doc, TableRow.LINKS)

@lobid2_part_of("isPartOf", "hasSuperordinate")
@defining(lobidUrl((doc \ "hbzId").asOpt[String].getOrElse(""))){ id =>
@defining(lobidUrl(doc)){ id =>
@subordinated("isPartOf.hasSuperordinate.id", id, "Bände", ("zugehöriger Band", "zugehörige Bände"))
@subordinated("containedIn.id", id, "Enthält", ("Beitrag", "Beiträge"))
}
Expand Down

0 comments on commit d741206

Please sign in to comment.