Search in sources :

Example 41 with JSONArray

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

the class AddLayerController method loadMap.

public void loadMap(Event event) {
    if (lbListLayers.getSelectedLayers().length > 0) {
        String[] sellayers = lbListLayers.getSelectedLayers();
        for (String s : sellayers) {
            String uid;
            String type = "";
            String treeName = "";
            String treePath = "";
            String legendurl = "";
            String metadata = "";
            JSONArray layerlist = CommonData.getLayerListJSONArray();
            for (int j = 0; j < layerlist.size(); j++) {
                JSONObject field = (JSONObject) layerlist.get(j);
                JSONObject layer = (JSONObject) field.get("layer");
                String name = field.get(StringConstants.ID).toString();
                if (name.equals(s)) {
                    uid = layer.get(StringConstants.ID).toString();
                    type = layer.get(StringConstants.TYPE).toString();
                    treeName = StringUtils.capitalize(field.get(StringConstants.NAME).toString());
                    treePath = layer.get("displaypath").toString();
                    legendurl = CommonData.getGeoServer() + "/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=9&LAYER=" + layer.get(StringConstants.NAME).toString() + "&styles=" + name + "_style";
                    metadata = CommonData.getLayersServer() + "/layers/view/more/" + uid;
                    break;
                }
            }
            getMapComposer().addWMSLayer(s, treeName, treePath, (float) 0.75, metadata, legendurl, StringConstants.ENVIRONMENTAL.equalsIgnoreCase(type) ? LayerUtilitiesImpl.GRID : LayerUtilitiesImpl.CONTEXTUAL, null, null, null);
            remoteLogger.logMapArea(treeName, "Layer - " + type, treePath, s, metadata);
        }
    }
    this.detach();
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray)

Example 42 with JSONArray

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

the class PhylogeneticDiversityComposer method fillPDTreeList.

private void fillPDTreeList() {
    JSONArray ja = null;
    String url = CommonData.getSettings().getProperty(CommonData.PHYLOLIST_URL) + "/phylo/getExpertTrees";
    JSONParser jp = new JSONParser();
    try {
        ja = (JSONArray) jp.parse(Util.readUrl(url));
    } catch (ParseException e) {
        LOGGER.error("failed to parse getExpertTrees");
    }
    if (ja == null || ja.size() == 0) {
        Events.echoEvent("onClose", this, null);
        getMapComposer().showMessage("Phylogenetic diversity tool is currently unavailable.");
        return;
    }
    trees = new Object[ja.size()];
    header = new ArrayList<String>();
    //restrict header to what is in the zul
    for (Component c : getFellow(StringConstants.TREES_HEADER).getChildren()) {
        header.add(c.getId().substring(3));
    }
    int row = 0;
    for (int i = 0; i < ja.size(); i++) {
        JSONObject j = (JSONObject) ja.get(i);
        Map<String, String> pdrow = new HashMap<String, String>();
        for (Object o : j.keySet()) {
            String key = (String) o;
            if (j.containsKey(key) && j.get(key) != null) {
                pdrow.put(key, j.get(key).toString());
            } else {
                pdrow.put(key, null);
            }
        }
        trees[row] = pdrow;
        row++;
    }
    treesList.setModel(new ListModelArray(trees, false));
    treesList.setItemRenderer(new ListitemRenderer() {

        public void render(Listitem li, Object data, int itemIdx) {
            Map<String, String> map = (Map<String, String>) data;
            for (int i = 0; i < header.size(); i++) {
                String value = map.get(header.get(i));
                if (value == null) {
                    value = "";
                }
                if ("treeViewUrl".equalsIgnoreCase(header.get(i))) {
                    Html img = new Html("<i class='icon-info-sign'></i>");
                    img.setAttribute("link", value.isEmpty() ? CommonData.getSettings().getProperty(CommonData.PHYLOLIST_URL) : value);
                    Listcell lc = new Listcell();
                    lc.setParent(li);
                    img.setParent(lc);
                    img.addEventListener(StringConstants.ONCLICK, new EventListener() {

                        @Override
                        public void onEvent(Event event) throws Exception {
                            //re-toggle the checked flag
                            Listitem li = (Listitem) event.getTarget().getParent().getParent();
                            li.getListbox().toggleItemSelection(li);
                            String metadata = (String) event.getTarget().getAttribute("link");
                            getMapComposer().activateLink(metadata, "Metadata", false);
                        }
                    });
                } else {
                    Listcell lc = new Listcell(value);
                    lc.setParent(li);
                }
            }
        }
    });
    treesList.setMultiple(true);
}
Also used : HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) Event(org.zkoss.zk.ui.event.Event) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) EventListener(org.zkoss.zk.ui.event.EventListener) Component(org.zkoss.zk.ui.Component) HashMap(java.util.HashMap) Map(java.util.Map)

Example 43 with JSONArray

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

the class LayerLegendGeneralComposer method afterCompose.

@Override
public void afterCompose() {
    super.afterCompose();
    MapLayer llc2MapLayer = null;
    Map m = Executions.getCurrent().getArg();
    if (m != null) {
        for (Object o : m.entrySet()) {
            if (((Map.Entry) o).getKey() instanceof String && "map_layer".equals(((Map.Entry) o).getKey())) {
                llc2MapLayer = (MapLayer) ((Map.Entry) o).getValue();
            }
        }
    }
    cbColour.setSelectedIndex(0);
    getMapComposer().setFacetsOpenListener(new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            cbColour.open();
        }
    });
    getMapComposer().setLayerLegendNameRefresh(new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            txtLayerName.setValue((String) event.getData());
        }
    });
    init(llc2MapLayer, llc2MapLayer.getSpeciesQuery(), llc2MapLayer.getRedVal(), llc2MapLayer.getGreenVal(), llc2MapLayer.getBlueVal(), llc2MapLayer.getSizeVal(), (int) (llc2MapLayer.getOpacity() * 100), llc2MapLayer.getColourMode(), (StringConstants.GRID.equals(llc2MapLayer.getColourMode())) ? 0 : ((llc2MapLayer.isClustered()) ? 1 : 2), llc2MapLayer.getSizeUncertain());
    getFellow("btnSearch").addEventListener(StringConstants.ONCLICK, new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            List<String[]> legendLinesFiltered = new ArrayList<String[]>();
            String txt = ((Textbox) getFellow("txtSearch")).getValue().toLowerCase();
            if (txt.length() > 0) {
                Integer groupCount = mapLayer.getClassificationGroupCount();
                JSONArray groupObjects = mapLayer.getClassificationObjects();
                List<JSONObject> model = new ArrayList<JSONObject>();
                for (int i = 0; i < groupCount; i++) {
                    if (((JSONObject) groupObjects.get(i)).get("name").toString().toLowerCase().contains(txt)) {
                        model.add((JSONObject) groupObjects.get(i));
                    }
                }
                lbClassificationGroup.setModel(new SimpleListModel(model));
                lbClassificationGroup.setActivePage(0);
                ((Button) getFellow("btnClear")).setDisabled(false);
            }
        }
    });
    getFellow("btnClear").addEventListener(StringConstants.ONCLICK, new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            ((Textbox) getFellow("txtSearch")).setValue("");
            ((Button) getFellow("btnClear")).setDisabled(true);
            Integer groupCount = mapLayer.getClassificationGroupCount();
            JSONArray groupObjects = mapLayer.getClassificationObjects();
            List<JSONObject> model = new ArrayList<JSONObject>();
            for (int i = 0; i < groupCount; i++) {
                model.add((JSONObject) groupObjects.get(i));
            }
            lbClassificationGroup.setModel(new SimpleListModel(model));
            lbClassificationGroup.setActivePage(0);
        }
    });
}
Also used : MapLayer(au.org.emii.portal.menu.MapLayer) JSONArray(org.json.simple.JSONArray) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject) CheckEvent(org.zkoss.zk.ui.event.CheckEvent) Event(org.zkoss.zk.ui.event.Event) JSONObject(org.json.simple.JSONObject) LegendObject(au.org.ala.legend.LegendObject) List(java.util.List) EventListener(org.zkoss.zk.ui.event.EventListener)

Example 44 with JSONArray

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

the class LayerLegendGeneralComposer method setupForClassificationLayers.

private void setupForClassificationLayers() {
    if (mapLayer.isPolygonLayer())
        return;
    String activeLayerName = StringConstants.NONE;
    JSONObject field = null;
    JSONObject layer = null;
    if (mapLayer != null && mapLayer.getUri() != null) {
        if (mapLayer.getBaseUri() != null) {
            activeLayerName = mapLayer.getBaseUri().replaceAll("^.*&style=", "").replaceAll("&.*", "").replaceAll("_style", "");
        } else {
            activeLayerName = mapLayer.getUri().replaceAll("^.*&style=", "").replaceAll("&.*", "").replaceAll("_style", "");
        }
        field = CommonData.getLayer(activeLayerName);
        if (field == null)
            return;
        layer = (JSONObject) field.get("layer");
    }
    LOGGER.debug("ACTIVE LAYER: " + activeLayerName);
    if (mapLayer != null && mapLayer.getSubType() == LayerUtilitiesImpl.ALOC) {
        divClassificationPicker.setVisible(true);
        //reset content
        Integer groupCount = mapLayer.getClassificationGroupCount();
        if (groupCount == null) {
            mapLayer.setClassificationGroupCount(getClassificationGroupCount(mapLayer.getName().replace("aloc_", "")));
            groupCount = 0;
        }
        for (int i = cbClassificationGroup.getItemCount() - 1; i >= 0; i--) {
            cbClassificationGroup.removeItemAt(i);
        }
        Comboitem ci = new Comboitem(StringConstants.NONE);
        ci.setParent(cbClassificationGroup);
        for (int i = 1; i <= groupCount; i++) {
            new Comboitem("Group " + i).setParent(cbClassificationGroup);
        }
        //is there a current selection?
        Integer groupSelection = mapLayer.getClassificationSelection();
        if (groupSelection == null) {
            groupSelection = 0;
            mapLayer.setClassificationSelection(groupSelection);
        }
        cbClassificationGroup.setSelectedIndex(groupSelection);
        getFellow("btnCreateArea").setVisible(false);
        cbClassificationGroup.setVisible(true);
        lbClassificationGroup.setVisible(false);
        hboxClassificationGroup.setVisible(false);
    } else if (layer != null && layer.containsKey("type") && layer.get("type").toString().equalsIgnoreCase("contextual")) {
        divClassificationPicker.setVisible(true);
        if (mapLayer.getClassificationGroupCount() == null || mapLayer.getClassificationGroupCount() == 0) {
            //build
            String fieldId = field.get(StringConstants.ID).toString();
            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");
            //sort
            List<JSONObject> list = objects.subList(0, objects.size());
            Collections.sort(list, new Comparator<JSONObject>() {

                @Override
                public int compare(JSONObject o1, JSONObject o2) {
                    String s1 = (o1 == null || !o1.containsKey("name")) ? "" : o1.get("name").toString();
                    String s2 = (o2 == null || !o2.containsKey("name")) ? "" : o2.get("name").toString();
                    return s1.compareTo(s2);
                }
            });
            JSONArray obj = new JSONArray();
            obj.addAll(list);
            mapLayer.setClassificationGroupCount(obj.size());
            mapLayer.setClassificationObjects(obj);
        }
        //reset content
        Integer groupCount = mapLayer.getClassificationGroupCount();
        JSONArray groupObjects = mapLayer.getClassificationObjects();
        lbClassificationGroup.setItemRenderer(new ListitemRenderer<JSONObject>() {

            @Override
            public void render(Listitem item, JSONObject data, int index) throws Exception {
                Checkbox cb = new Checkbox();
                final int idx = index;
                cb.addEventListener("onCheck", new EventListener<Event>() {

                    @Override
                    public void onEvent(Event event) throws Exception {
                        if (mapLayer != null) {
                            lbClassificationGroup.setMultiple(true);
                            String v = ((Listcell) event.getTarget().getParent().getParent().getChildren().get(1)).getLabel();
                            if (((CheckEvent) event).isChecked()) {
                                selectedList.add(v);
                            } else {
                                selectedList.remove(v);
                            }
                            lblSelectedCount.setValue(selectedList.size() + " checked");
                            getFellow("clearSelection").setVisible(selectedList.size() > 0);
                            getFellow("createInGroup").setVisible(selectedList.size() > 0);
                            if (getFellowIfAny("createOutGroup") != null)
                                getFellow("createOutGroup").setVisible(selectedList.size() > 0);
                            highlightSelect(idx);
                        }
                    }
                });
                determineCheckboxState(cb, data.get("name").toString());
                Listcell lc;
                lc = new Listcell();
                cb.setParent(lc);
                lc.setParent(item);
                lc = new Listcell(data.get("name").toString());
                lc.setParent(item);
                lc = new Listcell();
                Image img = new Image();
                img.setTooltip("Create as an area layer");
                img.setClass("icon-plus-sign");
                img.setParent(lc);
                lc.setParent(item);
                final JSONObject j = data;
                img.addEventListener("onClick", new EventListener<Event>() {

                    @Override
                    public void onEvent(Event event) throws Exception {
                        createAreaEcho(j.get("pid").toString());
                    }
                });
                lc = new Listcell();
                img = new Image();
                img.setTooltip("Zoom to area");
                img.setClass("icon-zoom-in");
                img.setParent(lc);
                lc.setParent(item);
                img.addEventListener("onClick", new EventListener<Event>() {

                    @Override
                    public void onEvent(Event event) throws Exception {
                        List<Double> b = Util.getBoundingBox(j.get("bbox").toString());
                        BoundingBox bbox = new BoundingBox();
                        bbox.setMinLongitude(b.get(0).floatValue());
                        bbox.setMinLatitude(b.get(1).floatValue());
                        bbox.setMaxLongitude(b.get(2).floatValue());
                        bbox.setMaxLatitude(b.get(3).floatValue());
                        getMapComposer().getOpenLayersJavascript().execute(getMapComposer().getOpenLayersJavascript().zoomToBoundingBox(bbox, false));
                    }
                });
            }
        });
        lbClassificationGroup.addEventListener("onSelect", new EventListener<Event>() {

            @Override
            public void onEvent(Event event) throws Exception {
                if (mapLayer != null) {
                    highlightSelect(lbClassificationGroup.getSelectedIndex());
                }
            }
        });
        List<JSONObject> model = new ArrayList<JSONObject>();
        for (int i = 0; i < groupCount; i++) {
            model.add((JSONObject) groupObjects.get(i));
        }
        lbClassificationGroup.setModel(new SimpleListModel(model));
        //is there a current selection?
        Integer groupSelection = mapLayer.getClassificationSelection();
        if (groupSelection == null) {
            groupSelection = 0;
            mapLayer.setClassificationSelection(groupSelection);
        }
        getFellow("btnCreateArea").setVisible(true);
        getFellow("btnCreateArea").setVisible(false);
        cbClassificationGroup.setVisible(false);
        lbClassificationGroup.setVisible(true);
        hboxClassificationGroup.setVisible(true);
    } else {
        getFellow("btnCreateArea").setVisible(false);
        divClassificationPicker.setVisible(false);
    }
}
Also used : JSONArray(org.json.simple.JSONArray) Image(org.zkoss.zul.Image) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject) Checkbox(org.zkoss.zul.Checkbox) BoundingBox(au.org.emii.portal.value.BoundingBox) CheckEvent(org.zkoss.zk.ui.event.CheckEvent) Event(org.zkoss.zk.ui.event.Event) JSONParser(org.json.simple.parser.JSONParser) List(java.util.List) ParseException(org.json.simple.parser.ParseException) EventListener(org.zkoss.zk.ui.event.EventListener)

Example 45 with JSONArray

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

the class MapComposer method activateLink.

public void activateLink(String uri, String label, boolean isExternal, String downloadPid) {
    closeExternalContentWindow();
    Window externalContentWindow = (Window) Executions.createComponents("WEB-INF/zul/ExternalContent.zul", layerControls, null);
    if (isExternal) {
        // change browsers current location
        Clients.evalJavaScript("window.location.href ='" + uri + "';");
    } else {
        Iframe iframe = (Iframe) externalContentWindow.getFellow("externalContentIframe");
        Html html = (Html) externalContentWindow.getFellow("externalContentHTML");
        String newUri = uri;
        if (newUri.charAt(0) == '*') {
            //html content
            newUri = newUri.substring(1);
            //url
            iframe.setHeight("0px");
            iframe.setSrc("");
            String content;
            if ("download".equalsIgnoreCase(label)) {
                // Added fast download option -
                // TODO refactor so this can be generated from same code that sets the downloadUrl (uri) in BiocacheQuery.java
                String fastDownloadUrl = newUri.replaceFirst("/occurrences/download", "/occurrences/index/download");
                StringBuilder sbContent = new StringBuilder();
                sbContent.append("<p id='termsOfUseDownload' style='padding:10px; margin-bottom: 0;'>");
                sbContent.append("By downloading this content you are agreeing to use it in accordance ");
                sbContent.append("with the Atlas of Living Australia <a href='http://www.ala.org.au/about/terms-of-use/#TOUusingcontent'>Terms of Use</a>");
                sbContent.append(" and any Data Provider Terms associated with the data download. ");
                sbContent.append("<br/><br/>");
                sbContent.append("Please provide the following details before downloading (* required)");
                sbContent.append("</p>");
                sbContent.append("    <form id='downloadForm' onSubmit='downloadSubmitButtonClick(); return false;' style='padding:10px;'>");
                sbContent.append("        <input type='hidden' name='url' id='downloadUrl' value='").append(uri).append("'/>");
                sbContent.append("        <input type='hidden' name='url' id='fastDownloadUrl' value='").append(fastDownloadUrl).append("'/>");
                sbContent.append("        <fieldset>");
                sbContent.append("            <p><label for='email'>Email</label>");
                sbContent.append("                <input type='text' name='email' id='email' value='' size='30'  />");
                sbContent.append("            </p>");
                sbContent.append("            <p><label for='filename'>File Name</label>");
                sbContent.append("                <input type='text' name='filename' id='filename' value='data' size='30'  />");
                sbContent.append("            </p>");
                sbContent.append("            <p><label for='reasonTypeId' style='vertical-align: top'>Download Reason *</label>");
                sbContent.append("            <select name='reasonTypeId' id='reasonTypeId'>");
                sbContent.append("            <option value=''>-- select a reason --</option>");
                JSONArray dlreasons = CommonData.getDownloadReasons();
                for (int i = 0; i < dlreasons.size(); i++) {
                    JSONObject dlr = (JSONObject) dlreasons.get(i);
                    sbContent.append("            <option value='").append((Long) dlr.get(StringConstants.ID)).append("'>").append(dlr.get(StringConstants.NAME)).append("</option>");
                }
                sbContent.append("            <select></p>");
                sbContent.append("                    <input style='display:none' type='radio' name='downloadType' value='fast' class='tooltip' checked='checked' title='Faster download but fewer fields are included'>");
                sbContent.append("            <p style='clear:both;'>&nbsp;</p>");
                sbContent.append("            <p style='text-align:center;'><input type='submit' value='Download All Records' id='downloadSubmitButton'/></p>");
                sbContent.append("        </fieldset>");
                sbContent.append("    </form>");
                content = sbContent.toString();
            } else {
                content = newUri;
            }
            //content
            html.setContent(content);
            html.setStyle("overflow: scroll;padding: 0 10px;");
            //for the 'reset window' button
            ((ExternalContentComposer) externalContentWindow).setSrc("");
            //update linked button
            externalContentWindow.getFellow(StringConstants.BREAKOUT).setVisible(false);
            externalContentWindow.setContentStyle("overflow:auto");
        } else {
            //url
            iframe.setHeight("100%");
            iframe.setSrc(newUri);
            //content
            html.setContent("");
            //for the 'reset window' button
            ((ExternalContentComposer) externalContentWindow).setSrc(newUri);
            //update linked button
            ((Toolbarbutton) externalContentWindow.getFellow(StringConstants.BREAKOUT)).setHref(newUri);
            externalContentWindow.getFellow(StringConstants.BREAKOUT).setVisible(true);
            externalContentWindow.setContentStyle("overflow:visible");
        }
        if (StringUtils.isNotBlank(downloadPid)) {
            String downloadUrl = CommonData.getSatServer() + "/ws/download/" + downloadPid;
            if (downloadPid.startsWith("http")) {
                downloadUrl = downloadPid;
            }
            ((Toolbarbutton) externalContentWindow.getFellow("download")).setHref(downloadUrl);
            externalContentWindow.getFellow("download").setVisible(true);
        } else {
            ((Toolbarbutton) externalContentWindow.getFellow("download")).setHref("");
            externalContentWindow.getFellow("download").setVisible(false);
        }
        // use the link description as the popup caption
        ((Caption) externalContentWindow.getFellow("caption")).setLabel(label);
        externalContentWindow.setPosition("center");
        try {
            externalContentWindow.setParent(layerControls);
            externalContentWindow.doModal();
        } catch (Exception e) {
            LOGGER.error("error opening information popup", e);
        }
    }
}
Also used : Window(org.zkoss.zul.Window) JSONArray(org.json.simple.JSONArray) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject)

Aggregations

JSONArray (org.json.simple.JSONArray)267 JSONObject (org.json.simple.JSONObject)238 JSONParser (org.json.simple.parser.JSONParser)73 Test (org.junit.Test)40 ParseException (org.json.simple.parser.ParseException)23 ArrayList (java.util.ArrayList)21 HttpClient (org.apache.commons.httpclient.HttpClient)21 IOException (java.io.IOException)17 HashMap (java.util.HashMap)17 GetMethod (org.apache.commons.httpclient.methods.GetMethod)17 Transaction (org.xel.Transaction)12 File (java.io.File)11 List (java.util.List)10 HttpResponse (org.apache.http.HttpResponse)10 BlockchainTest (org.xel.BlockchainTest)10 APICall (org.xel.http.APICall)10 Block (org.xel.Block)9 MapLayer (au.org.emii.portal.menu.MapLayer)8 Map (java.util.Map)8 FileReader (java.io.FileReader)6