changeset 974:30c85cb33a50

meta data service: made artifact optional and allow passing extra parameters. flys-artifacts/trunk@2400 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 25 Jul 2011 11:05:33 +0000
parents c30ada285d45
children a111f0984706
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/DataCage.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/FunctionResolver.java
diffstat 4 files changed, 125 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Sat Jul 23 18:47:08 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Jul 25 11:05:33 2011 +0000
@@ -1,3 +1,21 @@
+2011-07-21  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java:
+	  Made Artifact UUID optional and accept extra parameters, too. This enables
+	  the service to be used without an arttifact and test all filters.
+
+	  <art:meta xmlns:art="http://www.intevation.de/2009/artifacts">
+	     <art:outs value="computed_discharge_curve,floodmap"/>
+	     <art:parameters value="river:Elbe"/>
+	     <art:filters value="recommended"/>
+	  </art:meta>
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/FunctionResolver.java:
+	  'contain' accept Maps and collection, too.
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java:
+	  Made artifact option (= null) in recommendations.
+
 2011-07-21  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/services/meta/CompiledStatement.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java	Sat Jul 23 18:47:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java	Mon Jul 25 11:05:33 2011 +0000
@@ -42,9 +42,10 @@
     // old service. To be removed
     public static final String XPATH_RIVER = "/art:river/text()";
 
-    public static final String XPATH_UUID    = "/art:uuid/@value";
-    public static final String XPATH_OUTS    = "/art:outs/@value";
-    public static final String XPATH_FILTERS = "/art:filters/@value";
+    public static final String XPATH_UUID       = "/art:meta/art:uuid/@value";
+    public static final String XPATH_OUTS       = "/art:meta/art:outs/@value";
+    public static final String XPATH_PARAMETERS = "/art:meta/art:parameters/@value";
+    public static final String XPATH_FILTERS    = "/art:meta/art:filters/@value";
 
     /** The global context key of the artifact database */
     public static final String ARTIFACT_DATA_BASE_KEY =
@@ -102,63 +103,98 @@
         return result;
     }
 
+    protected static Map<String, Object> parameters(
+        String              parameters,
+        Map<String, Object> data
+    ) {
+        if (parameters != null) {
+            String [] parts = parameters.split("\\s*;\\s*");
+            for (String part: parts) {
+                String [] kv = part.split("\\s*:\\s*");
+                if (kv.length < 2 || (kv[0] = kv[0].trim()).length() == 0) {
+                    continue;
+                }
+                String [] values = kv[1].split("\\s*,\\s*");
+                data.put(kv[0], values.length == 1 ? values[0] : values);
+            }
+        }
+        return data;
+    }
+
+    protected static Map<String, Object> filters(
+        String              filters,
+        Map<String, Object> data
+    ) {
+        if (filters != null) {
+            for (String filter: filters.split("\\s*,\\*s")) {
+                data.put(filter, "true");
+            }
+        }
+        return data;
+    }
+
     protected Document newService(
         String        uuid,
         String        outsString,
+        String        parameters,
         String        filters,
         GlobalContext globalContext
     ) {
         Document result = XMLUtils.newDocument();
 
-        if (!StringUtils.checkUUID(uuid)) {
-            log.warn("'" + uuid + "' is not a UUID");
-            return result;
-        }
+        FLYSArtifact flysArtifact;
 
-        Object dbObject =
-            (ArtifactDatabase)globalContext.get(ARTIFACT_DATA_BASE_KEY);
+        log.debug("uuid: " + uuid);
+        log.debug("outs: " + outsString);
+        log.debug("filters: " + filters);
 
-        if (!(dbObject instanceof ArtifactDatabase)) {
-            log.error("Cannot find artifact database");
-            return result;
+        if (uuid != null && uuid.length() != 0) {
+            if (!StringUtils.checkUUID(uuid)) {
+                log.warn("'" + uuid + "' is not a UUID");
+                return result;
+            }
+
+            Object dbObject =
+                (ArtifactDatabase)globalContext.get(ARTIFACT_DATA_BASE_KEY);
+
+            if (!(dbObject instanceof ArtifactDatabase)) {
+                log.error("Cannot find artifact database");
+                return result;
+            }
+
+            ArtifactDatabase db = (ArtifactDatabase)dbObject;
+
+            Artifact artifact;
+
+            try {
+                artifact = db.getRawArtifact(uuid);
+            }
+            catch (ArtifactDatabaseException adbe) {
+                log.warn("fetching artifact failed", adbe);
+                return result;
+            }
+
+            if (!(artifact instanceof FLYSArtifact)) {
+                log.warn("artifact is not a FLYS artifact.");
+                return result;
+            }
+
+            flysArtifact = (FLYSArtifact)artifact;
+        }
+        else {
+            flysArtifact = null;
         }
 
-        ArtifactDatabase db = (ArtifactDatabase)dbObject;
-
-        Artifact artifact;
-
-        try {
-            artifact = db.getRawArtifact(uuid);
-        }
-        catch (ArtifactDatabaseException adbe) {
-            log.warn("fetching artifact failed", adbe);
-            return result;
-        }
-
-        if (!(artifact instanceof FLYSArtifact)) {
-            log.warn("artifact is not a FLYS artifact.");
-            return result;
-        }
-
-        FLYSArtifact flysArtifact = (FLYSArtifact)artifact;
+        Map<String, Object> data =
+            filters(filters,
+                parameters(parameters, 
+                    new HashMap<String, Object>()));
 
         String [] outs = outsString.split("\\s*,\\s*");
-
-        Map<String, Object> extras;
         
-        if (filters != null) {
-            extras = new HashMap<String, Object>();
-            for (String filter: filters.split("\\s*,\\*s")) {
-                extras.put(filter, "true");
-            }
-        }
-        else {
-            extras = null;
-        }
-
         DataCage dc = DataCage.getInstance();
 
-        dc.recommend(flysArtifact, outs, extras, result);
+        dc.recommend(flysArtifact, outs, data, result);
 
         return result;
     }
@@ -177,11 +213,14 @@
         String outs = XMLUtils.xpathString(
             data, XPATH_OUTS, ArtifactNamespaceContext.INSTANCE);
 
+        String parameters = XMLUtils.xpathString(
+            data, XPATH_PARAMETERS, ArtifactNamespaceContext.INSTANCE);
+
         String filters = XMLUtils.xpathString(
             data, XPATH_FILTERS, ArtifactNamespaceContext.INSTANCE);
 
-        return uuid != null && outs != null
-            ? newService(uuid, outs, filters, globalContext)
+        return outs != null
+            ? newService(uuid, outs, parameters, filters, globalContext)
             : oldService(data);
     }
 }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java	Sat Jul 23 18:47:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java	Mon Jul 25 11:05:33 2011 +0000
@@ -81,7 +81,9 @@
             parameters.putAll(extraParameters);
         }
 
-        artifactToParameters(artifact, parameters);
+        if (artifact != null) {
+            artifactToParameters(artifact, parameters);
+        }
 
         parameters.put("artifact-outs", outs);
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/FunctionResolver.java	Sat Jul 23 18:47:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/FunctionResolver.java	Mon Jul 25 11:05:33 2011 +0000
@@ -1,6 +1,8 @@
 package de.intevation.flys.artifacts.services.meta;
 
 import java.util.List;
+import java.util.Collection;
+import java.util.Map;
 import java.util.ArrayList;
 
 import javax.xml.xpath.XPathFunctionResolver;
@@ -36,14 +38,27 @@
         FUNCTIONS.addFunction("contains", 2, new XPathFunction() {
             @Override
             public Object evaluate(List args) throws XPathFunctionException {
+                Object haystack = args.get(0);
+                Object needle   = args.get(1);
                 try {
-                    Object [] haystack = (Object [])args.get(0);
-                    Object    needle   = args.get(1);
-                    for (Object straw: haystack) {
-                        if (straw.equals(needle)) {
-                            return Boolean.TRUE;
+                    if (haystack instanceof Collection) {
+                        return Boolean.valueOf(
+                            ((Collection)haystack).contains(needle));
+                    }
+
+                    if (haystack instanceof Map) {
+                        return Boolean.valueOf(
+                            ((Map)haystack).containsKey(needle));
+                    }
+
+                    if (haystack instanceof Object []) {
+                        for (Object straw: (Object [])haystack) {
+                            if (straw.equals(needle)) {
+                                return Boolean.TRUE;
+                            }
                         }
                     }
+
                     return Boolean.FALSE;
                 }
                 catch (Exception e) {

http://dive4elements.wald.intevation.org