Mercurial > dive4elements > gnv-client
changeset 670:b89b31293772
Implemented first things to store/load projects.
gnv/trunk@793 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 17 Mar 2010 13:31:38 +0000 |
parents | ef1ff5fdab5b |
children | fbbf2ffde11f |
files | gnv/ChangeLog gnv/pom.xml gnv/src/main/java/de/intevation/gnv/action/CommunicationKeys.java gnv/src/main/java/de/intevation/gnv/action/LoadAction.java gnv/src/main/java/de/intevation/gnv/action/StoreAction.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java gnv/src/main/resources/applicationMessages.properties gnv/src/main/resources/applicationMessages_en.properties gnv/src/main/webapp/WEB-INF/config/struts-config.xml gnv/src/main/webapp/WEB-INF/jsp/header.jsp gnv/src/main/webapp/styles/default.css |
diffstat | 13 files changed, 333 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- 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 <ingo.weinzierl@intevation.de> + + 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 <ingo.weinzierl@intevation.de> * src/main/webapp/WEB-INF/config/templates/describe-ui.xsl: Changed the
--- 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 @@ </repository> </repositories> <dependencies> + <dependency> + <groupId>commons-fileupload</groupId> + <artifactId>commons-fileupload</artifactId> + <version>1.2.1</version> + </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId>
--- 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"; }
--- /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 :
--- /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 :
--- 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> inputParameter) throws ArtifactDatabaseClientException; + + public void doExport( + ArtifactObject artifactFactory, + ArtifactObject currentArtifact, + OutputStream out) + throws ArtifactDatabaseClientException; + /** * @param factory * @param artifact
--- 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 <tim.englich@intevation.de> - * + * @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,
--- 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:
--- 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.
--- 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.
--- 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"/> </action> + <action path="/store" + type="de.intevation.gnv.action.StoreAction" + scope="request" + validate="false"> + <forward + name="success" + path="/WEB-INF/jsp/mainlayout.jsp"/> + <forward + name="failure" + path="/WEB-INF/jsp/mainlayout.jsp"/> + </action> + <action path="/load" + type="de.intevation.gnv.action.LoadAction" + scope="request" + validate="false"> + <forward + name="success" + path="/WEB-INF/jsp/mainlayout.jsp"/> + <forward + name="failure" + path="/WEB-INF/jsp/mainlayout.jsp"/> + </action> <action path="/start" type="de.intevation.gnv.action.FetchArtifactFactoriesAction" scope="request" @@ -87,6 +109,12 @@ <forward name="selectfis" path="/gnv/selectFis.do"/> + <forward + name="store" + path="/gnv/store.do"/> + <forward + name="load" + path="/gnv/load.do"/> </action> <action path="/out" type="de.intevation.gnv.action.DoOutputAction"
--- a/gnv/src/main/webapp/WEB-INF/jsp/header.jsp Tue Mar 16 10:30:13 2010 +0000 +++ b/gnv/src/main/webapp/WEB-INF/jsp/header.jsp Wed Mar 17 13:31:38 2010 +0000 @@ -9,7 +9,8 @@ exceptionMessage = (exceptionMessage != null && exceptionMessage.toString().endsWith(".") ? exceptionMessage.toString().substring(0,exceptionMessage.toString().length()-1) : exceptionMessage); boolean showmapviewercallBody = request.getAttribute("MAPVIEWERCALL") != null; - + + String exceptionProject = (String) request.getAttribute(CommunicationKeys.REQUEST_EXCEPTION_PROJECT); %> <%@page import="de.intevation.gnv.action.CommunicationKeys"%><html:xhtml /> <!-- Kopfleiste--> @@ -24,15 +25,26 @@ </a> </h1> - <div id="project"> - <label style="font-weight:bold;"> - <bean:message key="gnviewer.project.load"/> - | - <bean:message key="gnviewer.project.save" /> - </label> - <div id="project_load"> + <div id="project"> + <table> + <tr> + <td> + <form id="storeProject" action="<%=response.encodeURL("store.do")%>" method="post"> + <input type="submit" name="storeProject" value="<bean:message key="gnviewer.project.save"/>"/> + </form> + </td> + <td> + <form id="loadProject" action="<%=response.encodeURL("load.do")%>" method="post" enctype="multipart/form-data"> + <input type="submit" name="loadProject" value="<bean:message key="gnviewer.project.load"/>"/> + <input type="file" name="document" accept="application/xml"/> + </form> + </td> + </tr> + </table> + <%if (exceptionProject != null) {%> + <div class="projectException"><bean:message key="<%=exceptionProject%>"/></div> + <%}%> - </div> <br/> <%if (showmapviewercallBody){ %> <jsp:include page="includes/display_mapviewercall_inc.jsp" />
--- 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; }