changeset 1435:f6fbfdc813f0

Allow client to access artifacts data via CollectionItems and Themes. flys-client/trunk@3396 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 13 Dec 2011 09:51:47 +0000
parents 9115b2a28be1
children 29fc2d1dfe9b
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/CollectionHelper.java flys-client/src/main/java/de/intevation/flys/client/shared/model/AttributedTheme.java flys-client/src/main/java/de/intevation/flys/client/shared/model/Collection.java flys-client/src/main/java/de/intevation/flys/client/shared/model/CollectionItem.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultCollection.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultCollectionItem.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultData.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultTheme.java flys-client/src/main/java/de/intevation/flys/client/shared/model/Theme.java flys-client/src/main/java/de/intevation/flys/client/shared/model/ThemeList.java
diffstat 11 files changed, 298 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/ChangeLog	Tue Dec 13 09:51:47 2011 +0000
@@ -1,3 +1,33 @@
+2011-12-13	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
+
+	Provide access to Artifacts Data via Themes and CollectionItems.
+
+	* src/main/java/de/intevation/flys/client/server/CollectionHelper.java:
+	  Register data (key/value) from artifacts in CollectionItems.
+	  Associate Themes with CollectionItems.
+
+	* src/main/java/de/intevation/flys/client/shared/model/Theme.java:
+	  Added Accessors for CollectionItem.
+
+	* src/main/java/de/intevation/flys/client/shared/model/DefaultTheme.java,
+	  src/main/java/de/intevation/flys/client/shared/model/AttributedTheme.java:
+	  Added Accessors and instance of CollectionItem.
+
+	* src/main/java/de/intevation/flys/client/shared/model/CollectionItem.java,
+	  src/main/java/de/intevation/flys/client/shared/model/DefaultCollectionItem.java:
+	  Allow access to data from artifact, cosmetics.
+	
+	* src/main/java/de/intevation/flys/client/shared/model/Collection.java,
+	  src/main/java/de/intevation/flys/client/shared/model/DefaultCollection.java:
+	  Added setter for ThemeList(s).
+
+	* src/main/java/de/intevation/flys/client/shared/model/DefaultData.java:
+	  (createSimpleStringData, createSimpleStringDataArray): Convenvience/
+								 helpers.
+
+	* src/main/java/de/intevation/flys/client/shared/model/ThemeList.java:
+	  Helpers to get maps that map relevant information.
+
 2011-12-13	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	Fix CrossSectionKMService.
--- a/flys-client/src/main/java/de/intevation/flys/client/server/CollectionHelper.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/CollectionHelper.java	Tue Dec 13 09:51:47 2011 +0000
@@ -53,7 +53,6 @@
     private static final Logger logger =
         Logger.getLogger(CollectionHelper.class);
 
-
     public static final String ERROR_ADD_ARTIFACT = "error_add_artifact";
 
     public static final String ERROR_REMOVE_ARTIFACT = "error_remove_artifact";
@@ -327,14 +326,13 @@
             // do nothing
         }
 
-        Map<String, ThemeList> themeList = parseThemeLists(description);
         List<Recommendation> recommended = parseRecommendations(description);
+        Map<String, CollectionItem> collectionItems =
+            new HashMap<String, CollectionItem>();
 
         name = (name != null && name.length() > 0) ? name : uuid;
 
-        Collection c = !themeList.isEmpty()
-            ? new DefaultCollection(uuid, ttl, name, recommended, themeList)
-            : new DefaultCollection(uuid, ttl, name, recommended);
+        Collection c = new DefaultCollection(uuid, ttl, name, recommended);
 
         NodeList items = (NodeList) XMLUtils.xpath(
             description,
@@ -357,9 +355,13 @@
 
             if (item != null) {
                 c.addItem(item);
+                collectionItems.put(item.identifier() ,item);
             }
         }
 
+        Map<String, ThemeList> themeLists = parseThemeLists(description, collectionItems);
+        c.setThemeLists(themeLists);
+
         logger.debug(
             "Found " + c.getItemLength() + " collection items " +
             "for the Collection '" + c.identifier() + "'.");
@@ -368,7 +370,13 @@
     }
 
 
-    protected static Map<String, ThemeList> parseThemeLists(Document desc) {
+    /**
+     * @param collectionItems map to look up collection item mapping a themes
+     *                        (artifact) uuid.
+     */
+    protected static Map<String, ThemeList> parseThemeLists(
+        Document desc, Map<String, CollectionItem> collectionItems
+    ) {
         logger.debug("DescribeCollectionServiceImpl.parseThemeLists");
 
         NodeList lists = (NodeList) XMLUtils.xpath(
@@ -389,7 +397,8 @@
             String outName = node.getAttribute("name");
 
             if (outName.length() > 0) {
-                ThemeList list = parseThemeList(node);
+                ThemeList list = parseThemeList(node, collectionItems);
+
                 if (list.getThemeCount() > 0) {
                     themeList.put(outName, list);
                 }
@@ -400,7 +409,13 @@
     }
 
 
-    protected static ThemeList parseThemeList(Element node) {
+    /**
+     * @param collectionItems map to look up collection item mapping a themes
+     *                        (artifact) uuid.
+     */
+    protected static ThemeList parseThemeList(
+        Element node, Map<String, CollectionItem> collectionItems
+    ) {
         logger.debug("DescribeCollectionServiceImpl.parseThemeList");
 
         NodeList themes = node.getElementsByTagNameNS(
@@ -413,6 +428,7 @@
 
         for (int i = 0; i < num; i++) {
             Theme theme = parseTheme((Element)themes.item(i));
+            theme.setCollectionItem(collectionItems.get(theme.getArtifact()));
 
             if (theme != null) {
                 themeList.add(theme);
@@ -491,7 +507,15 @@
             modes = parseOutputModes(om);
         }
 
-        return new DefaultCollectionItem(uuid, hash, modes);
+        HashMap<String, String> dataItems = new HashMap<String, String>();
+
+        NodeList dataItemNodes = node.getElementsByTagNameNS(
+            uri, "data-items");
+
+        Element di = (Element)dataItemNodes.item(0);
+        dataItems = parseDataItems(di);
+
+        return new DefaultCollectionItem(uuid, hash, modes, dataItems);
     }
 
 
@@ -564,6 +588,84 @@
     }
 
 
+    /**
+     * Create a Key/Value map for data nodes of artifact/collectionitem.
+     */
+    protected static HashMap<String, String> parseDataItems(Element node) {
+        logger.debug("AddArtifactServiceImpl.parseDataItems");
+
+        if (node == null) {
+            logger.debug("The node for parsing DataItems is null!");
+            return null;
+        }
+
+        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
+
+        NodeList list = node.getElementsByTagNameNS(uri, "data");
+
+        int size = list.getLength();
+
+        if (size == 0) {
+            logger.debug("No static data-item nodes found!");
+        }
+
+        HashMap<String, String> data = new HashMap<String, String>(size*2);
+
+        for (int i = 0; i < size; i++) {
+            Element tmp = (Element)list.item(i);
+
+            String key = tmp.getAttributeNS(uri, "name");
+
+            if (key.length() == 0) {
+                logger.warn("Found an invalid data item mode.");
+                continue;
+            }
+
+            // XXX We are restricted on 1/1 key/values in the data map here.
+            NodeList valueNodes = tmp.getElementsByTagName("art:item");
+
+            Element item = (Element) valueNodes.item(0);
+            String value = item.getAttributeNS(uri, "value");
+            logger.debug("Found a data item " + key + " : " + value);
+
+            data.put(key, value);
+        }
+
+
+        // Dynamic data.
+        list = node.getElementsByTagNameNS(uri, "select");
+
+        size = list.getLength();
+
+        if (size == 0) {
+            logger.debug("No dynamic data-item nodes found!");
+        }
+
+        for (int i = 0; i < size; i++) {
+            Element tmp = (Element)list.item(i);
+
+            String key = tmp.getAttributeNS(uri, "name");
+
+            if (key.length() == 0) {
+                logger.warn("Found an invalid data item node (missing key).");
+                continue;
+            }
+
+            String value = tmp.getAttributeNS(uri, "defaultValue");
+
+            if (value.length() == 0) {
+                logger.warn("Found an invalid data item node (missing value).");
+                continue;
+            }
+
+            logger.debug("Found a (dyn) data item " + key + " : " + value);
+
+            data.put(key, value);
+        }
+
+        return data;
+    }
+
     protected static List<Facet> extractFacets(Element outmode) {
         logger.debug("DescribeCollectionServiceImpl - extractFacets()");
 
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/AttributedTheme.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/AttributedTheme.java	Tue Dec 13 09:51:47 2011 +0000
@@ -12,6 +12,8 @@
 
     protected Map<String, String> attributes;
 
+    /** CollectionItem associated with this facet/themes artifact. */
+    protected CollectionItem collectionItem;
 
     public AttributedTheme() {
         this.attributes = new HashMap<String, String>();
@@ -177,5 +179,19 @@
 
         return true;
     }
+
+
+    /** Get the CollectionItem representing the facets artifact. */
+    @Override
+    public CollectionItem getCollectionItem() {
+        return collectionItem;
+    }
+
+
+    /** Set the CollectionItem representing the facets artifact. */
+    @Override
+    public void setCollectionItem(CollectionItem ci) {
+        this.collectionItem = ci;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/Collection.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/Collection.java	Tue Dec 13 09:51:47 2011 +0000
@@ -38,6 +38,9 @@
 
     public ThemeList getThemeList(String outName);
 
+    /** Sets mapping outputname to ThemeList. */
+    public void setThemeLists(Map<String, ThemeList> map);
+
     public List<Recommendation> getRecommendations();
 
     public void addRecommendation(Recommendation recommendation);
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/CollectionItem.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/CollectionItem.java	Tue Dec 13 09:51:47 2011 +0000
@@ -2,6 +2,7 @@
 
 import java.io.Serializable;
 import java.util.List;
+import java.util.Map;
 
 
 /**
@@ -45,5 +46,12 @@
      * @return the facets of the wrapped artifact for a specific output mode.
      */
     List<Facet> getFacets(String outputmode);
+
+
+    /**
+     * Returns data key/value map.
+     * @return key/value data map
+     */
+    Map<String, String> getData();
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultCollection.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultCollection.java	Tue Dec 13 09:51:47 2011 +0000
@@ -193,6 +193,12 @@
     }
 
 
+    /** Set the outputname to themelist map. */
+    public void setThemeLists(Map<String, ThemeList> map) {
+        this.themeLists = map;
+    }
+
+
     public List<Recommendation> getRecommendations() {
         return recommendations;
     }
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultCollectionItem.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultCollectionItem.java	Tue Dec 13 09:51:47 2011 +0000
@@ -1,24 +1,28 @@
 package de.intevation.flys.client.shared.model;
 
 import java.util.List;
+import java.util.Map;
 
 
 /**
- * The default implementation of a CollectionItem.
+ * The default implementation of a CollectionItem (artifact).
  *
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
 public class DefaultCollectionItem implements CollectionItem {
 
-    /** The identifier that specifies the artifact related to this item.*/
+    /** The identifier that specifies the artifact related to this item. */
     protected String identifier;
 
-    /** The hash that specifies the artifact related to this item.*/
+    /** The hash that specifies the artifact related to this item. */
     protected String hash;
 
-    /** The list of output modes supported by the artifact of this item.*/
+    /** The list of output modes supported by the artifact of this item. */
     protected List<OutputMode> outputModes;
 
+    /** The map of datanames to data values. */
+    protected Map<String, String> data;
+
 
     /**
      * An empty constructor.
@@ -37,13 +41,17 @@
     public DefaultCollectionItem(
         String           identifier,
         String           hash,
-        List<OutputMode> modes) {
+        List<OutputMode> modes,
+        Map<String,String> data
+    ) {
         this.identifier  = identifier;
         this.hash        = hash;
         this.outputModes = modes;
+        this.data        = data;
     }
 
 
+
     public String identifier() {
         return identifier;
     }
@@ -69,5 +77,14 @@
 
         return null;
     }
+
+
+    /**
+     * Returns artifact data.
+     * @return key/value data map
+     */
+    public Map<String, String> getData() {
+        return this.data;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultData.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultData.java	Tue Dec 13 09:51:47 2011 +0000
@@ -1,5 +1,6 @@
 package de.intevation.flys.client.shared.model;
 
+import de.intevation.flys.client.shared.model.DefaultDataItem;
 
 /**
  * The default implementation of a {@link Data} item. This class just implements
@@ -94,5 +95,25 @@
     public DataItem getDefault() {
         return defaultItem;
     }
+
+
+    /** Conveniently create simplistic data. */
+    public static DefaultData createSimpleStringData(
+        String name,
+        String value
+    ) {
+        DefaultDataItem d = new DefaultDataItem(name, name, value);
+        return new DefaultData(name, null, null, new DataItem[] {d});
+    }
+
+    /** Conveniently create simplistic data array. */
+    public static Data[] createSimpleStringDataArray(
+        String name,
+        String value
+    ) {
+        DefaultDataItem d = new DefaultDataItem(name, name, value);
+        return new Data[]
+            { new DefaultData(name, null, null, new DataItem[] {d})};
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultTheme.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultTheme.java	Tue Dec 13 09:51:47 2011 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.client.shared.model;
 
+import de.intevation.flys.client.shared.model.CollectionItem;
+
 
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
@@ -20,6 +22,9 @@
 
     protected String description;
 
+    /** CollectionItem associated with this facet/themes artifact. */
+    protected CollectionItem collectionItem;
+
 
     public DefaultTheme() {
     }
@@ -34,13 +39,14 @@
         String  facet,
         String  description)
     {
-        this.position    = pos;
-        this.index       = index;
-        this.active      = active;
-        this.visible     = visible;
-        this.artifact    = art;
-        this.facet       = facet;
-        this.description = description;
+        this.position       = pos;
+        this.index          = index;
+        this.active         = active;
+        this.visible        = visible;
+        this.artifact       = art;
+        this.facet          = facet;
+        this.description    = description;
+        this.collectionItem = null;
     }
 
 
@@ -136,5 +142,17 @@
 
         return true;
     }
+
+    /** Get the CollectionItem representing the facets artifact. */
+    @Override
+    public CollectionItem getCollectionItem() {
+        return collectionItem;
+    }
+
+    /** Set the CollectionItem representing the facets artifact. */
+    @Override
+    public void setCollectionItem(CollectionItem ci) {
+        this.collectionItem = ci;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/Theme.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/Theme.java	Tue Dec 13 09:51:47 2011 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.client.shared.model;
 
+import de.intevation.flys.client.shared.model.CollectionItem;
+
 import java.io.Serializable;
 
 
@@ -31,5 +33,11 @@
     void setDescription(String description);
 
     boolean equals(Object o);
+
+    /** Get the CollectionItem representing the facets artifact. */
+    CollectionItem getCollectionItem();
+
+    /** Set the CollectionItem representing the facets artifact. */
+    void setCollectionItem(CollectionItem ci);
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/ThemeList.java	Tue Dec 13 09:13:03 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/ThemeList.java	Tue Dec 13 09:51:47 2011 +0000
@@ -2,10 +2,12 @@
 
 import java.io.Serializable;
 
+import java.util.LinkedHashMap;
 import java.util.List;
 
 
 /**
+ * Data Model for list of themes (shown facets).
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
 public class ThemeList implements Serializable {
@@ -96,5 +98,51 @@
             }
         }
     }
+
+
+    /**
+     * Create a map from index to description of facets that have a given name.
+     * Only visible facets are taken into account.
+     * @param facetName name to match against facets whose info to put in map.
+     * @return mapping of index to description
+     */
+    public LinkedHashMap<String, String> toMapIndexDescription(String facetName) {
+        int count = getThemeCount();
+        LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
+        for (int i = 0; i < count; i++) {
+            Theme theme = getThemeAt(i + 1);
+
+            if (theme == null || theme.getVisible() == 0) {
+                continue;
+            }
+
+            if (theme.getFacet().equals(facetName)) {
+                valueMap.put(String.valueOf(theme.getIndex()),
+                    theme.getDescription());
+            }
+        }
+        return valueMap;
+    }
+
+
+    public LinkedHashMap<String, String>
+        toMapArtifactUUIDDescription(String facetName
+    ) {
+        int count = getThemeCount();
+        LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
+        for (int i = 0; i < count; i++) {
+            Theme theme = getThemeAt(i + 1);
+
+            if (theme == null || theme.getVisible() == 0) {
+                continue;
+            }
+
+            if (theme.getFacet().equals(facetName)) {
+                valueMap.put(theme.getArtifact(),
+                    theme.getDescription());
+            }
+        }
+        return valueMap;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org