changeset 535:017371801479

Added a chart info service to fetch the info document for a specific chart from artifact server. flys-client/trunk@2033 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 01 Jun 2011 08:15:58 +0000
parents e08777967bad
children fe2860653f8d
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties flys-client/src/main/java/de/intevation/flys/client/client/services/ChartInfoService.java flys-client/src/main/java/de/intevation/flys/client/client/services/ChartInfoServiceAsync.java flys-client/src/main/java/de/intevation/flys/client/server/ChartInfoServiceImpl.java flys-client/src/main/java/de/intevation/flys/client/shared/Transform2D.java flys-client/src/main/webapp/WEB-INF/web.xml
diffstat 10 files changed, 300 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Fri May 27 15:38:24 2011 +0000
+++ b/flys-client/ChangeLog	Wed Jun 01 08:15:58 2011 +0000
@@ -1,3 +1,24 @@
+2011-06-01  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/shared/Transform2D.java: New.
+	  This class transforms coordinates into an other coordinate system based
+	  on specified scale and translation factors.
+
+	* src/main/java/de/intevation/flys/client/client/services/ChartInfoServiceAsync.java,
+	  src/main/java/de/intevation/flys/client/client/services/ChartInfoService.java,
+	  src/main/java/de/intevation/flys/client/server/ChartInfoServiceImpl.java:
+	  New. A service that fetches the chart info document provided by the
+	  artifact server and returns (currently) a Transform2D object.
+
+	* src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties,
+	  src/main/java/de/intevation/flys/client/client/FLYSConstants.properties,
+	  src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties,
+	  src/main/java/de/intevation/flys/client/client/FLYSConstants.java: Added
+	  an error string that should be displayed if the chart info service
+	  failed.
+
+	* src/main/webapp/WEB-INF/web.xml: Registered the chart info service.
+
 2011-05-27  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java:
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java	Fri May 27 15:38:24 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.java	Wed Jun 01 08:15:58 2011 +0000
@@ -231,5 +231,7 @@
     String error_feed_w_values_invalid();
 
     String error_no_meta_data_found();
+
+    String error_chart_info_service();
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties	Fri May 27 15:38:24 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants.properties	Wed Jun 01 08:15:58 2011 +0000
@@ -114,5 +114,6 @@
 error_feed_no_wq_mode_selected = No W/Q mode selected.
 error_feed_q_values_invalid = Selected Q values are outside the valid range.
 error_feed_w_values_invalid = Selected W values are outside the valid range.
+error_chart_info_service = Could not fetch meta information of the chart.
 
 error_no_meta_data_found = No meta data found.
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties	Fri May 27 15:38:24 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_de.properties	Wed Jun 01 08:15:58 2011 +0000
@@ -114,5 +114,6 @@
 error_feed_no_wq_mode_selected = Es ist kein W/Q Modus gew\u00e4hlt.
 error_feed_q_values_invalid = Die gew\u00e4hlten Q Werte liegen au\u00dferhalb des g\u00fcltigen Bereiches.
 error_feed_w_values_invalid = Die gew\u00e4hlten W Werte liegen au\u00dferhalb des g\u00fcltigen Bereiches.
+error_chart_info_service = Meta Informationen des Diagramms konnten nicht geladen werden.
 
 error_no_meta_data_found = Keine Meta-Daten gefunden.
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties	Fri May 27 15:38:24 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties	Wed Jun 01 08:15:58 2011 +0000
@@ -114,5 +114,6 @@
 error_feed_no_wq_mode_selected = No W/Q mode selected.
 error_feed_q_values_invalid = Selected Q values are outside the valid range.
 error_feed_w_values_invalid = Selected W values are outside the valid range.
+error_chart_info_service = Could not fetch meta information of the chart.
 
 error_no_meta_data_found = No meta data found.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/services/ChartInfoService.java	Wed Jun 01 08:15:58 2011 +0000
@@ -0,0 +1,25 @@
+package de.intevation.flys.client.client.services;
+
+import com.google.gwt.user.client.rpc.RemoteService;
+import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
+
+import de.intevation.flys.client.shared.Transform2D;
+import de.intevation.flys.client.shared.exceptions.ServerException;
+import de.intevation.flys.client.shared.model.Collection;
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+@RemoteServiceRelativePath("chart-info")
+public interface ChartInfoService extends RemoteService {
+
+    Transform2D getChartInfo(
+        Collection collection,
+        String     url,
+        String     locale,
+        String     type,
+        int        width,
+        int        height)
+    throws ServerException;
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/services/ChartInfoServiceAsync.java	Wed Jun 01 08:15:58 2011 +0000
@@ -0,0 +1,23 @@
+package de.intevation.flys.client.client.services;
+
+import com.google.gwt.user.client.rpc.AsyncCallback;
+
+import de.intevation.flys.client.shared.Transform2D;
+import de.intevation.flys.client.shared.model.Collection;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public interface ChartInfoServiceAsync {
+
+    public void getChartInfo(
+        Collection collection,
+        String     url,
+        String     locale,
+        String     type,
+        int        width,
+        int        height,
+        AsyncCallback<Transform2D> callback);
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/ChartInfoServiceImpl.java	Wed Jun 01 08:15:58 2011 +0000
@@ -0,0 +1,164 @@
+package de.intevation.flys.client.server;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+import javax.xml.xpath.XPathConstants;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import com.google.gwt.user.server.rpc.RemoteServiceServlet;
+
+import de.intevation.artifacts.common.ArtifactNamespaceContext;
+import de.intevation.artifacts.common.utils.ClientProtocolUtils;
+import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
+
+import de.intevation.artifacts.httpclient.http.HttpClient;
+import de.intevation.artifacts.httpclient.http.HttpClientImpl;
+
+import de.intevation.flys.client.shared.Transform2D;
+import de.intevation.flys.client.shared.exceptions.ServerException;
+import de.intevation.flys.client.shared.model.Collection;
+
+import de.intevation.flys.client.client.services.ChartInfoService;
+
+
+/**
+ * This service fetches a document that contains meta information for a specific
+ * chart.
+ *
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class ChartInfoServiceImpl
+extends      RemoteServiceServlet
+implements   ChartInfoService
+{
+    public static final String XPATH_TRANSFORM_MATRIX =
+        "/art:chartinfo/art:transformation-matrix";
+
+    public static final String EXCEPTION_STRING = "error_chart_info_service";
+
+
+    public Transform2D getChartInfo(
+        Collection collection,
+        String     url,
+        String     locale,
+        String     type,
+        int        width,
+        int        height)
+    throws ServerException
+    {
+        System.out.println("ChartInfoServiceImpl.getChartInfo");
+
+        Document request = ClientProtocolUtils.newOutCollectionDocument(
+                collection.identifier(),
+                type,
+                type,
+                getChartAttributes(width, height));
+
+        try {
+            HttpClient client = new HttpClientImpl(url, locale);
+            InputStream in    = client.collectionOut(
+                request,
+                collection.identifier(),
+                type + "_chartinfo");
+
+            Document info = XMLUtils.parseDocument(in);
+
+            return parseInfoDocument(info);
+        }
+        catch (IOException ioe) {
+            // do nothing
+        }
+
+        System.err.println("Error while fetching chart info.");
+
+        throw new ServerException(EXCEPTION_STRING);
+    }
+
+
+    /**
+     * This method returns a document which might contain parameters to adjust
+     * chart settings. The document is created using the information that are
+     * contained in the request object.
+     *
+     * @param req The request document.
+     *
+     * @return a document to adjust chart settings.
+     */
+    protected Document getChartAttributes(int width, int height) {
+        Document doc = XMLUtils.newDocument();
+
+        ElementCreator ec = new ElementCreator(
+            doc,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        Element size = ec.create("size");
+
+        ec.addAttr(size, "width", String.valueOf(width), true);
+        ec.addAttr(size, "height", String.valueOf(height), true);
+
+        doc.appendChild(size);
+
+        return doc;
+    }
+
+
+    /**
+     * Parses the chart info document and extract the Transform2D values.
+     *
+     * @param doc The chart info document.
+     *
+     * @return a Transform2D object to transfrom pixel coordinates into chart
+     * coordinates.
+     */
+    protected Transform2D parseInfoDocument(Document doc) {
+        System.out.println("ChartInfoServiceImpl.parseInfoDocument");
+
+        XMLDebug.out(doc);
+
+        Node matrix = (Node) XMLUtils.xpath(
+            doc,
+            XPATH_TRANSFORM_MATRIX,
+            XPathConstants.NODE,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (matrix == null) {
+            return new Transform2D(1d, 1d, 0d, 0d);
+        }
+
+        String sx = XMLUtils.xpathString(
+            matrix, "@art:sx", ArtifactNamespaceContext.INSTANCE);
+
+        String sy = XMLUtils.xpathString(
+            matrix, "@art:sy", ArtifactNamespaceContext.INSTANCE);
+
+        String tx = XMLUtils.xpathString(
+            matrix, "@art:tx", ArtifactNamespaceContext.INSTANCE);
+
+        String ty = XMLUtils.xpathString(
+            matrix, "@art:ty", ArtifactNamespaceContext.INSTANCE);
+
+        if (sx != null && sy != null && tx != null && ty != null) {
+            try {
+                return new Transform2D(
+                    Double.parseDouble(sx),
+                    Double.parseDouble(sy),
+                    Double.parseDouble(tx),
+                    Double.parseDouble(ty));
+            }
+            catch (NumberFormatException nfe) {
+                System.err.println("Error while parsing matrix values.");
+            }
+        }
+
+        System.err.println("No matrix values found.");
+
+        return new Transform2D(1d, 1d, 0d, 0d);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/Transform2D.java	Wed Jun 01 08:15:58 2011 +0000
@@ -0,0 +1,52 @@
+package de.intevation.flys.client.shared;
+
+import java.io.Serializable;
+
+
+/**
+ * This object supports a linear transformation to transform xy coordinates into
+ * an other coordinate system based on scale and translation values.
+ *
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class Transform2D implements Serializable {
+
+    protected double sx;
+    protected double sy;
+
+    protected double tx;
+    protected double ty;
+
+
+    public Transform2D() {
+    }
+
+
+    /**
+     * Creates a new transformation with scale and translation factors.
+     *
+     * @param sx The scale factor for the x axis.
+     * @param sy The scale factor for the y axis.
+     * @param tx The translation factor for the x axis.
+     * @param ty The translation factor for the y axis.
+     */
+    public Transform2D(double sx, double sy, double tx, double ty) {
+        this.sx  = sx;
+        this.sy  = sy;
+        this.tx  = tx;
+        this.ty  = ty;
+    }
+
+
+    /**
+     * Transforms the pixel x and y into a new coordinate system based on the
+     * scale and translation values specified in the constructor.
+     */
+    public double[] transform(double x, double y) {
+        double resX = sx * x + tx;
+        double resY = sy * y + ty;
+
+        return new double[] { resX, resY };
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/webapp/WEB-INF/web.xml	Fri May 27 15:38:24 2011 +0000
+++ b/flys-client/src/main/webapp/WEB-INF/web.xml	Wed Jun 01 08:15:58 2011 +0000
@@ -177,6 +177,16 @@
     <url-pattern>/flys/collection-attribute</url-pattern>
   </servlet-mapping>
 
+  <servlet>
+    <servlet-name>ChartInfoService</servlet-name>
+    <servlet-class>de.intevation.flys.client.server.ChartInfoServiceImpl</servlet-class>
+  </servlet>
+
+  <servlet-mapping>
+    <servlet-name>ChartInfoService</servlet-name>
+    <url-pattern>/flys/chart-info</url-pattern>
+  </servlet-mapping>
+
   <!-- Default page to serve -->
   <welcome-file-list>
     <welcome-file>FLYS.html</welcome-file>

http://dive4elements.wald.intevation.org