changeset 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 3cbdf1b77ea5
children e8fc770d2f8c
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/hyk.xml flys-artifacts/doc/conf/conf.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/HYKArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StaticHYKState.java
diffstat 6 files changed, 418 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Jan 18 13:33:08 2012 +0000
+++ b/flys-artifacts/ChangeLog	Wed Jan 18 13:39:16 2012 +0000
@@ -1,3 +1,17 @@
+2012-01-18	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
+
+	Added stub hyk infrastructure.
+
+	* doc/conf/artifacts/hyk.xml: Added trivial transition configuration
+	  for hyk artifacts.
+
+	* doc/conf/conf.xml: Register HYK artifact and its transition conf.
+
+	* src/main/java/de/intevation/flys/artifacts/HYKArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java,
+	  src/main/java/de/intevation/flys/artifacts/states/StaticHYKState.java:
+	  Added static 'hyk' artifact, facet and state.
+
 2012-01-18	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/doc/conf/artifacts/hyk.xml	Wed Jan 18 13:39:16 2012 +0000
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<artifact name="hyk">
+  <states>
+    <state id="state.hyk.static"
+               description="state.hyk.static"
+               state="de.intevation.flys.artifacts.states.StaticHYKState">
+      <outputmodes>
+        <outputmode name="cross_section" description="output.cross_section" mime-type="image/png" type="chart">
+          <facets>
+            <facet name="hyk" description="hyk data" />
+          </facets>
+        </outputmode>
+      </outputmodes>
+    </state>
+  </states>
+</artifact>
--- a/flys-artifacts/doc/conf/conf.xml	Wed Jan 18 13:33:08 2012 +0000
+++ b/flys-artifacts/doc/conf/conf.xml	Wed Jan 18 13:39:16 2012 +0000
@@ -17,6 +17,9 @@
             <artifact-factory name="riveraxis" description="Factory to create an artifact to be used in WINFO"
                 ttl="3600000"
                 artifact="de.intevation.flys.artifacts.RiverAxisArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
+            <artifact-factory name="hyk" description="Factory to create an artifact to be used for hyks"
+                ttl="3600000"
+                artifact="de.intevation.flys.artifacts.HYKArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
             <artifact-factory name="new_map" description="Factory to create an artifact to be used for new map"
                 ttl="3600000"
                 artifact="de.intevation.flys.artifacts.MapArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
@@ -131,6 +134,7 @@
         <artifact name="winfo" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/winfo.xml" />
         <artifact name="waterlevel" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/waterlevel.xml" />
         <artifact name="annotation" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/annotation.xml" />
+        <artifact name="hyk" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/hyk.xml" />
         <artifact name="wmsbackground" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/wmsbackground.xml" />
         <artifact name="new_map" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/map.xml" />
         <artifact name="new_chart" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/chart.xml" />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/HYKArtifact.java	Wed Jan 18 13:39:16 2012 +0000
@@ -0,0 +1,124 @@
+package de.intevation.flys.artifacts;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactFactory;
+import de.intevation.artifacts.CallMeta;
+
+import de.intevation.artifacts.common.ArtifactNamespaceContext;
+import de.intevation.artifacts.common.utils.XMLUtils;
+
+import de.intevation.flys.artifacts.states.DefaultState;
+
+import de.intevation.artifactdatabase.state.Facet;
+
+
+/**
+ * Artifact describing the area between two WKms.
+ */
+public class HYKArtifact extends StaticFLYSArtifact {
+
+    /** Access ids of doc. */
+    public static final String XPATH_IDS = "/art:action/art:ids/@value";
+
+    /** Name of Artifact. */
+    public static final String HYK_ARTIFACT_NAME = "hyk";
+
+    /** Name of data item keeping the km of cs master. */
+    public static final String HYK_KM = "hyk_artifact.data.km";
+
+    /** Own logger. */
+    private static final Logger logger =
+        Logger.getLogger(HYKArtifact.class);
+
+
+    /** Return given name. */
+    @Override
+    public String getName() {
+        return HYK_ARTIFACT_NAME;
+    }
+
+
+    /** Store ids, do super.setup. */
+    @Override
+    public void setup(
+        String          identifier,
+        ArtifactFactory factory,
+        Object          context,
+        CallMeta        callMeta,
+        Document        data)
+    {
+        logger.info("HYKArtifact.setup");
+
+        super.setup(identifier, factory, context, callMeta, data);
+
+        String ids = XMLUtils.xpathString(
+            data, XPATH_IDS, ArtifactNamespaceContext.INSTANCE);
+
+        addStringData("ids", ids);
+    }
+
+
+    /** Set km as Data. */
+    public void setKm(double km) {
+        addStringData(HYK_KM, Double.toString(km));
+    }
+
+
+    /**
+     * Get a DataItem casted to double (0 if fails).
+     * TODO move to FlysArtifact.
+     */
+    public double getDataAsDouble(String dataName) {
+        String val = getDataAsString(dataName);
+        if (val == null) {
+            logger.warn("Data not available.");
+            return 0d;
+        }
+        try {
+            return Double.valueOf(val);
+        }
+        catch (NumberFormatException e) {
+            logger.warn("Could not get data " + dataName + " as double", e);
+            return 0;
+        }
+    }
+
+
+    /** Get km from state data. */
+    public double getKm() {
+        return getDataAsDouble(HYK_KM);
+    }
+
+    /** Do not copy data from daddyfact. */
+    @Override
+    protected void initialize(
+        Artifact artifact,
+        Object   context,
+        CallMeta callMeta)
+    {
+        logger.debug("HYKArtifact.initialize");
+        WINFOArtifact winfo = (WINFOArtifact) artifact;
+        importData(winfo, "river");
+
+        List<Facet> fs = new ArrayList<Facet>();
+
+        DefaultState state = (DefaultState) getCurrentState(context);
+        state.computeInit(this, hash(), context, callMeta, fs);
+        if (!fs.isEmpty()) { 
+            logger.debug("Facets to add in HYKArtifact.initialize ."); 
+            facets.put(getCurrentStateId(), fs); 
+        } 
+        else { 
+            logger.debug("No facets to add in HYKArtifact.initialize ("
+                + state.getID() + ").");
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /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 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StaticHYKState.java	Wed Jan 18 13:39:16 2012 +0000
@@ -0,0 +1,121 @@
+package de.intevation.flys.artifacts.states;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
+
+import de.intevation.artifactdatabase.state.Facet;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.HYKArtifact;
+
+import de.intevation.flys.artifacts.model.FacetTypes;
+import de.intevation.flys.artifacts.model.HYKFacet;
+import de.intevation.flys.artifacts.model.HYKFactory;
+
+import de.intevation.flys.utils.FLYSUtils;
+
+
+/**
+ * Only state of a HYKArtifact.
+ */
+public class StaticHYKState
+extends      DefaultState
+implements   FacetTypes
+{
+    /** The logger that is used in this state. */
+    private static Logger logger = Logger.getLogger(StaticHYKState.class);
+
+
+    /**
+     * From this state can not be continued.
+     */
+    @Override
+    protected String getUIProvider() {
+        return "noinput";
+    }
+
+
+    /**
+     * Compute, create Facets, do the same stuff as all the other states do.
+     */
+    protected Object compute(
+        HYKArtifact   hyk,
+        CallMeta      metaLocale,
+        String        hash,
+        List<Facet>   facets,
+        Object        old
+    ) {
+        logger.debug("StaticHYKState.compute");
+        String id = getID();
+
+        // TODO caching!
+
+        /*
+        WQKms res = old instanceof WQKms
+            ? (WQKms)old
+            : winfo.getWQKms();
+
+        WQKms wqkms = res;
+        */
+
+        // List<Zone> res;
+        Object res = null;
+
+        // Compare against cached objects.
+
+        // Need river id and km.
+        List<HYKFactory.Zone> zones = (List<HYKFactory.Zone>)
+            HYKFactory.getHYKs(FLYSUtils.getRiver(hyk).getId(), hyk.getKm());
+
+        if (facets == null) {
+            logger.debug("StaticHYKState.compute no facets");
+            return zones;
+        }
+
+        Facet facet = new HYKFacet(0, "I am hyked.");
+
+        facets.add(facet);
+
+        return zones;
+    }
+
+
+    /**
+     * Get data, create the facets.
+     *
+     * @param context Ignored.
+     */
+    @Override
+    public Object computeFeed(
+        FLYSArtifact artifact,
+        String       hash,
+        CallContext  context,
+        List<Facet>  facets,
+        Object       old
+    ) {
+        return compute((HYKArtifact) artifact, context.getMeta(),
+            hash, facets, old);
+    }
+
+
+    /**
+     * Create the facets.
+     * @param context Ignored.
+     */
+    @Override
+    public Object computeInit(
+        FLYSArtifact artifact,
+        String       hash,
+        Object       context,
+        CallMeta     meta,
+        List<Facet>  facets
+    ) {
+        return compute((HYKArtifact) artifact, meta, hash, facets,
+            null);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org