diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java @ 2131:e50a928187cd

Added stubby hyk infrastructure. flys-artifacts/trunk@3706 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 18 Jan 2012 13:39:16 +0000
parents
children 04b6b6a4564d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java	Wed Jan 18 13:39:16 2012 +0000
@@ -0,0 +1,139 @@
+package de.intevation.flys.artifacts.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import java.math.BigDecimal;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+import de.intevation.flys.model.HYKFormation;
+import de.intevation.flys.model.HYKFlowZone;
+
+import de.intevation.flys.artifacts.cache.CacheFactory;
+
+import de.intevation.flys.backend.SessionHolder;
+
+
+/**
+ * Factory to access HYKs (hydrographic values).
+ */
+public class HYKFactory
+{
+    private static Logger logger = Logger.getLogger(HYKFactory.class);
+
+    public static String HYK_CACHE_NAME = "hykache";
+
+
+    /** Do not instantiate a HYKFactory. */
+    private HYKFactory() {
+    }
+
+
+    /**
+     * Get List of Zones for given river and km.
+     */
+    public static Object getHYKs(int riverId, double km) {
+        logger.debug("HYKFactory.getHYKs");
+
+        Cache cache = CacheFactory.getCache(HYK_CACHE_NAME);
+
+        String cacheKey;
+
+        if (cache != null) {
+            cacheKey = "" + riverId + "_" + km;
+            Element element = cache.get(cacheKey);
+            if (element != null) {
+                logger.debug("Got hyk from cache");
+                return (List<Zone>)element.getValue();
+            }
+        }
+        else {
+            cacheKey = null;
+        }
+
+        List<Zone> zones = getZonesUncached(riverId, km);
+
+        if (zones != null && cacheKey != null) {
+            logger.debug("Store hykzones in cache.");
+            Element element = new Element(cacheKey, zones);
+            cache.put(element);
+        }
+
+        return zones;
+    }
+
+
+    /**
+     * 
+     * @param column the position columns value
+     * @param wst_id database id of the wst
+     * @return according WKms.
+     */
+    public static List<Zone> getZonesUncached(int riverId, double km) {
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("HYKFactory.getZoneUncached " + riverId + " km " + km);
+        }
+
+        Session session = SessionHolder.HOLDER.get();
+        Query query = session.createQuery(
+            "from HYKFormation where entry.HYK.river.id = :riverid " +
+            " and entry.km between " +
+                ":km - cast(distance_vl/1000.0 - 0.001 as big_decimal) and " +
+                ":km + cast(distance_vl/1000.0 + 0.001 as big_decimal)");
+        query.setParameter("riverid", riverId);
+        query.setParameter("km", new BigDecimal(km));
+        logger.debug("Big km " + new BigDecimal(km));
+        List<HYKFormation> forms = query.list();
+
+        List<Zone> zones = new ArrayList<Zone>();
+        for (HYKFormation form : forms) {
+            logger.debug("One HYKFormation found (entry: "
+                + form.getEntry().getId()
+                + ") /km " + form.getEntry().getKm() + ".");
+
+            // Create respective zones.
+            for (HYKFlowZone flowZone: form.getZones()) {
+                Zone z = new Zone(flowZone.getA().doubleValue(),
+                    flowZone.getB().doubleValue(),
+                    flowZone.getType().getName());
+                zones.add(z);
+            }
+        }
+
+        return zones;
+    }
+
+    /** Labelled section. */
+    public static class Zone {
+        /** Lower end of segment. */
+        protected double  from;
+        /** Upper end of segment. */
+        protected double  to;
+        /** The label. */
+        protected String name;
+
+        /** Constructor for labelled section. */
+        public Zone (double from, double to, String name) {
+            this.from = from;
+            this.to   = to;
+            this.name = name;
+        }
+
+        public double getFrom() {
+            return from;
+        }
+
+        public String getName() {
+            return name;
+        }
+    } // public static class Zone
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org