comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java @ 1057:04967dc9c83f

The title of a layer can be changed by the user. Product 'Layer' and 'Horizontalschnitt' now use the same code path to write the meta information file. (issue198) gnv-artifacts/trunk@1134 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 27 May 2010 16:43:00 +0000
parents dfd02f8d3602
children 7096a2e13676
comparison
equal deleted inserted replaced
1056:0318fda0001e 1057:04967dc9c83f
5 import java.io.FileOutputStream; 5 import java.io.FileOutputStream;
6 import java.io.IOException; 6 import java.io.IOException;
7 import java.io.OutputStream; 7 import java.io.OutputStream;
8 import java.util.Date; 8 import java.util.Date;
9 9
10 import javax.xml.xpath.XPathConstants;
11
10 import org.apache.log4j.Logger; 12 import org.apache.log4j.Logger;
13
11 import org.w3c.dom.Document; 14 import org.w3c.dom.Document;
12 import org.w3c.dom.Element; 15 import org.w3c.dom.Element;
13 import org.w3c.dom.Node; 16 import org.w3c.dom.Node;
14 17
15 import de.intevation.artifactdatabase.XMLUtils; 18 import de.intevation.artifactdatabase.XMLUtils;
32 public static final String NODE_SERVER = "server"; 35 public static final String NODE_SERVER = "server";
33 public static final String NODE_MAP = "map"; 36 public static final String NODE_MAP = "map";
34 public static final String NODE_TTL = "ttl"; 37 public static final String NODE_TTL = "ttl";
35 38
36 public static final String META_FILE_NAME = "meta.xml"; 39 public static final String META_FILE_NAME = "meta.xml";
37 public static final String ISOLINES_NAME = "isolines.shp"; 40
38 public static final String POLYGON_NAME = "polygons.shp"; 41 public static final String XPATH_META = "/art:meta";
39
40
41 public static final String CONTEXT_LAYER_TITLE = "wms.title";
42 42
43 /** 43 /**
44 * Constructor. 44 * Constructor.
45 */ 45 */
46 private MetaWriter() { 46 private MetaWriter() {
47 } 47 }
48 48
49 /** 49
50 * Writes a meta information file for product type 'Layer'. 50 /**
51 * Just create a new document and insert the root node.
51 * 52 *
52 * @param context CallContext object. 53 * @return the meta document with necessary root node.
53 * @param meta the document where the information should be placed in. 54 */
54 * @return the meta document. 55 public static Document initMeta() {
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(); 56 Document meta = XMLUtils.newDocument();
85 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( 57 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
86 meta, 58 meta,
87 ArtifactNamespaceContext.NAMESPACE_URI, 59 ArtifactNamespaceContext.NAMESPACE_URI,
88 ArtifactNamespaceContext.NAMESPACE_PREFIX); 60 ArtifactNamespaceContext.NAMESPACE_PREFIX);
89 61
90 Element root = creator.create("meta"); 62 Element root = creator.create("meta");
91 meta.appendChild(root); 63 meta.appendChild(root);
92 64
93 writeAbstractMeta(context, meta, root); 65 return meta;
94 writePolygonMeta(context, meta, root, uuid, paramType); 66 }
95 writeIsolineMeta(context, meta, root, uuid, paramType); 67
96 68 public static boolean insertLayer(
97 boolean success = writeMetaFile(path, meta); 69 CallContext context,
98 70 Document document,
99 if (success){ 71 String name,
100 return meta; 72 String title,
101 }else{ 73 String data,
102 return null; 74 String model,
103 } 75 String type,
104 } 76 String status
77 ) {
78 if (document == null) {
79 document = initMeta();
80 }
81
82 Node meta = (Node) XMLUtils.xpath(
83 document,
84 XPATH_META,
85 XPathConstants.NODE,
86 ArtifactNamespaceContext.INSTANCE);
87
88 if (meta == null) {
89 logger.error("No meta node was found in the given document. "
90 + "Cannot insert layer!");
91 return false;
92 }
93
94 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
95 document,
96 ArtifactNamespaceContext.NAMESPACE_URI,
97 ArtifactNamespaceContext.NAMESPACE_PREFIX);
98
99 Element layerEl = creator.create(LayerInfo.LAYER);
100 Element nameEl = creator.create(LayerInfo.LAYER_NAME);
101 Element titleEl = creator.create(LayerInfo.LAYER_TITLE);
102 Element dataEl = creator.create(LayerInfo.LAYER_DATA);
103 Element modelEl = creator.create(LayerInfo.LAYER_MODEL);
104 Element typeEl = creator.create(LayerInfo.LAYER_TYPE);
105 Element statusEl = creator.create(LayerInfo.LAYER_STATUS);
106
107 modelEl.setTextContent(model);
108 nameEl.setTextContent(name);
109 titleEl.setTextContent(title);
110 typeEl.setTextContent(type);
111 statusEl.setTextContent(status);
112 dataEl.setTextContent(data);
113
114 layerEl.appendChild(modelEl);
115 layerEl.appendChild(nameEl);
116 layerEl.appendChild(titleEl);
117 layerEl.appendChild(dataEl);
118 layerEl.appendChild(typeEl);
119 layerEl.appendChild(statusEl);
120
121 if (logger.isDebugEnabled()) {
122 logger.debug("--------------- WMS LAYER PARAMS ---------------");
123 logger.debug("Name : " + name);
124 logger.debug("Title : " + title);
125 logger.debug("Data : " + data);
126 logger.debug("Type : " + type);
127 logger.debug("Model : " + model);
128 logger.debug("Status: " + status);
129 }
130
131 meta.appendChild(layerEl);
132
133 return true;
134 }
135
105 136
106 /** 137 /**
107 * Method to write the <i>meta</i> document down to a file. 138 * Method to write the <i>meta</i> document down to a file.
108 * 139 *
109 * @param path The destination of the file. 140 * @param path The destination of the file.
158 * 189 *
159 * @param callContext The CallContext object. 190 * @param callContext The CallContext object.
160 * @param document The meta information document. 191 * @param document The meta information document.
161 * @param meta The element where the new information need to be appended to. 192 * @param meta The element where the new information need to be appended to.
162 */ 193 */
163 public static void writeAbstractMeta( 194 public static void insertAbstractMeta(
164 CallContext callContext, 195 CallContext callContext,
165 Document document, 196 Document document
166 Element meta
167 ) { 197 ) {
198 if (document == null) {
199 document = initMeta();
200 }
201
202 Node meta = (Node) XMLUtils.xpath(
203 document,
204 XPATH_META,
205 XPathConstants.NODE,
206 ArtifactNamespaceContext.INSTANCE);
207
168 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( 208 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
169 document, 209 document,
170 ArtifactNamespaceContext.NAMESPACE_URI, 210 ArtifactNamespaceContext.NAMESPACE_URI,
171 ArtifactNamespaceContext.NAMESPACE_PREFIX); 211 ArtifactNamespaceContext.NAMESPACE_PREFIX);
172 212
176 String server = (String) 216 String server = (String)
177 context.get(GNVArtifactContext.MAPSERVER_SERVER_PATH_KEY); 217 context.get(GNVArtifactContext.MAPSERVER_SERVER_PATH_KEY);
178 218
179 String map = (String) 219 String map = (String)
180 context.get(GNVArtifactContext.MAPSERVER_MAP_PATH_KEY); 220 context.get(GNVArtifactContext.MAPSERVER_MAP_PATH_KEY);
221
222 Long time = callContext.getTimeToLive();
223 time = time != null ? time + new Date().getTime() : null;
224 String ttl = time != null ? time.toString() : null;
181 225
182 if (logger.isDebugEnabled()) { 226 if (logger.isDebugEnabled()) {
183 logger.debug("MAPSERVER PATH: " + server); 227 logger.debug("MAPSERVER PATH: " + server);
184 logger.debug("MAP PATH: " + map); 228 logger.debug("MAP PATH: " + map);
229 logger.debug("TTL: " + ttl);
185 } 230 }
186 231
187 Element mapserver = creator.create(NODE_MAPSERVER); 232 Element mapserver = creator.create(NODE_MAPSERVER);
188 Element serverPath = creator.create(NODE_SERVER); 233 Element serverPath = creator.create(NODE_SERVER);
189 Element mapPath = creator.create(NODE_MAP); 234 Element mapPath = creator.create(NODE_MAP);
235 Element timetolive = creator.create(NODE_TTL);
190 236
191 mapPath.setTextContent(map); 237 mapPath.setTextContent(map);
192 serverPath.setTextContent(server); 238 serverPath.setTextContent(server);
239 timetolive.setTextContent(ttl);
193 240
194 mapserver.appendChild(serverPath); 241 mapserver.appendChild(serverPath);
195 mapserver.appendChild(mapPath); 242 mapserver.appendChild(mapPath);
243 mapserver.appendChild(timetolive);
196 meta.appendChild(mapserver); 244 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 } 245 }
374 } 246 }
375 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 247 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org