[Dive4elements-commits] [PATCH 5 of 8] Refactor GaugeOverviewInfoService and MeasurementStationInfoService

Wald Commits scm-commit at wald.intevation.org
Wed Oct 24 17:09:25 CEST 2012


# HG changeset patch
# User Björn Ricks <bjoern.ricks at intevation.de>
# Date 1351089419 -7200
# Node ID 5da024c2af624030b635737f1aa954331dac716e
# Parent  cac0878b4a92a41961ae9062fb9d743c20ce0f0a
Refactor GaugeOverviewInfoService and MeasurementStationInfoService

Refactor RiverInfoService out of GaugeOverviewInfoService and
MeasurementStationInfoService to share common code of both classes in a base
class.

diff -r cac0878b4a92 -r 5da024c2af62 flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/GaugeOverviewInfoService.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/GaugeOverviewInfoService.java	Wed Oct 24 15:57:48 2012 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/GaugeOverviewInfoService.java	Wed Oct 24 16:36:59 2012 +0200
@@ -22,54 +22,21 @@
 /**
  * @author <a href="mailto:bjoern.ricks at intevation.de">Björn Ricks</a>
  */
-public class GaugeOverviewInfoService extends FLYSService {
+public class GaugeOverviewInfoService extends RiverInfoService {
 
     private static final Logger logger = Logger.getLogger(
             GaugeOverviewInfoService.class);
 
-    public static final String RIVER_XPATH = "/art:river/text()";
-
     @Override
     public Document doProcess(
         Document      data,
         GlobalContext globalContext,
         CallMeta      callMeta
     ) {
+        Document result = super.doProcess(data, globalContext, callMeta);
+
         logger.debug("GaugeOverviewInfoService.process");
 
-        String riverstr = XMLUtils.xpathString(
-            data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE);
-
-        River river = RiverFactory.getRiver(riverstr);
-
-        Document result = XMLUtils.newDocument();
-
-        if (river == null) {
-            logger.warn("No river with name " + riverstr + " found.");
-            return result;
-        }
-
-        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
-            result,
-            ArtifactNamespaceContext.NAMESPACE_URI,
-            ArtifactNamespaceContext.NAMESPACE_PREFIX);
-
-        Element go = ec.create("gauge-info");
-
-        double[] minmax  = river.determineMinMaxDistance();
-        double[] minmaxq = river.determineMinMaxQ();
-
-        Element r = ec.create("river");
-        ec.addAttr(r, "name", river.getName(), true);
-        ec.addAttr(r, "start", Double.toString(minmax[0]), true);
-        ec.addAttr(r, "end", Double.toString(minmax[1]), true);
-        ec.addAttr(r, "wstunit", river.getWstUnit().getName(), true);
-        ec.addAttr(r, "kmup", Boolean.toString(river.getKmUp()), true);
-        ec.addAttr(r, "minq", Double.toString(minmaxq[0]), true);
-        ec.addAttr(r, "maxq", Double.toString(minmaxq[1]), true);
-        ec.addAttr(r, "official", Long.toString(river.getOfficialNumber()),
-                    true);
-
         Element egs = ec.create("gauges");
 
         List<Gauge> gauges = river.getGauges();
@@ -86,12 +53,12 @@
                 ec.addAttr(eg, "name", gauge.getName(), true);
             }
 
-            String aeo = getGaugeValue(gauge.getAeo());
+            String aeo = getStringValue(gauge.getAeo());
             if (aeo != null) {
                 ec.addAttr(eg, "aeo", aeo, true);
             }
 
-            String datum = getGaugeValue(gauge.getDatum());
+            String datum = getStringValue(gauge.getDatum());
             if (datum != null) {
                 ec.addAttr(eg, "datum", datum, true);
             }
@@ -111,10 +78,10 @@
                 }
             }
             MinMaxWQ minmaxwq = gauge.fetchMaxMinWQ();
-            String minw = getGaugeValue(minmaxwq.getMinW());
-            String maxw = getGaugeValue(minmaxwq.getMaxW());
-            String minq = getGaugeValue(minmaxwq.getMinQ());
-            String maxq = getGaugeValue(minmaxwq.getMaxQ());
+            String minw = getStringValue(minmaxwq.getMinW());
+            String maxw = getStringValue(minmaxwq.getMaxW());
+            String minq = getStringValue(minmaxwq.getMinQ());
+            String maxq = getStringValue(minmaxwq.getMaxQ());
 
             if (minw != null) {
                 ec.addAttr(eg, "minw", minw, true);
@@ -129,7 +96,7 @@
                 ec.addAttr(eg, "maxq", maxq, true);
             }
 
-            String station = getGaugeValue(gauge.getStation());
+            String station = getStringValue(gauge.getStation());
             if (station != null) {
                 ec.addAttr(eg, "station", station, true);
             }
@@ -142,19 +109,8 @@
             egs.appendChild(eg);
         }
 
-        go.appendChild(r);
-        go.appendChild(egs);
-        result.appendChild(go);
+        riverele.appendChild(egs);
 
         return result;
     }
-
-    /**
-     * Returns a Double from a BigDecimal value or null if value is null
-     */
-    private static String getGaugeValue(BigDecimal value) {
-        return value != null
-            ? Double.toString(value.doubleValue())
-            : "";
-    }
 }
diff -r cac0878b4a92 -r 5da024c2af62 flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MeasurementStationInfoService.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MeasurementStationInfoService.java	Wed Oct 24 15:57:48 2012 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MeasurementStationInfoService.java	Wed Oct 24 16:36:59 2012 +0200
@@ -21,53 +21,18 @@
 /**
  * @author <a href="mailto:bjoern.ricks at intevation.de">Björn Ricks</a>
  */
-public class MeasurementStationInfoService extends FLYSService {
+public class MeasurementStationInfoService extends RiverInfoService {
 
     private static final Logger logger = Logger.getLogger(
             GaugeOverviewInfoService.class);
 
-    public static final String RIVER_XPATH = "/art:river/text()";
-
     @Override
     public Document doProcess(
         Document      data,
         GlobalContext globalContext,
         CallMeta      callMeta
     ) {
-        logger.debug("MeasurementStationInfoService.process");
-
-        String riverstr = XMLUtils.xpathString(
-            data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE);
-
-        River river = RiverFactory.getRiver(riverstr);
-
-        Document result = XMLUtils.newDocument();
-
-        if (river == null) {
-            logger.warn("No river with name " + riverstr + " found.");
-            return result;
-        }
-
-        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
-            result,
-            ArtifactNamespaceContext.NAMESPACE_URI,
-            ArtifactNamespaceContext.NAMESPACE_PREFIX);
-
-        Element go = ec.create("masurement-station-info");
-
-        double[] minmax  = river.determineMinMaxDistance();
-        double[] minmaxq = river.determineMinMaxQ();
-
-        Element r = ec.create("river");
-        ec.addAttr(r, "name", river.getName(), true);
-        ec.addAttr(r, "start", Double.toString(minmax[0]), true);
-        ec.addAttr(r, "end", Double.toString(minmax[1]), true);
-        ec.addAttr(r, "wstunit", river.getWstUnit().getName(), true);
-        ec.addAttr(r, "kmup", Boolean.toString(river.getKmUp()), true);
-        ec.addAttr(r, "minq", Double.toString(minmaxq[0]), true);
-        ec.addAttr(r, "maxq", Double.toString(minmaxq[1]), true);
-        ec.addAttr(r, "official", Long.toString(river.getOfficialNumber()),
-                    true);
+        Document result = super.doProcess(data, globalContext, callMeta);
 
         Element egs = ec.create("measurement-stations");
 
@@ -116,20 +81,9 @@
             egs.appendChild(eg);
         }
 
-        go.appendChild(r);
-        go.appendChild(egs);
-        result.appendChild(go);
+        this.riverele.appendChild(egs);
 
         return result;
     }
 
-    /**
-     * Returns a Double as String from a BigDecimal value.
-     *
-     * If value is null an empty String is returned.
-     */
-    private static String getStringValue(BigDecimal value) {
-        return value != null
-            ? Double.toString(value.doubleValue()) : "";
-    }
 }
diff -r cac0878b4a92 -r 5da024c2af62 flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/RiverInfoService.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/RiverInfoService.java	Wed Oct 24 16:36:59 2012 +0200
@@ -0,0 +1,85 @@
+package de.intevation.flys.artifacts.services;
+
+import java.math.BigDecimal;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.GlobalContext;
+import de.intevation.artifacts.common.ArtifactNamespaceContext;
+import de.intevation.artifacts.common.utils.XMLUtils;
+
+import de.intevation.flys.artifacts.model.RiverFactory;
+import de.intevation.flys.model.River;
+
+/**
+ * @author <a href="mailto:bjoern.ricks at intevation.de">Björn Ricks</a>
+ */
+public class RiverInfoService extends FLYSService {
+
+    private static final Logger logger = Logger.getLogger(
+            RiverInfoService.class);
+
+    protected static final String RIVER_XPATH = "/art:river/text()";
+
+    protected XMLUtils.ElementCreator ec;
+    protected River river;
+    protected Element riverele;
+
+    protected Document doProcess(
+        Document      data,
+        GlobalContext globalContext,
+        CallMeta      callMeta
+    ) {
+        String rivername = XMLUtils.xpathString(
+            data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE);
+
+        river = RiverFactory.getRiver(rivername);
+
+        Document result = XMLUtils.newDocument();
+
+        if (river == null) {
+            logger.warn("No river with name " + rivername + " found.");
+            return null;
+        }
+
+        ec = new XMLUtils.ElementCreator(
+            result,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        riverele = ec.create("river-info");
+
+        double[] minmax  = river.determineMinMaxDistance();
+        double[] minmaxq = river.determineMinMaxQ();
+
+        Element r = ec.create("river");
+        ec.addAttr(r, "name", river.getName(), true);
+        ec.addAttr(r, "start", Double.toString(minmax[0]), true);
+        ec.addAttr(r, "end", Double.toString(minmax[1]), true);
+        ec.addAttr(r, "wstunit", river.getWstUnit().getName(), true);
+        ec.addAttr(r, "kmup", Boolean.toString(river.getKmUp()), true);
+        ec.addAttr(r, "minq", Double.toString(minmaxq[0]), true);
+        ec.addAttr(r, "maxq", Double.toString(minmaxq[1]), true);
+        ec.addAttr(r, "official", Long.toString(river.getOfficialNumber()),
+                    true);
+
+        riverele.appendChild(r);
+        result.appendChild(riverele);
+
+        return result;
+    }
+
+    /**
+     * Returns a Double as String from a BigDecimal value.
+     *
+     * If value is null an empty String is returned.
+     */
+    protected static String getStringValue(BigDecimal value) {
+        return value != null
+            ? Double.toString(value.doubleValue()) : "";
+    }
+}


More information about the Dive4elements-commits mailing list