changeset 1023:5cb84ceee2a0

Datacage: Templates now can nest <dc:elements> into other elements in the body of <dc:context> flys-artifacts/trunk@2481 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 10 Aug 2011 10:08:01 +0000 (2011-08-10)
parents 930a9ce24294
children ba88157c6aa3
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/meta-data-user.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/StackFrames.java
diffstat 6 files changed, 149 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Aug 10 08:43:38 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Aug 10 10:08:01 2011 +0000
@@ -1,3 +1,19 @@
+2011-08-10  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* doc/conf/meta-data-user.xml: Added grouping element around w/q of each
+	  longitudinal section artifact.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java,
+	  src/main/java/de/intevation/flys/artifacts/datacage/templating/StackFrames.java:
+	  Added some debugging capabilities.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java:
+	  Added isEmpty() method.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java:
+	  Now it is possible to nest <dc:elements> into other elements in the <dc:context>
+	  body. This is useful and needed for grouping and repeating results.
+
 2011-08-10  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java:
--- a/flys-artifacts/doc/conf/meta-data-user.xml	Wed Aug 10 08:43:38 2011 +0000
+++ b/flys-artifacts/doc/conf/meta-data-user.xml	Wed Aug 10 10:08:01 2011 +0000
@@ -30,14 +30,17 @@
                             <dc:statement>SELECT name AS facet_name, num as facet_num, description AS facet_description
                                           FROM facets WHERE out_id = ${out_id} ORDER BY num ASC, name DESC
                             </dc:statement>
-                            <dc:elements>
-                              <dc:element name="${facet_name}">
-                                <dc:attribute name="description" value="${facet_description}"/>
-                                <dc:attribute name="num" value="${facet_num}"/>
-                                <dc:attribute name="factory" value="TODO"/>
-                                <dc:attribute name="artifact-id" value="${a_gid}"/>
-                              </dc:element>
-                            </dc:elements>
+                            <longitudinal_section_columns>
+                                <dc:attribute name="description" value="${river} TODO: More infos about parameters!"/>
+                                <dc:elements>
+                                  <dc:element name="${facet_name}">
+                                    <dc:attribute name="description" value="${facet_description}"/>
+                                    <dc:attribute name="num" value="${facet_num}"/>
+                                    <dc:attribute name="factory" value="TODO"/>
+                                    <dc:attribute name="artifact-id" value="${a_gid}"/>
+                                  </dc:element>
+                                </dc:elements>
+                            </longitudinal_section_columns>
                           </dc:context>
                         </dc:elements>
                       </dc:context>
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Aug 10 08:43:38 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Aug 10 10:08:01 2011 +0000
@@ -3,6 +3,7 @@
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
 
+import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.HashMap;
@@ -87,6 +88,7 @@
         protected List<NamedConnection>                   connections;
         protected Map<String, CompiledStatement.Instance> statements;
         protected Deque<NamedConnection>                  connectionsStack;
+        protected Deque<ResultData>                       resultsStack;
 
         public BuildHelper(
             Node                  output,
@@ -99,6 +101,7 @@
 
             this.connections = connections;
             connectionsStack = new ArrayDeque<NamedConnection>();
+            resultsStack     = new ArrayDeque<ResultData>();
             this.output      = output;
             frames           = new StackFrames(parameters);
             owner            = getOwnerDocument(output);
@@ -129,33 +132,33 @@
         protected void context(Node parent, Element current)
         throws SQLException
         {
-            NodeList elements = current.getElementsByTagNameNS(
-                DC_NAMESPACE_URI, "elements");
+            log.debug("dc:context");
 
-            if (elements.getLength() < 1) {
-                log.warn("no elements found -> ignore");
-                return;
+            NodeList subs = current.getChildNodes();
+            int S = subs.getLength();
+
+            // check only direct children
+            Node stmntNode = null;
+            for (int i = 0; i < S; ++i) {
+                Node node = subs.item(i);
+                String ns;
+                if (node.getNodeType() == Node.ELEMENT_NODE
+                &&  node.getLocalName().equals("statement")
+                && (ns = node.getNamespaceURI()) != null
+                && ns.equals(DC_NAMESPACE_URI)) {
+                    stmntNode = node;
+                    break;
+                }
             }
 
-            NodeList subs = elements.item(0).getChildNodes();
-            int S = subs.getLength();
-
-            if (S < 1) {
-                log.warn("elements is empty -> ignore");
-                return;
-            }
-
-            NodeList stmntNode = current.getElementsByTagNameNS(
-                DC_NAMESPACE_URI, "statement");
-
-            if (stmntNode.getLength() < 1) {
-                log.warn("dc:context: too less statements");
+            if (stmntNode == null) {
+                log.warn("dc:context: cannot find statement");
                 return;
             }
 
             String con = current.getAttribute("connection");
 
-            String stmntText = stmntNode.item(0).getTextContent();
+            String stmntText = stmntNode.getTextContent();
 
             String key = con + "-" + stmntText;
 
@@ -180,30 +183,68 @@
                 }
             }
 
-            connectionsStack.push(connection);
-            try {
-                ResultData rd = csi.execute(
-                    connection.connection,
-                    frames,
-                    connection.cached);
-
-                String [] columns = rd.getColumnLabels();
+            ResultData rd = csi.execute(
+                connection.connection,
+                frames,
+                connection.cached);
 
-                for (Object [] row: rd.getRows()) {
-                    frames.enter();
-                    try {
-                        frames.put(columns, row);
-                        for (int i = 0; i < S; ++i) {
-                            build(parent, subs.item(i));
-                        }
-                    }
-                    finally {
-                        frames.leave();
+            // only descent if there are results
+            if (!rd.isEmpty()) {
+                resultsStack.push(rd);
+                connectionsStack.push(connection);
+                try {
+                    for (int i = 0; i < S; ++i) {
+                        build(parent, subs.item(i));
                     }
                 }
+                finally {
+                    connectionsStack.pop();
+                    resultsStack.pop();
+                }
             }
-            finally {
-                connectionsStack.pop();
+        }
+
+        protected void elements(Node parent, Element current) 
+        throws SQLException
+        {
+            log.debug("dc:elements");
+
+            if (resultsStack.isEmpty()) {
+                log.warn("dc:elements without having results");
+                return;
+            }
+
+            NodeList subs = current.getChildNodes();
+            int S = subs.getLength();
+
+            if (S == 0) {
+                log.debug("dc:elements has no children");
+                return;
+            }
+
+            ResultData rd = resultsStack.peek();
+
+            String [] columns = rd.getColumnLabels();
+
+            //if (log.isDebugEnabled()) {
+            //    log.debug("pushing vars: "
+            //        + java.util.Arrays.toString(columns));
+            //}
+
+            for (Object [] row: rd.getRows()) {
+                frames.enter();
+                try {
+                    frames.put(columns, row);
+                    //if (log.isDebugEnabled()) {
+                    //    log.debug("current vars: " + frames.dump());
+                    //}
+                    for (int i = 0; i < S; ++i) {
+                        build(parent, subs.item(i));
+                    }
+                }
+                finally {
+                    frames.leave();
+                }
             }
         }
 
@@ -428,11 +469,15 @@
                     else if ("element".equals(localName)) {
                         element(parent, (Element)current);
                     }
+                    else if ("elements".equals(localName)) {
+                        elements(parent, (Element)current);
+                    }
                     else if ("text".equals(localName)) {
                         text(parent, (Element)current);
                     }
-                    else if ("comment".equals(localName)) {
-                        // ignore comments in output
+                    else if ("comment".equals(localName)
+                         ||  "statement".equals(localName)) {
+                        // ignore comments and statements in output
                     }
                     else if ("convert".equals(localName)) {
                         convert(parent, (Element)current);
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java	Wed Aug 10 08:43:38 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java	Wed Aug 10 10:08:01 2011 +0000
@@ -18,8 +18,12 @@
 
 import de.intevation.flys.artifacts.cache.CacheFactory;
 
+import org.apache.log4j.Logger;
+
 public class CompiledStatement
 {
+    private static Logger log = Logger.getLogger(CompiledStatement.class);
+
     public static final String DATACAGE_DB_CACHE =
         "datacage.db";
 
@@ -47,6 +51,7 @@
         )
         throws SQLException
         {
+            log.debug("executeCached");
             Object [] values = new Object[numVars];
 
             StringBuilder sb = new StringBuilder(original);
@@ -101,7 +106,11 @@
         ) 
         throws SQLException
         {
+            log.debug("executeUncached");
             if (preparedStatement == null) {
+                if (log.isDebugEnabled()) {
+                    log.debug("preparing statement: " + statement);
+                }
                 preparedStatement = connection.prepareStatement(statement);
             }
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java	Wed Aug 10 08:43:38 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java	Wed Aug 10 10:08:01 2011 +0000
@@ -56,5 +56,9 @@
     public List<Object []> getRows() {
         return rows;
     }
+
+    public boolean isEmpty() {
+        return rows.isEmpty();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/StackFrames.java	Wed Aug 10 08:43:38 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/StackFrames.java	Wed Aug 10 10:08:01 2011 +0000
@@ -1,9 +1,11 @@
 package de.intevation.flys.artifacts.datacage.templating;
 
+import java.util.ArrayList;
 import java.util.Map;
 import java.util.List;
 import java.util.HashMap;
-import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Set;
 
 import javax.xml.xpath.XPathVariableResolver;
 
@@ -83,5 +85,25 @@
         }
         return get(variableName.getLocalPart());
     }
+
+    public String dump() {
+        StringBuilder sb = new StringBuilder("[");
+        Set<String> already = new HashSet<String>();
+
+        boolean first = true;
+
+        for (int i = frames.size()-1; i >= 0; --i) {
+            Map<String, Object> frame = frames.get(i);
+            for (Map.Entry<String, Object> entry: frame.entrySet()) {
+                if (already.add(entry.getKey())) {
+                    if (first) { first = false;   }
+                    else       { sb.append(", "); }
+                    sb.append('\'').append(entry.getKey())
+                      .append("'='").append(entry.getValue()).append('\'');
+                }
+            }
+        }
+        return sb.append(']').toString();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org