# HG changeset patch # User Ingo Weinzierl # Date 1268832698 0 # Node ID b89b312937723a014d5afa86ead1e185457d2f87 # Parent ef1ff5fdab5b1449666efe66df1b67ebc69c6858 Implemented first things to store/load projects. gnv/trunk@793 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r ef1ff5fdab5b -r b89b31293772 gnv/ChangeLog --- a/gnv/ChangeLog Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/ChangeLog Wed Mar 17 13:31:38 2010 +0000 @@ -1,3 +1,45 @@ +2010-03-17 Ingo Weinzierl + + Issue208 First steps for exporting artifacts. + + * src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java, + src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java: + New method to export an artifact. + + * pom.xml: Added Apache common-fileupload 1.2.1 lib. + + * src/main/java/de/intevation/gnv/action/CommunicationKeys.java: Further + error message key added which is displayed beneath project load/store + buttons if an error occured while these operations. + + * src/main/resources/applicationMessages.properties, + src/main/resources/applicationMessages_en.properties: Added error messages + for errors which occure while loading/storing projects. + + * src/main/webapp/styles/default.css: Added a new style class to adjust + error messages which may occur while loading/storing projects. + + * src/main/java/de/intevation/gnv/action/StoreAction.java: New controller + which triggers an artifact export. After pushing the corresponding button, + the artifact is returned as xml document and a file dialog is displayed. + + * src/main/java/de/intevation/gnv/action/LoadAction.java: New controller to + import artifacts which have former been exported. XML documents are + successfully read from fileupload. + TODO: Use these documents to create artifacts. No artifacts are loaded + yet! + + * src/main/java/de/intevation/gnv/util/XMLUtils.java: Added a method to + write documents to a stream. + + * src/main/webapp/WEB-INF/config/struts-config.xml: Added LoadAction and + StoreAction. The controller are available under /gnv/load and /gnv/store. + + * src/main/webapp/WEB-INF/jsp/header.jsp: Removed placeholder string for + storing/loading the current project state and added buttons to start + an export of the current project's state (the current artifact) or reload + an artifact from a xml document. + 2010-03-16 Ingo Weinzierl * src/main/webapp/WEB-INF/config/templates/describe-ui.xsl: Changed the diff -r ef1ff5fdab5b -r b89b31293772 gnv/pom.xml --- a/gnv/pom.xml Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/pom.xml Wed Mar 17 13:31:38 2010 +0000 @@ -18,6 +18,11 @@ + + commons-fileupload + commons-fileupload + 1.2.1 + junit junit diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/java/de/intevation/gnv/action/CommunicationKeys.java --- a/gnv/src/main/java/de/intevation/gnv/action/CommunicationKeys.java Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/CommunicationKeys.java Wed Mar 17 13:31:38 2010 +0000 @@ -13,4 +13,5 @@ public final static String REQUEST_EXCEPTION_INPUT_ID = "request_exception_input_id"; public final static String REQUEST_EXCEPTION_MESSAGE = "request_exception_message"; public final static String REQUEST_EXCEPTION_VALUE = "request_exception_value"; + public final static String REQUEST_EXCEPTION_PROJECT = "request_exception_project"; } diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/java/de/intevation/gnv/action/LoadAction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/LoadAction.java Wed Mar 17 13:31:38 2010 +0000 @@ -0,0 +1,77 @@ +package de.intevation.gnv.action; + +import de.intevation.gnv.propertiesreader.PropertiesReaderFactory; +import de.intevation.gnv.propertiesreader.PropertiesReader; +import de.intevation.gnv.util.XMLUtils; + +import java.io.InputStream; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.fileupload.FileItemIterator; +import org.apache.commons.fileupload.FileItemStream; +import org.apache.commons.fileupload.servlet.ServletFileUpload; + +import org.apache.log4j.Logger; + +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; + +import org.w3c.dom.Document; + +/** + * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) + */ +public class LoadAction extends ArtifactDatabaseActionBase { + + public static final String RESOURCE_UPLOAD_FAILURE = "upload.failure"; + + private static Logger logger = Logger.getLogger(LoadAction.class); + + public LoadAction() { + super(); + } + + public ActionForward execute( + ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws Exception + { + logger.info("Import artifact."); + + ServletFileUpload upload = new ServletFileUpload(); + Document artifactDocument = null; + + FileItemIterator iter = upload.getItemIterator(request); + while (iter.hasNext()) { + FileItemStream item = (FileItemStream) iter.next(); + String name = item.getFieldName(); + + if (name.equals("document")) { + InputStream stream = item.openStream(); + artifactDocument = XMLUtils.readDocument(stream); + } + } + + // no document found or not valid + if (artifactDocument == null) { + logger.error("Upload failure: No document found or invalid."); + + request.setAttribute( + CommunicationKeys.REQUEST_EXCEPTION_PROJECT, + RESOURCE_UPLOAD_FAILURE); + + return super.getExceptionForward(mapping); + } + + // TODO Do something with our nice xml document + // Use DescribeUI Action to set required attributes which are parsed in + // jsp files. + return super.execute(mapping, form, request, response); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/java/de/intevation/gnv/action/StoreAction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/StoreAction.java Wed Mar 17 13:31:38 2010 +0000 @@ -0,0 +1,87 @@ +package de.intevation.gnv.action; + +import de.intevation.gnv.action.sessionmodel.SessionModel; +import de.intevation.gnv.action.sessionmodel.SessionModelFactory; + +import de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient; +import de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClientFactory; + +import de.intevation.gnv.artifactdatabase.objects.ArtifactObject; + +import java.io.OutputStream; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; + +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; + +/** + * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) + */ +public class StoreAction extends ArtifactDatabaseActionBase { + + public static final String RESOURCE_DOWNLOAD_FAILURE = "no.artifact.chosen"; + + private static Logger logger = Logger.getLogger(StoreAction.class); + + public StoreAction() { + super(); + } + + public ActionForward execute( + ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws Exception + { + SessionModelFactory sf = SessionModelFactory.getInstance(); + SessionModel sm = sf.getSessionModel(request); + + ArtifactDatabaseClientFactory adcf = + ArtifactDatabaseClientFactory.getInstance(); + ArtifactDatabaseClient adc = + adcf.getArtifactDatabaseClient(getLocale(request)); + ArtifactObject artifact = sm.getCurrentArtifact(); + + // no artifact set at the moment (which means, the user didn't select a + // fis yet. + if (artifact == null) { + logger.warn("No artifact/fis selected yet."); + + request.setAttribute( + CommunicationKeys.REQUEST_EXCEPTION_PROJECT, + RESOURCE_DOWNLOAD_FAILURE); + + return super.execute(mapping, form, request, response); + } + + logger.info("Export artifact " + artifact.getId()); + setHeaders(response, artifact.getId()); + + OutputStream out = response.getOutputStream(); + adc.doExport( + sm.getSelectedArtifactFactory(), + artifact, + out); + + out.flush(); + out.close(); + + return null; + } + + protected void setHeaders(HttpServletResponse response, String uuid) { + String filename = "GNVArtefakt_" + uuid + ".xml"; + + response.setHeader("Content-Type", "application/xml"); + response.setHeader( + "Content-Disposition", + "attachment;filename=" + filename); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java --- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java Wed Mar 17 13:31:38 2010 +0000 @@ -108,6 +108,13 @@ Collection inputParameter) throws ArtifactDatabaseClientException; + + public void doExport( + ArtifactObject artifactFactory, + ArtifactObject currentArtifact, + OutputStream out) + throws ArtifactDatabaseClientException; + /** * @param factory * @param artifact diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java --- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Wed Mar 17 13:31:38 2010 +0000 @@ -1,6 +1,3 @@ -/** - * - */ package de.intevation.gnv.artifactdatabase.client; import java.io.IOException; @@ -55,8 +52,8 @@ import de.intevation.gnv.util.XMLUtils; /** - * @author Tim Englich - * + * @author Tim Englich (tim.englich@intevation.de) + * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) */ public class DefaultArtifactDatabaseClient implements ArtifactDatabaseClient { /** @@ -724,6 +721,32 @@ } } + + public void doExport( + ArtifactObject artifactFactory, + ArtifactObject currentArtifact, + OutputStream out) + throws ArtifactDatabaseClientException + { + try { + //String url = getArtifactUrl(artifactFactory, currentArtifact); + String url = ((ArtifactFactory) artifactFactory).getDataBaseUrl(); + url += "/export/" + currentArtifact.getId(); + log.debug("+++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + log.debug("REQUEST: " + url); + Document response = doGetRequest(url); + + XMLUtils.toStream(response, out); + } + catch (Exception e) { + log.debug("THIS IS MY ERROR."); + log.error(e, e); + throw new ArtifactDatabaseClientException(e); + } + + } + + private Document createOutRequestBody( ArtifactObject currentArtifact, String target, diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java --- a/gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java Wed Mar 17 13:31:38 2010 +0000 @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.StringBufferInputStream; import java.io.StringWriter; @@ -219,5 +220,27 @@ transformer.transform(source, result); return sw.getBuffer().toString(); } + + public static boolean toStream(Document document, OutputStream out) { + try { + Transformer transformer = + TransformerFactory.newInstance().newTransformer(); + DOMSource source = new DOMSource(document); + StreamResult result = new StreamResult(out); + transformer.transform(source, result); + return true; + } + catch (TransformerConfigurationException tce) { + logger.error(tce.getLocalizedMessage(), tce); + } + catch (TransformerFactoryConfigurationError tfce) { + logger.error(tfce.getLocalizedMessage(), tfce); + } + catch (TransformerException te) { + logger.error(te.getLocalizedMessage(), te); + } + + return false; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/resources/applicationMessages.properties --- a/gnv/src/main/resources/applicationMessages.properties Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/src/main/resources/applicationMessages.properties Wed Mar 17 13:31:38 2010 +0000 @@ -102,3 +102,5 @@ input.is.not.valid.for.this.state=Der von Ihnen eingegebene Wert ist ung\u00fcltig. Bitte versuchen Sie es erneut. input.not.a.integer= ist keine g\u00fcltige Ganzzahl. Bitte versuchen Sie es erneut. input.not.a.double= ist keine g\u00fcltige Flie\u00dfkommazahl. Bitte versuchen Sie es erneut. +upload.failure=Beim Hochladen der Datei ist ein Fehler aufgetreten. +no.artifact.chosen=Es ist noch kein FIS gew\u00e4hlt. diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/resources/applicationMessages_en.properties --- a/gnv/src/main/resources/applicationMessages_en.properties Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/src/main/resources/applicationMessages_en.properties Wed Mar 17 13:31:38 2010 +0000 @@ -100,3 +100,5 @@ input.is.not.valid.for.this.state=You entered an invalid value. Please try again. input.not.a.integer= is not a valid integer. Please try again. input.not.a.double= is not a valid floating-point number. Please try again. +upload.failure=An error occured while uploading file. +no.artifact.chosen=No fis selected yet. diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/webapp/WEB-INF/config/struts-config.xml --- a/gnv/src/main/webapp/WEB-INF/config/struts-config.xml Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/src/main/webapp/WEB-INF/config/struts-config.xml Wed Mar 17 13:31:38 2010 +0000 @@ -33,6 +33,28 @@ name="success" path="/WEB-INF/jsp/mainlayout.jsp"/> + + + + + + + + + + <%@page import="de.intevation.gnv.action.CommunicationKeys"%> @@ -24,15 +25,26 @@ -
- -
+
+ + + + + +
+
" method="post"> + "/> +
+
+
" method="post" enctype="multipart/form-data"> + "/> + +
+
+ <%if (exceptionProject != null) {%> +
+ <%}%> -

<%if (showmapviewercallBody){ %> diff -r ef1ff5fdab5b -r b89b31293772 gnv/src/main/webapp/styles/default.css --- a/gnv/src/main/webapp/styles/default.css Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/src/main/webapp/styles/default.css Wed Mar 17 13:31:38 2010 +0000 @@ -211,11 +211,19 @@ position:absolute; top:27px; left:10px; - width: 400px; + width: 500px; text-align: left; + font-size: 1.2em; } +.projectException { + color: red; + font-size: 10px; + font-weight: bold; + margin: 2px 2px; +} + #load_error { position:absolute; top:27px; @@ -398,7 +406,7 @@ } div#basefilter { - margin-top: 30px; + margin-top: 50px; width: 325px; }