# HG changeset patch # User Sascha L. Teichmann # Date 1311591933 0 # Node ID 30c85cb33a509f6afbda5abb75cfc1ad225334c7 # Parent c30ada285d4516fe7eb47f4164cdf35a1ca690d1 meta data service: made artifact optional and allow passing extra parameters. flys-artifacts/trunk@2400 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r c30ada285d45 -r 30c85cb33a50 flys-artifacts/ChangeLog --- 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 + + * 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. + + + + + + + + * 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 * src/main/java/de/intevation/flys/artifacts/services/meta/CompiledStatement.java: diff -r c30ada285d45 -r 30c85cb33a50 flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.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 parameters( + String parameters, + Map 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 filters( + String filters, + Map 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 data = + filters(filters, + parameters(parameters, + new HashMap())); String [] outs = outsString.split("\\s*,\\s*"); - - Map extras; - if (filters != null) { - extras = new HashMap(); - 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); } } diff -r c30ada285d45 -r 30c85cb33a50 flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java --- 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); diff -r c30ada285d45 -r 30c85cb33a50 flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/FunctionResolver.java --- 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) {