[PATCH] 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)

Wald Commits scm-commit at wald.intevation.org
Wed Oct 1 12:25:58 CEST 2014


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1412159019 -7200
# Node ID 7341bebc79d06f4b5f5d3210872a9130a8fe621f
# Parent  dc3f5514129939c86c8a11a261ba6819b606c3d6
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).

diff -r dc3f55141299 -r 7341bebc79d0 artifacts/doc/conf/generators/discharge-diagrams.xml
--- 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"/>
diff -r dc3f55141299 -r 7341bebc79d0 artifacts/src/main/java/org/dive4elements/river/exports/injector/ComputedDischargeInjector.java
--- 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]);
-        }
-    }
-}
diff -r dc3f55141299 -r 7341bebc79d0 artifacts/src/main/java/org/dive4elements/river/exports/injector/DischargeInjector.java
--- 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;
-    }
-
-}
diff -r dc3f55141299 -r 7341bebc79d0 artifacts/src/main/java/org/dive4elements/river/exports/injector/FixWQInjector.java
--- 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;
-        }
-    }
-}
diff -r dc3f55141299 -r 7341bebc79d0 artifacts/src/main/java/org/dive4elements/river/exports/injector/HistoricalDischargeInjector.java
--- 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);
-    }
-}
diff -r dc3f55141299 -r 7341bebc79d0 artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromGaugeNameInjector.java
--- /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;
+    }
+
+}
diff -r dc3f55141299 -r 7341bebc79d0 artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromGaugeNumberInjector.java
--- /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);
+    }
+}
diff -r dc3f55141299 -r 7341bebc79d0 artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromLocationInjector.java
--- /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.");
+    }
+}
diff -r dc3f55141299 -r 7341bebc79d0 artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromRequestInjector.java
--- /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;
+        }
+    }
+}


More information about the Dive4Elements-commits mailing list