changeset 673:93b4dedc4e37

Implemented a mechanism to import artifacts from xml structured documents. gnv/trunk@801 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 19 Mar 2010 10:04:28 +0000
parents e9d54c989110
children 5b195ee3b307
files gnv/ChangeLog gnv/src/main/java/de/intevation/gnv/action/LoadAction.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/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
diffstat 8 files changed, 145 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/gnv/ChangeLog	Fri Mar 19 08:51:45 2010 +0000
+++ b/gnv/ChangeLog	Fri Mar 19 10:04:28 2010 +0000
@@ -1,3 +1,29 @@
+2010-03-19  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/action/LoadAction.java: Use the uploaded
+	  file to start an artifact import. Create a new Artifact object if the
+	  import was successful and fetch the current artifact description with user
+	  interface part to restore the gui. Display an error message if something 
+	  failed while importing.
+
+	* src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java,
+	  src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java:
+	  Added a new method doImport to import artifacts from xml documents. A
+	  describe document without ui part is returned if an import was successful.
+
+	* src/main/resources/applicationMessages.properties,
+	  src/main/resources/applicationMessages_en.properties: Added error messages
+	  for the case that something failed while importing artifacts from xml
+	  documents.
+
+	* src/main/webapp/WEB-INF/jsp/header.jsp: Replace whitespaces from exception
+	  request parameter with dots. Exceptions thrown by the artifact server
+	  corresponds to a specific resource key. Failures coming from the artifact
+	  server are displayed language specific on this way.
+
+	* src/main/webapp/WEB-INF/config/struts-config.xml: Added missing forwards
+	  to import action.
+
 2010-03-19  Tim Englich  <tim.englich@intevation.de>
 
 	* src/main/java/de/intevation/gnv/artifactdatabase/objects/ParametrizedArtifactFactory.java (addParameters):
--- a/gnv/src/main/java/de/intevation/gnv/action/LoadAction.java	Fri Mar 19 08:51:45 2010 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/action/LoadAction.java	Fri Mar 19 10:04:28 2010 +0000
@@ -1,10 +1,17 @@
 package de.intevation.gnv.action;
 
-import de.intevation.gnv.propertiesreader.PropertiesReaderFactory;
-import de.intevation.gnv.propertiesreader.PropertiesReader;
+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.Artifact;
+import de.intevation.gnv.artifactdatabase.objects.ArtifactFactory;
+import de.intevation.gnv.util.ArtifactNamespaceContext;
 import de.intevation.gnv.util.XMLUtils;
 
 import java.io.InputStream;
+import java.io.IOException;
+import java.util.List;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -17,7 +24,7 @@
 
 import org.apache.struts.action.ActionForm;
 import org.apache.struts.action.ActionForward;
-import org.apache.struts.action.ActionMapping;        
+import org.apache.struts.action.ActionMapping;
 
 import org.w3c.dom.Document;
 
@@ -26,7 +33,8 @@
  */
 public class LoadAction extends ArtifactDatabaseActionBase {
 
-    public static final String RESOURCE_UPLOAD_FAILURE = "upload.failure";
+    public static final String RESOURCE_UPLOAD_FAILURE   = "upload.failure";
+    public static final String RESOURCE_INVALID_ARTIFACT = "invalid.artifact";
 
     private static Logger logger = Logger.getLogger(LoadAction.class);
 
@@ -46,32 +54,88 @@
         ServletFileUpload upload  = new ServletFileUpload();
         Document artifactDocument = null;
 
-        FileItemIterator iter = upload.getItemIterator(request);
-        while (iter.hasNext()) {
-            FileItemStream item = (FileItemStream) iter.next();
-            String name         = item.getFieldName();
+        try {
+            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);
+                // the file input field of our form is named 'document'
+                if (name.equals("document")) {
+                    InputStream stream = item.openStream();
+
+                    if (stream.available() == 0) {
+                        logger.error("No artifact document found.");
+                        request.setAttribute(
+                            CommunicationKeys.REQUEST_EXCEPTION_PROJECT,
+                            RESOURCE_UPLOAD_FAILURE);
+
+                        return super.getExceptionForward(mapping);
+                    }
+
+                    artifactDocument   = XMLUtils.readDocument(stream);
+                    if (artifactDocument == null) {
+                        request.setAttribute(
+                            CommunicationKeys.REQUEST_EXCEPTION_PROJECT,
+                            RESOURCE_INVALID_ARTIFACT);
+
+                        return super.getExceptionForward(mapping);
+                    }
+
+                    ArtifactDatabaseClientFactory adcf =
+                        ArtifactDatabaseClientFactory.getInstance();
+                    ArtifactDatabaseClient adc =
+                        adcf.getArtifactDatabaseClient(getLocale(request));
+                    ArtifactFactory factory = (ArtifactFactory)
+                        (( List)adc.getArtifactFactories()).get(0);
+
+                    SessionModelFactory sf = SessionModelFactory.getInstance();
+                    SessionModel        sm = sf.getSessionModel(request);
+                    sm.selectArtifactFactory(factory.getId());
+
+                    Document describe = adc.doImport(
+                        factory,
+                        artifactDocument);
+
+                    String uuid = XMLUtils.xpathString(
+                        describe,
+                        "/art:result/art:uuid/@value",
+                        ArtifactNamespaceContext.INSTANCE);
+                    String hash = XMLUtils.xpathString(
+                        describe,
+                        "/art:result/art:hash/@value",
+                        ArtifactNamespaceContext.INSTANCE);
+
+                    Artifact artifact = new Artifact(uuid, hash);
+                    sm.setCurrentArtifact(artifact);
+
+                    if (logger.isDebugEnabled()) {
+                        logger.debug("Imported artifact UUID: "+artifact.getId());
+                        logger.debug("Imported artifact HASH: "+artifact.getHash());
+                    }
+
+                    return new DescribeUIAction().execute(
+                        mapping, form, request, response);
+                }
             }
         }
-
-        // no document found or not valid
-        if (artifactDocument == null) {
-            logger.error("Upload failure: No document found or invalid.");
-
+        catch (IOException ioe) {
+            logger.error(ioe, ioe);
             request.setAttribute(
                 CommunicationKeys.REQUEST_EXCEPTION_PROJECT,
-                RESOURCE_UPLOAD_FAILURE);
+                ioe.getMessage());
 
             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);
+        // no document found or not valid
+        logger.error("Upload failure: No document found or invalid.");
+
+        request.setAttribute(
+            CommunicationKeys.REQUEST_EXCEPTION_PROJECT,
+            RESOURCE_UPLOAD_FAILURE);
+
+        return super.getExceptionForward(mapping);
     }
 }
 // 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	Fri Mar 19 08:51:45 2010 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java	Fri Mar 19 10:04:28 2010 +0000
@@ -10,6 +10,7 @@
 import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseClientException;
 import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseInputException;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactDescription;
+import de.intevation.gnv.artifactdatabase.objects.ArtifactFactory;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactObject;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactStatisticsSet;
 import de.intevation.gnv.artifactdatabase.objects.InputParameter;
@@ -115,6 +116,12 @@
         OutputStream   out)
     throws ArtifactDatabaseClientException;
 
+
+    public Document doImport(
+        ArtifactFactory factory,
+        Document        document)
+    throws ArtifactDatabaseClientException, IOException;
+
     /**
      * @param factory
      * @param artifact
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Fri Mar 19 08:51:45 2010 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Fri Mar 19 10:04:28 2010 +0000
@@ -257,9 +257,6 @@
      */
     private InputStream doPostRequest(String requestUrl, Document requestBody)
                                                                               throws IOException {
-        log.debug("##################################################");
-        log.debug(new XMLUtils().writeDocument2String(requestBody));
-        log.debug("##################################################");
         Client client = new Client(Protocol.HTTP);
         Request request = initialize(new Request(Method.POST, requestUrl));
         String documentBody = new XMLUtils().writeDocument2String(requestBody);
@@ -394,6 +391,7 @@
     throws ArtifactDatabaseClientException, ArtifactDatabaseInputException {
         try {
             String url = this.getArtifactUrl(artifactFactory, currentArtifact);
+            log.debug("DESCRIBE URL: " + url);
             Document request = this.createDescribeRequestBody(currentArtifact,
                                                               includeUI);
             InputStream describeResult = this.doPostRequest(url, request);
@@ -729,17 +727,13 @@
     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);
         }
@@ -747,6 +741,18 @@
     }
 
 
+    public Document doImport(
+        ArtifactFactory factory,
+        Document        document)
+    throws ArtifactDatabaseClientException, IOException
+    {
+        String url           = factory.getDataBaseUrl() + "/import";
+        InputStream response = doPostRequest(url, document);
+
+        return XMLUtils.readDocument(response);
+    }
+
+
     private Document createOutRequestBody(
         ArtifactObject             currentArtifact,
         String                     target,
--- a/gnv/src/main/resources/applicationMessages.properties	Fri Mar 19 08:51:45 2010 +0000
+++ b/gnv/src/main/resources/applicationMessages.properties	Fri Mar 19 10:04:28 2010 +0000
@@ -104,3 +104,6 @@
 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.
+invalid.artifact=Ung\u00fcltiges Artefakt Dokument gefunden.
+mismatching.checksum=Ung\u00fcltiges Artefakt Dokument gefunden.
+no.such.factory=Ung\u00fcltiges Artefakt Dokument gefunden.
--- a/gnv/src/main/resources/applicationMessages_en.properties	Fri Mar 19 08:51:45 2010 +0000
+++ b/gnv/src/main/resources/applicationMessages_en.properties	Fri Mar 19 10:04:28 2010 +0000
@@ -102,3 +102,6 @@
 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.
+invalid.artifact=Invalid artifact found.
+mismatching.checksum=Invalid artifact found.
+no.such.factory=Invalid artifact found.
--- a/gnv/src/main/webapp/WEB-INF/config/struts-config.xml	Fri Mar 19 08:51:45 2010 +0000
+++ b/gnv/src/main/webapp/WEB-INF/config/struts-config.xml	Fri Mar 19 10:04:28 2010 +0000
@@ -54,6 +54,12 @@
            <forward
                 name="failure"
                 path="/WEB-INF/jsp/mainlayout.jsp"/>
+           <forward
+               name="back"
+               path="/gnv/back.do"/>
+           <forward
+               name="selectfis"
+               path="/gnv/selectFis.do"/>
         </action>
         <action path="/start" 
                 type="de.intevation.gnv.action.FetchArtifactFactoriesAction"
--- a/gnv/src/main/webapp/WEB-INF/jsp/header.jsp	Fri Mar 19 08:51:45 2010 +0000
+++ b/gnv/src/main/webapp/WEB-INF/jsp/header.jsp	Fri Mar 19 10:04:28 2010 +0000
@@ -11,6 +11,8 @@
     boolean showmapviewercallBody = request.getAttribute("MAPVIEWERCALL") != null;
 
     String exceptionProject = (String) request.getAttribute(CommunicationKeys.REQUEST_EXCEPTION_PROJECT);
+    exceptionProject        = exceptionProject != null ? exceptionProject.toLowerCase() : null;
+    exceptionProject        = exceptionProject != null ? exceptionProject.replaceAll(" ", ".") : null;
 %>
 <%@page import="de.intevation.gnv.action.CommunicationKeys"%><html:xhtml />
 <!-- Kopfleiste-->

http://dive4elements.wald.intevation.org