Search in sources :

Example 96 with JSONParser

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

the class BiocacheQuery method getSolrName.

public String getSolrName() {
    if (solrName != null) {
        return solrName;
    }
    HttpClient client = new HttpClient();
    String url = biocacheServer + QUERY_TITLE_URL + "&q=" + getQ() + getQc() + "&pageSize=0&facet=false";
    LOGGER.debug("Retrieving query metadata: " + url);
    GetMethod get = new GetMethod(url);
    try {
        int result = client.executeMethod(get);
        String response = get.getResponseBodyAsString();
        if (result == 200) {
            JSONParser jp = new JSONParser();
            JSONObject jo = (JSONObject) jp.parse(response);
            if (jo.containsKey("queryTitle")) {
                String title = jo.get("queryTitle").toString();
                //clean default parameter
                title = title.replace(" AND <span>null</span>", "");
                title = title.replace(" AND null", "");
                //clean spans
                int p1 = title.indexOf("<span");
                while (p1 >= 0) {
                    int p2 = title.indexOf('>', p1);
                    int p3 = title.indexOf("</span>", p2);
                    title = title.substring(0, p1) + title.substring(p2 + 1, p3) + (p3 + 7 < title.length() ? title.substring(p3 + 7, title.length()) : "");
                    p1 = title.indexOf("<span");
                }
                solrName = title;
                LOGGER.debug("solrName12=" + solrName);
                return solrName;
            }
        }
    } catch (Exception e) {
        LOGGER.error("error getting solr name for a query: " + url, e);
    }
    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 97 with JSONParser

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

the class BiocacheQuery method getSpeciesClassification.

private Map<String, String> getSpeciesClassification(String lsid) {
    String[] classificationList = { StringConstants.KINGDOM, StringConstants.PHYLUM, StringConstants.CLASS, StringConstants.ORDER, StringConstants.FAMILY, StringConstants.GENUS, StringConstants.SPECIES, StringConstants.SUB_SPECIES };
    Map<String, String> classification = new LinkedHashMap<String, String>();
    String snUrl = "true".equalsIgnoreCase(CommonData.getSettings().getProperty("new.bie")) ? CommonData.getBieServer() + BIE_SPECIES_WS + lsid + ".json" : CommonData.getBieServer() + BIE_SPECIES + lsid + ".json";
    LOGGER.debug(snUrl);
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(snUrl);
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
        client.executeMethod(get);
        String slist = get.getResponseBodyAsString();
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(slist);
        String r = ((JSONObject) jo.get("taxonConcept")).get("rankString").toString();
        JSONObject joOcc = (JSONObject) jo.get("classification");
        for (String c : classificationList) {
            if (c.equals(r)) {
                break;
            }
            if (joOcc.containsKey(c.replace("ss", "zz"))) {
                classification.put(joOcc.get(c.replace("ss", "zz")).toString(), joOcc.get(c.replace("ss", "zz") + "Guid").toString());
            }
        }
    } catch (Exception e) {
        LOGGER.error("error getting scientific name at: " + snUrl, e);
    }
    return classification;
}
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 98 with JSONParser

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

the class BiocacheQuery method getClassification.

/**
     * Retrieves the classification information from the BIE for the supplied GUID.
     *
     * @param lsid
     * @return
     */
public static Map<String, String> getClassification(String lsid) {
    String[] classificationList = { StringConstants.KINGDOM, StringConstants.PHYLUM, StringConstants.CLASS, StringConstants.ORDER, StringConstants.FAMILY, StringConstants.GENUS, StringConstants.SPECIES, StringConstants.SUB_SPECIES, StringConstants.SCIENTIFIC_NAME };
    Map<String, String> classification = new LinkedHashMap<String, String>();
    String snUrl = "true".equalsIgnoreCase(CommonData.getSettings().getProperty("new.bie")) ? CommonData.getBieServer() + BIE_SPECIES_WS + lsid + ".json" : CommonData.getBieServer() + BIE_SPECIES + lsid + ".json";
    LOGGER.debug(snUrl);
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(snUrl);
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
        client.executeMethod(get);
        String slist = get.getResponseBodyAsString();
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(slist);
        JSONObject joOcc = (JSONObject) jo.get("classification");
        for (String c : classificationList) {
            //NC stop exception where a rank can't be found
            String s = "true".equalsIgnoreCase(CommonData.getSettings().getProperty("new.bie")) ? c : c.replace("ss", "zz");
            if (joOcc.containsKey(s)) {
                String value = joOcc.get(s).toString();
                if (value != null) {
                    classification.put(s, value);
                }
            }
        }
        if ("true".equalsIgnoreCase(CommonData.getSettings().getProperty("new.bie"))) {
            joOcc = (JSONObject) jo.get("taxonConcept");
            if (joOcc != null) {
                if (ArrayUtils.contains(classificationList, joOcc.get("rankString"))) {
                    classification.put(joOcc.get("rankString").toString(), joOcc.get("nameString").toString());
                }
            }
        }
    } catch (Exception e) {
        LOGGER.debug("Error getting scientific name for: " + lsid);
    }
    return classification;
}
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 99 with JSONParser

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

the class PhylogeneticDiversityComposer method getSelectedAreas.

public List<SelectedArea> getSelectedAreas() {
    List<SelectedArea> selectedAreas = new ArrayList<SelectedArea>();
    Vbox vboxArea = (Vbox) getFellowIfAny("vboxArea");
    for (Component c : vboxArea.getChildren()) {
        if ((c instanceof Checkbox) && ((Checkbox) c).isChecked()) {
            SelectedArea sa = null;
            String area = ((Checkbox) c).getValue();
            try {
                if (StringConstants.CURRENT.equals(area)) {
                    sa = new SelectedArea(null, getMapComposer().getViewArea());
                } else if (StringConstants.AUSTRALIA.equals(area)) {
                    sa = new SelectedArea(null, CommonData.getSettings().getProperty(CommonData.AUSTRALIA_WKT));
                } else if (StringConstants.WORLD.equals(area)) {
                    sa = new SelectedArea(null, CommonData.WORLD_WKT);
                } else {
                    List<MapLayer> layers = getMapComposer().getPolygonLayers();
                    for (MapLayer ml : layers) {
                        if (area.equals(ml.getName())) {
                            sa = new SelectedArea(ml, null);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                LOGGER.warn("Unable to retrieve selected area", e);
            }
            if (sa != null) {
                selectedAreas.add(sa);
            }
        }
    }
    //add all areas from a selection
    if (autoCompleteLayerSelection != null && cAreasFromLayer.isChecked()) {
        if (autoCompleteLayerAreas == null) {
            String fieldId = CommonData.getLayerFacetNameDefault(autoCompleteLayerSelection);
            JSONParser jp = new JSONParser();
            JSONObject objJson = null;
            try {
                objJson = (JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/field/" + fieldId));
            } catch (ParseException e) {
                LOGGER.error("failed to parse for: " + fieldId);
            }
            JSONArray objects = (JSONArray) objJson.get("objects");
            autoCompleteLayerAreas = new ArrayList();
            for (int i = 0; i < objects.size(); i++) {
                MapLayer ml = createMapLayerForObject((JSONObject) objects.get(i));
                SelectedArea sa = new SelectedArea(ml, null);
                autoCompleteLayerAreas.add(sa);
            }
        }
        selectedAreas.addAll(autoCompleteLayerAreas);
    }
    return selectedAreas;
}
Also used : SelectedArea(au.org.emii.portal.menu.SelectedArea) MapLayer(au.org.emii.portal.menu.MapLayer) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) Component(org.zkoss.zk.ui.Component)

Example 100 with JSONParser

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

the class PointComparison method layerList.

private String layerList() {
    if (layernames == null) {
        String url = CommonData.getSettings().getProperty("layers_batch_intersect_url") + "/fields";
        try {
            JSONParser jp = new JSONParser();
            JSONArray fields = (JSONArray) jp.parse(IOUtils.toString(new URL(url).openStream()));
            //sort
            SortedMap<String, Integer> sortOrder = new TreeMap<String, Integer>(new Comparator<String>() {

                @Override
                public int compare(String s1, String s2) {
                    return s1.toLowerCase().compareTo(s2.toLowerCase());
                }
            });
            for (int i = 0; i < fields.size(); i++) {
                JSONObject field = (JSONObject) fields.get(i);
                sortOrder.put(field.get("name").toString(), i);
            }
            for (String key : sortOrder.keySet()) {
                JSONObject field = (JSONObject) fields.get(sortOrder.get(key));
                if (layernames == null) {
                    layernames = "";
                } else {
                    layernames += ",";
                }
                layernames += field.get("id").toString();
                layerDisplayNames.put(field.get("id").toString(), field.get("name").toString());
            }
        } catch (Exception e) {
            LOGGER.error("error getting layer names", e);
        }
    }
    return layernames;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) URL(java.net.URL)

Aggregations

JSONParser (org.json.simple.parser.JSONParser)146 JSONObject (org.json.simple.JSONObject)136 JSONArray (org.json.simple.JSONArray)61 HttpClient (org.apache.commons.httpclient.HttpClient)44 ParseException (org.json.simple.parser.ParseException)40 GetMethod (org.apache.commons.httpclient.methods.GetMethod)34 Test (org.junit.Test)18 IOException (java.io.IOException)15 File (java.io.File)13 URL (java.net.URL)13 InputStreamReader (java.io.InputStreamReader)11 PostMethod (org.apache.commons.httpclient.methods.PostMethod)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 MapLayer (au.org.emii.portal.menu.MapLayer)9 Point (com.vividsolutions.jts.geom.Point)9 FileReader (java.io.FileReader)9 ArrayList (java.util.ArrayList)9 InputStream (java.io.InputStream)7 Facet (au.org.ala.legend.Facet)6