comparison 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
comparison
equal deleted inserted replaced
1434:9115b2a28be1 1435:f6fbfdc813f0
51 public class CollectionHelper { 51 public class CollectionHelper {
52 52
53 private static final Logger logger = 53 private static final Logger logger =
54 Logger.getLogger(CollectionHelper.class); 54 Logger.getLogger(CollectionHelper.class);
55 55
56
57 public static final String ERROR_ADD_ARTIFACT = "error_add_artifact"; 56 public static final String ERROR_ADD_ARTIFACT = "error_add_artifact";
58 57
59 public static final String ERROR_REMOVE_ARTIFACT = "error_remove_artifact"; 58 public static final String ERROR_REMOVE_ARTIFACT = "error_remove_artifact";
60 59
61 public static final String XPATH_FACETS = "art:facets/art:facet"; 60 public static final String XPATH_FACETS = "art:facets/art:facet";
325 } 324 }
326 catch (NumberFormatException nfe) { 325 catch (NumberFormatException nfe) {
327 // do nothing 326 // do nothing
328 } 327 }
329 328
330 Map<String, ThemeList> themeList = parseThemeLists(description);
331 List<Recommendation> recommended = parseRecommendations(description); 329 List<Recommendation> recommended = parseRecommendations(description);
330 Map<String, CollectionItem> collectionItems =
331 new HashMap<String, CollectionItem>();
332 332
333 name = (name != null && name.length() > 0) ? name : uuid; 333 name = (name != null && name.length() > 0) ? name : uuid;
334 334
335 Collection c = !themeList.isEmpty() 335 Collection c = new DefaultCollection(uuid, ttl, name, recommended);
336 ? new DefaultCollection(uuid, ttl, name, recommended, themeList)
337 : new DefaultCollection(uuid, ttl, name, recommended);
338 336
339 NodeList items = (NodeList) XMLUtils.xpath( 337 NodeList items = (NodeList) XMLUtils.xpath(
340 description, 338 description,
341 "art:artifact-collection/art:artifacts/art:artifact", 339 "art:artifact-collection/art:artifacts/art:artifact",
342 XPathConstants.NODESET, 340 XPathConstants.NODESET,
355 (Element)items.item(i), 353 (Element)items.item(i),
356 i == 0); 354 i == 0);
357 355
358 if (item != null) { 356 if (item != null) {
359 c.addItem(item); 357 c.addItem(item);
360 } 358 collectionItems.put(item.identifier() ,item);
361 } 359 }
360 }
361
362 Map<String, ThemeList> themeLists = parseThemeLists(description, collectionItems);
363 c.setThemeLists(themeLists);
362 364
363 logger.debug( 365 logger.debug(
364 "Found " + c.getItemLength() + " collection items " + 366 "Found " + c.getItemLength() + " collection items " +
365 "for the Collection '" + c.identifier() + "'."); 367 "for the Collection '" + c.identifier() + "'.");
366 368
367 return c; 369 return c;
368 } 370 }
369 371
370 372
371 protected static Map<String, ThemeList> parseThemeLists(Document desc) { 373 /**
374 * @param collectionItems map to look up collection item mapping a themes
375 * (artifact) uuid.
376 */
377 protected static Map<String, ThemeList> parseThemeLists(
378 Document desc, Map<String, CollectionItem> collectionItems
379 ) {
372 logger.debug("DescribeCollectionServiceImpl.parseThemeLists"); 380 logger.debug("DescribeCollectionServiceImpl.parseThemeLists");
373 381
374 NodeList lists = (NodeList) XMLUtils.xpath( 382 NodeList lists = (NodeList) XMLUtils.xpath(
375 desc, 383 desc,
376 "/art:artifact-collection/art:attribute/art:outputs/art:output", 384 "/art:artifact-collection/art:attribute/art:outputs/art:output",
387 Element node = (Element)lists.item(i); 395 Element node = (Element)lists.item(i);
388 396
389 String outName = node.getAttribute("name"); 397 String outName = node.getAttribute("name");
390 398
391 if (outName.length() > 0) { 399 if (outName.length() > 0) {
392 ThemeList list = parseThemeList(node); 400 ThemeList list = parseThemeList(node, collectionItems);
401
393 if (list.getThemeCount() > 0) { 402 if (list.getThemeCount() > 0) {
394 themeList.put(outName, list); 403 themeList.put(outName, list);
395 } 404 }
396 } 405 }
397 } 406 }
398 407
399 return themeList; 408 return themeList;
400 } 409 }
401 410
402 411
403 protected static ThemeList parseThemeList(Element node) { 412 /**
413 * @param collectionItems map to look up collection item mapping a themes
414 * (artifact) uuid.
415 */
416 protected static ThemeList parseThemeList(
417 Element node, Map<String, CollectionItem> collectionItems
418 ) {
404 logger.debug("DescribeCollectionServiceImpl.parseThemeList"); 419 logger.debug("DescribeCollectionServiceImpl.parseThemeList");
405 420
406 NodeList themes = node.getElementsByTagNameNS( 421 NodeList themes = node.getElementsByTagNameNS(
407 ArtifactNamespaceContext.NAMESPACE_URI, 422 ArtifactNamespaceContext.NAMESPACE_URI,
408 "facet"); 423 "facet");
411 426
412 List<Theme> themeList = new ArrayList<Theme>(num); 427 List<Theme> themeList = new ArrayList<Theme>(num);
413 428
414 for (int i = 0; i < num; i++) { 429 for (int i = 0; i < num; i++) {
415 Theme theme = parseTheme((Element)themes.item(i)); 430 Theme theme = parseTheme((Element)themes.item(i));
431 theme.setCollectionItem(collectionItems.get(theme.getArtifact()));
416 432
417 if (theme != null) { 433 if (theme != null) {
418 themeList.add(theme); 434 themeList.add(theme);
419 } 435 }
420 } 436 }
489 Element om = (Element)outputmodes.item(0); 505 Element om = (Element)outputmodes.item(0);
490 506
491 modes = parseOutputModes(om); 507 modes = parseOutputModes(om);
492 } 508 }
493 509
494 return new DefaultCollectionItem(uuid, hash, modes); 510 HashMap<String, String> dataItems = new HashMap<String, String>();
511
512 NodeList dataItemNodes = node.getElementsByTagNameNS(
513 uri, "data-items");
514
515 Element di = (Element)dataItemNodes.item(0);
516 dataItems = parseDataItems(di);
517
518 return new DefaultCollectionItem(uuid, hash, modes, dataItems);
495 } 519 }
496 520
497 521
498 /** 522 /**
499 * This method extracts the OutputModes available for a specific 523 * This method extracts the OutputModes available for a specific
561 } 585 }
562 586
563 return modes; 587 return modes;
564 } 588 }
565 589
590
591 /**
592 * Create a Key/Value map for data nodes of artifact/collectionitem.
593 */
594 protected static HashMap<String, String> parseDataItems(Element node) {
595 logger.debug("AddArtifactServiceImpl.parseDataItems");
596
597 if (node == null) {
598 logger.debug("The node for parsing DataItems is null!");
599 return null;
600 }
601
602 String uri = ArtifactNamespaceContext.NAMESPACE_URI;
603
604 NodeList list = node.getElementsByTagNameNS(uri, "data");
605
606 int size = list.getLength();
607
608 if (size == 0) {
609 logger.debug("No static data-item nodes found!");
610 }
611
612 HashMap<String, String> data = new HashMap<String, String>(size*2);
613
614 for (int i = 0; i < size; i++) {
615 Element tmp = (Element)list.item(i);
616
617 String key = tmp.getAttributeNS(uri, "name");
618
619 if (key.length() == 0) {
620 logger.warn("Found an invalid data item mode.");
621 continue;
622 }
623
624 // XXX We are restricted on 1/1 key/values in the data map here.
625 NodeList valueNodes = tmp.getElementsByTagName("art:item");
626
627 Element item = (Element) valueNodes.item(0);
628 String value = item.getAttributeNS(uri, "value");
629 logger.debug("Found a data item " + key + " : " + value);
630
631 data.put(key, value);
632 }
633
634
635 // Dynamic data.
636 list = node.getElementsByTagNameNS(uri, "select");
637
638 size = list.getLength();
639
640 if (size == 0) {
641 logger.debug("No dynamic data-item nodes found!");
642 }
643
644 for (int i = 0; i < size; i++) {
645 Element tmp = (Element)list.item(i);
646
647 String key = tmp.getAttributeNS(uri, "name");
648
649 if (key.length() == 0) {
650 logger.warn("Found an invalid data item node (missing key).");
651 continue;
652 }
653
654 String value = tmp.getAttributeNS(uri, "defaultValue");
655
656 if (value.length() == 0) {
657 logger.warn("Found an invalid data item node (missing value).");
658 continue;
659 }
660
661 logger.debug("Found a (dyn) data item " + key + " : " + value);
662
663 data.put(key, value);
664 }
665
666 return data;
667 }
566 668
567 protected static List<Facet> extractFacets(Element outmode) { 669 protected static List<Facet> extractFacets(Element outmode) {
568 logger.debug("DescribeCollectionServiceImpl - extractFacets()"); 670 logger.debug("DescribeCollectionServiceImpl - extractFacets()");
569 671
570 NodeList facetList = (NodeList) XMLUtils.xpath( 672 NodeList facetList = (NodeList) XMLUtils.xpath(

http://dive4elements.wald.intevation.org