[PATCH 1 of 3] Added context injectors for discharge curves

Wald Commits scm-commit at wald.intevation.org
Wed Sep 10 10:02:50 CEST 2014


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1410336071 -7200
# Node ID c7ca9c228314cffedb7eb33220e89ed338105d8b
# Parent  61fd22f65bd459b99ea2e142e31cf55db85d8dfc
Added context injectors for discharge curves.

diff -r 61fd22f65bd4 -r c7ca9c228314 artifacts/src/main/java/org/dive4elements/river/exports/injector/ComputedDischargeInjector.java
--- /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);
+    }
+}
diff -r 61fd22f65bd4 -r c7ca9c228314 artifacts/src/main/java/org/dive4elements/river/exports/injector/DischargeInjector.java
--- /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);
+    }
+
+}
diff -r 61fd22f65bd4 -r c7ca9c228314 artifacts/src/main/java/org/dive4elements/river/exports/injector/FixWQInjector.java
--- /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;
+        }
+    }
+}
diff -r 61fd22f65bd4 -r c7ca9c228314 artifacts/src/main/java/org/dive4elements/river/exports/injector/HistoricalDischargeInjector.java
--- /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);
+    }
+}
diff -r 61fd22f65bd4 -r c7ca9c228314 artifacts/src/main/java/org/dive4elements/river/exports/injector/InjectorConstants.java
--- /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;
+}


More information about the Dive4Elements-commits mailing list