changeset 4218:6ae99d996f79

flys/issue903: Display the discharge tables of the selected gauge as chart in historical discharge calculation. There is a new class 'DischargeTablesChart' which is a Canvas and displays the chart using the new service 'DischargeTablesServiceImpl'.
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 23 Oct 2012 12:01:19 +0200
parents 08b6458909a9
children b7535b66490e
files flys-client/src/main/java/de/intevation/flys/client/client/ui/WQSimpleArrayPanel.java flys-client/src/main/java/de/intevation/flys/client/client/widgets/DischargeTablesChart.java flys-client/src/main/java/de/intevation/flys/client/server/DischargeTablesServiceImpl.java flys-client/src/main/webapp/WEB-INF/web.xml
diffstat 4 files changed, 247 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/WQSimpleArrayPanel.java	Tue Oct 23 11:58:47 2012 +0200
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/WQSimpleArrayPanel.java	Tue Oct 23 12:01:19 2012 +0200
@@ -29,7 +29,7 @@
 import de.intevation.flys.client.client.ui.wq.ClickableQDTable;
 import de.intevation.flys.client.client.ui.wq.ClickableWTable;
 import de.intevation.flys.client.client.ui.wq.ClickableWTable.ClickMode;
-import de.intevation.flys.client.client.ui.wq.QDTable;
+import de.intevation.flys.client.client.widgets.DischargeTablesChart;
 import de.intevation.flys.client.shared.model.ArtifactDescription;
 import de.intevation.flys.client.shared.model.Data;
 import de.intevation.flys.client.shared.model.DataItem;
@@ -208,12 +208,15 @@
 
         Tab w = new Tab(MSG.wq_table_w());
         Tab q = new Tab(MSG.wq_table_q());
+        Tab c = new Tab(MSG.chart());
 
         w.setPane(wTable);
         q.setPane(qTable);
+        c.setPane(new DischargeTablesChart(this.artifact));
 
         tabs.addTab(w, 0);
         tabs.addTab(q, 1);
+        tabs.addTab(c, 2);
 
         helperContainer.addMember(tabs);
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/widgets/DischargeTablesChart.java	Tue Oct 23 12:01:19 2012 +0200
@@ -0,0 +1,90 @@
+package de.intevation.flys.client.client.widgets;
+
+import com.google.gwt.core.client.GWT;
+import com.smartgwt.client.widgets.Canvas;
+import com.smartgwt.client.widgets.Img;
+import com.smartgwt.client.widgets.events.ResizedEvent;
+import com.smartgwt.client.widgets.events.ResizedHandler;
+
+import de.intevation.flys.client.shared.model.Artifact;
+import de.intevation.flys.client.shared.model.ArtifactDescription;
+
+
+public class DischargeTablesChart extends Canvas implements ResizedHandler {
+
+    protected Artifact artifact;
+
+    protected Img img;
+
+    public DischargeTablesChart() {
+        super();
+    }
+
+    public DischargeTablesChart(Artifact artifact) {
+        super();
+        this.artifact = artifact;
+        init();
+    }
+
+    private void init() {
+        addChild(createImage());
+        addResizedHandler(this);
+        setSize("100%", "100%");
+    }
+
+    protected Img createImage() {
+        img = new Img(getUrl());
+        img.setSize("100%", "100%");
+
+        return img;
+    }
+
+    protected String getUrl() {
+        String url = GWT.getModuleBaseURL();
+        url += "dischargetablesoverview";
+        url += "?gauge=" + getGauge();
+        url += "&format=png";
+
+        String[] timerange = getTimerange();
+        url += "&lower=" + timerange[0];
+        url += "&upper=" + timerange[1];
+
+        int width = 600;
+        int height = 400;
+        if (img != null) {
+            width = img.getWidth();
+            height = img.getHeight();
+        }
+
+        url += "&width=" + String.valueOf(width);
+        url += "&height=" + String.valueOf(height);
+
+        // add time millis to 'deactivate' caching
+        url += "&timemillis" + System.currentTimeMillis();
+
+        GWT.log("DischargeTablesService URL = '" + url + "'");
+        return url;
+    }
+
+    protected String getGauge() {
+        ArtifactDescription desc = artifact.getArtifactDescription();
+        return desc.getReferenceGauge();
+    }
+
+    protected String[] getTimerange() {
+        ArtifactDescription desc = artifact.getArtifactDescription();
+        String yearStr = desc.getDataValueAsString("year_range");
+
+        if (yearStr != null && yearStr.length() > 0) {
+            return yearStr.split(";");
+        }
+
+        return new String[2];
+    }
+
+    @Override
+    public void onResized(ResizedEvent event) {
+        GWT.log("resized discharge tables overview chart");
+        img.setSrc(getUrl());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/DischargeTablesServiceImpl.java	Tue Oct 23 12:01:19 2012 +0200
@@ -0,0 +1,143 @@
+package de.intevation.flys.client.server;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.artifacts.httpclient.http.HttpClient;
+import de.intevation.artifacts.httpclient.http.HttpClientImpl;
+import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler;
+
+
+public class DischargeTablesServiceImpl extends HttpServlet {
+
+    private static final Logger logger = Logger
+        .getLogger(DischargeInfoServiceImpl.class);
+
+    private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(
+        DateFormat.MEDIUM, Locale.GERMANY);
+
+    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
+        logger.info("ChartOutputServiceImpl.doGet");
+
+        String url = getServletContext().getInitParameter("server-url");
+        String locale = req.getParameter("locale");
+
+        prepareHeader(req, resp);
+
+        Document requestDoc = createRequestDoc(req);
+
+        HttpClient client = new HttpClientImpl(url, locale);
+
+        try {
+            OutputStream out = resp.getOutputStream();
+            InputStream stream = (InputStream) client.callService(url,
+                "dischargetablesoverview", requestDoc,
+                new StreamResponseHandler());
+
+            byte[] b = new byte[4096];
+            try {
+                int i;
+                while ((i = stream.read(b)) >= 0) {
+                    out.write(b, 0, i);
+                }
+            }
+            finally {
+                stream.close();
+            }
+        }
+        catch (IOException ioe) {
+            logger.error("Error while fetching discharge tables chart!", ioe);
+        }
+    }
+
+    protected void prepareHeader(HttpServletRequest req,
+        HttpServletResponse resp) {
+        resp.setHeader("Content-Type", "image/png");
+    }
+
+    protected Document createRequestDoc(HttpServletRequest req) {
+        Document request = XMLUtils.newDocument();
+        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(request,
+            null, null);
+
+        Element service = creator.create("service");
+        Element gauge = creator.create("gauge");
+        Element extent = creator.create("extent");
+        Element format = creator.create("format");
+        Element timerange = creator.create("timerange");
+
+        creator.addAttr(gauge, "name", extractRequestGauge(req));
+        creator.addAttr(extent, "width", extractRequestWidth(req));
+        creator.addAttr(extent, "height", extractRequestHeight(req));
+        creator.addAttr(format, "type", extractRequestFormat(req));
+        creator.addAttr(timerange, "lower", extractRequestLowerTime(req));
+        creator.addAttr(timerange, "upper", extractRequestUpperTime(req));
+
+        request.appendChild(service);
+        service.appendChild(gauge);
+        service.appendChild(extent);
+        service.appendChild(format);
+        service.appendChild(timerange);
+
+        return request;
+    }
+
+    protected String extractRequestGauge(HttpServletRequest req) {
+        return req.getParameter("gauge");
+    }
+
+    protected String extractRequestWidth(HttpServletRequest req) {
+        return req.getParameter("width");
+    }
+
+    protected String extractRequestHeight(HttpServletRequest req) {
+        return req.getParameter("height");
+    }
+
+    protected String extractRequestFormat(HttpServletRequest req) {
+        return req.getParameter("format");
+    }
+
+    protected String extractRequestLowerTime(HttpServletRequest req) {
+        String lowerStr = req.getParameter("lower");
+        try {
+            long lowerMillis = Long.parseLong(lowerStr);
+            Date lower = new Date(lowerMillis);
+
+            return DATE_FORMAT.format(lower);
+        }
+        catch (NumberFormatException nfe) {
+            logger.warn("Cannot parse time millies.", nfe);
+        }
+
+        return null;
+    }
+
+    protected String extractRequestUpperTime(HttpServletRequest req) {
+        String upperStr = req.getParameter("upper");
+        try {
+            long upperMillis = Long.parseLong(upperStr);
+            Date upper = new Date(upperMillis);
+
+            return DATE_FORMAT.format(upper);
+        }
+        catch (NumberFormatException nfe) {
+            logger.warn("Cannot parse time millies.", nfe);
+        }
+
+        return null;
+    }
+}
--- a/flys-client/src/main/webapp/WEB-INF/web.xml	Tue Oct 23 11:58:47 2012 +0200
+++ b/flys-client/src/main/webapp/WEB-INF/web.xml	Tue Oct 23 12:01:19 2012 +0200
@@ -267,6 +267,16 @@
     <url-pattern>/flys/chart</url-pattern>
   </servlet-mapping>
 
+  <servlet>
+  	<servlet-name>DischargeTablesOverviewService</servlet-name>
+  	<servlet-class>de.intevation.flys.client.server.DischargeTablesServiceImpl</servlet-class>
+  </servlet>
+
+  <servlet-mapping>
+  	<servlet-name>DischargeTablesOverviewService</servlet-name>
+  	<url-pattern>/flys/dischargetablesoverview</url-pattern>
+  </servlet-mapping>
+
   <!-- MapFish Print -->
   <servlet>
     <servlet-name>MapOutputService</servlet-name>

http://dive4elements.wald.intevation.org