changeset 8251:c7ca9c228314

Added context injectors for discharge curves.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 10 Sep 2014 10:01:11 +0200 (2014-09-10)
parents 61fd22f65bd4
children 1e30489d0958
files artifacts/src/main/java/org/dive4elements/river/exports/injector/ComputedDischargeInjector.java artifacts/src/main/java/org/dive4elements/river/exports/injector/DischargeInjector.java artifacts/src/main/java/org/dive4elements/river/exports/injector/FixWQInjector.java artifacts/src/main/java/org/dive4elements/river/exports/injector/HistoricalDischargeInjector.java artifacts/src/main/java/org/dive4elements/river/exports/injector/InjectorConstants.java
diffstat 5 files changed, 192 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/ComputedDischargeInjector.java	Wed Sep 10 10:01:11 2014 +0200
@@ -0,0 +1,34 @@
+package org.dive4elements.river.exports.injector;
+
+import org.apache.log4j.Logger;
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.artifacts.ContextInjector;
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.access.RangeAccess;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import static org.dive4elements.river.exports.injector.InjectorConstants.CURRENT_KM;
+
+public class ComputedDischargeInjector
+implements ContextInjector
+{
+
+    private static Logger log = Logger.getLogger(ComputedDischargeInjector.class);
+
+    @Override
+    public void setup(Element cfg) {
+    }
+
+    @Override
+    public void injectContext(
+        CallContext ctx,
+        Artifact artifact,
+        Document doc
+    ) {
+        RangeAccess access = new RangeAccess((D4EArtifact)artifact);
+        double km = access.getFrom();
+        ctx.putContextValue(CURRENT_KM, km);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/DischargeInjector.java	Wed Sep 10 10:01:11 2014 +0200
@@ -0,0 +1,40 @@
+package org.dive4elements.river.exports.injector;
+
+import org.apache.log4j.Logger;
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.artifacts.ContextInjector;
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.access.RangeAccess;
+import org.dive4elements.river.model.Gauge;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import static org.dive4elements.river.exports.injector.InjectorConstants.CURRENT_KM;
+import static org.dive4elements.river.exports.injector.InjectorConstants.GAUGE_EPSILON;;
+
+
+public class DischargeInjector
+implements ContextInjector
+{
+
+    private static Logger log = Logger.getLogger(DischargeInjector.class);
+
+    @Override
+    public void setup(Element cfg) {
+    }
+
+    @Override
+    public void injectContext(CallContext ctx, Artifact artifact, Document doc) {
+        RangeAccess access = new RangeAccess((D4EArtifact)artifact);
+        double km = access.getFrom();
+        Gauge gauge = access.getRiver().determineGaugeByStation(
+            km - GAUGE_EPSILON, km + GAUGE_EPSILON);
+        if (gauge == null) {
+            log.error("No Gauge could be found at station " + km + "!");
+            return;
+        }
+        ctx.putContextValue(CURRENT_KM, km);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/FixWQInjector.java	Wed Sep 10 10:01:11 2014 +0200
@@ -0,0 +1,63 @@
+package org.dive4elements.river.exports.injector;
+
+import org.apache.log4j.Logger;
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.ArtifactNamespaceContext;
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.artifacts.ContextInjector;
+import org.dive4elements.artifacts.common.utils.XMLUtils;
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.access.RangeAccess;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import static org.dive4elements.river.exports.injector.InjectorConstants.CURRENT_KM;
+
+
+public class FixWQInjector
+implements ContextInjector
+{
+    private static final Logger log = Logger.getLogger(FixWQInjector.class);
+
+    public static final Double INVALID_KM = Double.valueOf(-1d);
+    public static final String XPATH_CHART_CURRENTKM =
+        "/art:action/art:attributes/art:currentKm/@art:km";
+    @Override
+    public void setup(Element cfg) {
+
+    }
+
+    @Override
+    public void injectContext(
+        CallContext ctx,
+        Artifact artifact,
+        Document request) {
+        Double km = getCurrentKmFromRequest(request);
+        if (km == Double.NaN) {
+            RangeAccess access = new RangeAccess((D4EArtifact)artifact);
+            km = access.getFrom();
+            log.debug("from access");
+        }
+        log.debug("current km: " + km);
+        ctx.putContextValue(CURRENT_KM, km);
+    }
+
+    private Double getCurrentKmFromRequest(Document request) {
+
+        String km = XMLUtils.xpathString(
+            request,
+            XPATH_CHART_CURRENTKM,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (km == null) {
+            return Double.NaN;
+        }
+
+        try {
+            return Double.valueOf(km);
+        }
+        catch (NumberFormatException nfe) {
+            return Double.NaN;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/HistoricalDischargeInjector.java	Wed Sep 10 10:01:11 2014 +0200
@@ -0,0 +1,47 @@
+package org.dive4elements.river.exports.injector;
+
+import org.apache.log4j.Logger;
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.artifacts.ContextInjector;
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess;
+import org.dive4elements.river.artifacts.access.RangeAccess;
+import org.dive4elements.river.model.Gauge;
+import org.dive4elements.river.model.River;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import static org.dive4elements.river.exports.injector.InjectorConstants.CURRENT_KM;
+
+public class HistoricalDischargeInjector
+implements ContextInjector
+{
+    private static Logger log =
+        Logger.getLogger(HistoricalDischargeInjector.class);
+
+    @Override
+    public void setup(Element cfg) {
+
+    }
+
+    @Override
+    public void injectContext(
+        CallContext ctx,
+        Artifact artifact,
+        Document request
+    ) {
+        HistoricalDischargeAccess access =
+            new HistoricalDischargeAccess((D4EArtifact)artifact);
+        Long gaugeNumber = access.getOfficialGaugeNumber();
+        if (gaugeNumber == null) {
+            return;
+        }
+        Gauge gauge = Gauge.getGaugeByOfficialNumber(gaugeNumber);
+        if (gauge == null) {
+            return;
+        }
+        double km = gauge.getStation().doubleValue();
+        ctx.putContextValue(CURRENT_KM, km);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/InjectorConstants.java	Wed Sep 10 10:01:11 2014 +0200
@@ -0,0 +1,8 @@
+package org.dive4elements.river.exports.injector;
+
+
+public interface InjectorConstants
+{
+    public static final String CURRENT_KM = "currentkm";
+    public static final double GAUGE_EPSILON = 0.1;
+}

http://dive4elements.wald.intevation.org