changeset 963:f48cef242e7f

Datacage: recommendation mechanism now accepts nodes to append results. flys-artifacts/trunk@2382 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 20 Jul 2011 22:03:50 +0000
parents 2de1808503be
children ff6ce301c472
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java
diffstat 3 files changed, 69 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Jul 20 20:51:13 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Jul 20 22:03:50 2011 +0000
@@ -1,3 +1,15 @@
+2011-07-21  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java:
+	  Now it is possible to directly pass a Node as a root to the builder.
+	  The owning document if fetch by Node.getOwnerDocument(). This is
+	  useful if you want to generate the recommendation directly into
+	  an already existing document under a given node.
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java:
+	  Changed the signature of recommend() to accept a node where to
+	  append the recommendations.
+
 2011-07-20  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Wed Jul 20 20:51:13 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Wed Jul 20 22:03:50 2011 +0000
@@ -39,15 +39,22 @@
 
     protected Document template;
 
+    protected static Document getOwnerDocument(Node node) {
+        Document document = node.getOwnerDocument();
+        return document != null ? document : (Document)node;
+    }
+
+
     public class BuildHelper
     {
-        protected Document                       output;
+        protected Node                           output;
+        protected Document                       owner;
         protected StackFrames                    frames;
         protected Connection                     connection;
         protected Map<String, CompiledStatement> statements;
 
         public BuildHelper(
-            Document            output,
+            Node                output,
             Connection          connection,
             Map<String, Object> parameters
         ) {
@@ -55,6 +62,7 @@
             this.connection = connection;
             frames          = new StackFrames(parameters);
             statements      = new HashMap<String, CompiledStatement>();
+            owner           = getOwnerDocument(output);
         }
 
         public void build(List<Node> elements) throws SQLException {
@@ -146,7 +154,7 @@
                 return;
             }
 
-            Element element = output.createElement(attr);
+            Element element = owner.createElement(attr);
 
             NodeList children = current.getChildNodes();
             for (int i = 0, N = children.getLength(); i < N; ++i) {
@@ -161,7 +169,7 @@
         {
             log.debug("dc:text");
             String value = expand(current.getTextContent());
-            parent.appendChild(output.createTextNode(value));
+            parent.appendChild(owner.createTextNode(value));
         }
 
 
@@ -368,7 +376,7 @@
                 }
             }
 
-            Node copy = output.importNode(current, false);
+            Node copy = owner.importNode(current, false);
 
             NodeList children = current.getChildNodes();
             for (int i = 0, N = children.getLength(); i < N; ++i) {
@@ -386,23 +394,7 @@
         this.template = template;
     }
 
-    public Document build(Connection connection)
-    throws SQLException
-    {
-        return build(connection, XMLUtils.newDocument(), null);
-    }
-
-    public Document build(
-        Connection          connection,
-        Document            output,
-        Map<String, Object> parameters
-    )
-    throws SQLException
-    {
-        NodeList roots = template.getElementsByTagNameNS(
-            DC_NAMESPACE_URI, "template");
-
-        BuildHelper helper = new BuildHelper(output, connection, parameters);
+    protected static List<Node> rootsToList(NodeList roots) {
 
         List<Node> elements = new ArrayList<Node>();
 
@@ -415,9 +407,23 @@
                 }
             }
         }
-        helper.build(elements);
 
-        return output;
+        return elements;
+    }
+
+    public void build(
+        Connection          connection,
+        Node                output,
+        Map<String, Object> parameters
+    )
+    throws SQLException
+    {
+        BuildHelper helper = new BuildHelper(output, connection, parameters);
+
+        NodeList roots = template.getElementsByTagNameNS(
+            DC_NAMESPACE_URI, "template");
+
+        helper.build(rootsToList(roots));
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java	Wed Jul 20 20:51:13 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java	Wed Jul 20 22:03:50 2011 +0000
@@ -15,6 +15,7 @@
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Node;
 
 import org.hibernate.Session;
 
@@ -52,23 +53,11 @@
         return builder;
     }
 
-    public Document recommend(FLYSArtifact artifact, String [] outs) {
-        return recommend(artifact, outs, null);
-    }
-
-    public Document recommend(
-        FLYSArtifact        artifact,
-        String []           outs,
-        Map<String, Object> extraParameters
+    protected static void artifactToParameters(
+        FLYSArtifact        artifact, 
+        Map<String, Object> parameters
     ) {
-        Map<String, Object> parameters = new HashMap<String, Object>();
-
-        if (extraParameters != null) {
-            parameters.putAll(extraParameters);
-        }
-
         parameters.put("current-state-id", artifact.getCurrentStateId());
-        parameters.put("artifact-outs", outs);
 
         for (StateData sd: artifact.getAllData()) {
             Object value = sd.getValue();
@@ -78,16 +67,34 @@
             String key = sd.getName().replace('.', '-');
             parameters.put(key, value);
         }
-
-        return process(parameters);
     }
 
-    public Document process(final Map<String, Object> parameters) {
-        final Document result = XMLUtils.newDocument();
+    public void  recommend(
+        FLYSArtifact        artifact,
+        String []           outs,
+        Map<String, Object> extraParameters,
+        Node                result
+    ) {
+        Map<String, Object> parameters = new HashMap<String, Object>();
 
+        if (extraParameters != null) {
+            parameters.putAll(extraParameters);
+        }
+
+        artifactToParameters(artifact, parameters);
+
+        parameters.put("artifact-outs", outs);
+
+        recommend(parameters, result);
+    }
+
+    public void recommend(
+        final Map<String, Object> parameters,
+        final Node                result
+    ) {
         if (builder != null) {
             log.error("builder not configured properly.");
-            return result;
+            return;
         }
 
         Session session = SessionHolder.HOLDER.get();
@@ -100,8 +107,6 @@
                 builder.build(connection, result, parameters);
             }
         });
-
-        return result;
     }
 
     public static synchronized DataCage getInstance() {

http://dive4elements.wald.intevation.org