changeset 2494:88b51e6e0334

Issue 657. Added UI and service for shapefile upload. flys-client/trunk@4294 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 25 Apr 2012 07:58:58 +0000
parents f905c936ffff
children d0066abb025c
files flys-client/ChangeLog flys-client/pom.xml flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties flys-client/src/main/java/de/intevation/flys/client/client/ui/DigitizePanel.java flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java flys-client/src/main/webapp/WEB-INF/web.xml
diffstat 9 files changed, 270 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Fri Apr 20 15:23:27 2012 +0000
+++ b/flys-client/ChangeLog	Wed Apr 25 07:58:58 2012 +0000
@@ -1,3 +1,25 @@
+2012-04-25  Raimund Renkert <raimund.renkert@intevation.de>
+
+	Issue 657.
+
+	* src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java:
+	  New. Service to upload a base64 encoded file embedded in xml structure.
+
+	* src/main/java/de/intevation/flys/client/client/ui/DigitizePanel.java:
+	  Added upload form to the UI.
+
+	* src/main/webapp/WEB-INF/web.xml:
+	  Registered new servlet and added mapping.
+
+	* pom.xml:
+	  Added dependency apache commons fileupload.
+
+	* src/main/java/de/intevation/flys/client/client/FLYSConstants.java,
+	  src/main/java/de/intevation/flys/client/client/FLYSConstants.properties,
+	  src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties,
+	  src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties:
+	  Added i18n strings.
+
 2012-04-20	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/server/FixingsKMChartServiceImpl.java:
--- a/flys-client/pom.xml	Fri Apr 20 15:23:27 2012 +0000
+++ b/flys-client/pom.xml	Wed Apr 25 07:58:58 2012 +0000
@@ -75,6 +75,11 @@
       <version>2.6</version>
     </dependency>
     <dependency>
+      <groupId>commons-fileupload</groupId>
+      <artifactId>commons-fileupload</artifactId>
+      <version>1.2.1</version>
+    </dependency>
+    <dependency>
       <groupId>org.gwtopenmaps.openlayers</groupId>
       <artifactId>gwt-openlayers-client</artifactId>
       <version>0.6</version>
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java	Fri Apr 20 15:23:27 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java	Wed Apr 25 07:58:58 2012 +0000
@@ -623,6 +623,10 @@
 
     String requireDGM();
 
+    String upload_file();
+
+    String shape_file_upload();
+
     // data cage
 
     String waterlevels();
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties	Fri Apr 20 15:23:27 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties	Wed Apr 25 07:58:58 2012 +0000
@@ -315,6 +315,8 @@
 map_themepanel_header_style = Style
 zoomToLayer = Layer-Zoom
 requireDGM = You need to choose a DEM.
+upload_file = upload
+shape_file_upload = Upload shapefile
 
 # data cage
 waterlevels = Waterlevels
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties	Fri Apr 20 15:23:27 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties	Wed Apr 25 07:58:58 2012 +0000
@@ -315,6 +315,8 @@
 map_themepanel_header_style = Stil
 zoomToLayer = Layer-Zoom
 requireDGM = Sie m\u00fcssen ein DGM ausw\u00e4hlen.
+upload_file = hochladen
+shape_file_upload = Shapedatei hochladen
 
 # data cage
 waterlevels = Wasserst\u00e4nde
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties	Fri Apr 20 15:23:27 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties	Wed Apr 25 07:58:58 2012 +0000
@@ -315,6 +315,8 @@
 map_themepanel_header_style = Style
 zoomToLayer = Layer-Zoom
 requireDGM = You need to choose a DEM.
+upload_file = upload
+shape_file_upload = Upload shapefile
 
 # data cage
 waterlevels = Waterlevels
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/DigitizePanel.java	Fri Apr 20 15:23:27 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/DigitizePanel.java	Wed Apr 25 07:58:58 2012 +0000
@@ -1,18 +1,30 @@
 package de.intevation.flys.client.client.ui;
 
 import java.util.List;
+import java.util.LinkedHashMap;
 
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.user.client.rpc.AsyncCallback;
 
 import com.smartgwt.client.util.SC;
 import com.smartgwt.client.widgets.Canvas;
+import com.smartgwt.client.widgets.Button;
+import com.smartgwt.client.widgets.Label;
 import com.smartgwt.client.widgets.events.ResizedEvent;
 import com.smartgwt.client.widgets.events.ResizedHandler;
+import com.smartgwt.client.widgets.events.ClickEvent;
+import com.smartgwt.client.widgets.events.ClickHandler;
 import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;
 import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
 import com.smartgwt.client.widgets.events.VisibilityChangedHandler;
 import com.smartgwt.client.widgets.events.VisibilityChangedEvent;
+import com.smartgwt.client.widgets.layout.VLayout;
+import com.smartgwt.client.widgets.form.DynamicForm;
+import com.smartgwt.client.widgets.form.fields.UploadItem;
+import com.smartgwt.client.widgets.form.fields.SelectItem;
+import com.smartgwt.client.types.VerticalAlignment;
+import com.smartgwt.client.widgets.HTMLPane;
+import com.smartgwt.client.types.Encoding;
 
 import org.gwtopenmaps.openlayers.client.Map;
 import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
@@ -104,6 +116,101 @@
     }
 
 
+    /**
+     * This method creates the content of the widget.
+     *
+     * @param data The {@link DataList} object.
+     *
+     * @return a combobox.
+     */
+    protected Canvas createWidget(DataList data) {
+        GWT.log("DigitizePanel - createWidget()");
+
+        VLayout layout   = new VLayout();
+        layout.setAlign(VerticalAlignment.TOP);
+        layout.setHeight(25);
+
+        LinkedHashMap initial = new LinkedHashMap();
+
+        form = new DynamicForm();
+
+        int size = data.size();
+
+        for (int i = 0; i < size; i++) {
+            Data d = data.get(i);
+
+            Label label = new Label(d.getDescription());
+            label.setValign(VerticalAlignment.TOP);
+            label.setHeight(20);
+            label.setWidth(400);
+
+            SelectItem combobox = new SelectItem(d.getLabel());
+            combobox.setWidth(250);
+
+            LinkedHashMap<String, String> it = new LinkedHashMap<String, String>();
+
+            boolean  defaultSet = false;
+            boolean  first      = true;
+
+            DataItem def      = d.getDefault();
+            String   defValue = def != null ? def.getStringValue() : null;
+
+            if (defValue != null && defValue.length() > 0) {
+                initial.put(d.getLabel(), def.getStringValue());
+                defaultSet = true;
+            }
+
+            // I was here.
+            for (DataItem item: d.getItems()) {
+                if (!defaultSet && first) {
+                    initial.put(d.getLabel(), item.getStringValue());
+                    first = false;
+                }
+
+                it.put(item.getStringValue(), item.getLabel());
+            }
+
+            label.setWidth(50);
+            combobox.setValueMap(it);
+            combobox.setShowTitle(false);
+            form.setItems(combobox);
+
+            HTMLPane frame = new HTMLPane();
+            frame.setWidth("1px");
+            frame.setHeight("1px");
+            frame.setContents("<iframe id='uploadTarget' name='uploadTarget'></iframe>");
+
+            final DynamicForm uploadForm = new DynamicForm();
+            uploadForm.setAction("flys/fileupload?uuid=" + artifact.getUuid());
+            uploadForm.setTarget("uploadTarget");
+            uploadForm.setEncoding(Encoding.MULTIPART);
+            Label uploadLabel = new Label(MSG.shape_file_upload());
+            uploadLabel.setHeight(20);
+            UploadItem uploadItem = new UploadItem();
+            uploadItem.setShowTitle(false);
+            uploadForm.setFields(uploadItem);
+            Button submit = new Button(MSG.upload_file());
+            submit.addClickHandler(new ClickHandler() {
+                public void onClick(ClickEvent e) {
+                    uploadForm.submitForm();
+                }
+            });
+            layout.addMember(frame);
+            layout.addMember(label);
+            layout.addMember(form);
+            layout.addMember(uploadLabel);
+            layout.addMember(uploadForm);
+            layout.addMember(submit);
+        }
+
+        form.setValues(initial);
+
+        layout.setAlign(VerticalAlignment.TOP);
+
+        return layout;
+    }
+
+
     @Override
     protected Data[] getData() {
         Data[] data  = super.getData();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java	Wed Apr 25 07:58:58 2012 +0000
@@ -0,0 +1,116 @@
+package de.intevation.flys.client.server;
+
+import java.io.InputStream;
+import java.io.ByteArrayOutputStream;
+
+import org.apache.log4j.Logger;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.fileupload.FileItemIterator; 
+import org.apache.commons.fileupload.FileItemStream; 
+import org.apache.commons.fileupload.servlet.ServletFileUpload; 
+import org.apache.commons.fileupload.FileUploadException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.google.gwt.user.server.rpc.RemoteServiceServlet;
+
+import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
+
+import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
+import de.intevation.artifacts.httpclient.http.HttpClient;
+import de.intevation.artifacts.httpclient.http.HttpClientImpl;
+
+import de.intevation.flys.client.shared.exceptions.ServerException;
+
+public class FileUploadServiceImpl
+extends      HttpServlet
+{
+    private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class);
+
+    @Override
+    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
+        processPost(req, resp);
+    }
+
+
+    protected void processPost(HttpServletRequest req, HttpServletResponse resp) {
+        logger.debug("handling post request.");
+
+        String url  = getServletContext().getInitParameter("server-url");
+
+        Document request = createFileXML(req);;
+
+        if (request == null) {
+            return;
+        }
+        HttpClient client = new HttpClientImpl(url);
+
+        try {
+            Document result = client.callService(url, "fileupload", request);
+
+            if (result == null) {
+                logger.warn("FileUpload service returned no result.");
+            }
+
+            return;
+        }
+        catch (ConnectionException ce) {
+            logger.error(ce, ce);
+        }
+    }
+
+
+    protected Document createFileXML(HttpServletRequest req) {
+           ServletFileUpload upload = new ServletFileUpload();
+
+        try{
+            FileItemIterator iter = upload.getItemIterator(req);
+
+            while (iter.hasNext()) {
+                FileItemStream item = iter.next();
+
+                String name = item.getFieldName();
+                InputStream stream = item.openStream();
+
+
+                // Process the input stream
+                ByteArrayOutputStream out = new ByteArrayOutputStream();
+                int len;
+                byte[] buffer = new byte[stream.available()];
+                while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
+                    out.write(buffer, 0, len);
+                }
+
+                Base64 encoder = new Base64();
+                String b64File = Base64.encodeBase64String(buffer);
+
+                Document fileDoc = XMLUtils.newDocument();
+
+                ElementCreator ec = new ElementCreator(fileDoc, null, null);
+                Element root = ec.create("upload");
+                Element id = ec.create("artifact-uuid");
+                id.setTextContent(req.getParameter("uuid"));
+
+                Element data = ec.create("data");
+                data.setTextContent(b64File);
+
+                fileDoc.appendChild(root);
+                root.appendChild(id);
+                root.appendChild(data);
+
+                return fileDoc;
+            }
+        }
+        catch(Exception e){
+            logger.debug("Failed to create xml document containing the file.");
+            logger.debug(e, e);
+        }
+        return null;
+    }
+}
--- a/flys-client/src/main/webapp/WEB-INF/web.xml	Fri Apr 20 15:23:27 2012 +0000
+++ b/flys-client/src/main/webapp/WEB-INF/web.xml	Wed Apr 25 07:58:58 2012 +0000
@@ -413,6 +413,16 @@
     <url-pattern>/flys/map-urls</url-pattern>
   </servlet-mapping>
 
+  <servlet>
+    <servlet-name>FileUpload</servlet-name>
+    <servlet-class>de.intevation.flys.client.server.FileUploadServiceImpl</servlet-class>
+  </servlet>
+  
+  <servlet-mapping>
+    <servlet-name>FileUpload</servlet-name>
+    <url-pattern>/flys/fileupload</url-pattern>
+  </servlet-mapping>
+
   <!-- Default page to serve -->
   <welcome-file-list>
     <welcome-file>FLYS.html</welcome-file>

http://dive4elements.wald.intevation.org