EditQueries.java [src/edit] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2021, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package edit;

import com.jayway.jsonpath.ReadContext;
import static edit.EditConnection.EDIT_CATALOGS;
import static edit.EditConnection.EDIT_URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * EDIT EditQueries (Ecological Dynamics Interpretive Tool).
 * (https://edit.jornada.nmsu.edu/resources/esd)
 *
 * @author od
 */
public class EditQueries {

  public static String getEcoClassNameFor(ReadContext r, String ecId) throws Exception {
    List<String> names = r.read(String.format("$.ecoclasses[?(@.id == '%s')].name", ecId));
    return names.isEmpty() ? null : names.get(0);
  }


  public static List getAnnualProd(ReadContext resp, int landUse, int state, int community) throws Exception {
//    System.out.println("Query: " + landUse + "/" + state + "/" + community);
    return resp.read(String.format(
        "$.states[?(@.landUse == %d && @.state == %d && @.community == %d)].tables.annualProduction", landUse, state, community));
  }


  public static List getEcoSystemStatesNarratives(ReadContext resp) throws Exception {
    return resp.read(
        "$.states[?(@.type == 'ecosystem state')].narratives");
  }


  public static List getPlantCommunityNarrativesByState(ReadContext resp, int state) throws Exception {
    return resp.read(String.format(
        "$.states[?(@.type == 'plant community' && @.state == %d)].narratives", state));
  }


  public static List getPlantCommunityImagesByPlantCommunity(ReadContext resp, int state, int community) throws Exception {
    return resp.read(String.format(
        "$.states[?(@.type == 'plant community' && @.state == %d && @.community == %d)].images", state, community));
  }


  public static List getCompositionGroups(ReadContext resp) throws Exception {
    return resp.read(String.format("$.rangeComposition.*"));
  }


  public static String getSiteDescrUrl(String ecId) {
    return String.format(EDIT_URL + EDIT_CATALOGS, Utils.getMLRA(ecId), ecId);
  }


  /*
   * {"metadata":{"source":"https:\/\/edit.jornada.nmsu.edu","date":"2022-08-19"},
   * "synonyms":[
   * {"geoUnit":"003X","id":"F003XD805OR","legacyId":"F003XD805OR","synonym":"F006XY805OR","legacy":"No","date":"2020-08-31"},
   * {"geoUnit":"003X","id":"F003XD805OR","legacyId":"F003XD805OR","synonym":"F006XY805OR","legacy":"Yes","date":"2020-08-31"},
   * {"geoUnit":"003X","id":"F003XD806OR","legacyId":"F003XD806OR","synonym":"F006XY806OR","legacy":"No","date":"2020-08-31"},
   * {"geoUnit":"003X","id":"F003XD806OR","legacyId":"F003XD806OR","synonym":"F006XY806OR","legacy":"Yes","date":"2020-08-31"},
   * {"geoUnit":"004A","id":"R004AB406OR","legacyId":"R004AB406OR","synonym":"F004AB406OR","legacy":"No","date":"2020-05-27"},
   * ...
   *
   *
   * 'synonym' is the original id 'legacyId' is the new id
   */
  static List<Map<String, String>> getSynonyms(ReadContext resp, String ecId) throws Exception {
    return resp.read(String.format(
        "$.synonyms[?(@.legacy == 'Yes' && @.synonym == '%s')]", ecId));
  }


  public static String getSynonymFor(ReadContext resp, String ecId) throws Exception {
    Map<String, String> m = getSynonymEntry(resp, ecId);
    if (m != null)
      return m.get("legacyId");
    return ecId;
  }


  public static Map<String, String> getSynonymEntry(ReadContext resp, String ecId) throws Exception {
    List<Map<String, String>> l = getSynonyms(resp, ecId);
    // if there is no synonym, return the original one.
    if (l.isEmpty())
      return null;

    // Sort entries by date if there are multiple modifications.
    // decending order: most recent change will be fist element.
    if (l.size() > 1)
      Collections.sort(l, (Map<String, String> o1, Map<String, String> o2)
          -> (-1) * o1.get("date").compareTo(o2.get("date")));

    // take the first one (most recent change)
    return l.get(0);
  }

}