changeset 2565:72f77b2210c2

FixingsOverview: Added XSL transformation which directly converts the output of the fixings overview service into an HTML representation. flys-artifacts/trunk@4091 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 21 Feb 2012 18:22:06 +0000
parents 3f038d54bc87
children 8bacd0ce3704
files flys-artifacts/ChangeLog flys-artifacts/contrib/fixoverview2html.xsl flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java
diffstat 3 files changed, 162 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Feb 20 19:13:19 2012 +0000
+++ b/flys-artifacts/ChangeLog	Tue Feb 21 18:22:06 2012 +0000
@@ -1,3 +1,21 @@
+2012-02-21  Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	 * src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java:
+	   - Calculate the extent of the river by the extents of the fixings.
+	   Reason: The river Elbe has only has fixings up to km 351. The other half
+	   of the river is empty which results in a sparse looking overview.
+	   - Add the name of the river as name attribute to resulting <river> element.
+
+	 * contrib/fixoverview2html.xsl: New. Demo XSL transformation to turn
+	   the output of the overview service directly into HTML. Used in the
+	   client a variant of this script could be used for producing the content
+	   of the assistance widget. Usage:
+
+	   $ curl http://localhost:8181/service/fixings-overview \
+	     -d @fix-overview.xml |                              \
+	     xsltproc contrib/fixoverview2html.xsl -             \
+	     > fixings-overview.html
+
 2012-02-20  Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/services/FixingsOverviewService.java:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/contrib/fixoverview2html.xsl	Tue Feb 21 18:22:06 2012 +0000
@@ -0,0 +1,121 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet
+  version="1.0"
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+  <xsl:output method="xml" encoding="UTF-8"/>
+
+  <xsl:template name="percent">
+    <xsl:param name="sector"/>
+    <xsl:variable name="rfrom" select="number(/fixings/river/@from)"/>
+    <xsl:variable name="rto" select="number(/fixings/river/@to)"/>
+    <xsl:variable name="rlength" select="$rto - $rfrom"/>
+
+    <xsl:variable name="sfrom" select="number($sector/@from)"/>
+    <xsl:variable name="sto" select="number($sector/@to)"/>
+    <xsl:variable name="slength" select="$sto - $sfrom"/>
+    <xsl:value-of select="100.0 * ($slength div $rlength)"/>
+  </xsl:template>
+
+  <xsl:template name="emit-gap">
+    <xsl:param name="sector"/>
+    <xsl:variable name="preds" select="$sector/preceding-sibling::sector"/>
+    <xsl:variable name="num-preds" select="count($preds)"/>
+    <xsl:variable name="glength">
+      <xsl:choose>
+        <xsl:when test="$num-preds &lt; 1">
+          <xsl:variable name="rfrom" select="number(/fixings/river/@from)"/>
+          <xsl:value-of select="number($sector/@from) - $rfrom"/>
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:variable name="bend" select="number($preds[last()]/@to)"/>
+          <xsl:value-of select="number($sector/@from) - $bend"/>
+        </xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+    <xsl:choose>
+      <xsl:when test="number($glength) &lt; 0.05 "/>
+      <xsl:otherwise>
+        <div>
+          <xsl:attribute name="style">
+            <xsl:text>width:</xsl:text>
+              <xsl:variable name="rfrom" select="number(/fixings/river/@from)"/>
+              <xsl:variable name="rto" select="number(/fixings/river/@to)"/>
+              <xsl:variable name="rlength" select="$rto - $rfrom"/>
+              <xsl:value-of select="100.0 * ($glength div $rlength)"/>
+            <xsl:text>%;float:left</xsl:text>
+          </xsl:attribute>
+          <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>
+        </div>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match="sector" mode="sectors">
+    <xsl:call-template name="emit-gap">
+      <xsl:with-param name="sector" select="."/>
+    </xsl:call-template>
+    <div>
+      <xsl:attribute name="style">
+        <xsl:text>background:</xsl:text>
+        <xsl:choose>
+          <xsl:when test="@class = '0'">green</xsl:when>
+          <xsl:when test="@class = '1'">blue</xsl:when>
+          <xsl:when test="@class = '2'">magenta</xsl:when>
+          <xsl:when test="@class = '3'">red</xsl:when>
+          <xsl:otherwise>black</xsl:otherwise>
+        </xsl:choose>
+        <xsl:text>;width:</xsl:text>
+        <xsl:call-template name="percent">
+          <xsl:with-param name="sector" select="."/>
+        </xsl:call-template>
+        <xsl:text>%</xsl:text>
+        <xsl:text>;float:left</xsl:text>
+      </xsl:attribute>
+      <xsl:attribute name="title">
+        <xsl:text>km </xsl:text>
+        <xsl:value-of select="@from"/>
+        <xsl:text> - </xsl:text>
+        <xsl:value-of select="@to"/>
+      </xsl:attribute>
+      <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>
+    </div>
+  </xsl:template>
+
+  <xsl:template match="event">
+    <tr>
+      <td>
+        <xsl:apply-templates mode="sectors"/>
+      </td>
+      <td align="center">
+        <xsl:attribute name="title"><xsl:value-of select="@name"/></xsl:attribute>
+        <xsl:value-of select="@date"/>
+      </td>
+    </tr>
+  </xsl:template>
+
+  <xsl:template match="events">
+    <table width="95%" border="1" cellspacing="0" cellpadding="0">
+      <colgroup>
+        <col width="85%"/>
+        <col width="15%"/>
+      </colgroup>
+      <xsl:apply-templates/>
+    </table>
+  </xsl:template>
+
+  <xsl:template match="/">
+    <html>
+      <head>
+        <title>Fixierungen:</title>
+      </head>
+      <body>
+        <xsl:apply-templates/>
+      </body>
+    </html>
+  </xsl:template>
+
+  <xsl:template match="text()"/>
+  <xsl:template match="text()" mode="sectors"/>
+
+</xsl:stylesheet>
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java	Mon Feb 20 19:13:19 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java	Tue Feb 21 18:22:06 2012 +0000
@@ -40,16 +40,6 @@
         "WHERE" +
         "    name = :name";
 
-    public static final String SQL_RIVER_EXTENT =
-        "SELECT" +
-        "    MIN(wcv.position) AS start_km," +
-        "    MAX(wcv.position) AS end_km " +
-        "FROM wst_column_values wcv" +
-        "    JOIN wst_columns wc ON wcv.wst_column_id = wc.id " +
-        "    JOIN wsts        w  ON wc.wst_id = w.id " +
-        "WHERE" +
-        "    w.kind = 0 AND w.river_id = :river_id";
-
     public static final String SQL_GAUGES =
         "SELECT" +
         "    g.id AS gauge_id," +
@@ -153,6 +143,11 @@
             return !disjoint(other);
         }
 
+        public void extend(Range other) {
+            if (other.start < start) start = other.start;
+            if (other.end   > end  ) end   = other.end;
+        }
+
         public boolean clip(Range other) {
             if (disjoint(other)) return false;
 
@@ -539,6 +534,12 @@
             }
         }
 
+        public void adjustExtent(Range extent) {
+            for (Column column: columns) {
+                extent.extend(column);
+            }
+        }
+
         public void loadColumnsQRanges(
             SQLQuery    query,
             GaugeFinder gaugeFinder
@@ -570,6 +571,7 @@
 
     public FixingsOverview() {
         fixings = new ArrayList<Fixing>();
+        extent  = new Range(Double.MAX_VALUE, -Double.MAX_VALUE);
     }
 
     public FixingsOverview(String riverName) {
@@ -599,26 +601,6 @@
         return true;
     }
 
-    protected boolean loadRiverExtent(Session session) {
-        SQLQuery query = session.createSQLQuery(SQL_RIVER_EXTENT)
-            .addScalar("start_km", StandardBasicTypes.DOUBLE)
-            .addScalar("end_km",   StandardBasicTypes.DOUBLE);
-
-        query.setInteger("river_id", riverId);
-
-        List<Object []> list = query.list();
-
-        if (list.isEmpty()) {
-            log.warn("River " + riverId + " has no extent.");
-            return false;
-        }
-
-        Object [] row = list.get(0);
-        extent = new Range((Double)row[0], (Double)row[1]);
-
-        return true;
-    }
-
     protected GaugeFinder loadGauges(Session session) {
         SQLQuery query = session.createSQLQuery(SQL_GAUGES)
             .addScalar("gauge_id", StandardBasicTypes.INTEGER)
@@ -705,10 +687,15 @@
         }
     }
 
+    protected void adjustExtent() {
+        for (Fixing fixing: fixings) {
+            fixing.adjustExtent(extent);
+        }
+    }
+
     public boolean load(Session session) {
 
-        if (!loadRiver(session)
-        ||  !loadRiverExtent(session)) {
+        if (!loadRiver(session)) {
             return false;
         }
 
@@ -722,6 +709,9 @@
         loadFixings(session);
         loadFixingsColumns(session);
         loadFixingsColumnsKmRange(session);
+
+        adjustExtent();
+
         loadFixingsColumnsQRanges(session, gaugeFinder);
 
         return true;
@@ -883,6 +873,7 @@
         riverElement.setAttribute("from", String.valueOf(extent.start));
         riverElement.setAttribute("to",   String.valueOf(extent.end));
         riverElement.setAttribute("rid",  String.valueOf(riverId));
+        riverElement.setAttribute("name", riverName);
 
         fixingsElement.appendChild(riverElement);
 

http://dive4elements.wald.intevation.org