Search in sources :

Example 1 with UploadEvent

use of org.zkoss.zk.ui.event.UploadEvent in project spatial-portal by AtlasOfLivingAustralia.

the class SandboxPasteController method doFileUpload.

public void doFileUpload(Event event) {
    UploadEvent ue = null;
    if (event instanceof UploadEvent) {
        ue = (UploadEvent) event;
    } else if (event instanceof ForwardEvent) {
        ue = (UploadEvent) ((ForwardEvent) event).getOrigin();
    }
    if (ue == null) {
        LOGGER.debug("unable to upload file");
        return;
    } else {
        LOGGER.debug("fileUploaded()");
    }
    try {
        Media m = ue.getMedia();
        String filename = m.getName();
        String contentType = m.getContentType();
        String format = m.getFormat();
        boolean loaded = false;
        if (!loaded) {
            try {
                loaded = upload(m.getByteData(), filename, contentType);
                LOGGER.debug(m.getContentType() + " with getByteData");
            } catch (Exception e) {
            //failed to read, will try another method
            }
        }
        if (!loaded) {
            try {
                loaded = upload(IOUtils.toByteArray(m.getStreamData()), filename, contentType);
                LOGGER.debug(m.getContentType() + " with getStreamData");
            } catch (Exception e) {
            //failed to read, will try another method
            }
        }
        try {
            Reader r = m.getReaderData();
            InputStream is = new ReaderInputStream(r, "UTF-8");
            loaded = upload(IOUtils.toByteArray(is), filename, contentType);
            LOGGER.debug(m.getContentType() + " with getReaderData");
        } catch (Exception e) {
        //failed to read, will try another method
        }
        if (!loaded) {
            try {
                loaded = upload(m.getStringData().getBytes(), filename, contentType);
                LOGGER.debug(m.getContentType() + " with getStringData");
            } catch (Exception e) {
                //last one, report error
                getMapComposer().showMessage(CommonData.lang("error_uploading_file"));
                LOGGER.error("unable to load user layer list: ", e);
            }
        }
        //process if successful upload
        if (loaded) {
            ((Iframe) getFellow("sandboxFrame")).setSrc("sandboxPreview.html?uploadId=" + uploadId + "&uploadFn=" + uploadFn);
            //display frame and trigger javascript setup function
            getMapComposer().getOpenLayersJavascript().execute("$('#sandboxContainer')[0].style.display = \"inline\";setTimeout(\"setUrls()\",500)");
            getFellow("fileUpload").setVisible(false);
            getFellow("fileUpload").detach();
            getFellow("divSandbox").setVisible(false);
            setTop("10px");
        }
    } catch (Exception ex) {
        LOGGER.error("error reading uploaded file", ex);
    }
}
Also used : Iframe(org.zkoss.zul.Iframe) ReaderInputStream(org.apache.xmlbeans.impl.common.ReaderInputStream) ForwardEvent(org.zkoss.zk.ui.event.ForwardEvent) ReaderInputStream(org.apache.xmlbeans.impl.common.ReaderInputStream) InputStream(java.io.InputStream) Media(org.zkoss.util.media.Media) Reader(java.io.Reader) UploadEvent(org.zkoss.zk.ui.event.UploadEvent)

Example 2 with UploadEvent

use of org.zkoss.zk.ui.event.UploadEvent in project spatial-portal by AtlasOfLivingAustralia.

the class AreaUploadShapefile method onUpload$btnFileUpload.

public void onUpload$btnFileUpload(Event event) {
    UploadEvent ue = null;
    if ("onUpload".equals(event.getName())) {
        ue = (UploadEvent) event;
    } else if ("onForward".equals(event.getName())) {
        ue = (UploadEvent) ((ForwardEvent) event).getOrigin();
    }
    if (ue == null) {
        LOGGER.debug("unable to upload file");
        return;
    } else {
        LOGGER.debug("fileUploaded()");
    }
    try {
        Media m = ue.getMedia();
        LOGGER.debug("m.getName(): " + m.getName());
        LOGGER.debug("getContentType: " + m.getContentType());
        LOGGER.debug("getFormat: " + m.getFormat());
        UserDataDTO ud = new UserDataDTO(txtLayerName.getValue());
        ud.setFilename(m.getName());
        byte[] kmldata = getKml(m);
        if (kmldata.length > 0) {
            loadUserLayerKML(m.getName(), kmldata, ud);
        } else if (m.getName().toLowerCase().endsWith("zip")) {
            Map args = new HashMap();
            args.put(StringConstants.LAYERNAME, txtLayerName.getValue());
            args.put(StringConstants.MEDIA, m);
            String windowname = "areashapewizard";
            if (getFellowIfAny(windowname) != null) {
                getFellowIfAny(windowname).detach();
            }
            Window window = (Window) Executions.createComponents("WEB-INF/zul/add/area/AreaUploadShapefileWizard.zul", this.getParent(), args);
            try {
                window.setParent(this.getParent());
                window.doModal();
            } catch (SuspendNotAllowedException e) {
            // we are really closing the window without opening/displaying to the user
            }
        } else if (m.getName().toLowerCase().endsWith("zip_removeme")) {
            Map input = Zipper.unzipFile(m.getName(), m.getStreamData(), "/data/ala/runtime/output/layers/");
            String type = "";
            String file = "";
            if (input.containsKey(StringConstants.TYPE)) {
                type = (String) input.get(StringConstants.TYPE);
            }
            if (input.containsKey(StringConstants.FILE)) {
                file = (String) input.get(StringConstants.FILE);
            }
            if ("shp".equalsIgnoreCase(type)) {
                LOGGER.debug("Uploaded file is a shapefile. Loading...");
                Map shape = ShapefileUtils.loadShapefile(new File(file));
                if (shape != null) {
                    String wkt = (String) shape.get(StringConstants.WKT);
                    LOGGER.debug("Got shapefile wkt...validating");
                    String msg = "";
                    boolean invalid = false;
                    try {
                        WKTReader wktReader = new WKTReader();
                        com.vividsolutions.jts.geom.Geometry g = wktReader.read(wkt);
                        //NC 20130319: Ensure that the WKT is valid according to the WKT standards.
                        IsValidOp op = new IsValidOp(g);
                        if (!op.isValid()) {
                            //this will fix some issues
                            g = g.buffer(0);
                            op = new IsValidOp(g);
                        }
                        if (!op.isValid()) {
                            invalid = true;
                            LOGGER.warn(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + op.getValidationError().getMessage());
                            msg = op.getValidationError().getMessage();
                        //TODO Fix invalid WKT text using https://github.com/tudelft-gist/prepair maybe???
                        } else if (g.isRectangle()) {
                            //NC 20130319: When the shape is a rectangle ensure that the points a specified in the correct order.
                            //get the new WKT for the rectangle will possibly need to change the order.
                            com.vividsolutions.jts.geom.Envelope envelope = g.getEnvelopeInternal();
                            String wkt2 = "POLYGON((" + envelope.getMinX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMinY() + "))";
                            if (!wkt.equals(wkt2)) {
                                LOGGER.debug("NEW WKT for Rectangle: " + wkt);
                                msg = CommonData.lang("error_wkt_anticlockwise");
                                invalid = true;
                            }
                        }
                        if (!invalid) {
                            invalid = !op.isValid();
                        }
                    } catch (ParseException parseException) {
                        LOGGER.error("error testing validity of uploaded shape file wkt", parseException);
                    }
                    if (invalid) {
                        ok = false;
                        getMapComposer().showMessage(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + msg);
                    } else {
                        layerName = getMapComposer().getNextAreaLayerName(txtLayerName.getValue());
                        MapLayer mapLayer = getMapComposer().addWKTLayer(wkt, layerName, txtLayerName.getValue());
                        ud.setUploadedTimeInMs(System.currentTimeMillis());
                        ud.setType("shapefile");
                        String metadata = "";
                        metadata += "User uploaded Shapefile \n";
                        metadata += "Name: " + ud.getName() + " <br />\n";
                        metadata += "Filename: " + ud.getFilename() + " <br />\n";
                        metadata += "Date: " + ud.getDisplayTime() + " <br />\n";
                        mapLayer.getMapLayerMetadata().setMoreInfo(metadata);
                        ok = true;
                    }
                }
            } else {
                LOGGER.debug("Unknown file type. ");
                getMapComposer().showMessage(CommonData.lang("error_unknown_file_type"));
            }
        } else {
            LOGGER.debug("Unknown file type. ");
            getMapComposer().showMessage(CommonData.lang("error_unknown_file_type"));
        }
    } catch (Exception ex) {
        getMapComposer().showMessage(CommonData.lang("error_upload_failed"));
        LOGGER.error("unable to load user area file: ", ex);
    }
}
Also used : Window(org.zkoss.zul.Window) HashMap(java.util.HashMap) MapLayer(au.org.emii.portal.menu.MapLayer) Media(org.zkoss.util.media.Media) WKTReader(com.vividsolutions.jts.io.WKTReader) ParseException(com.vividsolutions.jts.io.ParseException) SuspendNotAllowedException(org.zkoss.zk.ui.SuspendNotAllowedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UploadEvent(org.zkoss.zk.ui.event.UploadEvent) IsValidOp(com.vividsolutions.jts.operation.valid.IsValidOp) Geometry(com.vividsolutions.jts.geom.Geometry) UserDataDTO(au.org.ala.spatial.dto.UserDataDTO) ParseException(com.vividsolutions.jts.io.ParseException) SuspendNotAllowedException(org.zkoss.zk.ui.SuspendNotAllowedException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with UploadEvent

use of org.zkoss.zk.ui.event.UploadEvent in project spatial-portal by AtlasOfLivingAustralia.

the class UploadLayerListController method doFileUpload.

public void doFileUpload(Event event) {
    UploadEvent ue = null;
    if (event instanceof UploadEvent) {
        ue = (UploadEvent) event;
    } else if (event instanceof ForwardEvent) {
        ue = (UploadEvent) ((ForwardEvent) event).getOrigin();
    }
    if (ue == null) {
        LOGGER.debug("unable to upload file");
        return;
    } else {
        LOGGER.debug("fileUploaded()");
    }
    try {
        Media m = ue.getMedia();
        boolean loaded = false;
        try {
            loadLayerList(m.getReaderData());
            loaded = true;
            LOGGER.debug(m.getContentType() + " with getReaderData");
        } catch (Exception e) {
        //failed to read, will try another method
        }
        if (!loaded) {
            try {
                loadLayerList(new StringReader(new String(m.getByteData())));
                loaded = true;
                LOGGER.debug(m.getContentType() + " with getByteData");
            } catch (Exception e) {
            //failed to read, will try another method
            }
        }
        if (!loaded) {
            try {
                loadLayerList(new InputStreamReader(m.getStreamData()));
                loaded = true;
                LOGGER.debug(m.getContentType() + " with getStreamData");
            } catch (Exception e) {
            //failed to read, will try another method
            }
        }
        if (!loaded) {
            try {
                loadLayerList(new StringReader(m.getStringData()));
                LOGGER.debug(m.getContentType() + " with getStringData");
            } catch (Exception e) {
                //last one, report error
                getMapComposer().showMessage(CommonData.lang("error_uploading_file"));
                LOGGER.error("unable to load user layer list: ", e);
            }
        }
    } catch (Exception ex) {
        LOGGER.error("error reading uploaded file", ex);
    }
}
Also used : ForwardEvent(org.zkoss.zk.ui.event.ForwardEvent) InputStreamReader(java.io.InputStreamReader) Media(org.zkoss.util.media.Media) StringReader(java.io.StringReader) IOException(java.io.IOException) UploadEvent(org.zkoss.zk.ui.event.UploadEvent)

Example 4 with UploadEvent

use of org.zkoss.zk.ui.event.UploadEvent in project spatial-portal by AtlasOfLivingAustralia.

the class UploadSpeciesController method doFileUpload.

public void doFileUpload(UserDataDTO ud, Event event) {
    UploadEvent ue = null;
    if (event instanceof UploadEvent) {
        ue = (UploadEvent) event;
    } else if (event instanceof ForwardEvent) {
        ue = (UploadEvent) ((ForwardEvent) event).getOrigin();
    }
    if (ue == null) {
        LOGGER.debug("unable to upload file");
        return;
    } else {
        LOGGER.debug("fileUploaded()");
    }
    try {
        Media m = ue.getMedia();
        UserDataDTO u = ud;
        if (u == null) {
            u = new UserDataDTO(m.getName());
        }
        if (u.getName().trim().isEmpty()) {
            u.setName(m.getName());
        }
        u.setFilename(m.getName());
        if (u.getName() == null || u.getName().length() == 0) {
            u.setName(m.getName());
        }
        if (u.getDescription() == null || u.getDescription().length() == 0) {
            u.setDescription(m.getName());
        }
        u.setUploadedTimeInMs(System.currentTimeMillis());
        LOGGER.debug("Got file '" + u.getName() + "' with type '" + m.getContentType() + "'");
        //forget content types, do 'try'
        boolean loaded = false;
        try {
            loadUserPoints(u, m.getReaderData(), addToMap, tbName.getText(), tbDesc.getText(), getMapComposer(), eventListener);
            loaded = true;
            LOGGER.debug("read type " + m.getContentType() + " with getReaderData");
        } catch (Exception e) {
        //failed to read uploaded data, will try another method
        }
        if (!loaded) {
            try {
                loadUserPoints(u, new StringReader(new String(m.getByteData())), addToMap, tbName.getText(), tbDesc.getText(), getMapComposer(), eventListener);
                loaded = true;
                LOGGER.debug("read type " + m.getContentType() + " with getByteData");
            } catch (Exception e) {
            //failed to read uploaded data, will try another method
            }
        }
        if (!loaded) {
            try {
                loadUserPoints(u, new InputStreamReader(m.getStreamData()), addToMap, tbName.getText(), tbDesc.getText(), getMapComposer(), eventListener);
                loaded = true;
                LOGGER.debug("read type " + m.getContentType() + " with getStreamData");
            } catch (Exception e) {
            //failed to read uploaded data, will try another method
            }
        }
        if (!loaded) {
            try {
                loadUserPoints(u, new StringReader(m.getStringData()), addToMap, tbName.getText(), tbDesc.getText(), getMapComposer(), eventListener);
                LOGGER.debug("read type " + m.getContentType() + " with getStringData");
            } catch (Exception e) {
                //last one, report error
                getMapComposer().showMessage("Unable to load your file. Please try again.");
                LOGGER.error("unable to load user points: ", e);
            }
        }
        //call reset window on caller to perform refresh'
        if (callback != null) {
            try {
                callback.onEvent(new ForwardEvent("", null, null, new String[] { uploadLSID, uploadType }));
            } catch (Exception e) {
                LOGGER.error("failed to cancel species points upload", e);
            }
        }
    } catch (Exception ex) {
        LOGGER.error("unable to load user points", ex);
    }
}
Also used : ForwardEvent(org.zkoss.zk.ui.event.ForwardEvent) InputStreamReader(java.io.InputStreamReader) Media(org.zkoss.util.media.Media) StringReader(java.io.StringReader) UserDataDTO(au.org.ala.spatial.dto.UserDataDTO) WrongValueException(org.zkoss.zk.ui.WrongValueException) UploadEvent(org.zkoss.zk.ui.event.UploadEvent)

Aggregations

Media (org.zkoss.util.media.Media)4 UploadEvent (org.zkoss.zk.ui.event.UploadEvent)4 ForwardEvent (org.zkoss.zk.ui.event.ForwardEvent)3 UserDataDTO (au.org.ala.spatial.dto.UserDataDTO)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 MapLayer (au.org.emii.portal.menu.MapLayer)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 ParseException (com.vividsolutions.jts.io.ParseException)1 WKTReader (com.vividsolutions.jts.io.WKTReader)1 IsValidOp (com.vividsolutions.jts.operation.valid.IsValidOp)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ReaderInputStream (org.apache.xmlbeans.impl.common.ReaderInputStream)1 SAXException (org.xml.sax.SAXException)1 SuspendNotAllowedException (org.zkoss.zk.ui.SuspendNotAllowedException)1