changeset 2063:97a25b54eea3

Part 1 of #125: added a description for a location to the WaterlevelExport. flys-artifacts/trunk@3557 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 02 Jan 2012 08:50:50 +0000
parents ed6c6d437875
children ca8997aa683e
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/cache.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java
diffstat 6 files changed, 186 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Dec 29 14:21:52 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Jan 02 08:50:50 2012 +0000
@@ -1,3 +1,26 @@
+2012-01-02  Ingo Weinzierl <ingo@intevation.de>
+
+	PART I of flys/issue125 (W-INFO: Wasserspiegellagenberechnung / tabellarische Berechnungsausgabe)
+
+	* doc/conf/cache.xml: Registered a new Cache for the LocationProvider.
+
+	* src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java:
+	  New. This class is able to return the description of a location based on a
+	  river and kilometer parameter. The LocationProvider stores single
+	  locations into a Cache if one is configured for this class.
+
+	* src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java:
+	  Added a method that returns a single Annotation for a specific kilometer and
+	  river.
+
+	* src/main/java/de/intevation/flys/utils/FLYSUtils.java: Added a method
+	  getLocationDescription() that might be used to determine the description
+	  of a specified kilometer for a given river.
+
+	* src/main/java/de/intevation/flys/exports/WaterlevelExporter.java: Make use
+	  of FLYSUtils.getLocationDescription() to add a new column that contains
+	  the location description.
+
 2011-12-29  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/WQSelect.java: Write
--- a/flys-artifacts/doc/conf/cache.xml	Thu Dec 29 14:21:52 2011 +0000
+++ b/flys-artifacts/doc/conf/cache.xml	Mon Jan 02 08:50:50 2012 +0000
@@ -37,6 +37,16 @@
            memoryStoreEvictionPolicy="LFU"
            />
 
+    <cache name="location-provider"
+           maxElementsInMemory="5000"
+           eternal="false"
+           diskPersistent="true"
+           overflowToDisk="true"
+           timeToIdleSeconds="360"
+           timeToLiveSeconds="86400"
+           memoryStoreEvictionPolicy="LFU"
+           />
+
     <!-- This one is used to cache the distance infos per river as Lists -->
     <cache name="annotations"
            maxElementsInMemory="2000"
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java	Thu Dec 29 14:21:52 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java	Mon Jan 02 08:50:50 2012 +0000
@@ -49,6 +49,23 @@
         return query.list();
     }
 
+
+    public static Annotation getAnnotation(String river, double km) {
+        Session session = SessionHolder.HOLDER.get();
+
+        Query query = session.createQuery(
+            "from Annotation as a " +
+            "where a.range.river.name = :river AND a.range.a = :km");
+
+        query.setParameter("river", river);
+        query.setParameter("km", BigDecimal.valueOf(km));
+
+        List<Annotation> result = query.list();
+
+        return result != null && result.size() > 0 ? result.get(0) : null;
+    }
+
+
     /**
      * Get minimal "a" ("from") and maximal "b" ("to") value of annotations'
      * ranges of a river.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java	Mon Jan 02 08:50:50 2012 +0000
@@ -0,0 +1,89 @@
+package de.intevation.flys.artifacts.model;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.model.Annotation;
+import de.intevation.flys.model.Position;
+
+import de.intevation.flys.artifacts.cache.CacheFactory;
+import de.intevation.flys.artifacts.model.AnnotationsFactory;
+
+
+public class LocationProvider {
+
+    public static final String CACHE_KEY = "location-provider";
+
+
+    private static final Logger logger =
+        Logger.getLogger(LocationProvider.class);
+
+
+    private LocationProvider() {
+    }
+
+
+    public static String getLocation(String river, double km) {
+        return getLocation(getLocationHash(river, km), river, km);
+    }
+
+
+    public static String getLocation(String hash, String river, double km) {
+        logger.debug("Fetch location for '" + river + "' at '" + km + "'");
+
+        Cache cache = CacheFactory.getCache(CACHE_KEY);
+
+        if (cache != null) {
+            return getCachedLocation(cache, hash, river, km);
+        }
+        else {
+            logger.info("No Cache for Locations configured.");
+            return getUncachedLocation(river, km);
+        }
+    }
+
+
+    protected static String getCachedLocation(
+        Cache cache,
+        String hash,
+        String river,
+        double km
+    ) {
+        logger.debug("Fetch location from cache.");
+
+        Element element = cache.get(hash);
+
+        if (element == null) {
+            logger.debug("Element is not in cache yet.");
+
+            String location = getUncachedLocation(river, km);
+            element = new Element(hash, location);
+            cache.put(element);
+        }
+
+        return (String) element.getValue();
+    }
+
+
+    protected static String getUncachedLocation(String river, double km) {
+        logger.debug("Fetch location from backend.");
+
+        Annotation annotation = AnnotationsFactory.getAnnotation(river, km);
+
+        if (annotation != null) {
+            logger.debug("Found an annotation.");
+            Position pos = annotation.getPosition();
+            return pos.getValue();
+        }
+
+        return "";
+    }
+
+
+    protected static String getLocationHash(String river, double km) {
+        return river + "#" + km;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Thu Dec 29 14:21:52 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Mon Jan 02 08:50:50 2012 +0000
@@ -52,6 +52,15 @@
     public static final String CSV_Q_HEADER =
         "export.waterlevel.csv.header.q";
 
+    public static final String CSV_Q_DESC_HEADER =
+        "export.waterlevel.csv.header.q.desc";
+
+    public static final String CSV_LOCATION_HEADER =
+        "export.waterlevel.csv.header.location";
+
+    public static final String CSV_GAUGE_HEADER =
+        "export.waterlevel.csv.header.gauge";
+
     public static final String CSV_META_RESULT =
         "export.waterlevel.csv.meta.result";
 
@@ -80,9 +89,12 @@
     public static final Pattern NUMBERS_PATTERN =
         Pattern.compile("\\D*(\\d++.\\d*)\\D*");
 
-    public static final String DEFAULT_CSV_KM_HEADER = "Fluss-Km";
-    public static final String DEFAULT_CSV_W_HEADER  = "W [NN + m]";
-    public static final String DEFAULT_CSV_Q_HEADER  = "Q [m\u00b3/s]";
+    public static final String DEFAULT_CSV_KM_HEADER       = "Fluss-Km";
+    public static final String DEFAULT_CSV_W_HEADER        = "W [NN + m]";
+    public static final String DEFAULT_CSV_Q_HEADER        = "Q [m\u00b3/s]";
+    public static final String DEFAULT_CSV_Q_DESC_HEADER   = "Bezeichnung";
+    public static final String DEFAULT_CSV_LOCATION_HEADER = "Lage";
+    public static final String DEFAULT_CSV_GAUGE_HEADER    = "Bezugspegel";
 
 
     /** The storage that contains all WQKms objects for the different facets.*/
@@ -304,7 +316,10 @@
         writer.writeNext(new String[] {
             msg(CSV_KM_HEADER, DEFAULT_CSV_KM_HEADER),
             msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER),
-            msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER)
+            msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER),
+            msg(CSV_Q_DESC_HEADER, DEFAULT_CSV_Q_DESC_HEADER),
+            msg(CSV_LOCATION_HEADER, DEFAULT_CSV_LOCATION_HEADER),
+            msg(CSV_GAUGE_HEADER, DEFAULT_CSV_GAUGE_HEADER)
         });
     }
 
@@ -319,13 +334,18 @@
         int      size   = wqkms.size();
         double[] result = new double[3];
 
+        FLYSArtifact flys = (FLYSArtifact) master;
+
         for (int i = 0; i < size; i ++) {
             result = wqkms.get(i, result);
 
             writer.writeNext(new String[] {
                 kmf.format(result[2]),
                 wf.format(result[0]),
-                qf.format(result[1])
+                qf.format(result[1]),
+                "", // Bezeichnung
+                FLYSUtils.getLocationDescription(flys, result[2]),
+                "" // Bezugspegel
             });
         }
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Thu Dec 29 14:21:52 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Mon Jan 02 08:50:50 2012 +0000
@@ -28,6 +28,7 @@
 import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.WINFOArtifact;
 import de.intevation.flys.artifacts.model.RiverFactory;
+import de.intevation.flys.artifacts.model.LocationProvider;
 import de.intevation.flys.model.Gauge;
 import de.intevation.flys.model.MainValue;
 import de.intevation.flys.model.River;
@@ -554,5 +555,26 @@
 
         return url;
     }
+
+
+    /**
+     * This method returns the description for a given <i>km</i> for a specific
+     * river. The river is provided by the FLYSArtifact <i>flys</i>.
+     *
+     * @param flys The FLYSArtifact that provides a river.
+     * @param km The kilometer.
+     *
+     * @return the description for <i>km</i> or an empty string if no
+     * description was found.
+     */
+    public static String getLocationDescription(FLYSArtifact flys, double km) {
+        String river = getRivername(flys);
+
+        if (river == null) {
+            return "";
+        }
+
+        return LocationProvider.getLocation(river, km);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org