changeset 2145:e92bc9b0ca1d

DistanceInfoService now uses annotations from LocationProvider. flys-artifacts/trunk@3724 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 19 Jan 2012 15:43:57 +0000
parents a4bdf7d8527e
children 2d850e585176
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/cache.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java
diffstat 3 files changed, 45 insertions(+), 120 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jan 19 13:43:58 2012 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jan 19 15:43:57 2012 +0000
@@ -1,3 +1,11 @@
+2012-01-19	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* doc/conf/cache.xml: Removed cache "service-distanceinfo".
+
+	* src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java:
+	  Now uses the annotations from the LocationProvider and the
+	  XML documents are no longer cached.
+
 2012-01-19	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* doc/conf/cache.xml: Removed "annotations" cache.
--- a/flys-artifacts/doc/conf/cache.xml	Thu Jan 19 13:43:58 2012 +0000
+++ b/flys-artifacts/doc/conf/cache.xml	Thu Jan 19 15:43:57 2012 +0000
@@ -28,18 +28,9 @@
            memoryStoreEvictionPolicy="LRU"
            />
 
-    <!-- This one is used to cache the distance infos per river as Documents -->
-    <cache name="service-distanceinfo"
+    <cache name="location-provider"
            maxElementsInMemory="20"
            eternal="false"
-           timeToIdleSeconds="360"
-           timeToLiveSeconds="86400"
-           memoryStoreEvictionPolicy="LFU"
-           />
-
-    <cache name="location-provider"
-           maxElementsInMemory="5000"
-           eternal="false"
            diskPersistent="true"
            overflowToDisk="true"
            timeToIdleSeconds="360"
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java	Thu Jan 19 13:43:58 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java	Thu Jan 19 15:43:57 2012 +0000
@@ -1,6 +1,5 @@
 package de.intevation.flys.artifacts.services;
 
-import java.math.BigDecimal;
 import java.util.Iterator;
 
 import org.apache.log4j.Logger;
@@ -14,17 +13,10 @@
 import de.intevation.artifacts.common.ArtifactNamespaceContext;
 import de.intevation.artifacts.common.utils.XMLUtils;
 
-import de.intevation.flys.model.Annotation;
-import de.intevation.flys.model.Attribute;
-import de.intevation.flys.model.Position;
-import de.intevation.flys.model.Range;
-import de.intevation.flys.model.Edge;
+import de.intevation.flys.model.FastAnnotations;
 
-import de.intevation.flys.artifacts.model.AnnotationsFactory;
+import de.intevation.flys.artifacts.model.LocationProvider;
 
-import de.intevation.flys.artifacts.cache.CacheFactory;
-
-import net.sf.ehcache.Cache;
 
 /**
  * This service provides information about distances of a specified river.
@@ -33,15 +25,9 @@
  */
 public class DistanceInfoService extends FLYSService {
 
-    private static enum DistanceFilter {
-        NONE, LOCATIONS, DISTANCES
-    }
-
     /** The logger used in this service. */
     private static Logger logger = Logger.getLogger(DistanceInfoService.class);
 
-    public static final String CACHE_NAME = "service-distanceinfo";
-
     public static final String RIVER_XPATH = "/art:river/text()";
 
     public static final String FILTER_XPATH = "/art:river/art:filter/text()";
@@ -65,7 +51,7 @@
         String river = XMLUtils.xpathString(
             data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE);
 
-        String filter  = XMLUtils.xpathString(
+        String filterName  = XMLUtils.xpathString(
             data, FILTER_XPATH, ArtifactNamespaceContext.INSTANCE);
 
         if (river == null || (river = river.trim()).length() == 0) {
@@ -75,60 +61,22 @@
 
         logger.debug("Search distances for river: " + river);
 
-        Cache cache = CacheFactory.getCache(CACHE_NAME);
-
-        if (cache == null) {
-            logger.debug("no cache configured for distance info");
-            return getUncached(river, filter);
-        }
-
-
-        String key = getCacheKey(river, filter);
-
-        net.sf.ehcache.Element element = cache.get(key);
+        FastAnnotations fas = LocationProvider.getAnnotations(river);
 
-        if (element != null) {
-            logger.debug("distance info found in cache");
-            return (Document)element.getValue();
-        }
-
-        Document result = getUncached(river, filter);
+        FastAnnotations.Filter filter = selectFilter(filterName);
 
-        element = new net.sf.ehcache.Element(key, result);
-
-        logger.debug("store distance info found into cache");
-
-        cache.put(element);
-
-        return result;
+        return buildDocument(fas.filter(filter));
     }
 
-
-    protected String getCacheKey(String river, String filtertype) {
-        return filtertype != null && filtertype.length() > 0
-            ? river + "_" + filtertype
-            : river;
-    }
-
-
-    protected Document getUncached(String river, String filtertype) {
-
+    protected Document buildDocument(
+        Iterator<FastAnnotations.Annotation> iter
+    ) {
         Document result = XMLUtils.newDocument();
 
-        Iterator<Annotation> iter =
-            AnnotationsFactory.getAnnotationsIterator(river);
-
         Element all = result.createElement("distances");
 
-        DistanceFilter filter = getDistanceFilter(filtertype);
-
         while (iter.hasNext()) {
-            Annotation a = iter.next();
-            Element distance = buildDistanceNode(result, a, filter);
-
-            if (distance != null) {
-                all.appendChild(distance);
-            }
+            all.appendChild(buildNode(result, iter.next()));
         }
 
         result.appendChild(all);
@@ -136,75 +84,53 @@
         return result;
     }
 
+    protected static FastAnnotations.Filter selectFilter(String name) {
 
-    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;
+        if (name != null) {
+            if ("locations".equals(name)) return FastAnnotations.IS_POINT;
+            if ("distances".equals(name)) return FastAnnotations.IS_RANGE;
         }
 
-        logger.debug("Do not use any filter at all.");
-
-        return DistanceFilter.NONE;
+        return FastAnnotations.ALL;
     }
 
-
     /**
      * Builds an Element for a distance info.
      *
-     * @param anno The Annotation that provides information about the distance.
+     * @param an The Annotation that provides information about the distance.
      *
      * @return an Element that contains information about a distance.
      */
-    protected static Element buildDistanceNode(
-        Document       document,
-        Annotation     anno,
-        DistanceFilter filter
+    protected static Element buildNode(
+        Document                   document,
+        FastAnnotations.Annotation an
     ) {
-        Position   pos   = anno.getPosition();
-        Range      range = anno.getRange();
-        Attribute  attr  = anno.getAttribute();
-        Edge       edge  = anno.getEdge();
-        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());
+        distance.setAttribute("description", an.getPosition());
 
-        String riverSide = attr.getValue();
+        String riverSide = an.getAttribute();
 
         if (riverSide != null && riverSide.length() > 0) {
             distance.setAttribute("riverside", riverSide);
         }
 
-        if (a != null) {
-            distance.setAttribute("from", a.toString());
-        }
-        if (b != null) {
-            distance.setAttribute("to", b.toString());
+        distance.setAttribute("from", String.valueOf(an.getA()));
+
+        double b      = an.getB();
+        double bottom = an.getBottom();
+        double top    = an.getTop();
+
+        if (!Double.isNaN(b)) {
+            distance.setAttribute("to", String.valueOf(b));
         }
-        if (edge != null) {
-            BigDecimal bottom = edge.getBottom();
-            BigDecimal top    = edge.getTop();
-            if (bottom != null) {
-                distance.setAttribute("bottom", bottom.toString());
-            }
-            if (top != null) {
-                distance.setAttribute("top", top.toString());
-            }
+
+        if (!Double.isNaN(bottom)) {
+            distance.setAttribute("bottom", String.valueOf(bottom));
+        }
+
+        if (!Double.isNaN(top)) {
+            distance.setAttribute("top", String.valueOf(top));
         }
 
         return distance;

http://dive4elements.wald.intevation.org