[Dive4elements-commits] [PATCH 1 of 2] Datacage: Added <dc:filter> element. This allows cleaner way to narrow the datasets

Wald Commits scm-commit at wald.intevation.org
Thu Mar 28 16:54:36 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1364485875 -3600
# Node ID 627584bc05866903bcee0ce5101d78fcbaf195d3
# Parent  57f802db5c5ab39f2e4116273b6caa660ccaa26c
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>

diff -r 57f802db5c5a -r 627584bc0586 flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
--- 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");


More information about the Dive4elements-commits mailing list