changeset 415:7c018f466d6d

Datacage: Added river parameter to meta data services. flys-artifacts/trunk@1879 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 10 May 2011 12:43:12 +0000
parents 0385bcc4229a
children 340dc41a7ea3
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/StackFrames.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/TypeConverter.java flys-artifacts/src/main/resources/metadata/template.xml
diffstat 6 files changed, 134 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue May 10 12:19:17 2011 +0000
+++ b/flys-artifacts/ChangeLog	Tue May 10 12:43:12 2011 +0000
@@ -1,3 +1,22 @@
+2011-05-10	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java:
+	  Fetches river name from incoming XML document. If no river is given all 
+	  infos about all rivers are listed.
+
+	* src/main/resources/metadata/template.xml: Templates honors the 'river'
+	  parameter.
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java:
+	  Extended to pass parameters to the templating. Added support for
+	  type conversion.
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/StackFrames.java:
+	  Take parameters as an initial stack frame.
+	  
+	* src/main/java/de/intevation/flys/artifacts/services/meta/TypeConverter.java:
+	  New. Converts types off stacked variables.
+
 2011-05-10  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java: Added a
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java	Tue May 10 12:19:17 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java	Tue May 10 12:43:12 2011 +0000
@@ -10,6 +10,9 @@
 
 import org.apache.log4j.Logger;
 
+import java.util.Map;
+import java.util.HashMap;
+
 import de.intevation.artifacts.CallMeta;
 import de.intevation.artifacts.ServiceFactory;
 
@@ -30,6 +33,8 @@
 {
     private static Logger log = Logger.getLogger(MetaDataService.class);
 
+    public static final String XPATH_RIVER = "//river/@name";
+
     public static final String META_DATA_TEMPLATE = "/metadata/template.xml";
 
     protected Builder builder;
@@ -37,6 +42,20 @@
     public MetaDataService() {
     }
 
+    protected static Map<String, Object> extractParameters(Document data) {
+        HashMap<String, Object> parameters = new HashMap<String, Object>();
+
+        String river = XMLUtils.xpathString(data, XPATH_RIVER, null);
+
+        if (river == null || (river = river.trim()).length() == 0) {
+            river = "%"; // matches all rivers
+        }
+
+        parameters.put("river", river);
+
+        return parameters;
+    }
+
     @Override
     public Document process(
         Document data,
@@ -52,6 +71,8 @@
             return result;
         }
 
+        final Map<String, Object> parameters = extractParameters(data);
+
         Session session = SessionHolder.acquire();
         try {
             session.doWork(new Work() {
@@ -60,7 +81,7 @@
                 throws SQLException
                 {
                     log.debug("MetaDataService.execute");
-                    builder.build(connection, result);
+                    builder.build(connection, result, parameters);
                 }
             });
         }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Tue May 10 12:19:17 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Tue May 10 12:43:12 2011 +0000
@@ -26,24 +26,24 @@
     public static final String DC_NAMESPACE_URI =
         "http://www.intevation.org/2011/Datacage";
 
-    protected Document   template;
+    protected Document template;
 
     public class BuildHelper
     {
-        protected Document    output;
-        protected StackFrames frames;
-        protected Connection  connection;
+        protected Document                       output;
+        protected StackFrames                    frames;
+        protected Connection                     connection;
         protected Map<String, CompiledStatement> statements;
 
-        public BuildHelper() {
-            frames     = new StackFrames();
-            statements = new HashMap<String, CompiledStatement>();
-        }
-
-        public BuildHelper(Document output, Connection connection) {
-            this();
+        public BuildHelper(
+            Document            output,
+            Connection          connection,
+            Map<String, Object> parameters
+        ) {
             this.output     = output;
             this.connection = connection;
+            frames          = new StackFrames(parameters);
+            statements      = new HashMap<String, CompiledStatement>();
         }
 
         public void build(List<Node> elements) throws SQLException {
@@ -89,8 +89,10 @@
 
             String stmntText = stmntNode.item(0).getTextContent();
 
-            if (stmntText == null) {
-                log.warn("dc:context: no sql statement found");
+            if (stmntText == null
+            || (stmntText = stmntText.trim()).length() == 0) {
+                log.warn("dc:context: no sql statement found -> ignored");
+                return;
             }
 
             CompiledStatement cs = statements.get(stmntText);
@@ -167,6 +169,19 @@
             element.setAttribute(name, value);
         }
 
+        protected void convert(Node parent, Element current) {
+
+            String variable = expand(current.getAttribute("var"));
+            String type     = expand(current.getAttribute("type"));
+
+            if (frames.containsKey(variable)) {
+                Object object = TypeConverter.convert(
+                    frames.get(variable),
+                    type);
+                frames.put(variable, object);
+            }
+        }
+
         protected String expand(String s) {
             Matcher m = CompiledStatement.VAR.matcher(s);
 
@@ -202,6 +217,9 @@
                     else if ("text".equals(localName)) {
                         text(parent, (Element)current);
                     }
+                    else if ("convert".equals(localName)) {
+                        convert(parent, (Element)current);
+                    }
                     else {
                         log.warn("unknown '" + localName + "' -> ignore");
                     }
@@ -237,16 +255,20 @@
     public Document build(Connection connection)
     throws SQLException
     {
-        return build(connection, XMLUtils.newDocument());
+        return build(connection, XMLUtils.newDocument(), null);
     }
 
-    public Document build(Connection connection, Document output)
+    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);
+        BuildHelper helper = new BuildHelper(output, connection, parameters);
 
         List<Node> elements = new ArrayList<Node>();
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/StackFrames.java	Tue May 10 12:19:17 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/StackFrames.java	Tue May 10 12:43:12 2011 +0000
@@ -13,6 +13,13 @@
         frames = new ArrayList<Map<String, Object>>();
     }
 
+    public StackFrames(Map<String, Object> initialFrame) {
+        this();
+        if (initialFrame != null) {
+            frames.add(new HashMap<String, Object>(initialFrame));
+        }
+    }
+
     public void enter() {
         frames.add(new HashMap<String, Object>());
     }
@@ -21,6 +28,13 @@
         frames.remove(frames.size()-1);
     }
 
+    public void put(String key, Object value) {
+        int N = frames.size();
+        if (N > 0) {
+            frames.get(N-1).put(key, value);
+        }
+    }
+
     public void put(String [] keys, Object [] values) {
         Map<String, Object> top = frames.get(frames.size()-1);
         for (int i = 0; i < keys.length; ++i) {
@@ -28,6 +42,15 @@
         }
     }
 
+    public boolean containsKey(String key) {
+        for (int i = frames.size()-1; i >= 0; --i) {
+            if (frames.get(i).containsKey(key)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     public Object get(String key) {
         return get(key, null);
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/TypeConverter.java	Tue May 10 12:43:12 2011 +0000
@@ -0,0 +1,31 @@
+package de.intevation.flys.artifacts.services.meta;
+
+public class TypeConverter
+{
+    private TypeConverter() {
+    }
+
+    public static Object convert(Object object, String type) {
+
+        if (type == null) {
+            return object;
+        }
+
+        if ("Integer".equals(type)) {
+            return Integer.valueOf(object.toString());
+        }
+
+        if ("Double".equals(type)) {
+            return Double.valueOf(object.toString());
+        }
+
+        if ("String".equals(type)) {
+            return object.toString();
+        }
+
+        // TODO: Add more types
+
+        return object;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/resources/metadata/template.xml	Tue May 10 12:19:17 2011 +0000
+++ b/flys-artifacts/src/main/resources/metadata/template.xml	Tue May 10 12:43:12 2011 +0000
@@ -4,6 +4,7 @@
   <dc:context>
     <dc:statement>
       SELECT id AS river_id, name as river_name FROM rivers
+      WHERE name ILIKE ${river}
     </dc:statement>
     <dc:elements>
       <river>

http://dive4elements.wald.intevation.org