felix@2131: package de.intevation.flys.artifacts.model; felix@2131: felix@2131: import java.util.ArrayList; felix@2131: import java.util.List; felix@2131: felix@2131: import java.math.BigDecimal; felix@2131: felix@2131: import net.sf.ehcache.Cache; felix@2131: import net.sf.ehcache.Element; felix@2131: felix@2131: import org.apache.log4j.Logger; felix@2131: felix@2131: import org.hibernate.Query; felix@2131: import org.hibernate.Session; felix@2131: felix@2131: import de.intevation.flys.model.HYKFormation; felix@2131: import de.intevation.flys.model.HYKFlowZone; felix@2131: felix@2131: import de.intevation.flys.artifacts.cache.CacheFactory; felix@2131: felix@2131: import de.intevation.flys.backend.SessionHolder; felix@2131: felix@2131: felix@2131: /** felix@2131: * Factory to access HYKs (hydrographic values). felix@2131: */ felix@2131: public class HYKFactory felix@2131: { felix@2131: private static Logger logger = Logger.getLogger(HYKFactory.class); felix@2131: felix@2131: public static String HYK_CACHE_NAME = "hykache"; felix@2131: felix@2131: felix@2131: /** Do not instantiate a HYKFactory. */ felix@2131: private HYKFactory() { felix@2131: } felix@2131: felix@2131: felix@2131: /** felix@2131: * Get List of Zones for given river and km. felix@2131: */ felix@2131: public static Object getHYKs(int riverId, double km) { felix@2131: logger.debug("HYKFactory.getHYKs"); felix@2131: felix@2131: Cache cache = CacheFactory.getCache(HYK_CACHE_NAME); felix@2131: felix@2131: String cacheKey; felix@2131: felix@2131: if (cache != null) { felix@2131: cacheKey = "" + riverId + "_" + km; felix@2131: Element element = cache.get(cacheKey); felix@2131: if (element != null) { felix@2131: logger.debug("Got hyk from cache"); felix@2131: return (List)element.getValue(); felix@2131: } felix@2131: } felix@2131: else { felix@2131: cacheKey = null; felix@2131: } felix@2131: felix@2131: List zones = getZonesUncached(riverId, km); felix@2131: felix@2131: if (zones != null && cacheKey != null) { felix@2131: logger.debug("Store hykzones in cache."); felix@2131: Element element = new Element(cacheKey, zones); felix@2131: cache.put(element); felix@2131: } felix@2131: felix@2131: return zones; felix@2131: } felix@2131: felix@2131: felix@2131: /** felix@2131: * felix@2131: * @param column the position columns value felix@2131: * @param wst_id database id of the wst felix@2131: * @return according WKms. felix@2131: */ felix@2137: // TODO we also need to know the HYK-id to specify which set we are felix@2137: // inspecting. felix@2131: public static List getZonesUncached(int riverId, double km) { felix@2131: felix@2131: if (logger.isDebugEnabled()) { felix@2131: logger.debug("HYKFactory.getZoneUncached " + riverId + " km " + km); felix@2131: } felix@2131: felix@2131: Session session = SessionHolder.HOLDER.get(); felix@2137: felix@2137: // TODO respect interval better. respect multiples (sort, limit), felix@2137: // TODO respect flow direction of river. felix@2131: Query query = session.createQuery( felix@2131: "from HYKFormation where entry.HYK.river.id = :riverid " + felix@2131: " and entry.km between " + felix@2131: ":km - cast(distance_vl/1000.0 - 0.001 as big_decimal) and " + felix@2131: ":km + cast(distance_vl/1000.0 + 0.001 as big_decimal)"); felix@2131: query.setParameter("riverid", riverId); felix@2131: query.setParameter("km", new BigDecimal(km)); felix@2131: logger.debug("Big km " + new BigDecimal(km)); felix@2131: List forms = query.list(); felix@2131: felix@2131: List zones = new ArrayList(); felix@2131: for (HYKFormation form : forms) { felix@2131: logger.debug("One HYKFormation found (entry: " felix@2131: + form.getEntry().getId() felix@2131: + ") /km " + form.getEntry().getKm() + "."); felix@2131: felix@2131: // Create respective zones. felix@2131: for (HYKFlowZone flowZone: form.getZones()) { felix@2131: Zone z = new Zone(flowZone.getA().doubleValue(), felix@2131: flowZone.getB().doubleValue(), felix@2131: flowZone.getType().getName()); felix@2131: zones.add(z); felix@2131: } felix@2131: } felix@2131: felix@2131: return zones; felix@2131: } felix@2131: felix@2131: /** Labelled section. */ felix@2131: public static class Zone { felix@2131: /** Lower end of segment. */ felix@2131: protected double from; felix@2131: /** Upper end of segment. */ felix@2131: protected double to; felix@2131: /** The label. */ felix@2131: protected String name; felix@2131: felix@2131: /** Constructor for labelled section. */ felix@2131: public Zone (double from, double to, String name) { felix@2131: this.from = from; felix@2131: this.to = to; felix@2131: this.name = name; felix@2131: } felix@2131: felix@2137: /** Get upper value. */ felix@2137: public double getTo() { felix@2137: return to; felix@2137: } felix@2137: felix@2137: /** Get lower value. */ felix@2131: public double getFrom() { felix@2131: return from; felix@2131: } felix@2131: felix@2137: /** Get name (type). */ felix@2131: public String getName() { felix@2131: return name; felix@2131: } felix@2131: } // public static class Zone felix@2131: } felix@2131: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :