Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java @ 2199:7bac5afb9892
Skip 'inactive' points in output generation.
flys-artifacts/trunk@3815 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Mon, 30 Jan 2012 08:37:01 +0000 |
parents | 8c9c40459d8f |
children | 98a350bb91a9 |
line wrap: on
line source
package de.intevation.flys.artifacts.model; import java.util.ArrayList; import java.util.List; import java.math.BigDecimal; import java.io.Serializable; 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.HYK; 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 hykid, double km) { logger.debug("HYKFactory.getHYKs"); Cache cache = CacheFactory.getCache(HYK_CACHE_NAME); String cacheKey; if (cache != null) { cacheKey = "" + hykid + "_" + 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(hykid, km); if (zones != null && cacheKey != null) { logger.debug("Store hykzones in cache."); Element element = new Element(cacheKey, zones); cache.put(element); } return zones; } /** Return name for hyk with given id. */ public static String getHykName(int hykid) { logger.debug("HYKFactory.getHykName " + hykid); Session session = SessionHolder.HOLDER.get(); Query query = session.createQuery( "select description from HYK where id = :hykid "); query.setParameter("hykid", hykid); return (String) query.uniqueResult(); } /** * Ask DB for hyk zones. * @param hykid ID of the 'main' HYK to query. * @param km for which to get the hyk-zones. * @return according zones. */ public static List<Zone> getZonesUncached(int hykid, double km) { if (logger.isDebugEnabled()) { logger.debug("HYKFactory.getZoneUncached " + hykid + " km " + km); } Session session = SessionHolder.HOLDER.get(); // Find out flow-direction of river. // OPTIMIZE: 1) query kmUp directly 2) merge queries. Query rQuery = session.createQuery("from HYK where id = :hykid"); rQuery.setParameter("hykid", hykid); rQuery.setMaxResults(1); HYK hyk = (HYK) rQuery.uniqueResult(); BigDecimal flowDir = (hyk.getRiver().getKmUp()) ? new BigDecimal(1) : new BigDecimal(-1); // Query HYKFormations in range. Query query = session.createQuery( "from HYKFormation where entry.HYK.id = :hykid " + " and :km between entry.km and " + " entry.km + :flowDir * "+ " cast(distance_vl/1000.0 + 0.001 as big_decimal)" + " order by entry.km asc"); query.setParameter("hykid", hykid); query.setParameter("km", new BigDecimal(km)); query.setParameter("flowDir", flowDir); query.setMaxResults(1); List<HYKFormation> forms = query.list(); List<Zone> zones = new ArrayList<Zone>(); // Take the first one. if (forms.size() >= 1) { HYKFormation form = forms.get(0); // 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 implements Serializable { /** 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; } /** Get upper value. */ public double getTo() { return to; } /** Get lower value. */ public double getFrom() { return from; } /** Get name (type). */ public String getName() { return name; } } // public static class Zone } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :