changeset 5509:627584bc0586

Datacage: Added <dc:filter> element. This allows cleaner way to narrow the datasets. Example: <dc:context> <dc:statement> SELECT DISTINCT name AS hws_name, official AS hws_official, kind_id AS hws_kind FROM hws_lines WHERE river_id = ${river_id} </dc:statement> <dc:if test="dc:has-result()"> <lines> <dc:macro name="hws-lines"> <dc:elements> <hws factory="hwsfactory" name="{$hws_name}"/> </dc:elements> </dc:macro> <dc:filter expr="$hws_official=1"> <dc:if test="dc:has-result()"> <official> <dc:filter expr="$hws_kind=1"> <dc:if test="dc:has-result()"> <Durchlass><dc:call-macro name="hws-lines"></Durchlass> </dc:if> </dc:filter> <dc:filter expr="$hws_kind=2"> <dc:if test="dc:has-result()"> <Damm><dc:call-macro name="hws-lines"></Damm> </dc:if> </dc:filter> <dc:filter expr="$hws_kind=3"> <dc:if test="dc:has-result()"> <Graben><dc:call-macro name="hws-lines"></Graben> </dc:if> </dc:filter> </official> </dc:if> </dc:filter> </lines> </dc:if> </dc:context>
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 28 Mar 2013 16:51:15 +0100
parents 57f802db5c5a
children d4bee6d5c866
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
diffstat 1 files changed, 58 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Thu Mar 28 15:55:34 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Thu Mar 28 16:51:15 2013 +0100
@@ -186,7 +186,7 @@
         }
 
         /**
-         * Handle a \<context\> node.
+         * Handle a dc:context node.
          */
         protected void context(Node parent, Element current)
         throws SQLException
@@ -283,6 +283,43 @@
             return new ResultData(rd.getColumnLabels(), filtered);
         }
 
+        protected void filter(Node parent, Element current)
+        throws SQLException
+        {
+            String expr = current.getAttribute("expr");
+
+            if ((expr = expr.trim()).length() == 0) {
+                expr = null;
+            }
+
+            NodeList subs = current.getChildNodes();
+            int S = subs.getLength();
+            if (S == 0) {
+                log.debug("dc:filter has no children");
+                return;
+            }
+
+            ResultData orig = null;
+            Pair<Builder.NamedConnection, ResultData> pair = null;
+
+            if (expr != null && !connectionsStack.isEmpty()) {
+                pair = connectionsStack.peek();
+                orig = pair.getB();
+                pair.setB(createFilteredResultData(orig, expr));
+            }
+
+            try {
+                for (int i = 0; i < S; ++i) {
+                    build(parent, subs.item(i));
+                }
+            }
+            finally {
+                if (orig != null) {
+                    pair.setB(orig);
+                }
+            }
+        }
+
         /**
          * Kind of foreach over results of a statement within a context.
          */
@@ -313,7 +350,7 @@
             Pair<Builder.NamedConnection, ResultData> pair =
                 connectionsStack.peek();
 
-            ResultData rd = connectionsStack.peek().getB();
+            ResultData rd = pair.getB();
             ResultData orig = rd;
 
             if (filter != null) {
@@ -324,18 +361,10 @@
             try {
                 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));
                         }
@@ -661,45 +690,47 @@
                 }
                 else {
                     String localName = current.getLocalName();
+                    Element curr = (Element)curr;
                     if ("attribute".equals(localName)) {
-                        attribute(parent, (Element)current);
+                        attribute(parent, curr);
                     }
                     else if ("context".equals(localName)) {
-                        context(parent, (Element)current);
+                        context(parent, curr);
                     }
                     else if ("if".equals(localName)) {
-                        ifClause(parent, (Element)current);
+                        ifClause(parent, curr);
                     }
                     else if ("choose".equals(localName)) {
-                        choose(parent, (Element)current);
+                        choose(parent, curr);
                     }
                     else if ("call-macro".equals(localName)) {
-                        callMacro(parent, (Element)current);
+                        callMacro(parent, curr);
                     }
                     else if ("macro-body".equals(localName)) {
-                        macroBody(parent, (Element)current);
+                        macroBody(parent, curr);
                     }
-                    else if ("macro".equals(localName)) {
-                        // Simply ignore the definition.
+                    else if ("macro".equals(localName)
+                         ||  "comment".equals(localName)
+                         ||  "statement".equals(localName)) {
+                        // Simply ignore them.
                     }
                     else if ("element".equals(localName)) {
-                        element(parent, (Element)current);
+                        element(parent, curr);
                     }
                     else if ("elements".equals(localName)) {
-                        elements(parent, (Element)current);
+                        elements(parent, curr);
+                    }
+                    else if ("filter".equals(localName)) {
+                        filter(parent, curr);
                     }
                     else if ("text".equals(localName)) {
-                        text(parent, (Element)current);
+                        text(parent, curr);
                     }
                     else if ("variable".equals(localName)) {
-                        variable((Element)current);
-                    }
-                    else if ("comment".equals(localName)
-                         ||  "statement".equals(localName)) {
-                        // ignore comments and statements in output
+                        variable(curr);
                     }
                     else if ("convert".equals(localName)) {
-                        convert((Element)current);
+                        convert(curr);
                     }
                     else {
                         log.warn("unknown '" + localName + "' -> ignore");

http://dive4elements.wald.intevation.org