Search in sources :

Example 76 with JSONObject

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

the class MapComposer method loadDistributionMap.

private void loadDistributionMap(String lsids, String wkt) {
    String newWkt = wkt;
    if (CommonData.WORLD_WKT.equals(newWkt)) {
        newWkt = null;
    }
    //test for a valid lsid match
    String[] wmsNames = CommonData.getSpeciesDistributionWMS(lsids);
    String[] spcode = CommonData.getSpeciesDistributionSpcode(lsids);
    MapLayer ml;
    JSONParser jp = new JSONParser();
    if (wmsNames.length > 0 && (newWkt == null || newWkt.equals(CommonData.WORLD_WKT))) {
        //add all
        for (int i = 0; i < wmsNames.length; i++) {
            if (getMapLayerWMS(wmsNames[i]) == null) {
                //map this layer with its recorded scientific name
                try {
                    JSONObject jo = ((JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/distribution/" + spcode[i] + "?nowkt=true")));
                    String scientific = jo.get(StringConstants.SCIENTIFIC).toString();
                    String distributionAreaName = jo.get("area_name").toString();
                    String layerName = getNextAreaLayerName(scientific);
                    String html = Util.getMetadataHtmlForDistributionOrChecklist(spcode[i], null, layerName);
                    ml = addWMSLayer(layerName, getNextAreaLayerName(distributionAreaName), wmsNames[i], 0.35f, html, null, LayerUtilitiesImpl.WKT, null, null);
                    ml.setSPCode(spcode[i]);
                    setupMapLayerAsDistributionArea(ml);
                } catch (Exception e) {
                    LOGGER.error("failed to parse for distribution: " + spcode[i]);
                }
            }
        }
    } else if (wmsNames.length > 0 && newWkt != null && !newWkt.equals(CommonData.WORLD_WKT)) {
        String url = CommonData.getLayersServer() + "/distributions";
        try {
            HttpClient client = new HttpClient();
            PostMethod post = new PostMethod(url);
            post.addParameter(StringConstants.WKT, newWkt);
            post.addParameter(StringConstants.LSIDS, lsids);
            post.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
            int result = client.executeMethod(post);
            if (result == 200) {
                String txt = post.getResponseBodyAsString();
                JSONArray ja = (JSONArray) jp.parse(txt);
                List<String> found = new ArrayList();
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    if (jo.containsKey(StringConstants.WMSURL)) {
                        found.add(jo.get(StringConstants.WMSURL).toString());
                    }
                }
                for (int i = 0; i < wmsNames.length; i++) {
                    if (getMapLayerWMS(wmsNames[i]) == null) {
                        String scientific = ((JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/distribution/" + spcode[i] + "?nowkt=true"))).get(StringConstants.SCIENTIFIC).toString();
                        String layerName = getNextAreaLayerName(scientific + " area " + (i + 1));
                        String html = Util.getMetadataHtmlForDistributionOrChecklist(spcode[i], null, layerName);
                        ml = addWMSLayer(layerName, getNextAreaLayerName("Expert distribution: " + scientific), found.get(i), 0.35f, html, null, LayerUtilitiesImpl.WKT, null, null);
                        ml.setSPCode(spcode[i]);
                        setupMapLayerAsDistributionArea(ml);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("error posting distributions: " + url);
        }
    }
    openChecklistSpecies(lsids, newWkt, true);
}
Also used : JSONObject(org.json.simple.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HasMapLayer(au.org.emii.portal.menu.HasMapLayer) MapLayer(au.org.emii.portal.menu.MapLayer) HttpClient(org.apache.commons.httpclient.HttpClient) JSONArray(org.json.simple.JSONArray) XmlArrayList(com.thoughtworks.xstream.persistence.XmlArrayList) JSONParser(org.json.simple.parser.JSONParser) XmlArrayList(com.thoughtworks.xstream.persistence.XmlArrayList) List(java.util.List) ParseException(org.json.simple.parser.ParseException)

Example 77 with JSONObject

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

the class SpeciesListUtil method getLists.

private static JSONObject getLists(String user, Integer offset, Integer max, String sort, String order, String searchTerm) {
    StringBuilder sb = new StringBuilder(CommonData.getSpeciesListServer());
    sb.append("/ws/speciesList");
    sb.append("?user=");
    if (user != null && !"guest@ala.org.au".equals(user)) {
        sb.append(user);
    }
    if (offset != null) {
        sb.append("&offset=").append(offset.toString());
    }
    if (max != null) {
        sb.append("&max=").append(max);
    }
    if (sort != null) {
        sb.append("&sort=").append(sort);
    }
    if (order != null) {
        sb.append("&order=").append(order);
    }
    if (searchTerm != null) {
        //sb.append("&listName=ilike:%25" + searchTerm + "%25");
        try {
            sb.append("&q=" + URLEncoder.encode(searchTerm, "UTF-8"));
        } catch (Exception e) {
        }
    }
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(sb.toString());
    try {
        //permission to get private lists
        if (user != null) {
            get.addRequestHeader(StringConstants.COOKIE, "ALA-Auth=" + java.net.URLEncoder.encode(user, StringConstants.UTF_8));
        }
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.TEXT_PLAIN);
        int result = client.executeMethod(get);
        if (result == 200) {
            String rawJSON = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            return (JSONObject) jp.parse(rawJSON);
        } else {
            LOGGER.error("Unable to retrieve species list. " + result + " > " + get.getResponseBodyAsString());
        }
    } catch (Exception e) {
        LOGGER.error("Error retrieving public species list.", e);
    } finally {
        get.releaseConnection();
    }
    return null;
}
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 78 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 79 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 80 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)

Aggregations

JSONObject (org.json.simple.JSONObject)961 Test (org.junit.Test)312 JSONArray (org.json.simple.JSONArray)232 JSONParser (org.json.simple.parser.JSONParser)164 Map (java.util.Map)97 HashMap (java.util.HashMap)91 IOException (java.io.IOException)84 ArrayList (java.util.ArrayList)75 ParseException (org.json.simple.parser.ParseException)60 HttpClient (org.apache.commons.httpclient.HttpClient)39 File (java.io.File)38 List (java.util.List)38 BlockchainTest (org.xel.BlockchainTest)34 GetMethod (org.apache.commons.httpclient.methods.GetMethod)30 HttpURLConnection (java.net.HttpURLConnection)27 Tuple (org.apache.storm.tuple.Tuple)23 Transaction (org.xel.Transaction)23 APICall (org.xel.http.APICall)23 InputStreamReader (java.io.InputStreamReader)22 WriterConfiguration (org.apache.metron.common.configuration.writer.WriterConfiguration)22