changeset 5305:e60f65540cc2

Added filter to elements in datacage templating. (most work done by Sascha T.).
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 14 Mar 2013 17:09:44 +0100
parents ffa5eaad3a7b
children cd8b84af2ebc
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java
diffstat 2 files changed, 77 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Thu Mar 14 16:49:32 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Thu Mar 14 17:09:44 2013 +0100
@@ -236,6 +236,36 @@
             }
         }
 
+
+        protected ResultData createFilteredResultData(ResultData rd, String filter) {
+            if (filter == null) return rd;
+
+            List<Object []> rows = rd.getRows();
+            String [] columns = rd.getColumnLabels();
+
+            List<Object []> filtered = new ArrayList<Object[]>(rows.size());
+
+            for (Object [] row: rows) {
+                frames.enter();
+                try {
+                    frames.put(columns, row);
+                    boolean traverse = filter == null;
+
+                    if (!traverse) {
+                        Boolean b = evaluateXPathToBoolean(filter);
+                        traverse = b != null && b;
+                    }
+                    if (traverse) {
+                        filtered.add(row);
+                    }
+                }
+                finally {
+                    frames.leave();
+                }
+            }
+            return new ResultData(rd.getColumnLabels(), filtered);
+        }
+
         /**
          * Kind of foreach over results of a statement within a context.
          */
@@ -249,6 +279,12 @@
                 return;
             }
 
+            String filter = current.getAttribute("filter");
+
+            if ((filter = filter.trim()).length() == 0) {
+                filter = null;
+            }
+
             NodeList subs = current.getChildNodes();
             int S = subs.getLength();
 
@@ -257,29 +293,45 @@
                 return;
             }
 
-            ResultData rd = connectionsStack.peek().getB();
-
-            String [] columns = rd.getColumnLabels();
-
-            //if (log.isDebugEnabled()) {
-            //    log.debug("pushing vars: "
-            //        + java.util.Arrays.toString(columns));
-            //}
+            Pair<Builder.NamedConnection, ResultData> pair =
+                connectionsStack.peek();
 
-            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));
+            ResultData rd = connectionsStack.peek().getB();
+            ResultData orig = rd;
+
+            if (filter != null) {
+                ResultData rdCopy = createFilteredResultData(rd, filter);
+                pair.setB(rdCopy);
+                rd = rdCopy;
+            }
+            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));
+                        }
+                    }
+                    finally {
+                        frames.leave();
                     }
                 }
-                finally {
-                    frames.leave();
-                }
+            }
+            finally {
+                 if (filter != null) {
+                     pair.setB(orig);
+                 }
             }
         }
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java	Thu Mar 14 16:49:32 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java	Thu Mar 14 17:09:44 2013 +0100
@@ -25,6 +25,11 @@
     public ResultData() {
         rows = new ArrayList<Object []>();
     }
+    
+    public ResultData(String [] columns, List<Object []> rows) {
+        this.columns = columns;
+        this.rows = rows;
+    }
 
     public ResultData(ResultSetMetaData meta)
     throws SQLException

http://dive4elements.wald.intevation.org