changeset 2599:fb7975828ffa

#544 Added a service that allows querying gauge information. flys-artifacts/trunk@4175 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 20 Mar 2012 13:51:39 +0000
parents e96bf6c47c12
children 3f1cc396d253
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/conf.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/GaugeInfoService.java
diffstat 3 files changed, 174 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Mar 20 11:34:44 2012 +0000
+++ b/flys-artifacts/ChangeLog	Tue Mar 20 13:51:39 2012 +0000
@@ -1,3 +1,13 @@
+2012-03-20  Ingo Weinzierl <ingo@intevation.de>
+
+	flys/issue544 (Historische ATs: Eingabeunterstützung W/Q)
+
+	* src/main/java/de/intevation/flys/artifacts/services/GaugeInfoService.java:
+	  New service that returns information for gauges based on a specified
+	  river.
+
+	* doc/conf/conf.xml: Registered the GaugeInfoService.
+
 2012-03-20  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/services/DischargeInfoService.java:
--- a/flys-artifacts/doc/conf/conf.xml	Tue Mar 20 11:34:44 2012 +0000
+++ b/flys-artifacts/doc/conf/conf.xml	Tue Mar 20 13:51:39 2012 +0000
@@ -127,6 +127,10 @@
                 name="fixings-overview"
                 service="de.intevation.flys.artifacts.services.FixingsOverviewService"
                 description="Returns an overview of the fixings of a given river.">de.intevation.artifactdatabase.DefaultServiceFactory</service-factory>
+            <service-factory
+                name="gaugeinfo"
+                service="de.intevation.flys.artifacts.services.GaugeInfoService"
+                description="Returns an overview of the fixings of a given river.">de.intevation.artifactdatabase.DefaultServiceFactory</service-factory>
         </service-factories>
 
     </factories>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/GaugeInfoService.java	Tue Mar 20 13:51:39 2012 +0000
@@ -0,0 +1,160 @@
+package de.intevation.flys.artifacts.services;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.artifacts.common.ArtifactNamespaceContext;
+import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
+
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.GlobalContext;
+
+import de.intevation.flys.model.Gauge;
+import de.intevation.flys.model.Range;
+import de.intevation.flys.model.River;
+
+import de.intevation.flys.artifacts.model.RiverFactory;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class GaugeInfoService extends FLYSService {
+
+    interface Filter {
+        boolean apply(Gauge gauge);
+    }
+
+
+    protected class ReferenceNumberFilter implements Filter {
+        private long refNr;
+
+        public ReferenceNumberFilter(long refNr) {
+            this.refNr = refNr;
+        }
+
+        @Override
+        public boolean apply(Gauge  gauge) {
+            logger.debug("Test gauge '" + gauge.getName() + "'");
+
+            if (gauge != null && gauge.getOfficialNumber() == refNr) {
+                return true;
+            }
+            else {
+                return false;
+            }
+        }
+    } // end of ReferenceNumberFilter class
+
+
+    /** The logger that is used by this service.*/
+    private static Logger logger = Logger.getLogger(GaugeInfoService.class);
+
+
+    public static final String XPATH_RIVERNAME = "/art:river/@name";
+
+    public static final String XPATH_REFERENCE_NR
+        = "/art:river/art:filter/art:gauge/text()";
+
+
+    public GaugeInfoService() {
+    }
+
+
+    @Override
+    public Document doProcess(
+        Document      data,
+        GlobalContext context,
+        CallMeta      callMeta
+    ) {
+        logger.debug("GaugeInfoService.process");
+
+        // XXX REMOVE THIS
+        logger.debug(XMLUtils.toString(data));
+
+        River river = getRiverFromRequest(data);
+
+        List<Filter> filters  = getFilters(data);
+        List<Gauge> allGauges = river.getGauges();
+        List<Gauge> filtered  = new ArrayList<Gauge>();
+
+        for (Gauge g: allGauges) {
+            for (Filter f: filters) {
+                if (f.apply(g)) {
+                    filtered.add(g);
+                }
+            }
+        }
+
+        return buildInfoDocument(filtered);
+    }
+
+
+    protected River getRiverFromRequest(Document data) {
+        String rivername = XMLUtils.xpathString(
+            data,
+            XPATH_RIVERNAME,
+            ArtifactNamespaceContext.INSTANCE);
+
+        logger.debug("Return Gauge info for River '" + rivername + "'");
+
+        return rivername != null ? RiverFactory.getRiver(rivername) : null;
+    }
+
+
+    protected List<Filter> getFilters(Document data) {
+        List<Filter> filters = new ArrayList<Filter>();
+
+        String refNr = XMLUtils.xpathString(
+            data,
+            XPATH_REFERENCE_NR,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (refNr != null && refNr.length() > 0) {
+            try {
+                filters.add(new ReferenceNumberFilter(Long.valueOf(refNr)));
+            }
+            catch (NumberFormatException nfe) {
+                logger.warn(nfe, nfe);
+            }
+        }
+
+        return filters;
+    }
+
+
+    protected Document buildInfoDocument(List<Gauge> gauges) {
+        Document doc = XMLUtils.newDocument();
+
+        ElementCreator cr = new ElementCreator(
+            doc,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        Element service = cr.create("service");
+
+        logger.debug("Append " + gauges.size() + " gauges to info doc.");
+
+        for (Gauge g: gauges) {
+            Range r = g.getRange();
+
+            Element el = cr.create("gauge");
+            cr.addAttr(el, "name", g.getName());
+            cr.addAttr(el, "lower", String.valueOf(r.getA().doubleValue()));
+            cr.addAttr(el, "upper", String.valueOf(r.getB().doubleValue()));
+
+            service.appendChild(el);
+        }
+
+        doc.appendChild(service);
+
+        return doc;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org