comparison flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java @ 2506:47d07709ba09

Printing: extract layer order and visibility from collections describe. flys-client/trunk@4353 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 07 May 2012 17:18:07 +0000
parents 425bc486a40f
children 4aa70825bde1
comparison
equal deleted inserted replaced
2505:87ac5c532523 2506:47d07709ba09
1 package de.intevation.flys.client.server; 1 package de.intevation.flys.client.server;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.FileOutputStream;
7 import java.io.FileInputStream;
8 import java.io.BufferedInputStream;
9 import java.io.BufferedOutputStream;
10 import java.io.OutputStream;
11
12 import java.util.List;
13 import java.util.Map;
14 import java.util.ArrayList;
15 import java.util.LinkedHashMap;
16
17 import javax.servlet.ServletException;
18
19 import javax.servlet.http.HttpServlet;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22 2
23 import de.intevation.artifacts.common.ArtifactNamespaceContext; 3 import de.intevation.artifacts.common.ArtifactNamespaceContext;
24 4
25 import de.intevation.artifacts.common.utils.ClientProtocolUtils; 5 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
26 import de.intevation.artifacts.common.utils.JSON; 6 import de.intevation.artifacts.common.utils.JSON;
30 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; 10 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
31 11
32 import de.intevation.artifacts.httpclient.http.HttpClient; 12 import de.intevation.artifacts.httpclient.http.HttpClient;
33 import de.intevation.artifacts.httpclient.http.HttpClientImpl; 13 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
34 14
15 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
16
35 import de.intevation.flys.client.shared.model.MapConfig; 17 import de.intevation.flys.client.shared.model.MapConfig;
18
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import javax.servlet.ServletException;
35
36 import javax.servlet.http.HttpServlet;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
36 39
37 import org.apache.log4j.Logger; 40 import org.apache.log4j.Logger;
38 41
39 import org.mapfish.print.MapPrinter; 42 import org.mapfish.print.MapPrinter;
40 43
51 extends HttpServlet 54 extends HttpServlet
52 { 55 {
53 private static final Logger log = 56 private static final Logger log =
54 Logger.getLogger(MapPrintServiceImpl.class); 57 Logger.getLogger(MapPrintServiceImpl.class);
55 58
59 protected static class Layer implements Comparable<Layer> {
60
61 protected int pos;
62 protected String url;
63 protected String layers;
64 protected String description;
65
66 public Layer() {
67 }
68
69 public boolean setup(Element element) {
70
71 Element parent = (Element)element.getParentNode();
72 String parentName = parent.getAttribute("name");
73 if (!(parentName.equals("map")
74 || parentName.equals("floodmap"))) {
75 return false;
76 }
77
78 String ns = ArtifactNamespaceContext.NAMESPACE_URI;
79
80 String visible = element.getAttributeNS(ns, "visible");
81 String active = element.getAttributeNS(ns, "active");
82
83 if (visible.equals("0") || active.equals("0")) {
84 return false;
85 }
86
87 url = element.getAttributeNS(ns, "url");
88 layers = element.getAttributeNS(ns, "layers");
89 description = element.getAttributeNS(ns, "description");
90
91 try {
92 pos = Integer.parseInt(element.getAttributeNS(ns, "pos"));
93 }
94 catch (NumberFormatException nfe) {
95 return false;
96 }
97
98 return true;
99 }
100
101 public Map<String, Object> toMap() {
102 Map<String, Object> layer = new LinkedHashMap<String, Object>();
103
104 layer.put("type", "WMS");
105 List<Object> subLayers = new ArrayList<Object>(1);
106 subLayers.add(layers);
107 layer.put("layers", subLayers);
108 layer.put("baseURL", url);
109 layer.put("format", "image/png"); // TODO: Make configurable.
110
111 return layer;
112 }
113
114 @Override
115 public int compareTo(Layer other) {
116 int d = pos - other.pos;
117 if (d < 0) return -1;
118 return d > 0 ? +1 : 0;
119 }
120 } // class Layer
121
56 protected static String generateSpec( 122 protected static String generateSpec(
57 Document document, 123 Document descDocument,
124 Document outDocument,
58 double minX, double minY, 125 double minX, double minY,
59 double maxX, double maxY 126 double maxX, double maxY
60 ) { 127 ) {
61 System.err.println(XMLUtils.toString(document)); 128 MapConfig mapConfig = MapHelper.parseConfig(outDocument);
62 MapConfig mapConfig = MapHelper.parseConfig(document);
63 129
64 Map<String, Object> spec = new LinkedHashMap<String, Object>(); 130 Map<String, Object> spec = new LinkedHashMap<String, Object>();
65 spec.put("layout", "A4 portrait"); 131 spec.put("layout", "A4 portrait");
66 spec.put("title", "FLYS Druck"); 132 spec.put("title", "FLYS Druck");
67 spec.put("srs", "EPSG:" + mapConfig.getSrid()); 133 spec.put("srs", "EPSG:" + mapConfig.getSrid());
68 spec.put("dpi", Integer.valueOf(254)); 134 spec.put("dpi", Integer.valueOf(254));
69 spec.put("units", "degrees"); 135 spec.put("units", "degrees");
70 spec.put("outputFormat", "pdf"); 136 spec.put("outputFormat", "pdf");
71 137
72 List<Object> layers = new ArrayList<Object>(); 138
139 List<Layer> ls = new ArrayList<Layer>();
140 Layer l = new Layer();
73 141
74 String ns = ArtifactNamespaceContext.NAMESPACE_URI; 142 String ns = ArtifactNamespaceContext.NAMESPACE_URI;
75 143 NodeList facets = descDocument.getElementsByTagNameNS(ns, "facet");
76 NodeList facets = document.getElementsByTagNameNS(ns, "facet");
77 144
78 for (int i = 0, N = facets.getLength(); i < N; ++i) { 145 for (int i = 0, N = facets.getLength(); i < N; ++i) {
79 Element element = (Element)facets.item(i); 146 Element element = (Element)facets.item(i);
80 if (!element.getParentNode().getLocalName().equals("layers")) { 147 if (l.setup(element)) {
81 continue; 148 ls.add(l);
82 } 149 l = new Layer();
83 Map<String, Object> layer = new LinkedHashMap<String, Object>(); 150 }
84 151 }
85 layer.put("type", "WMS"); 152
86 List<Object> subLayers = new ArrayList<Object>(1); 153 // Establish Z order.
87 subLayers.add(element.getAttributeNS(ns, "layers")); 154 Collections.sort(ls);
88 layer.put("layers", subLayers); 155
89 layer.put("baseURL", element.getAttributeNS(ns, "url")); 156 List<Object> layers = new ArrayList<Object>(ls.size());
90 layer.put("format", "image/png"); 157
91 158 for (Layer layer: ls) {
92 layers.add(layer); 159 layers.add(layer.toMap());
93 } 160 }
94 161
95 spec.put("layers", layers); 162 spec.put("layers", layers);
96 spec.put("name", "Name"); 163 spec.put("name", "Name");
97 164
159 mapType = "floodmap"; 226 mapType = "floodmap";
160 } 227 }
161 228
162 String url = getURL(); 229 String url = getURL();
163 230
164 Document request = ClientProtocolUtils.newOutCollectionDocument( 231 Document requestOut =
165 uuid, mapType, mapType); 232 ClientProtocolUtils.newOutCollectionDocument(
166 233 uuid, mapType, mapType);
167 Document result; 234 Document requestDesc =
235 ClientProtocolUtils.newDescribeCollectionDocument(uuid);
236
237 Document outDocument;
238 Document descDocument;
239
168 try { 240 try {
169 HttpClient client = new HttpClientImpl(url); 241 HttpClient client = new HttpClientImpl(url);
170 InputStream is = client.collectionOut(request, uuid, mapType); 242
243 InputStream is = client.collectionOut(
244 requestOut, uuid, mapType);
245
171 try { 246 try {
172 result = XMLUtils.parseDocument(is); 247 outDocument = XMLUtils.parseDocument(is);
173 } 248 }
174 finally { 249 finally {
175 is.close(); 250 is.close();
176 } 251 is = null;
252 }
253
254 descDocument = (Document)client.doCollectionAction(
255 requestDesc, uuid, new DocumentResponseHandler());
177 } 256 }
178 catch (ConnectionException ce) { 257 catch (ConnectionException ce) {
179 log.error(ce); 258 log.error(ce);
180 throw new ServletException(ce); 259 throw new ServletException(ce);
181 } 260 }
182 261
183 String spec = generateSpec( 262 String spec = generateSpec(
184 result, 263 descDocument,
264 outDocument,
185 minX, minY, 265 minX, minY,
186 maxX, maxY); 266 maxX, maxY);
187 267
188 if (log.isDebugEnabled()) { 268 if (log.isDebugEnabled()) {
189 log.debug(spec); 269 log.debug(spec);

http://dive4elements.wald.intevation.org