diff flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java @ 2504:425bc486a40f

Generate map print spec dynamically from artifact flys-client/trunk@4349 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 04 May 2012 18:31:03 +0000
parents e3bd1f412421
children 47d07709ba09
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java	Fri May 04 17:03:04 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java	Fri May 04 18:31:03 2012 +0000
@@ -9,12 +9,31 @@
 import java.io.BufferedOutputStream;
 import java.io.OutputStream;
 
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+
 import javax.servlet.ServletException;
 
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import de.intevation.artifacts.common.ArtifactNamespaceContext;
+
+import de.intevation.artifacts.common.utils.ClientProtocolUtils;
+import de.intevation.artifacts.common.utils.JSON;
+import de.intevation.artifacts.common.utils.StringUtils;
+import de.intevation.artifacts.common.utils.XMLUtils;
+
+import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
+
+import de.intevation.artifacts.httpclient.http.HttpClient;
+import de.intevation.artifacts.httpclient.http.HttpClientImpl;
+
+import de.intevation.flys.client.shared.model.MapConfig;
+
 import org.apache.log4j.Logger;
 
 import org.mapfish.print.MapPrinter;
@@ -24,17 +43,9 @@
 
 import org.mapfish.print.utils.PJsonObject;
 
-import de.intevation.artifacts.common.utils.StringUtils;
-import de.intevation.artifacts.common.utils.XMLUtils;
-import de.intevation.artifacts.common.utils.ClientProtocolUtils;
-
-import de.intevation.artifacts.httpclient.http.HttpClient;
-import de.intevation.artifacts.httpclient.http.HttpClientImpl;
-import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
-
 import org.w3c.dom.Document;
-
-import de.intevation.flys.client.shared.model.MapConfig;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
 
 public class MapPrintServiceImpl
 extends      HttpServlet
@@ -42,101 +53,70 @@
     private static final Logger log =
         Logger.getLogger(MapPrintServiceImpl.class);
 
-    private static final String DEMO_JSON =
-    "{" +
-    "\"layout\": \"A4 portrait\"," +
-    "\"title\": \"A simple example\"," +
-    "\"srs\": \"EPSG:4326\"," +
-    "\"dpi\": 254," +
-    "\"units\": \"degrees\"," +
-    "\"outputFormat\": \"pdf\"," +
-    "\"layers\": [{" +
-    "    \"type\": \"WMS\"," +
-    "    \"layers\": [\"basic\"]," +
-    "    \"baseURL\": \"http://labs.metacarta.com/wms/vmap0\"," +
-    "    \"format\": \"image/jpeg\"" +
-    "}," +
-    "{" +
-    "    \"type\": \"WMS\"," +
-    "    \"layers\": [\"routes\"]," +
-    "    \"baseURL\": \"http://www.camptocamp.org/cgi-bin/mapserv_c2corg\"," +
-    "    \"format\": \"image/png\"" +
-    "},    {" +
-    "\"opacity\": 0.5," +
-    "\"customParams\": {}," +
-    "\"type\": \"Vector\"," +
-    "\"styles\": {" +
-    "    \"1\": {" +
-    "        \"fillColor\": \"red\"," +
-    "        \"strokeColor\": \"red\"," +
-    "        \"fillOpacity\": 0.4000000000000001," +
-    "        \"hoverFillColor\": \"white\"," +
-    "        \"hoverFillOpacity\": 0.8000000000000002," +
-    "        \"strokeOpacity\": 1," +
-    "        \"strokeWidth\": 0.5," +
-    "        \"strokeLinecap\": \"round\"," +
-    "        \"strokeDashstyle\": \"solid\"," +
-    "        \"hoverStrokeColor\": \"red\"," +
-    "        \"hoverStrokeOpacity\": 1.0," +
-    "        \"hoverStrokeWidth\": 0.2," +
-    "        \"pointRadius\": 6," +
-    "        \"hoverPointRadius\": 1," +
-    "        \"hoverPointUnit\": \"%\"," +
-    "        \"pointerEvents\": \"visiblePainted\"," +
-    "        \"cursor\": \"inherit\"" +
-    "    }" +
-    "}," +
-    "\"styleProperty\": \"_style\"," +
-    "\"geoJson\": {" +
-    "    \"type\": \"FeatureCollection\"," +
-    "    \"features\": [{" +
-    "        \"type\": \"Feature\"," +
-    "        \"id\": \"OpenLayers.Feature.Vector_243\"," +
-    "        \"properties\": {" +
-    "            \"_style\": 1," +
-    "            \"name\": \"\"," +
-    "            \"description\": \"\"," +
-    "            \"ext-comp-1048\": \"default\"" +
-    "        }," +
-    "        \"geometry\": {" +
-    "            \"type\": \"Polygon\"," +
-    "            \"coordinates\": [[[5, 45], [5, 47], [7, 47], [7, 45], [5, 45]]]" +
-    "        }" +
-    "    }]" +
-    "}," +
-    "\"name\": \"Cosmetic\"" +
-    "    }" +
-    "]," +
-    "\"pages\": [" +
-    "    {" +
-    "        \"center\": [6, 45.5]," +
-    "        \"scale\": 4000000," +
-    "        \"mapTitle\": \"First map\"," +
-    "        \"comment\": \"This is the first page selected by the user.\"," +
-    "        \"rotation\": 0," +
-    "        \"data\": {" +
-    "            \"data\": [" +
-    "                {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," +
-    "                {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" +
-    "            ]," +
-    "            \"columns\": [\"id\", \"name\", \"icon\"]" +
-    "        }" +
-    "    },{" +
-    "        \"center\": [6, 45.5]," +
-    "        \"scale\": 4000000," +
-    "        \"mapTitle\": \"First map\"," +
-    "        \"comment\": \"This is the first page selected by the user.\"," +
-    "        \"rotation\": 0," +
-    "        \"data\": {" +
-    "            \"data\": [" +
-    "                {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," +
-    "                {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" +
-    "            ]," +
-    "            \"columns\": [\"id\", \"name\", \"icon\"]" +
-    "        }" +
-    "    }" +
-    "]" +
-    "}";
+    protected static String generateSpec(
+        Document document,
+        double minX, double minY,
+        double maxX, double maxY
+    ) {
+        System.err.println(XMLUtils.toString(document));
+        MapConfig mapConfig = MapHelper.parseConfig(document);
+
+        Map<String, Object> spec = new LinkedHashMap<String, Object>();
+        spec.put("layout", "A4 portrait");
+        spec.put("title", "FLYS Druck");
+        spec.put("srs", "EPSG:" + mapConfig.getSrid());
+        spec.put("dpi", Integer.valueOf(254));
+        spec.put("units", "degrees");
+        spec.put("outputFormat", "pdf");
+
+        List<Object> layers = new ArrayList<Object>();
+
+        String ns = ArtifactNamespaceContext.NAMESPACE_URI;
+
+        NodeList facets = document.getElementsByTagNameNS(ns, "facet");
+
+        for (int i = 0, N = facets.getLength(); i < N; ++i) {
+            Element element = (Element)facets.item(i);
+            if (!element.getParentNode().getLocalName().equals("layers")) {
+                continue;
+            }
+            Map<String, Object> layer = new LinkedHashMap<String, Object>();
+
+            layer.put("type", "WMS");
+            List<Object> subLayers = new ArrayList<Object>(1);
+            subLayers.add(element.getAttributeNS(ns, "layers"));
+            layer.put("layers", subLayers);
+            layer.put("baseURL", element.getAttributeNS(ns, "url"));
+            layer.put("format", "image/png");
+
+            layers.add(layer);
+        }
+
+        spec.put("layers", layers);
+        spec.put("name", "Name");
+
+        List<Object> pages = new ArrayList<Object>(1);
+
+        spec.put("pages", pages);
+
+        Map<String, Object> page = new LinkedHashMap<String, Object>();
+
+        List<Object> bounds = new ArrayList<Object>(4);
+        bounds.add(Double.valueOf(minX));
+        bounds.add(Double.valueOf(minY));
+        bounds.add(Double.valueOf(maxX));
+        bounds.add(Double.valueOf(maxY));
+
+        page.put("bbox", bounds);
+
+        page.put("mapTitle", "FLYS Karte");
+        page.put("comment", "Eine mit FLYS gedruckte Karte.");
+        page.put("rotation", Integer.valueOf(0));
+
+        pages.add(page);
+
+        return JSON.toJSONString(spec);
+    }
 
 
     @Override
@@ -200,11 +180,17 @@
             throw new ServletException(ce);
         }
 
-        MapConfig mapConfig = MapHelper.parseConfig(result);
+        String spec = generateSpec(
+            result,
+            minX, minY,
+            maxX, maxY);
 
-        System.err.println(XMLUtils.toString(result));
+        if (log.isDebugEnabled()) {
+            log.debug(spec);
+            //System.err.println(spec);
+        }
 
-        producePDF(DEMO_JSON, resp);
+        producePDF(spec, resp);
     }
 
     protected String getURL() throws ServletException {
@@ -237,7 +223,7 @@
     protected void producePDF(String json, HttpServletResponse resp)
     throws ServletException, IOException
     {
-        PJsonObject jsonSpec = MapPrinter.parseSpec(DEMO_JSON);
+        PJsonObject jsonSpec = MapPrinter.parseSpec(json);
 
         MapPrinter printer = getMapPrinter();
 

http://dive4elements.wald.intevation.org