Search in sources :

Example 31 with JSONObject

use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.

the class Util method getMetadataHtmlForAreaChecklist.

public static String getMetadataHtmlForAreaChecklist(String spcode, String layerName) {
    if (spcode == null) {
        return null;
    }
    try {
        int count = CommonData.getSpeciesChecklistCountByWMS(CommonData.getSpeciesChecklistWMSFromSpcode(spcode)[1]);
        String url = CommonData.getLayersServer() + "/checklist/" + spcode;
        String jsontxt = Util.readUrl(url);
        if (jsontxt == null || jsontxt.length() == 0) {
            return null;
        }
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(jsontxt);
        String html = "Checklist area\n";
        html += "<table class='md_table'>";
        String lastClass = "";
        if (layerName != null && jo.containsKey(StringConstants.GEOM_IDX)) {
            html += "<tr class='" + lastClass + "'><td class='md_th'>Number of scientific names: </td><td class='md_spacer'/><td class='md_value'><a href='#' onClick='openAreaChecklist(\"" + jo.get(StringConstants.GEOM_IDX) + "\")'>" + count + "</a></td></tr>";
        } else {
            html += "<tr class='" + lastClass + "'><td class='md_th'>Number of scientific names: </td><td class='md_spacer'/><td class='md_value'>" + count + "</td></tr>";
        }
        lastClass = lastClass.length() == 0 ? "md_grey-bg" : "";
        if (jo != null && jo.containsKey(StringConstants.METADATA_U)) {
            html += "<tr class='" + lastClass + "'><td class='md_th'>Metadata link: </td><td class='md_spacer'/><td class='md_value'><a target='_blank' href='" + jo.get(StringConstants.METADATA_U) + "'>" + jo.get(StringConstants.METADATA_U) + "</a></td></tr>";
            lastClass = lastClass.length() == 0 ? "md_grey-bg" : "";
        }
        if (jo != null && jo.containsKey(StringConstants.AREA_NAME)) {
            html += "<tr class='" + lastClass + "'><td class='md_th'>Area name: </td><td class='md_spacer'/><td class='md_value'>" + jo.get(StringConstants.AREA_NAME) + "</td></tr>";
            lastClass = lastClass.length() == 0 ? "md_grey-bg" : "";
        }
        if (jo != null && jo.containsKey(StringConstants.AREA_KM)) {
            html += "<tr class='" + lastClass + "'><td class='md_th'>Area sq km: </td><td class='md_spacer'/><td class='md_value'>" + jo.get(StringConstants.AREA_KM) + "</td></tr>";
            lastClass = lastClass.length() == 0 ? "md_grey-bg" : "";
        }
        try {
            if (jo != null && jo.containsKey(StringConstants.PID) && jo.containsKey(StringConstants.AREA_NAME)) {
                String fid;
                fid = Util.getStringValue(null, StringConstants.FID, Util.readUrl(CommonData.getLayersServer() + "/object/" + jo.get(StringConstants.PID)));
                String spid = Util.getStringValue("\"id\":\"" + fid + "\"", "spid", Util.readUrl(CommonData.getLayersServer() + "/fields"));
                if (spid != null) {
                    String layerInfoUrl = CommonData.getLayersServer() + "/layers/view/more/" + spid;
                    html += "<tr class='" + lastClass + "'><td class='md_th'>More about this area: </td><td class='md_spacer'/><td class='md_value'><a target='_blank' href='" + layerInfoUrl + "'>" + layerInfoUrl + "</a></td></tr>";
                }
            }
        } catch (Exception e) {
            LOGGER.error("error building metadata HTML", e);
        }
        html += "</table>";
        return html;
    } catch (Exception e) {
        LOGGER.error("error building html metadata for distributions area spcode=" + spcode, e);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) ParseException(com.vividsolutions.jts.io.ParseException)

Example 32 with JSONObject

use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.

the class LsidCountsDynamic method count.

private static int count(String lsid) {
    HttpClient client = new HttpClient();
    String url = null;
    try {
        url = CommonData.getBiocacheServer() + "/occurrences/search?facet=off&pageSize=0&fq=" + URLEncoder.encode("geospatial_kosher:*", StringConstants.UTF_8) + "&q=" + URLEncoder.encode("lsid:" + lsid, StringConstants.UTF_8) + CommonData.getBiocacheQc();
        LOGGER.debug(url);
        GetMethod get = new GetMethod(url);
        client.getHttpConnectionManager().getParams().setSoTimeout(30000);
        client.executeMethod(get);
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(get.getResponseBodyAsString());
        return ((Long) jo.get("totalRecords")).intValue();
    } catch (Exception e) {
        LOGGER.error("error getting LSID count for : " + url, e);
    }
    return -1;
}
Also used : JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser)

Example 33 with JSONObject

use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.

the class Util method getDistributionOrChecklist.

public static String[] getDistributionOrChecklist(String spcode) {
    try {
        StringBuilder sbProcessUrl = new StringBuilder();
        sbProcessUrl.append("/distribution/").append(spcode).append("?nowkt=true");
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(CommonData.getLayersServer() + sbProcessUrl.toString());
        LOGGER.debug(CommonData.getLayersServer() + sbProcessUrl.toString());
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
        int result = client.executeMethod(get);
        if (result == 200) {
            String txt = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONObject jo = (JSONObject) jp.parse(txt);
            if (jo == null) {
                return new String[0];
            } else {
                String[] output = new String[14];
                String scientific = jo.containsKey(StringConstants.SCIENTIFIC) ? jo.get(StringConstants.SCIENTIFIC).toString() : "";
                String auth = jo.containsKey(StringConstants.AUTHORITY) ? jo.get(StringConstants.AUTHORITY).toString() : "";
                String common = jo.containsKey(StringConstants.COMMON_NAM) ? jo.get(StringConstants.COMMON_NAM).toString() : "";
                String family = jo.containsKey(StringConstants.FAMILY) ? jo.get(StringConstants.FAMILY).toString() : "";
                String genus = jo.containsKey(StringConstants.GENUS) ? jo.get(StringConstants.GENUS).toString() : "";
                String name = jo.containsKey(StringConstants.SPECIFIC_N) ? jo.get(StringConstants.SPECIFIC_N).toString() : "";
                String min = jo.containsKey(StringConstants.MIN_DEPTH) ? jo.get(StringConstants.MIN_DEPTH).toString() : "";
                String max = jo.containsKey(StringConstants.MAX_DEPTH) ? jo.get(StringConstants.MAX_DEPTH).toString() : "";
                String md = jo.containsKey(StringConstants.METADATA_U) ? jo.get(StringConstants.METADATA_U).toString() : "";
                String lsid = jo.containsKey(StringConstants.LSID) ? jo.get(StringConstants.LSID).toString() : "";
                String areaName = jo.containsKey(StringConstants.AREA_NAME) ? jo.get(StringConstants.AREA_NAME).toString() : "";
                String areaKm = jo.containsKey(StringConstants.AREA_KM) ? jo.get(StringConstants.AREA_KM).toString() : "";
                String dataResourceId = jo.containsKey(StringConstants.DATA_RESOURCE_UID) ? jo.get(StringConstants.DATA_RESOURCE_UID).toString() : "";
                output[0] = spcode;
                output[1] = scientific;
                output[2] = auth;
                output[3] = common;
                output[4] = family;
                output[5] = genus;
                output[6] = name;
                output[7] = min;
                output[8] = max;
                output[9] = md;
                output[10] = lsid;
                output[11] = areaName;
                output[12] = areaKm;
                output[13] = dataResourceId;
                return output;
            }
        }
    } catch (Exception e) {
        LOGGER.error("error building distributions list", e);
    }
    return new String[0];
}
Also used : JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser) ParseException(com.vividsolutions.jts.io.ParseException)

Example 34 with JSONObject

use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.

the class CommonData method dynamicSpeciesListColumns.

private static String dynamicSpeciesListColumns() {
    StringBuilder sb = new StringBuilder();
    try {
        JSONParser jp = new JSONParser();
        JSONObject threatened = (JSONObject) jp.parse(Util.readUrl(settings.getProperty("species_list_url", "") + "/ws/speciesList/?isThreatened=eq:true&isAuthoritative=eq:true"));
        JSONObject invasive = (JSONObject) jp.parse(Util.readUrl(settings.getProperty("species_list_url", "") + "/ws/speciesList/?isInvasive=eq:true&isAuthoritative=eq:true"));
        JSONObject[] lists = { threatened, invasive };
        for (JSONObject o : lists) {
            if (sb.length() == 0)
                sb.append("Conservation");
            else
                sb.append("|Invasive");
            JSONArray ja = (JSONArray) o.get("lists");
            for (int i = 0; i < ja.size(); i++) {
                sb.append(",").append(((JSONObject) ja.get(i)).get("dataResourceUid"));
            }
        }
    } catch (Exception e) {
        LOGGER.error("failed to get species lists for threatened or invasive species", e);
    }
    return sb.toString();
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) Point(com.vividsolutions.jts.geom.Point)

Example 35 with JSONObject

use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.

the class EnvironmentalList method setupListEntries.

void setupListEntries() {
    listEntries = new ArrayList<ListEntryDTO>();
    JSONArray ja = CommonData.getLayerListJSONArray();
    for (int i = 0; i < ja.size(); i++) {
        JSONObject field = (JSONObject) ja.get(i);
        JSONObject layer = (JSONObject) field.get("layer");
        listEntries.add(new ListEntryDTO(field.get(StringConstants.ID).toString(), field.containsKey(StringConstants.NAME) ? field.get(StringConstants.NAME).toString() : field.get(StringConstants.ID).toString(), layer.containsKey(StringConstants.CLASSIFICATION1) ? layer.get(StringConstants.CLASSIFICATION1).toString() : "", layer.containsKey(StringConstants.CLASSIFICATION2) ? layer.get(StringConstants.CLASSIFICATION2).toString() : "", layer.containsKey(StringConstants.TYPE) ? layer.get(StringConstants.TYPE).toString() : "", layer.containsKey(StringConstants.DOMAIN) ? layer.get(StringConstants.DOMAIN).toString() : "", layer));
    }
    if (includeAnalysisLayers) {
        for (MapLayer ml : mapComposer.getAnalysisLayers()) {
            ListEntryDTO le = null;
            if (ml.getSubType() == LayerUtilitiesImpl.ALOC) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.CLASSIFICATION, "Contextual", null, null);
            } else if (ml.getSubType() == LayerUtilitiesImpl.MAXENT) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.PREDICTION, StringConstants.ENVIRONMENTAL, null, null);
            } else if (ml.getSubType() == LayerUtilitiesImpl.GDM) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.GDM, StringConstants.ENVIRONMENTAL, null, null);
            } else if (ml.getSubType() == LayerUtilitiesImpl.ODENSITY) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.OCCURRENCE_DENSITY, StringConstants.ENVIRONMENTAL, null, null);
            } else if (ml.getSubType() == LayerUtilitiesImpl.SRICHNESS) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.SPECIES_RICHNESS, StringConstants.ENVIRONMENTAL, null, null);
            }
            if (le != null) {
                listEntries.add(le);
            }
        }
    }
    java.util.Collections.sort(listEntries, new Comparator<ListEntryDTO>() {

        @Override
        public int compare(ListEntryDTO e1, ListEntryDTO e2) {
            return (e1.getCatagory1() + " " + e1.getCatagory2() + " " + e1.getDisplayName()).compareTo(e2.getCatagory1() + " " + e2.getCatagory2() + " " + e2.getDisplayName());
        }
    });
}
Also used : ListEntryDTO(au.org.ala.spatial.dto.ListEntryDTO) JSONObject(org.json.simple.JSONObject) MapLayer(au.org.emii.portal.menu.MapLayer) JSONArray(org.json.simple.JSONArray)

Aggregations

JSONObject (org.json.simple.JSONObject)2110 JSONArray (org.json.simple.JSONArray)601 JSONParser (org.json.simple.parser.JSONParser)389 Test (org.junit.Test)341 Test (org.junit.jupiter.api.Test)247 HashMap (java.util.HashMap)214 IOException (java.io.IOException)199 ArrayList (java.util.ArrayList)190 ParseException (org.json.simple.parser.ParseException)171 Map (java.util.Map)140 Date (java.util.Date)74 InputStreamReader (java.io.InputStreamReader)62 List (java.util.List)61 File (java.io.File)60 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)60 URL (java.net.URL)45 HashSet (java.util.HashSet)42 HttpURLConnection (java.net.HttpURLConnection)41 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)41 HttpClient (org.apache.commons.httpclient.HttpClient)41