changeset 121:e0ded17a4846

Implemented the feed() operation. flys-artifacts/trunk@1442 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 09 Mar 2011 14:13:57 +0000
parents 5243ac559e16
children d3b8b0b1d010
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java
diffstat 3 files changed, 141 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Mar 09 14:07:03 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Mar 09 14:13:57 2011 +0000
@@ -1,3 +1,16 @@
+2011-03-09  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java:
+	  Implemented the abstract method getName(). It returns the constant
+	  'winfo' string.
+
+	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java:
+	  Implemented the feed action. The data of an incoming feed() operation is
+	  stored in StateData objects that are saved in a map in the artifact.
+
+	  NOTE: There is no input validation and no i18n of error messages (see
+	  TODO).
+
 2011-03-09  Ingo Weinzierl <ingo@intevation.de>
 
 	* TODO: This file contains some open points that need to be done.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Wed Mar 09 14:07:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Wed Mar 09 14:13:57 2011 +0000
@@ -1,10 +1,17 @@
 package de.intevation.flys.artifacts;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+
+import javax.xml.xpath.XPathConstants;
 
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.CallContext;
@@ -13,6 +20,7 @@
 import de.intevation.artifacts.common.utils.XMLUtils;
 
 import de.intevation.artifactdatabase.DefaultArtifact;
+import de.intevation.artifactdatabase.data.DefaultStateData;
 import de.intevation.artifactdatabase.data.StateData;
 import de.intevation.artifactdatabase.state.State;
 import de.intevation.artifactdatabase.state.StateEngine;
@@ -31,8 +39,9 @@
     private static Logger logger = Logger.getLogger(FLYSArtifact.class);
 
 
-    /** The XPath to the name of the artifact in its configuration. */
-    public static final String XPATH_ARTIFACT_NAME = "@name";
+    /** The XPath that points to the input data elements of the FEED document.*/
+    public static final String XPATH_FEED_INPUT =
+        "/art:action/art:data/art:input";
 
 
     /** The identifier of the current state. */
@@ -41,6 +50,39 @@
     /** The name of the artifact.*/
     protected String name;
 
+    /** The data that have been inserted into this artifact.*/
+    protected Map<String, StateData> data;
+
+
+    /**
+     * The default constructor that creates an empty FLYSArtifact.
+     */
+    public FLYSArtifact() {
+        data = new HashMap<String, StateData>();
+    }
+
+
+    /**
+     * Returns the name of the concrete artifact.
+     *
+     * @return the name of the concrete artifact.
+     */
+    public abstract String getName();
+
+
+    /**
+     * Returns the FLYSContext from context object.
+     *
+     * @param context The CallContext or the FLYSContext.
+     *
+     * @return the FLYSContext.
+     */
+    protected FLYSContext getFlysContext(Object context) {
+        return context instanceof FLYSContext
+            ? (FLYSContext) context
+            : (FLYSContext) ((CallContext) context).globalContext();
+    }
+
 
     /**
      * Initialize the artifact and insert new data if <code>data</code> contains
@@ -62,14 +104,13 @@
 
         super.setup(identifier, factory, context, data);
 
-        String name = XMLUtils.xpathString(
-            data, XPATH_ARTIFACT_NAME, ArtifactNamespaceContext.INSTANCE);
-        setName(name);
-
         FLYSContext flysContext = (FLYSContext) context;
         StateEngine engine      = (StateEngine) flysContext.get(
             FLYSContext.STATE_ENGINE_KEY);
 
+        String name = getName();
+
+        logger.debug("Set initial state for artifact '" + name + "'");
         List<State> states = engine.getStates(name);
 
         setCurrentState(states.get(0));
@@ -86,32 +127,29 @@
      */
     @Override
     public Document feed(Document target, CallContext context) {
-        Document result = XMLUtils.newDocument();
-
-        // TODO IMPLEMENT ME
-
-        return result;
-    }
-
+        Document doc = XMLUtils.newDocument();
 
-    /**
-     * This method returns the name of the concrete artifact.
-     *
-     * @return the name of the concrete artifact.
-     */
-    public String getName() {
-        return name;
-    }
+        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
+            doc,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
 
-
+        Element result = creator.create("result");
+        doc.appendChild(result);
 
-    /**
-     * This method sets the name of this artifact.
-     *
-     * @param name the name for this artifact.
-     */
-    protected void setName(String name) {
-        this.name = name;
+        try {
+            saveData(target, XPATH_FEED_INPUT);
+            creator.addAttr(result, "type", "SUCCESS", true);
+        }
+        catch (IllegalArgumentException iae) {
+            creator.addAttr(result, "type", "FAILURE", true);
+
+            // TODO I18N this message - getMessage() returns a lookup string, no
+            // human readable error message
+            result.setTextContent(iae.getMessage());
+        }
+
+        return doc;
     }
 
 
@@ -135,7 +173,6 @@
     }
 
 
-
     /**
      * Set the current state of this artifact. <b>NOTE</b>We don't store the
      * State object itself - which is not necessary - but its identifier. So
@@ -155,7 +192,7 @@
      * @return the current State of the artifact.
      */
     protected State getCurrentState(Object context) {
-        FLYSContext flysContext = (FLYSContext) context;
+        FLYSContext flysContext = getFlysContext(context);
         StateEngine engine      = (StateEngine) flysContext.get(
             FLYSContext.STATE_ENGINE_KEY);
 
@@ -164,17 +201,57 @@
 
 
     /**
-     * This method extracts the data that is contained in the FEED document.
+     * Adds a new StateData item to the data pool of this artifact.
+     *
+     * @param name the name of the data object.
+     * @param data the data object itself.
+     */
+    protected void addData(String name, StateData data) {
+        this.data.put(name, data);
+    }
+
+
+    /**
+     * This method stores the data that is contained in the FEED document.
      *
      * @param feed The FEED document.
      * @param xpath The XPath that points to the data nodes.
-     *
-     * @return a StateData array.
      */
-    public StateData[] extractData(Document feed, String xpath) {
+    public void saveData(Document feed, String xpath)
+    throws IllegalArgumentException
+    {
+        if (feed == null || xpath == null || xpath.length() == 0) {
+            throw new IllegalArgumentException("feed.no.input.data");
+        }
 
-        // TODO IMPLEMENT ME
-        return null;
+        NodeList nodes = (NodeList) XMLUtils.xpath(
+            feed,
+            xpath,
+            XPathConstants.NODESET,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (nodes == null || nodes.getLength() == 0) {
+            throw new IllegalArgumentException("feed.no.input.data");
+        }
+
+        int count = nodes.getLength();
+        logger.debug("Try to save " + count + " data items.");
+
+        for (int i = 0; i < count; i++) {
+            Node node = nodes.item(i);
+
+            String name = XMLUtils.xpathString(
+                node, "@art:name", ArtifactNamespaceContext.INSTANCE);
+            String value = XMLUtils.xpathString(
+                node, "@art:value", ArtifactNamespaceContext.INSTANCE);
+
+            if (name != null && value != null) {
+                logger.debug("Save data item for '" + name + "' : " + value);
+
+                // TODO ADD INPUT VALIDATION!
+                addData(name, new DefaultStateData(name, null, null, value));
+            }
+        }
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Mar 09 14:07:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Mar 09 14:13:57 2011 +0000
@@ -31,6 +31,10 @@
     private static Logger logger = Logger.getLogger(WINFOArtifact.class);
 
 
+    /** The name of the artifact.*/
+    public static final String ARTIFACT_NAME = "winfo";
+
+
     /**
      * The default constructor.
      */
@@ -85,5 +89,15 @@
 
         return description;
     }
+
+
+    /**
+     * Returns the name of the concrete artifact.
+     *
+     * @return the name of the concrete artifact.
+     */
+    public String getName() {
+        return ARTIFACT_NAME;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org