Mercurial > dive4elements > river
diff flys-client/src/main/java/de/intevation/flys/client/server/CollectionHelper.java @ 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 | ab8eb2f544f2 |
children | 4df2d9a4b9b4 |
line wrap: on
line diff
--- 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()");