# HG changeset patch # User Felix Wolfsteller # Date 1320315923 0 # Node ID 6ed439ff61bf55b98c74e9ee0af487c867a633aa # Parent e55f1a8519234b58449ce851b8bc906d1e42bbcc Changed theme-mapping mechanism to include further condition (on master-artifacts attributes), added point themes for longitudinal.ws for calculations at locations. flys-artifacts/trunk@3151 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r e55f1a851923 -r 6ed439ff61bf flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu Nov 03 10:19:29 2011 +0000 +++ b/flys-artifacts/ChangeLog Thu Nov 03 10:25:23 2011 +0000 @@ -1,3 +1,31 @@ +2011-11-03 Felix Wolfsteller + + Added new matching options for theme-mappings to allow more + overspecification of defaults (e.g. now name,description-pattern and + master-artifacts attributes are matched). The first full match from + the configuration file is done. + New matching option in the masterAttr- field of a mapping are + super-basic until new use-cases come up. + Concrete new themes are point-styles of Ws when locations where chosen + to calculate. + + * doc/conf/themes.xml: Added newly defined Themes. + + * src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java: + Put master-artifact in flys-context. + + * src/main/java/de/intevation/flys/themes/ThemeMapping.java: + Accept masterAttr in constructor. + (masterAttrMatches): New, check masterAttr-condition against + artifact. + + * src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java: + When creating ThemeMappings, pass in masterAttr. + + * src/main/java/de/intevation/flys/themes/ThemeFactory.java: + (getTheme): Evaluate masterAttr-condition, always return first full + match. + 2011-11-03 Felix Wolfsteller * src/main/java/de/intevation/flys/artifacts/context/FLYSContext.java: diff -r e55f1a851923 -r 6ed439ff61bf flys-artifacts/doc/conf/themes.xml --- a/flys-artifacts/doc/conf/themes.xml Thu Nov 03 10:19:29 2011 +0000 +++ b/flys-artifacts/doc/conf/themes.xml Thu Nov 03 10:25:23 2011 +0000 @@ -185,6 +185,146 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -577,10 +717,32 @@ - + - + + + + + + + + + + + + + + + + + + + @@ -599,7 +761,8 @@ - + + @@ -618,6 +781,7 @@ + diff -r e55f1a851923 -r 6ed439ff61bf flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java Thu Nov 03 10:19:29 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java Thu Nov 03 10:25:23 2011 +0000 @@ -360,6 +360,9 @@ String pattern = (String) XMLUtils.xpath( node, "@pattern", XPathConstants.STRING); + String masterAttrPattern = (String) XMLUtils.xpath( + node, "@masterAttr", XPathConstants.STRING); + if (from != null && to != null) { List tm = mapping.get(from); @@ -368,7 +371,8 @@ mapping.put(from, tm); } - tm.add(new ThemeMapping(from, to, pattern)); + tm.add(new ThemeMapping( + from, to, pattern, masterAttrPattern)); } } diff -r e55f1a851923 -r 6ed439ff61bf flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java --- a/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java Thu Nov 03 10:19:29 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java Thu Nov 03 10:25:23 2011 +0000 @@ -494,8 +494,8 @@ /** - * Returns the attribute that belongs to an artifact stored in this - * collection. + * Returns the attribute that belongs to an artifact and facet stored in + * this collection. * * @param uuid The Artifact's uuid. * @param outname The name of the requested output. @@ -723,6 +723,17 @@ ? (FLYSContext) context : (FLYSContext) context.globalContext(); + // Push artifact in flysContext. + ArtifactDatabase db = context.getDatabase(); + try { + FLYSArtifact artifact = (FLYSArtifact) db.getRawArtifact(uuid); + log.debug("Got raw artifact"); + flysContext.put(flysContext.ARTIFACT_KEY, artifact); + } + catch (Exception e) { + log.error("Exception caught when trying to get art.", e); + } + Theme t = ThemeFactory.getTheme(flysContext, facet, pattern); if (t != null) { diff -r e55f1a851923 -r 6ed439ff61bf flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java --- a/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java Thu Nov 03 10:19:29 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java Thu Nov 03 10:25:23 2011 +0000 @@ -15,16 +15,21 @@ import de.intevation.artifacts.common.utils.XMLUtils; import de.intevation.flys.artifacts.context.FLYSContext; - +import de.intevation.flys.artifacts.FLYSArtifact; /** * @author Ingo Weinzierl + * + * Mapping-matching rules: + * */ public class ThemeFactory { private static Logger logger = Logger.getLogger(ThemeFactory.class); + /** Trivial, hidden constructor. */ private ThemeFactory() { + ; } @@ -66,19 +71,32 @@ } + /** + * Get first matching theme for facet. + * + * @param name Name to match "from" of theme mapping. + * @param pattern String to 'compare' to pattern in mapping. + * + * @return First matching theme. + */ public static Theme getTheme(FLYSContext c, String name, String pattern) { + logger.debug("Search theme for: " + name + " - " + pattern); + if (c == null || name == null) { logger.warn("Cannot search for theme."); return null; } + // Fetch mapping and themes. Map> map = (Map>) c.get(FLYSContext.THEME_MAPPING); Map t = (Map) c.get(FLYSContext.THEMES); + FLYSArtifact artifact = (FLYSArtifact) c.get(FLYSContext.ARTIFACT_KEY); + if (map == null || map.size() == 0 || t == null || t.size() == 0) { logger.warn("No mappings or themes found. Cannot retrieve theme!"); return null; @@ -87,32 +105,17 @@ List mapping = map.get(name); if (mapping == null) { - logger.warn("No theme fount for mapping: " + name); + logger.warn("No theme found for mapping: " + name); return null; } - int patternLen = pattern != null ? pattern.length() : 0; - - Theme defaultTheme = null; - + // Take first mapping of which all conditions are satisfied. for (ThemeMapping tm: mapping) { - if (name.equals(tm.getFrom())) { - String tmPattern = tm.getPatternStr(); - - int tmLen = tmPattern != null ? tmPattern.length() : 0; - - if (tmLen == 0 && patternLen == 0) { - // we want and get the default theme - return t.get(tm.getTo()); - } - else if (tmLen == 0 && patternLen > 0) { - // found a defaultTheme, lets save it - defaultTheme = t.get(tm.getTo()); - } - else if (tm.applyPattern(pattern)) { - // found the special theme we have searched for - return t.get(tm.getTo()); - } + if (name.equals(tm.getFrom()) + && tm.applyPattern(pattern) + && tm.masterAttrMatches(artifact)) + { + return t.get(tm.getTo()); } } @@ -120,11 +123,6 @@ "No theme found for '" + name + "' with pattern '" + pattern + "'."; - if (defaultTheme != null) { - logger.info(msg + " Use the default instead."); - return defaultTheme; - } - logger.warn(msg); return null; diff -r e55f1a851923 -r 6ed439ff61bf flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeMapping.java --- a/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeMapping.java Thu Nov 03 10:19:29 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeMapping.java Thu Nov 03 10:25:23 2011 +0000 @@ -1,28 +1,51 @@ package de.intevation.flys.themes; +import org.apache.log4j.Logger; + import java.io.Serializable; import java.util.regex.Matcher; import java.util.regex.Pattern; +import de.intevation.flys.artifacts.FLYSArtifact; +/** + * Represents mapping to a theme (including conditions). + */ public class ThemeMapping implements Serializable { + /** The logger that is used in this class */ + private static Logger logger = Logger.getLogger(ThemeMapping.class); + + /** Name from which to map. */ protected String from; + + /** Name to which to map. */ protected String to; + + /** Given pattern (held against facet description). */ protected String patternStr; + /** Given masterAttr pattern (held against masterartifacts attributes). */ + protected String masterAttr; + protected Pattern pattern; public ThemeMapping(String from, String to) { - this(from, to, null); + this(from, to, null, null); } - public ThemeMapping(String from, String to, String patternStr) { + public ThemeMapping( + String from, + String to, + String patternStr, + String masterAttr) + { this.from = from; this.to = to; this.patternStr = patternStr; + this.masterAttr = masterAttr; this.pattern = Pattern.compile(patternStr); } @@ -33,19 +56,62 @@ } + /** + * Get name of theme that is mapped to. + */ public String getTo() { return to; } + /** + * Get pattern. + */ public String getPatternStr() { return patternStr; } + /** + * Match regular expression against text. + * + * @param text string to be matched against. + * @return true if pattern matches text or pattern is empty. + */ public boolean applyPattern(String text) { + if (patternStr == null || patternStr.equals("")) { + return true; + } Matcher m = pattern.matcher(text); return m.matches(); } + + + /** + * Inspects Artifacts data given the masterAttr-condition. + * + * The only condition implemented so far is 'key==value', for which + * the Artifacts data with name "key" has to be of value "value" in order + * for true to be returned. + * + * @param artifact Artifact of which to inspect data. + * @return true if no condition is specified or condition is met. + */ + public boolean masterAttrMatches(FLYSArtifact artifact) { + if (masterAttr == null || masterAttr.equals("")) { + return true; + } + + // Operator split. + String[] parts = masterAttr.split("=="); + if (parts.length != 2) { + logger.error("ThemeMapping could not parse masterAttr.-condition:_" + + masterAttr + "_"); + return false; + } + + // Test. + return artifact.getDataAsString(parts[0]).equals(parts[1]); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :