changeset 15:175c1eecaf84

added export kml feature (not finished)
author Sebastian Kruse <skruse@mpiwg-berlin.mpg.de>
date Mon, 26 Nov 2012 14:40:43 +0100
parents fef6080e83ad
children 775477d89709
files src/econnect/wp3_3/client/core/ApplicationConstants.java src/econnect/wp3_3/client/core/ApplicationConstants.properties src/econnect/wp3_3/client/core/StiConstants.java src/econnect/wp3_3/client/services/ExportWriterInterface.java src/econnect/wp3_3/client/services/ExportWriterInterfaceAsync.java src/econnect/wp3_3/client/widgets/table/DynamicStiTable.java src/econnect/wp3_3/server/ExportServlet.java src/econnect/wp3_3/server/ExportWriterImpl.java
diffstat 8 files changed, 229 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/econnect/wp3_3/client/core/ApplicationConstants.java	Mon Nov 26 14:39:42 2012 +0100
+++ b/src/econnect/wp3_3/client/core/ApplicationConstants.java	Mon Nov 26 14:40:43 2012 +0100
@@ -203,4 +203,8 @@
 	 */
 	String linkImage();
 
+	/**
+	 * @return image for export button
+	 */
+	String exportImage();
 }
--- a/src/econnect/wp3_3/client/core/ApplicationConstants.properties	Mon Nov 26 14:39:42 2012 +0100
+++ b/src/econnect/wp3_3/client/core/ApplicationConstants.properties	Mon Nov 26 14:40:43 2012 +0100
@@ -37,4 +37,6 @@
 openDisabledImage = images/open_0.png
 refineImage = images/zoom.png
 cancelImage = images/cancel.png
-linkImage = images/link.png
\ No newline at end of file
+linkImage = images/link.png
+#TODO: richtiges Bild eintragen
+exportImage = images/cancel.png 
--- a/src/econnect/wp3_3/client/core/StiConstants.java	Mon Nov 26 14:39:42 2012 +0100
+++ b/src/econnect/wp3_3/client/core/StiConstants.java	Mon Nov 26 14:40:43 2012 +0100
@@ -100,6 +100,9 @@
   @DefaultStringValue("Delete dataset")
   String deleteDataSet();
 
+  @DefaultStringValue("Export dataset")
+  String exportDataSet();
+
   @DefaultStringValue("Copy selected elements in new dataset")
   String storeSelected();
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/econnect/wp3_3/client/services/ExportWriterInterface.java	Mon Nov 26 14:40:43 2012 +0100
@@ -0,0 +1,11 @@
+package econnect.wp3_3.client.services;
+
+import com.google.gwt.user.client.rpc.RemoteService;
+import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
+
+@RemoteServiceRelativePath("ExportWriterImpl")
+public interface ExportWriterInterface extends RemoteService {
+    
+    public String writeKMLFile(String kmlFileContent);
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/econnect/wp3_3/client/services/ExportWriterInterfaceAsync.java	Mon Nov 26 14:40:43 2012 +0100
@@ -0,0 +1,9 @@
+package econnect.wp3_3.client.services;
+
+import com.google.gwt.user.client.rpc.AsyncCallback;
+
+public interface ExportWriterInterfaceAsync {
+
+	void writeKMLFile(String kmlFileContent, AsyncCallback<String> callback);
+
+}
--- a/src/econnect/wp3_3/client/widgets/table/DynamicStiTable.java	Mon Nov 26 14:39:42 2012 +0100
+++ b/src/econnect/wp3_3/client/widgets/table/DynamicStiTable.java	Mon Nov 26 14:40:43 2012 +0100
@@ -1,7 +1,12 @@
 package econnect.wp3_3.client.widgets.table;
 
+import java.util.ArrayList;
+
+import com.google.gwt.user.client.rpc.AsyncCallback;
 import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.FormPanel;
 import com.google.gwt.user.client.ui.Grid;
+import com.google.gwt.user.client.ui.Hidden;
 import com.google.gwt.user.client.ui.Image;
 import com.google.gwt.user.client.ui.Label;
 import com.google.gwt.user.client.ui.HTML;
@@ -20,9 +25,8 @@
 import econnect.wp3_3.client.core.DataSet;
 import econnect.wp3_3.client.core.StiConstants;
 import econnect.wp3_3.client.core.StiCore;
-
-import java.util.ArrayList;
-import java.util.Date;
+import econnect.wp3_3.client.services.ExportWriterInterface;
+import econnect.wp3_3.client.services.ExportWriterInterfaceAsync;
 
 /**
  * Implementation of a dynamic table for one dataset
@@ -112,9 +116,14 @@
 	/**
      * ArrayList that holds all elements that can be shown 
     */	
-	private ArrayList<DataObject> actualObjectSet; 
+	private ArrayList<DataObject> actualObjectSet;
+	
+	private ExportWriterInterfaceAsync exportWriter;
 	
-    /**
+	final FormPanel exportFormPanel = new FormPanel();
+	final Hidden hiddenVal = new Hidden();
+
+	/**
      * Constructor for initialization of the dynamic table
      * 
      * @param core object to allow interaction with all javascript components
@@ -260,6 +269,23 @@
 		footer.setWidget(0, 2, previous);
 		footer.setWidget(0, 3, next);
 		footer.addStyleName("center");
+		
+		//This Panel will hold the name of the export-file
+		//for download from the ExportServlet
+		final FormPanel exportFormPanel = new FormPanel("_sel");
+		final Hidden hiddenVal = new Hidden();
+		hiddenVal.setName("ExportedFilename");
+		exportFormPanel.add(hiddenVal);
+		
+		Image export = new Image(constants.exportImage());
+		export.addClickHandler(new ClickHandler() {
+			public void onClick(ClickEvent event) {
+				//create KML file
+				createKMLFile();
+			}
+		});
+		export.addStyleName("export");
+		export.setTitle(textConstants.exportDataSet());
 
 		Image delete = new Image(constants.deleteImage());
 		delete.addClickHandler(new ClickHandler() {
@@ -270,8 +296,11 @@
 		delete.addStyleName("delete");
 		delete.setTitle(textConstants.deleteDataSet());
 		
-		Grid func = new Grid(1,1);
-		func.setWidget(0, 0, delete);
+		Grid func = new Grid(1,3);
+		//the FormPanel is invisible, see above
+		func.setWidget(0, 0, exportFormPanel);
+		func.setWidget(0, 1, export);
+		func.setWidget(0, 2, delete);
 		func.addStyleName("center");
 		
 		Grid tableControls = new Grid(1,4);
@@ -461,6 +490,65 @@
 	}
 	
     /**
+     * Creates a (preliminary) KML file from the data in the current data set  
+    */
+	private void createKMLFile(){
+		String kmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://www.opengis.net/kml/2.2\"><Document>";
+		
+		for( int i=0; i<this.dataSet.getObjects().length(); i++){
+			DataObject currentDataObject = this.dataSet.getObjects().get(i);
+			
+			String name = currentDataObject.getName();
+			String description = currentDataObject.getDescription();
+			String place = currentDataObject.getPlace();
+			String lat = currentDataObject.getLat();
+			String lon = currentDataObject.getLon();
+			String timeStamp = currentDataObject.getTimeStamp();
+			String timeSpanStart = currentDataObject.getTimeSpanStart();
+			String timeSpanEnd = currentDataObject.getTimeSpanEnd();
+			
+			String kmlEntry = "<Placemark>";
+
+			kmlEntry += "<name><![CDATA[" + name + "]]></name>";
+			kmlEntry += "<address><![CDATA[" + place + "]]></address>";
+			kmlEntry += "<description><![CDATA[" + description + "]]></description>";
+			kmlEntry += "<Point><coordinates>" + lon + "," + lat + "</coordinates></Point>";
+			
+			if ( (timeStamp != null) && (timeStamp != "undefined") ) {
+				kmlEntry += "<TimeStamp><when>" + timeStamp + "</when></TimeStamp>";
+			} else {
+				kmlEntry += "<TimeSpan><begin>" + timeSpanStart + "</begin><end>" + timeSpanEnd + "</end></TimeSpan>";				
+			}
+			
+
+			kmlEntry += "</Placemark>";
+			
+			kmlContent += kmlEntry;
+		}
+		
+		kmlContent += "</Document></kml>";
+
+        exportWriter = GWT.create(ExportWriterInterface.class);
+        
+		exportWriter.writeKMLFile(kmlContent, 
+									new AsyncCallback<String>() {
+            							public void onFailure(Throwable caught) {
+            								int i = 1;
+            							}
+            							public void onSuccess(String exportFileName) {
+            				                hiddenVal.setValue(exportFileName);
+
+            				                String url = GWT.getModuleBaseURL();
+            				                url = url + "ExportServlet";
+
+            				                exportFormPanel.setAction(url);
+            				                exportFormPanel.submit();
+            							}
+									}
+            					);
+	}
+	
+    /**
      * Checks, if the previous or next button has to be hidden
     */
 	private void checkHide(){
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/econnect/wp3_3/server/ExportServlet.java	Mon Nov 26 14:40:43 2012 +0100
@@ -0,0 +1,65 @@
+package econnect.wp3_3.server;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class ExportServlet extends HttpServlet {
+	
+	   private void streamFileTo(HttpServletResponse response, File file)
+	           throws IOException, FileNotFoundException {
+		   response.setBufferSize(32768); 
+	       int bufSize = response.getBufferSize(); 
+
+	       OutputStream os = response.getOutputStream();
+	       FileInputStream fis = new FileInputStream(file);
+	       byte[] buffer = new byte[bufSize];
+	       int bytesRead = 0;
+	       while ((bytesRead = fis.read(buffer)) > 0) {
+	           os.write(buffer, 0, bytesRead);
+	       }
+	       os.flush();
+	       fis.close();
+	   }
+
+	   private void prepareResponseFor(HttpServletResponse response, File file) {
+	       StringBuilder type = new StringBuilder("attachment; filename=");
+	       type.append(file.getName());
+	       response.setContentLength((int) file.length());
+	       response.setContentType("application/octet-stream");
+	       response.setHeader("Content-Disposition", type.toString());
+	   }
+	   
+	   public void doGet(HttpServletRequest request, HttpServletResponse response)
+            throws ServletException {
+
+        OutputStream out = null;
+        File exportedFile = null;
+        try {
+            String fileName = request.getParameter("ExportedFilename");
+            exportedFile = new File("kmlexport/" + fileName);
+            
+            prepareResponseFor(response, exportedFile);
+            streamFileTo(response, exportedFile);
+            
+        } catch (Exception e) {
+            throw new ServletException("Exception in Export Servlet", e);
+        } finally {
+            if (out != null) {
+            	try {out.close();} catch(Exception e){}
+            }
+            
+            if (exportedFile != null) {
+                exportedFile.delete();
+            }
+            
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/econnect/wp3_3/server/ExportWriterImpl.java	Mon Nov 26 14:40:43 2012 +0100
@@ -0,0 +1,39 @@
+package econnect.wp3_3.server;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import com.google.gwt.user.server.rpc.RemoteServiceServlet;
+
+import econnect.wp3_3.client.services.ExportWriterInterface;
+
+
+public class ExportWriterImpl extends RemoteServiceServlet implements ExportWriterInterface {
+
+	@Override
+	public String writeKMLFile(String kmlFileContent) {
+
+        String fileName = "test.kml";
+        
+        File exportFolder = new File("kmlexport");
+        
+        if (!exportFolder.exists()) {
+            exportFolder.mkdir();
+        }
+        
+        File kmlFile = new File("kmlexport/" + fileName); 
+        
+        try {
+	        BufferedWriter out = new BufferedWriter(new FileWriter(kmlFile));
+	        out.write(kmlFileContent);
+	        out.close();
+        } catch (IOException e) {
+        	return null;
+        }
+		
+		return(fileName);
+	}
+
+}