Skip to content

Commit

Permalink
Cache table "isil2opac_hbzid"
Browse files Browse the repository at this point in the history
Don't make a URL lookup to get the concordance every time an item is shown.
Load thie concordance from a gitsubmodule table and cache it as HashMap.

Follows #1893.
  • Loading branch information
dr0i committed Sep 26, 2023
1 parent 078eac9 commit e018b4a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
21 changes: 21 additions & 0 deletions web/app/Global.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* Copyright 2017, hbz. Licensed under the EPL 2.0 */

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import controllers.resources.Lobid;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
Expand All @@ -18,6 +23,7 @@
import play.Application;
import play.GlobalSettings;
import play.Logger;
import play.libs.Json;

/**
* Application global settings.
Expand Down Expand Up @@ -48,6 +54,8 @@ public class Global extends GlobalSettings {
private LocalIndex localIndex = null;
private Client client = null;

private static final String ISIL2OPAC ="../link-templates/isil2opac_hbzid.json";

@Override
public void onStart(Application app) {
super.onStart(app);
Expand All @@ -71,6 +79,7 @@ else if (!app.isTest()) {
if (client != null) {
Search.elasticsearchClient = client;
}
Lobid.isil2opac = loadIsil2Opac();
}

@Override
Expand All @@ -96,4 +105,16 @@ private static void addHosts(TransportClient client) {
}
}
}

private static JsonNode loadIsil2Opac() {
JsonNode node = null;
try (InputStream stream =
new FileInputStream(ISIL2OPAC)) {
node = Json.parse(stream);
}
catch (IOException e) {
Logger.error("Could not create OPAC URL", e);
}
return node;
}
}
13 changes: 3 additions & 10 deletions web/app/controllers/resources/Lobid.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

package controllers.resources;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -51,6 +48,7 @@ public class Lobid {
static final String ORGS_ROOT = Application.CONFIG.getString("orgs.api");
/** Timeout for API calls in milliseconds. */
public static final int API_TIMEOUT = 50000;
public static JsonNode isil2opac = null;

/**
* @param url The URL to call
Expand Down Expand Up @@ -391,21 +389,16 @@ private static void mapIsilsToUris(JsonNode items,
* @return The OPAC URL for the given item, or null
*/
public static String opacUrl(String itemUri) {
try (InputStream stream =
new URL(Application.CONFIG.getString("isil2opac_hbzid")).openStream()) {
JsonNode json = Json.parse(stream);

String[] hbzId_isil_sig =
itemUri.substring(itemUri.indexOf("items/") + 6).split(":");
String hbzId = hbzId_isil_sig[0];
String isil = hbzId_isil_sig[1];
Logger.debug("From item URI {}, got ISIL {} and HBZ-ID {}", itemUri, isil,
hbzId);
JsonNode urlTemplate = json.get(isil);
JsonNode urlTemplate = isil2opac.get(isil);
if (urlTemplate != null)
return urlTemplate.asText().replace("{hbzid}", hbzId);
} catch (IOException e) {
Logger.error("Could not create OPAC URL", e);
}
return null;
}

Expand Down
2 changes: 0 additions & 2 deletions web/conf/resources.conf_template
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ hbz01.api="http://lobid.org/hbz01"
mrcx.api="https://lobid.org/marcxml/"
orgs.api="http://lobid.org/organisations/"

isil2opac_hbzid = "https://raw.githubusercontent.com/hbz/link-templates/master/isil2opac_hbzid.json"

webhook = {
alma = {
update = {
Expand Down

0 comments on commit e018b4a

Please sign in to comment.