changeset 922:95356252c309

Added the option to call the distance info service with a filter to fetch locations or distances only. flys-artifacts/trunk@2272 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 01 Jul 2011 08:33:52 +0000
parents 610d0e0f4f85
children 7ca4a287cd0e
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java
diffstat 2 files changed, 65 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jun 30 11:32:17 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jul 01 08:33:52 2011 +0000
@@ -1,3 +1,9 @@
+2011-07-01  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java:
+	  Added the option to use a filter to reduce the number of items returned
+	  by this service.
+
 2011-06-30  Ingo Weinzierl <ingo@intevation.de>
 
 	flys/issue159 (WINFO: Radiobutton - Ortsauswahl bei "W für ungleichwertigen Abflusslängsschnitt" entfernen)
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java	Thu Jun 30 11:32:17 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java	Fri Jul 01 08:33:52 2011 +0000
@@ -37,6 +37,10 @@
  */
 public class DistanceInfoService extends DefaultService {
 
+    private static enum DistanceFilter {
+        NONE, LOCATIONS, DISTANCES
+    }
+
     /** The logger used in this service.*/
     private static Logger logger = Logger.getLogger(DistanceInfoService.class);
 
@@ -44,6 +48,8 @@
 
     public static final String RIVER_XPATH = "/art:river/text()";
 
+    public static final String FILTER_XPATH = "/art:river/art:filter/text()";
+
 
     /**
      * The default constructor.
@@ -62,6 +68,9 @@
         String river = XMLUtils.xpathString(
             data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE);
 
+        String filter  = XMLUtils.xpathString(
+            data, FILTER_XPATH, ArtifactNamespaceContext.INSTANCE);
+
         if (river == null || (river = river.trim()).length() == 0) {
             logger.warn("No river specified. Cannot return distance info!");
             return XMLUtils.newDocument();
@@ -73,19 +82,22 @@
 
         if (cache == null) {
             logger.debug("no cache configured for distance info");
-            return getUncached(river);
+            return getUncached(river, filter);
         }
 
-        net.sf.ehcache.Element element = cache.get(river);
+
+        String key = getCacheKey(river, filter);
+
+        net.sf.ehcache.Element element = cache.get(key);
 
         if (element != null) {
             logger.debug("distance info found in cache");
             return (Document)element.getValue();
         }
 
-        Document result = getUncached(river);
+        Document result = getUncached(river, filter);
 
-        element = new net.sf.ehcache.Element(river, result);
+        element = new net.sf.ehcache.Element(key, result);
 
         logger.debug("store distance info found into cache");
 
@@ -94,7 +106,15 @@
         return result;
     }
 
-    protected Document getUncached(String river) {
+
+    protected String getCacheKey(String river, String filtertype) {
+        return filtertype != null && filtertype.length() > 0
+            ? river + "_" + filtertype
+            : river;
+    }
+
+
+    protected Document getUncached(String river, String filtertype) {
 
         Document result = XMLUtils.newDocument();
 
@@ -106,10 +126,15 @@
 
             Element all = result.createElement("distances");
 
+            DistanceFilter filter = getDistanceFilter(filtertype);
+
             while (iter.hasNext()) {
                 Annotation a = iter.next();
-                Element distance = buildDistanceNode(result, a);
-                all.appendChild(distance);
+                Element distance = buildDistanceNode(result, a, filter);
+
+                if (distance != null) {
+                    all.appendChild(distance);
+                }
             }
 
             result.appendChild(all);
@@ -123,6 +148,22 @@
     }
 
 
+    protected static DistanceFilter getDistanceFilter(String type) {
+        if (type.equals("locations")) {
+            logger.debug("Found 'location' filter.");
+            return DistanceFilter.LOCATIONS;
+        }
+        else if (type.equals("distances")) {
+            logger.debug("Found 'distances' filter.");
+            return DistanceFilter.DISTANCES;
+        }
+
+        logger.debug("Do not use any filter at all.");
+
+        return DistanceFilter.NONE;
+    }
+
+
     /**
      * This method build an Element for a distance info.
      *
@@ -132,8 +173,9 @@
      * @return an Element that contains information about a distance.
      */
     protected static Element buildDistanceNode(
-        Document   document,
-        Annotation anno
+        Document       document,
+        Annotation     anno,
+        DistanceFilter filter
     ) {
         Position   pos   = anno.getPosition();
         Range      range = anno.getRange();
@@ -142,6 +184,14 @@
         BigDecimal a     = range.getA();
         BigDecimal b     = range.getB();
 
+        if (b == null && filter == DistanceFilter.DISTANCES) {
+            return null;
+        }
+
+        if (b != null && filter == DistanceFilter.LOCATIONS) {
+            return null;
+        }
+
         Element distance = document.createElement("distance");
 
         distance.setAttribute("description", pos.getValue());

http://dive4elements.wald.intevation.org