changeset 1716:3ca999f507b7

flys/issue314: Variables in datacage are now case insensitive. flys-artifacts/trunk@2994 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 17 Oct 2011 15:53:03 +0000
parents a70e0cbc5e02
children 755a890ecf0e
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/meta-data.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java 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/FunctionResolver.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 8 files changed, 100 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Oct 17 14:55:45 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Oct 17 15:53:03 2011 +0000
@@ -1,3 +1,23 @@
+2011-10-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	flys/issue314: Variables in datacage are now case insensitive.
+	(H2 returns meta data variables uppercase)
+
+	* doc/conf/meta-data.xml:
+	  Made a statement more precise. Added some debug output.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java:
+	  Use uppercase variable names.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/templating/StackFrames.java,
+	  src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java,
+	  src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java,
+	  src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java:
+	  Variables are now treated as uppercase.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java:
+	  Input variables are now treated uppercase.
+
 2011-10-17  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/ManagedDomFacet.java,
--- a/flys-artifacts/doc/conf/meta-data.xml	Mon Oct 17 14:55:45 2011 +0000
+++ b/flys-artifacts/doc/conf/meta-data.xml	Mon Oct 17 15:53:03 2011 +0000
@@ -312,14 +312,20 @@
                         ORDER BY c.creation DESC
           </dc:statement>
           <dc:elements>
+            <dc:comment><!--
+            <hello_collection><dc:attribute name="description" value="Collection: id=${collection_id} name='${collection_name}'"/></hello_collection>
+            --></dc:comment>
             <dc:context>
-              <dc:statement>SELECT id AS a_id, state AS a_state, gid AS a_gid, creation AS a_creation
+              <dc:statement>SELECT m.id AS a_id, m.state AS a_state, m.gid AS a_gid, m.creation AS a_creation
                             FROM   master_artifacts m
-                            WHERE  collection_id = ${collection_id} AND gid &lt;&gt; CAST(${artifact-id} AS uuid)
-                                   AND EXISTS (
-                                      SELECT id FROM artifact_data WHERE artifact_id = m.id AND k = 'river' AND v = ${river})
+                            WHERE  m.collection_id = ${collection_id} AND m.gid &lt;&gt; CAST(${artifact-id} AS uuid)
+                            AND EXISTS (
+                                SELECT id FROM artifact_data ad WHERE ad.artifact_id = m.id AND k = 'river' AND v = ${river})
               </dc:statement>
               <dc:elements>
+              <dc:comment><!--
+                <hello_artifact><dc:attribute name="description" value="Artifact UUID: ${a_gid}"/></hello_artifact>
+              --></dc:comment>
                 <dc:choose>
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java	Mon Oct 17 14:55:45 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java	Mon Oct 17 15:53:03 2011 +0000
@@ -25,6 +25,7 @@
 
 import de.intevation.artifacts.common.utils.Config;
 import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.artifacts.common.utils.StringUtils;
 
 import de.intevation.flys.artifacts.FLYSArtifact;
 
@@ -117,19 +118,28 @@
         FLYSArtifact        artifact, 
         Map<String, Object> parameters
     ) {
-        parameters.put("current-state-id", artifact.getCurrentStateId());
-        parameters.put("artifact-id",      artifact.identifier());
+        parameters.put("CURRENT-STATE-ID", artifact.getCurrentStateId());
+        parameters.put("ARTIFACT-ID",      artifact.identifier());
 
         for (StateData sd: artifact.getAllData()) {
             Object value = sd.getValue();
             if (value == null) {
                 continue;
             }
-            String key = sd.getName().replace('.', '-');
+            String key = sd.getName().replace('.', '-').toUpperCase();
             parameters.put(key, value);
         }
     }
 
+    public static void convertKeysToUpperCase(
+        Map<String, Object> src,
+        Map<String, Object> dst
+    ) {
+        for (Map.Entry<String, Object> entry: src.entrySet()) {
+            dst.put(entry.getKey().toUpperCase(), entry.getValue());
+        }
+    }
+
     public void  recommend(
         FLYSArtifact        artifact,
         String              userId,
@@ -140,20 +150,20 @@
         Map<String, Object> parameters = new HashMap<String, Object>();
 
         if (extraParameters != null) {
-            parameters.putAll(extraParameters);
+            convertKeysToUpperCase(extraParameters, parameters);
         }
 
         if (userId != null) {
-            parameters.put("user-id", userId);
+            parameters.put("USER-ID", userId);
         }
 
         if (artifact != null) {
             artifactToParameters(artifact, parameters);
         }
 
-        parameters.put("artifact-outs", outs);
+        parameters.put("ARTIFACT-OUTS", StringUtils.toUpperCase(outs));
 
-        parameters.put("parameters", parameters);
+        parameters.put("PARAMETERS", parameters);
 
         recommend(parameters, userId, result);
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Mon Oct 17 14:55:45 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Mon Oct 17 15:53:03 2011 +0000
@@ -417,22 +417,30 @@
             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);
+            Object [] result = new Object[1];
+
+            if (frames.getStore(variable, result)) {
+                Object object = TypeConverter.convert(result[0], type);
+                frames.put(variable.toUpperCase(), object);
             }
         }
 
         protected String expand(String s) {
             Matcher m = CompiledStatement.VAR.matcher(s);
 
+            Object [] result = new Object[1];
+
             StringBuffer sb = new StringBuffer();
             while (m.find()) {
                 String key = m.group(1);
-                Object value = frames.get(key);
-                m.appendReplacement(sb, value != null ? value.toString() : "");
+                result[0] = null;
+                if (frames.getStore(key, result)) {
+                    m.appendReplacement(
+                        sb, result[0] != null ? result[0].toString() : "");
+                }
+                else {
+                    m.appendReplacement(sb, "\\${" + key + "}");
+                }
             }
             m.appendTail(sb);
             return sb.toString();
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java	Mon Oct 17 14:55:45 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/CompiledStatement.java	Mon Oct 17 15:53:03 2011 +0000
@@ -188,7 +188,7 @@
         int index = 0;
 
         while (m.find()) {
-            String key = m.group(1);
+            String key = m.group(1).toUpperCase();
             List<Integer> indices = positions.get(key);
             if (indices == null) {
                 indices = new ArrayList<Integer>();
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java	Mon Oct 17 14:55:45 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java	Mon Oct 17 15:53:03 2011 +0000
@@ -44,6 +44,11 @@
             public Object evaluate(List args) throws XPathFunctionException {
                 Object haystack = args.get(0);
                 Object needle   = args.get(1);
+
+                if (needle instanceof String) {
+                    needle = ((String)needle).toUpperCase();
+                }
+
                 try {
                     if (haystack instanceof Collection) {
                         return Boolean.valueOf(
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java	Mon Oct 17 14:55:45 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/ResultData.java	Mon Oct 17 15:53:03 2011 +0000
@@ -9,9 +9,13 @@
 import java.util.List;
 import java.util.ArrayList;
 
+import org.apache.log4j.Logger;
+
 public class ResultData
 implements   Serializable
 {
+    private static Logger log = Logger.getLogger(ResultData.class);
+
     protected String [] columns;
 
     protected List<Object []> rows;
@@ -25,12 +29,21 @@
     {
         this();
 
+        boolean debug = log.isDebugEnabled();
+
         int N = meta.getColumnCount();
 
         columns = new String[N];
 
+        if (debug) {
+            log.debug("ResultSet column names:");
+        }
+
         for (int i = 1; i <= N; ++i) {
-            columns[i-1] = meta.getColumnLabel(i);
+            columns[i-1] = meta.getColumnLabel(i).toUpperCase();
+            if (debug) {
+                log.debug("    " + i + ": " + columns[i-1]);
+            }
         }
     }
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/StackFrames.java	Mon Oct 17 14:55:45 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/StackFrames.java	Mon Oct 17 15:53:03 2011 +0000
@@ -54,6 +54,7 @@
     }
 
     public boolean containsKey(String key) {
+        key = key.toUpperCase();
         for (int i = frames.size()-1; i >= 0; --i) {
             if (frames.get(i).containsKey(key)) {
                 return true;
@@ -66,8 +67,25 @@
         return get(key, null);
     }
 
+    public boolean getStore(String key, Object [] result) {
+
+        key = key.toUpperCase();
+
+        for (int i = frames.size()-1; i >= 0; --i) {
+            Map<String, Object> frame = frames.get(i);
+            if (frame.containsKey(key)) {
+                result[0] = frame.get(key);
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     public Object get(String key, Object def) {
 
+        key = key.toUpperCase();
+
         for (int i = frames.size()-1; i >= 0; --i) {
             Map<String, Object> frame = frames.get(i);
             if (frame.containsKey(key)) {

http://dive4elements.wald.intevation.org