comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents dfd02f8d3602
children 04967dc9c83f
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.utils;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStream;
8 import java.util.Date;
9
10 import org.apache.log4j.Logger;
11 import org.w3c.dom.Document;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Node;
14
15 import de.intevation.artifactdatabase.XMLUtils;
16 import de.intevation.artifacts.ArtifactNamespaceContext;
17 import de.intevation.artifacts.CallContext;
18 import de.intevation.gnv.artifacts.context.GNVArtifactContext;
19 import de.intevation.gnv.wms.LayerInfo;
20
21 /**
22 * This class provides some methods to create files storing meta information
23 * about wms layers and a map service which serves these layers.
24 *
25 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
26 */
27 public class MetaWriter {
28
29 private static Logger logger = Logger.getLogger(MetaWriter.class);
30
31 public static final String NODE_MAPSERVER = "mapserver";
32 public static final String NODE_SERVER = "server";
33 public static final String NODE_MAP = "map";
34 public static final String NODE_TTL = "ttl";
35
36 public static final String META_FILE_NAME = "meta.xml";
37 public static final String ISOLINES_NAME = "isolines.shp";
38 public static final String POLYGON_NAME = "polygons.shp";
39
40
41 public static final String CONTEXT_LAYER_TITLE = "wms.title";
42
43 /**
44 * Constructor.
45 */
46 private MetaWriter() {
47 }
48
49 /**
50 * Writes a meta information file for product type 'Layer'.
51 *
52 * @param context CallContext object.
53 * @param meta the document where the information should be placed in.
54 * @return the meta document.
55 */
56 public static Node writeLayerMeta(CallContext context, Document meta){
57 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
58 meta,
59 ArtifactNamespaceContext.NAMESPACE_URI,
60 ArtifactNamespaceContext.NAMESPACE_PREFIX);
61 Element root = creator.create("meta");
62 meta.appendChild(root);
63
64 writeAbstractMeta(context, meta, root);
65 return root;
66 }
67
68
69 /**
70 * Writes a meta information file for product type 'Horizontalschnitt'.
71 *
72 * @param context The CallContext object.
73 * @param uuid The UUID of the current artifact.
74 * @param path The destination of the meta file.
75 * @param paramType The parameter type.
76 * @return the meta document.
77 */
78 public static Document writeHorizontalcrosssectionMeta(
79 CallContext context,
80 String uuid,
81 String path,
82 String paramType)
83 {
84 Document meta = XMLUtils.newDocument();
85 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
86 meta,
87 ArtifactNamespaceContext.NAMESPACE_URI,
88 ArtifactNamespaceContext.NAMESPACE_PREFIX);
89
90 Element root = creator.create("meta");
91 meta.appendChild(root);
92
93 writeAbstractMeta(context, meta, root);
94 writePolygonMeta(context, meta, root, uuid, paramType);
95 writeIsolineMeta(context, meta, root, uuid, paramType);
96
97 boolean success = writeMetaFile(path, meta);
98
99 if (success){
100 return meta;
101 }else{
102 return null;
103 }
104 }
105
106 /**
107 * Method to write the <i>meta</i> document down to a file.
108 *
109 * @param path The destination of the file.
110 * @param meta The xml document storing the meta information.
111 */
112 public static boolean writeMetaFile(String path, Document meta) {
113 try {
114 File metaFile = new File(path, META_FILE_NAME);
115
116 if (metaFile.exists()) {
117 logger.info("Delete old meta information file.");
118 metaFile.delete();
119 }
120
121 if (!metaFile.createNewFile() || !metaFile.canWrite()) {
122 logger.error("Error while writing meta file: "+metaFile.toString());
123 return false;
124 }
125
126 OutputStream out = null;
127 boolean success = false;
128 try {
129 out = new FileOutputStream(metaFile);
130 success = XMLUtils.toStream(meta, out);
131 }
132 finally {
133 if (out != null) {
134 try { out.close(); }
135 catch (IOException ioe) {}
136 }
137 }
138
139 if (!success && metaFile.exists()) {
140 metaFile.delete();
141 }
142
143 return success;
144 }
145 catch (FileNotFoundException fnfe) {
146 logger.error(fnfe);
147 return false;
148 }
149 catch (IOException ioe) {
150 logger.error(ioe, ioe);
151 return false;
152 }
153 }
154
155
156 /**
157 * Append meta information about the mapservice itself.
158 *
159 * @param callContext The CallContext object.
160 * @param document The meta information document.
161 * @param meta The element where the new information need to be appended to.
162 */
163 public static void writeAbstractMeta(
164 CallContext callContext,
165 Document document,
166 Element meta
167 ) {
168 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
169 document,
170 ArtifactNamespaceContext.NAMESPACE_URI,
171 ArtifactNamespaceContext.NAMESPACE_PREFIX);
172
173 GNVArtifactContext context =
174 (GNVArtifactContext) callContext.globalContext();
175
176 String server = (String)
177 context.get(GNVArtifactContext.MAPSERVER_SERVER_PATH_KEY);
178
179 String map = (String)
180 context.get(GNVArtifactContext.MAPSERVER_MAP_PATH_KEY);
181
182 if (logger.isDebugEnabled()) {
183 logger.debug("MAPSERVER PATH: " + server);
184 logger.debug("MAP PATH: " + map);
185 }
186
187 Element mapserver = creator.create(NODE_MAPSERVER);
188 Element serverPath = creator.create(NODE_SERVER);
189 Element mapPath = creator.create(NODE_MAP);
190
191 mapPath.setTextContent(map);
192 serverPath.setTextContent(server);
193
194 mapserver.appendChild(serverPath);
195 mapserver.appendChild(mapPath);
196 meta.appendChild(mapserver);
197 }
198
199 /**
200 * Append layer information to the meta document.
201 *
202 * @param callContext The CallContext object.
203 * @param document The meta document.
204 * @param root The element where the new information need to be appended to.
205 * @param uuid The UUID of the current artifact.
206 * @param paramType The parameter type (e.g. salinity).
207 * @param layerType The layer type.
208 * @param shapefileName the name of the Shapefile
209 * @param shapefileName the Title of the Layer
210 */
211 public static void writeLayerMeta(
212 CallContext callContext,
213 Document document,
214 Node root,
215 String uuid,
216 String paramType,
217 String layerType,
218 String shapefileName,
219 String layerTitle
220 ) {
221 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
222 document,
223 ArtifactNamespaceContext.NAMESPACE_URI,
224 ArtifactNamespaceContext.NAMESPACE_PREFIX);
225
226 Long time = callContext.getTimeToLive();
227 time = time != null ? time + new Date().getTime() : null;
228 String ttl = time != null ? time.toString() : null;
229
230 logger.debug("Artifacts time to live: " + ttl);
231
232 Element layer = creator.create(LayerInfo.LAYER);
233 Element model = creator.create(LayerInfo.LAYER_MODEL);
234 Element name = creator.create(LayerInfo.LAYER_NAME);
235 Element title = creator.create(LayerInfo.LAYER_TITLE);
236 Element type = creator.create(LayerInfo.LAYER_TYPE);
237 Element status = creator.create(LayerInfo.LAYER_STATUS);
238 Element data = creator.create(LayerInfo.LAYER_DATA);
239 Element timeToLive = creator.create(NODE_TTL);
240
241 model.setTextContent(paramType);
242 name.setTextContent(uuid);
243 type.setTextContent(layerType);
244 status.setTextContent("OFF");
245 data.setTextContent(shapefileName);
246 timeToLive.setTextContent(ttl);
247
248 title.setTextContent(layerTitle);
249
250 layer.appendChild(model);
251 layer.appendChild(name);
252 layer.appendChild(title);
253 layer.appendChild(type);
254 layer.appendChild(status);
255 layer.appendChild(data);
256 layer.appendChild(timeToLive);
257
258 root.appendChild(layer);
259 }
260
261
262 /**
263 * Append polygon layer information to meta document.
264 *
265 * @param context
266 * @param document The meta document.
267 * @param meta The element where the new information need to be appended to.
268 * @param uuid The UUID of the current artifact.
269 * @param paramType The parameter type (e.g. salinity).
270 */
271 public static void writePolygonMeta(
272 CallContext context,
273 Document document,
274 Element meta,
275 String uuid,
276 String paramType
277 ) {
278 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
279 document,
280 ArtifactNamespaceContext.NAMESPACE_URI,
281 ArtifactNamespaceContext.NAMESPACE_PREFIX);
282
283 Long time = context.getTimeToLive();
284 time = time != null ? time + new Date().getTime() : null;
285 String ttl = time != null ? time.toString() : null;
286
287 logger.debug("Artifacts time to live: " + ttl);
288
289 Element layer = creator.create(LayerInfo.LAYER);
290 Element model = creator.create(LayerInfo.LAYER_MODEL);
291 Element name = creator.create(LayerInfo.LAYER_NAME);
292 Element title = creator.create(LayerInfo.LAYER_TITLE);
293 Element type = creator.create(LayerInfo.LAYER_TYPE);
294 Element status = creator.create(LayerInfo.LAYER_STATUS);
295 Element data = creator.create(LayerInfo.LAYER_DATA);
296 Element timeToLive = creator.create(NODE_TTL);
297
298 model.setTextContent(paramType);
299 name.setTextContent(uuid);
300 title.setTextContent(
301 (String) context.getContextValue(CONTEXT_LAYER_TITLE));
302 type.setTextContent("POLYGON");
303 status.setTextContent("OFF");
304 data.setTextContent(POLYGON_NAME);
305 timeToLive.setTextContent(ttl);
306
307 layer.appendChild(model);
308 layer.appendChild(name);
309 layer.appendChild(title);
310 layer.appendChild(type);
311 layer.appendChild(status);
312 layer.appendChild(data);
313 layer.appendChild(timeToLive);
314
315 meta.appendChild(layer);
316 }
317
318
319 /**
320 * Append isoline layer information to meta document.
321 *
322 * @param context
323 * @param document The meta document.
324 * @param meta The element where the new information need to be appended to.
325 * @param uuid The UUID of the current artifact.
326 * @param paramType The parameter type (e.g. salinity).
327 */
328 public static void writeIsolineMeta(
329 CallContext context,
330 Document document,
331 Element meta,
332 String uuid,
333 String paramType
334 ) {
335 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
336 document,
337 ArtifactNamespaceContext.NAMESPACE_URI,
338 ArtifactNamespaceContext.NAMESPACE_PREFIX);
339
340 Long time = context.getTimeToLive();
341 time = time != null ? time + new Date().getTime() : null;
342 String ttl = time != null ? time.toString() : null;
343
344 logger.debug("Artifacts time to live: " + ttl);
345
346 Element layer = creator.create(LayerInfo.LAYER);
347 Element model = creator.create(LayerInfo.LAYER_MODEL);
348 Element name = creator.create(LayerInfo.LAYER_NAME);
349 Element title = creator.create(LayerInfo.LAYER_TITLE);
350 Element type = creator.create(LayerInfo.LAYER_TYPE);
351 Element status = creator.create(LayerInfo.LAYER_STATUS);
352 Element data = creator.create(LayerInfo.LAYER_DATA);
353 Element timeToLive = creator.create(NODE_TTL);
354
355 model.setTextContent(paramType+"_isolines");
356 name.setTextContent(uuid);
357 title.setTextContent(
358 (String) context.getContextValue(CONTEXT_LAYER_TITLE));
359 type.setTextContent("LINE");
360 status.setTextContent("OFF");
361 data.setTextContent(ISOLINES_NAME);
362 timeToLive.setTextContent(ttl);
363
364 layer.appendChild(model);
365 layer.appendChild(name);
366 layer.appendChild(title);
367 layer.appendChild(type);
368 layer.appendChild(status);
369 layer.appendChild(data);
370 layer.appendChild(timeToLive);
371
372 meta.appendChild(layer);
373 }
374 }
375 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org