EditConnection.java [src/edit] Revision: 33d29b967cf0533bfd3a2bf80268569a6f6d005b  Date: Tue Mar 16 10:43:17 MDT 2021
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edit;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.ReadContext;
import csip.Client;
import csip.Config;
import csip.LRUCache;
import csip.SessionLogger;
import java.util.logging.Level;

/**
 * Rest service access to Queries (Ecological Dynamics Interpretive Tool).
 * (https://edit.jornada.nmsu.edu/resources/esd)
 *
 * @author od
 */
public class EditConnection {

  // Queries cache
  static final int EDIT_CACHE_SIZE = Config.getInt("edit.con.cache",
      64);

  // Queries service timeout in sec.
  static final int EDIT_TIMEOUT = Config.getInt("edit.con.timeout",
      5);

  static final String EDIT_URL = Config.getString("edit.url",
      "https://edit.jornada.nmsu.edu");

  // %s (053C) MLRA id
  // e.g. "/services/downloads/esd/053C/class-list.json"
  static final String EDIT_CATALOGS = Config.getString("edit.catalogs",
      "/catalogs/esd/%s/%s");

  // %s (053C) MLRA id
  // e.g. "/services/downloads/esd/053C/class-list.json"
  static final String EDIT_CLASSLIST = Config.getString("edit.classlist",
      "/services/downloads/esd/%s/class-list.json");

  // %s (053C) MLRA id, %s (R053CY010SD) EcoclassID
  // e.g. "/services/models/esd/053C/R053CY010SD/states.json"
  static final String EDIT_STATES_TRANS = Config.getString("edit.statestrans",
      "/services/models/esd/%s/%s/states.json");

  // %s (053C) MLRA id, %s (R053CY010SD) EcoclassID
  // e.g. "/services/plant-community-tables/esd/053C/R053CY010SD/1/1/1/annual-production.json"
  static final String EDIT_PCANNUALPROD = Config.getString("edit.pcannualprod",
      "/services/models/esd/%s/%s/plant-community-tables.json?table=annual%%20production");

  static final Configuration CONF = Configuration.defaultConfiguration()
      .addOptions(Option.SUPPRESS_EXCEPTIONS);

  // <url> -> <response>
  final LRUCache<String, ReadContext> cache = new LRUCache<>(EDIT_CACHE_SIZE);

  SessionLogger log;


  public EditConnection(SessionLogger log) {
    this.log = log;
  }


  public EditConnection() {
    this(null);
  }


  ReadContext callEdit(String url) throws Exception {
    ReadContext resp;
    // System.out.println("Calling : " + url);
    synchronized (cache) {
      resp = cache.get(url);
    }
    if (resp != null) {
      if (log != null && log.isLoggable(Level.INFO)) {
        log.info("GET from Cache: " + url + ": " + resp);
      }
      return resp;
    }
    try (Client c = new Client(EDIT_TIMEOUT)) {
      String r = c.doGET(url);
      String err = JsonPath.using(CONF).parse(r).read("$.error.message");
      if (err != null) {
        throw new Exception("Error calling '" + url + "': " + r);
      }
      resp = JsonPath.parse(r);
      synchronized (cache) {
        cache.put(url, resp);
      }
      if (log != null && log.isLoggable(Level.INFO)) {
        log.info("GET from Edit: " + url + ": " + resp);
      }
      return resp;
    }
  }


  public ReadContext fetchEcoClassList(String ecId) throws Exception {
    return callEdit(String.format(EDIT_URL + EDIT_CLASSLIST, Utils.getMLRA(ecId)));
  }


  public ReadContext fetchEcoSystemStates(String ecId) throws Exception {
    return callEdit(String.format(EDIT_URL + EDIT_STATES_TRANS, Utils.getMLRA(ecId), ecId));
  }


  public ReadContext fetchAnnualProd(String ecId) throws Exception {
    return callEdit(String.format(EDIT_URL + EDIT_PCANNUALPROD, Utils.getMLRA(ecId), ecId));
  }

}