changeset 8349:7341bebc79d0

Let injectors be strictly distinct from each other and give more meaningful names (they know what they do, not where they will be used. The latter is configuration).
author Tom Gottfried <tom@intevation.de>
date Wed, 01 Oct 2014 12:23:39 +0200
parents dc3f55141299
children 427a813b1791 418b9d9a3564
files artifacts/doc/conf/generators/discharge-diagrams.xml 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/KmFromGaugeNameInjector.java artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromGaugeNumberInjector.java artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromLocationInjector.java artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromRequestInjector.java
diffstat 9 files changed, 191 insertions(+), 198 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/doc/conf/generators/discharge-diagrams.xml	Wed Oct 01 12:13:28 2014 +0200
+++ b/artifacts/doc/conf/generators/discharge-diagrams.xml	Wed Oct 01 12:23:39 2014 +0200
@@ -4,7 +4,7 @@
   <output-generator names="historical_discharge_wq,historical_discharge_wq_chartinfo"
       class="org.dive4elements.river.exports.DischargeGenerator"
       converter="org.dive4elements.river.exports.DiagramAttributes"
-      injectors="org.dive4elements.river.exports.injector.HistoricalDischargeInjector,org.dive4elements.river.exports.injector.PNPInjector">
+      injectors="org.dive4elements.river.exports.injector.KmFromGaugeNumberInjector.java,org.dive4elements.river.exports.injector.PNPInjector">
     <title key="chart.historical.discharge.title" default="chart.historical.discharge.title"/>
     &discharge-defaults;
   </output-generator>
@@ -12,7 +12,7 @@
   <output-generator names="computed_discharge_curve_chartinfo,computed_discharge_curve"
       class="org.dive4elements.river.exports.DischargeGenerator"
       converter="org.dive4elements.river.exports.DiagramAttributes"
-      injectors="org.dive4elements.river.exports.injector.ComputedDischargeInjector,org.dive4elements.river.exports.injector.PNPInjector">
+      injectors="org.dive4elements.river.exports.injector.KmFromLocationInjector,org.dive4elements.river.exports.injector.PNPInjector">
     <title key="chart.computed.discharge.curve.title" default="chart.computed.discharge.curve.title"/>
     <subtitle key="chart.computed.discharge.curve.subtitle" default="-">
         <arg expr="artifact.river"/>
@@ -24,7 +24,7 @@
   <output-generator names="discharge_curve_chartinfo,discharge_curve"
       class="org.dive4elements.river.exports.DischargeGenerator"
       converter="org.dive4elements.river.exports.DiagramAttributes"
-      injectors="org.dive4elements.river.exports.injector.DischargeInjector,org.dive4elements.river.exports.injector.PNPInjector">
+      injectors="org.dive4elements.river.exports.injector.KmFromGaugeNameInjector,org.dive4elements.river.exports.injector.PNPInjector">
     <title key="chart.discharge.curve.title" default="chart.discharge.curve.title"/>
     <subtitle key="chart.computed.discharge.curve.subtitle" default="-">
         <arg expr="artifact.river"/>
@@ -36,7 +36,7 @@
   <output-generator names="fix_wq_curve,fix_wq_curve_chartinfo"
       class="org.dive4elements.river.exports.DischargeGenerator"
       converter="org.dive4elements.river.exports.DiagramAttributes"
-      injectors="org.dive4elements.river.exports.injector.FixWQInjector,org.dive4elements.river.exports.injector.PNPInjector">
+      injectors="org.dive4elements.river.exports.injector.KmFromRequestInjector,org.dive4elements.river.exports.injector.PNPInjector">
       <title key="chart.fixings.discharge.curve.title" default="Fixierungsanalyse W/Q"/>
     <subtitle key="chart.computed.discharge.curve.subtitle" default="-">
         <arg expr="artifact.river"/>
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/ComputedDischargeInjector.java	Wed Oct 01 12:13:28 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-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);
-        if (access.hasFrom()) {
-            ctx.putContextValue(CURRENT_KM, access.getFrom());
-        }
-        else if (access.getLocations() != null &&
-            access.getLocations().length > 0) {
-            ctx.putContextValue(CURRENT_KM, access.getLocations()[0]);
-        }
-    }
-}
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/DischargeInjector.java	Wed Oct 01 12:13:28 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,47 +0,0 @@
-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;
-
-
-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);
-        if (access.hasFrom()) {
-            ctx.putContextValue(CURRENT_KM, access.getFrom());
-            return;
-        }
-        D4EArtifact d4e = (D4EArtifact)artifact;
-        String name = d4e.getDataAsString("gauge_name");
-        if (name == null || name.equals("")) {
-            return;
-        }
-        Gauge gauge = access.getRiver().determineGaugeByName(name);
-        if (gauge == null) {
-            log.error("No Gauge could be found for name " + name + "!");
-            return;
-        }
-        ctx.putContextValue(CURRENT_KM, gauge.getStation());
-        return;
-    }
-
-}
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/FixWQInjector.java	Wed Oct 01 12:13:28 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,63 +0,0 @@
-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 (Double.isNaN(km)) {
-            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;
-        }
-    }
-}
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/HistoricalDischargeInjector.java	Wed Oct 01 12:13:28 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,45 +0,0 @@
-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.model.Gauge;
-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/KmFromGaugeNameInjector.java	Wed Oct 01 12:23:39 2014 +0200
@@ -0,0 +1,43 @@
+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;
+
+
+public class KmFromGaugeNameInjector
+implements ContextInjector
+{
+
+    private static Logger log = Logger.getLogger(KmFromGaugeNameInjector.class);
+
+    @Override
+    public void setup(Element cfg) {
+    }
+
+    @Override
+    public void injectContext(CallContext ctx, Artifact artifact, Document doc) {
+        D4EArtifact d4e = (D4EArtifact)artifact;
+        RangeAccess access = new RangeAccess(d4e);
+        String name = d4e.getDataAsString("gauge_name");
+        if (name == null || name.equals("")) {
+            return;
+        }
+        Gauge gauge = access.getRiver().determineGaugeByName(name);
+        if (gauge == null) {
+            log.error("No Gauge could be found for name " + name + "!");
+            return;
+        }
+        ctx.putContextValue(CURRENT_KM, gauge.getStation());
+        return;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromGaugeNumberInjector.java	Wed Oct 01 12:23:39 2014 +0200
@@ -0,0 +1,45 @@
+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.model.Gauge;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import static org.dive4elements.river.exports.injector.InjectorConstants.CURRENT_KM;
+
+public class KmFromGaugeNumberInjector
+implements ContextInjector
+{
+    private static Logger log =
+        Logger.getLogger(KmFromGaugeNumberInjector.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/KmFromLocationInjector.java	Wed Oct 01 12:23:39 2014 +0200
@@ -0,0 +1,38 @@
+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 KmFromLocationInjector
+implements ContextInjector
+{
+
+    private static Logger log = Logger.getLogger(KmFromLocationInjector.class);
+
+    @Override
+    public void setup(Element cfg) {
+    }
+
+    @Override
+    public void injectContext(
+        CallContext ctx,
+        Artifact artifact,
+        Document doc
+    ) {
+        RangeAccess access = new RangeAccess((D4EArtifact)artifact);
+        if (access.getLocations() != null &&
+            access.getLocations().length > 0) {
+            ctx.putContextValue(CURRENT_KM, access.getLocations()[0]);
+            return;
+        }
+        log.warn("No locations accessible.");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromRequestInjector.java	Wed Oct 01 12:23:39 2014 +0200
@@ -0,0 +1,61 @@
+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 KmFromRequestInjector
+implements ContextInjector
+{
+    private static final Logger log = Logger.getLogger(KmFromRequestInjector.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 (Double.isNaN(km)) {
+            RangeAccess access = new RangeAccess((D4EArtifact)artifact);
+            km = access.getFrom();
+        }
+        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;
+        }
+    }
+}

http://dive4elements.wald.intevation.org