[Dive4elements-commits] [PATCH 4 of 8] Add a service to return measurement stations of a river
Wald Commits
scm-commit at wald.intevation.org
Wed Oct 24 17:09:24 CEST 2012
# HG changeset patch
# User Björn Ricks <bjoern.ricks at intevation.de>
# Date 1351087068 -7200
# Node ID cac0878b4a92a41961ae9062fb9d743c20ce0f0a
# Parent 6302db90b63fcc089591432c65375a05a02d3997
Add a service to return measurement stations of a river
The new service returns an xml representation of the measurement stations on a
river.
diff -r 6302db90b63f -r cac0878b4a92 flys-artifacts/doc/conf/conf.xml
--- a/flys-artifacts/doc/conf/conf.xml Wed Oct 24 15:56:19 2012 +0200
+++ b/flys-artifacts/doc/conf/conf.xml Wed Oct 24 15:57:48 2012 +0200
@@ -206,6 +206,10 @@
name="dischargetablesoverview"
service="de.intevation.flys.artifacts.services.DischargeTablesOverview"
description="Returns an overview of the fixings of a given river.">de.intevation.artifactdatabase.DefaultServiceFactory</service-factory>
+ <service-factory
+ name="measurementstationinfo"
+ service="de.intevation.flys.artifacts.services.MeasurementStationInfoService"
+ description="Returns an overview of the measurement stations of a given river.">de.intevation.artifactdatabase.DefaultServiceFactory</service-factory>
</service-factories>
</factories>
diff -r 6302db90b63f -r cac0878b4a92 flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MeasurementStationInfoService.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MeasurementStationInfoService.java Wed Oct 24 15:57:48 2012 +0200
@@ -0,0 +1,135 @@
+package de.intevation.flys.artifacts.services;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+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.MeasurementStation;
+import de.intevation.flys.model.Range;
+import de.intevation.flys.model.River;
+
+/**
+ * @author <a href="mailto:bjoern.ricks at intevation.de">Björn Ricks</a>
+ */
+public class MeasurementStationInfoService extends FLYSService {
+
+ 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);
+
+ Element egs = ec.create("measurement-stations");
+
+ List<MeasurementStation> mstations = river.getMeasurementStations();
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("Loaded stations: " + mstations);
+ }
+
+ for (MeasurementStation mstation: mstations) {
+ Element eg = ec.create("measurement-station");
+
+ String name = mstation.getName();
+ if (name != null) {
+ ec.addAttr(eg, "name", name, true);
+ }
+
+ String type = mstation.getMeasurementType();
+ if (type != null) {
+ ec.addAttr(eg, "type", type, true);
+ }
+
+ String riverside = mstation.getRiverside();
+ if (riverside != null) {
+ ec.addAttr(eg, "riverside", riverside, true);
+ }
+
+ Double station = mstation.getStation();
+ if (station != null) {
+ ec.addAttr(eg, "station", Double.toString(station), true);
+ }
+
+ Range range = mstation.getRange();
+ if (range != null) {
+ BigDecimal a = range.getA();
+ if (a != null) {
+ ec.addAttr(eg, "start", getStringValue(a), true);
+ }
+
+ BigDecimal b = range.getB();
+ if (b != null) {
+ ec.addAttr(eg, "end", getStringValue(b), true);
+ }
+ }
+
+ egs.appendChild(eg);
+ }
+
+ go.appendChild(r);
+ go.appendChild(egs);
+ result.appendChild(go);
+
+ 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()) : "";
+ }
+}
More information about the Dive4elements-commits
mailing list