changeset 3936:4e1b3b4ef132

Add new Artifacts and State for displaying discharge curves at a gauge flys-artifacts/trunk@5636 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 28 Sep 2012 08:44:49 +0000
parents 6cdd994ee8a4
children c254939fae52
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/conf.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/AbstractStaticStateArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/GaugeDischargeCurveArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeDischargeCurveFacet.java
diffstat 5 files changed, 238 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Sep 28 08:41:13 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Sep 28 08:44:49 2012 +0000
@@ -1,3 +1,12 @@
+2012-09-29	Björn Ricks	<bjoern.ricks@intevation.de>
+
+	* doc/conf/conf.xml,
+	  src/main/java/de/intevation/flys/artifacts/GaugeDischargeCurveArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/AbstractStaticStateArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/model/GaugeDischargeCurveFacet.java:
+	  Implement new Artifacts and State for displaying discharge curves at a
+	  gauge.
+
 2012-09-29	Björn Ricks	<bjoern.ricks@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/StaticState.java:
--- a/flys-artifacts/doc/conf/conf.xml	Fri Sep 28 08:41:13 2012 +0000
+++ b/flys-artifacts/doc/conf/conf.xml	Fri Sep 28 08:44:49 2012 +0000
@@ -121,6 +121,10 @@
             <artifact-factory name="bedheight" description="Factory to create an artifact used in minfo datacage."
                 ttl="3600000"
                 artifact="de.intevation.flys.artifacts.BedHeightsArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
+
+            <artifact-factory name="gaugedischargecurve" description="Factory to create an artifact to show a discharge curve for a gauge."
+                ttl="3600000"
+                artifact="de.intevation.flys.artifacts.GaugeDischargeCurveArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
         </artifact-factories>
 
         <user-factory name="default" description="Factory to create new users">de.intevation.artifactdatabase.DefaultUserFactory</user-factory>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AbstractStaticStateArtifact.java	Fri Sep 28 08:44:49 2012 +0000
@@ -0,0 +1,69 @@
+package de.intevation.flys.artifacts;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import de.intevation.artifactdatabase.state.State;
+
+/**
+ * A abstract baseclass for Artifacts which are using only one static state.
+ *
+ * This class is intended to be used without the config/stateengine to generate
+ * the static state.
+ *
+ * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
+ */
+public abstract class AbstractStaticStateArtifact extends StaticFLYSArtifact {
+
+    private transient State staticstate;
+
+    /**
+     * Get a list containing the one and only State.
+     * @param  context ignored.
+     * @return list with one and only state.
+     */
+    @Override
+    protected List<State> getStates(Object context) {
+        ArrayList<State> states = new ArrayList<State>();
+        states.add(getStaticState());
+        return states;
+    }
+
+
+    /**
+     * Get the "current" state.
+     * @param cc ignored.
+     * @return always the set static state.
+     */
+    @Override
+    public State getCurrentState(Object cc) {
+        return getStaticState();
+    }
+
+    /**
+     * A child class must override this method to set its static state
+     */
+    protected abstract void initStaticState();
+
+    protected void setStaticState(State state) {
+        this.staticstate = state;
+    }
+
+    protected State getStaticState() {
+        if (staticstate == null) {
+            initStaticState();
+        }
+        return staticstate;
+    }
+
+    /**
+     * Get the state.
+     * @param context ignored.
+     * @param stateID ignored.
+     * @return the state.
+     */
+    @Override
+    protected State getState(Object context, String stateID) {
+        return getStaticState();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/GaugeDischargeCurveArtifact.java	Fri Sep 28 08:44:49 2012 +0000
@@ -0,0 +1,99 @@
+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.artifactdatabase.state.DefaultOutput;
+import de.intevation.artifactdatabase.state.Facet;
+
+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.StaticState;
+import de.intevation.flys.artifacts.StaticFLYSArtifact;
+import de.intevation.flys.artifacts.model.GaugeDischargeCurveFacet;
+
+import de.intevation.flys.model.Gauge;
+
+import de.intevation.flys.utils.FLYSUtils;
+
+/**
+ * Artifact to calculate a discharge curve from a gauge overview info
+ *
+ * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
+ */
+public class GaugeDischargeCurveArtifact
+extends      AbstractStaticStateArtifact
+{
+
+    private static final Logger logger =
+        Logger.getLogger(GaugeDischargeCurveArtifact.class);
+
+    public static final String XPATH_RIVER = "/art:action/art:river/@name";
+    public static final String XPATH_GAUGE = "/art:action/art:gauge/@reference";
+    public static final String NAME = "gaugedischargecurve";
+    public static final String STATIC_STATE_NAME = "state.gaugedischargecurve.static";
+
+    /**
+     * Setup initializes the data by extracting the river and gauge from
+     * the XML Document.
+     */
+    @Override
+    public void setup(
+        String          identifier,
+        ArtifactFactory factory,
+        Object          context,
+        CallMeta        callmeta,
+        Document        data)
+    {
+        logger.debug("GaugeDischargeCurveArtifact.setup");
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("GaugeDischargeCurveState.setup" + XMLUtils.toString(data));
+        }
+        String gaugeref = XMLUtils.xpathString(data, XPATH_GAUGE,
+                ArtifactNamespaceContext.INSTANCE);
+        String rivername = XMLUtils.xpathString(data, XPATH_RIVER,
+                ArtifactNamespaceContext.INSTANCE);
+
+        addStringData("river", rivername);
+        addStringData("reference_gauge", gaugeref);
+
+        Gauge gauge = FLYSUtils.getReferenceGauge(this);
+
+        Facet gfacet = new GaugeDischargeCurveFacet(rivername, gauge);
+
+        List<Facet> fs = new ArrayList<Facet>(1);
+        fs.add(gfacet);
+
+        addFacets(STATIC_STATE_NAME, fs);
+
+        super.setup(identifier, factory, context, callmeta, data);
+    }
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    @Override
+    protected void initStaticState() {
+        StaticState state = new StaticState(STATIC_STATE_NAME);
+        List<Facet> fs = facets.get(STATIC_STATE_NAME);
+        DefaultOutput output = new DefaultOutput(
+            "discharge_curve",
+            "output.discharge_curve", "image/png",
+            fs,
+            "chart");
+
+        state.addOutput(output);
+        setStaticState(state);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeDischargeCurveFacet.java	Fri Sep 28 08:44:49 2012 +0000
@@ -0,0 +1,57 @@
+package de.intevation.flys.artifacts.model;
+
+import java.util.Arrays;
+import java.util.Map;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
+
+import de.intevation.artifactdatabase.state.DefaultFacet;
+import de.intevation.artifactdatabase.state.Facet;
+
+import de.intevation.flys.artifacts.model.WQKms;
+
+import de.intevation.flys.model.Gauge;
+
+/**
+ * A Facet that returns discharge curve data at a gauge
+ *
+ * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
+ */
+public class GaugeDischargeCurveFacet
+extends      DefaultFacet
+{
+    private Gauge gauge;
+    private String river;
+
+    public GaugeDischargeCurveFacet(String river, Gauge gauge) {
+        super(0, "facet.gauge.discharge_curve", "facet.gauge.discharge_curve");
+        this.river = river;
+        this.gauge = gauge;
+    }
+
+    @Override
+    public Object getData(Artifact art, CallContext context) {
+
+        DischargeTables dt = new DischargeTables(river, gauge.getName());
+
+        Map<String, double [][]> map = dt.getValues(100d);
+
+        double [][] values = map.get(name);
+        if (values == null) {
+            return null;
+        }
+        double [] kms = new double[values[0].length];
+        Arrays.fill(kms, gauge.getStation().doubleValue());
+        return new WQKms(kms, values[0], values[1], gauge.getName());
+    }
+
+    @Override
+    public Facet deepCopy() {
+        GaugeDischargeCurveFacet copy =
+            new GaugeDischargeCurveFacet(this.river, this.gauge);
+        copy.set(this);
+        return copy;
+    }
+}

http://dive4elements.wald.intevation.org