# HG changeset patch # User Raimund Renkert # Date 1335340738 0 # Node ID 88b51e6e03345bfe22a01ec46de674862fde30cc # Parent f905c936ffff9ed97f63d2a33b94afc39cbaa560 Issue 657. Added UI and service for shapefile upload. flys-client/trunk@4294 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r f905c936ffff -r 88b51e6e0334 flys-client/ChangeLog --- 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 + + 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 * src/main/java/de/intevation/flys/client/server/FixingsKMChartServiceImpl.java: diff -r f905c936ffff -r 88b51e6e0334 flys-client/pom.xml --- 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 @@ 2.6 + commons-fileupload + commons-fileupload + 1.2.1 + + org.gwtopenmaps.openlayers gwt-openlayers-client 0.6 diff -r f905c936ffff -r 88b51e6e0334 flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java --- 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(); diff -r f905c936ffff -r 88b51e6e0334 flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties --- 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 diff -r f905c936ffff -r 88b51e6e0334 flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties --- 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 diff -r f905c936ffff -r 88b51e6e0334 flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties --- 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 diff -r f905c936ffff -r 88b51e6e0334 flys-client/src/main/java/de/intevation/flys/client/client/ui/DigitizePanel.java --- 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 it = new LinkedHashMap(); + + 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(""); + + 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(); diff -r f905c936ffff -r 88b51e6e0334 flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java --- /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; + } +} diff -r f905c936ffff -r 88b51e6e0334 flys-client/src/main/webapp/WEB-INF/web.xml --- 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 @@ /flys/map-urls + + FileUpload + de.intevation.flys.client.server.FileUploadServiceImpl + + + + FileUpload + /flys/fileupload + + FLYS.html