Mercurial > dive4elements > gnv-client
diff 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 |
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java Thu May 27 08:48:16 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java Thu May 27 16:43:00 2010 +0000 @@ -7,7 +7,10 @@ import java.io.OutputStream; import java.util.Date; +import javax.xml.xpath.XPathConstants; + import org.apache.log4j.Logger; + import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -34,11 +37,8 @@ public static final String NODE_TTL = "ttl"; public static final String META_FILE_NAME = "meta.xml"; - public static final String ISOLINES_NAME = "isolines.shp"; - public static final String POLYGON_NAME = "polygons.shp"; - - public static final String CONTEXT_LAYER_TITLE = "wms.title"; + public static final String XPATH_META = "/art:meta"; /** * Constructor. @@ -46,41 +46,13 @@ private MetaWriter() { } - /** - * Writes a meta information file for product type 'Layer'. - * - * @param context CallContext object. - * @param meta the document where the information should be placed in. - * @return the meta document. - */ - public static Node writeLayerMeta(CallContext context, Document meta){ - XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( - meta, - ArtifactNamespaceContext.NAMESPACE_URI, - ArtifactNamespaceContext.NAMESPACE_PREFIX); - Element root = creator.create("meta"); - meta.appendChild(root); - - writeAbstractMeta(context, meta, root); - return root; - } - /** - * Writes a meta information file for product type 'Horizontalschnitt'. + * Just create a new document and insert the root node. * - * @param context The CallContext object. - * @param uuid The UUID of the current artifact. - * @param path The destination of the meta file. - * @param paramType The parameter type. - * @return the meta document. + * @return the meta document with necessary root node. */ - public static Document writeHorizontalcrosssectionMeta( - CallContext context, - String uuid, - String path, - String paramType) - { + public static Document initMeta() { Document meta = XMLUtils.newDocument(); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( meta, @@ -90,19 +62,78 @@ Element root = creator.create("meta"); meta.appendChild(root); - writeAbstractMeta(context, meta, root); - writePolygonMeta(context, meta, root, uuid, paramType); - writeIsolineMeta(context, meta, root, uuid, paramType); - - boolean success = writeMetaFile(path, meta); + return meta; + } - if (success){ - return meta; - }else{ - return null; + public static boolean insertLayer( + CallContext context, + Document document, + String name, + String title, + String data, + String model, + String type, + String status + ) { + if (document == null) { + document = initMeta(); } + + Node meta = (Node) XMLUtils.xpath( + document, + XPATH_META, + XPathConstants.NODE, + ArtifactNamespaceContext.INSTANCE); + + if (meta == null) { + logger.error("No meta node was found in the given document. " + + "Cannot insert layer!"); + return false; + } + + XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( + document, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element layerEl = creator.create(LayerInfo.LAYER); + Element nameEl = creator.create(LayerInfo.LAYER_NAME); + Element titleEl = creator.create(LayerInfo.LAYER_TITLE); + Element dataEl = creator.create(LayerInfo.LAYER_DATA); + Element modelEl = creator.create(LayerInfo.LAYER_MODEL); + Element typeEl = creator.create(LayerInfo.LAYER_TYPE); + Element statusEl = creator.create(LayerInfo.LAYER_STATUS); + + modelEl.setTextContent(model); + nameEl.setTextContent(name); + titleEl.setTextContent(title); + typeEl.setTextContent(type); + statusEl.setTextContent(status); + dataEl.setTextContent(data); + + layerEl.appendChild(modelEl); + layerEl.appendChild(nameEl); + layerEl.appendChild(titleEl); + layerEl.appendChild(dataEl); + layerEl.appendChild(typeEl); + layerEl.appendChild(statusEl); + + if (logger.isDebugEnabled()) { + logger.debug("--------------- WMS LAYER PARAMS ---------------"); + logger.debug("Name : " + name); + logger.debug("Title : " + title); + logger.debug("Data : " + data); + logger.debug("Type : " + type); + logger.debug("Model : " + model); + logger.debug("Status: " + status); + } + + meta.appendChild(layerEl); + + return true; } + /** * Method to write the <i>meta</i> document down to a file. * @@ -160,11 +191,20 @@ * @param document The meta information document. * @param meta The element where the new information need to be appended to. */ - public static void writeAbstractMeta( + public static void insertAbstractMeta( CallContext callContext, - Document document, - Element meta + Document document ) { + if (document == null) { + document = initMeta(); + } + + Node meta = (Node) XMLUtils.xpath( + document, + XPATH_META, + XPathConstants.NODE, + ArtifactNamespaceContext.INSTANCE); + XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( document, ArtifactNamespaceContext.NAMESPACE_URI, @@ -179,197 +219,29 @@ String map = (String) context.get(GNVArtifactContext.MAPSERVER_MAP_PATH_KEY); + Long time = callContext.getTimeToLive(); + time = time != null ? time + new Date().getTime() : null; + String ttl = time != null ? time.toString() : null; + if (logger.isDebugEnabled()) { logger.debug("MAPSERVER PATH: " + server); logger.debug("MAP PATH: " + map); + logger.debug("TTL: " + ttl); } Element mapserver = creator.create(NODE_MAPSERVER); Element serverPath = creator.create(NODE_SERVER); Element mapPath = creator.create(NODE_MAP); + Element timetolive = creator.create(NODE_TTL); mapPath.setTextContent(map); serverPath.setTextContent(server); + timetolive.setTextContent(ttl); mapserver.appendChild(serverPath); mapserver.appendChild(mapPath); + mapserver.appendChild(timetolive); meta.appendChild(mapserver); } - - /** - * Append layer information to the meta document. - * - * @param callContext The CallContext object. - * @param document The meta document. - * @param root The element where the new information need to be appended to. - * @param uuid The UUID of the current artifact. - * @param paramType The parameter type (e.g. salinity). - * @param layerType The layer type. - * @param shapefileName the name of the Shapefile - * @param shapefileName the Title of the Layer - */ - public static void writeLayerMeta( - CallContext callContext, - Document document, - Node root, - String uuid, - String paramType, - String layerType, - String shapefileName, - String layerTitle - ) { - XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( - document, - ArtifactNamespaceContext.NAMESPACE_URI, - ArtifactNamespaceContext.NAMESPACE_PREFIX); - - Long time = callContext.getTimeToLive(); - time = time != null ? time + new Date().getTime() : null; - String ttl = time != null ? time.toString() : null; - - logger.debug("Artifacts time to live: " + ttl); - - Element layer = creator.create(LayerInfo.LAYER); - Element model = creator.create(LayerInfo.LAYER_MODEL); - Element name = creator.create(LayerInfo.LAYER_NAME); - Element title = creator.create(LayerInfo.LAYER_TITLE); - Element type = creator.create(LayerInfo.LAYER_TYPE); - Element status = creator.create(LayerInfo.LAYER_STATUS); - Element data = creator.create(LayerInfo.LAYER_DATA); - Element timeToLive = creator.create(NODE_TTL); - - model.setTextContent(paramType); - name.setTextContent(uuid); - type.setTextContent(layerType); - status.setTextContent("OFF"); - data.setTextContent(shapefileName); - timeToLive.setTextContent(ttl); - - title.setTextContent(layerTitle); - - layer.appendChild(model); - layer.appendChild(name); - layer.appendChild(title); - layer.appendChild(type); - layer.appendChild(status); - layer.appendChild(data); - layer.appendChild(timeToLive); - - root.appendChild(layer); - } - - - /** - * Append polygon layer information to meta document. - * - * @param context - * @param document The meta document. - * @param meta The element where the new information need to be appended to. - * @param uuid The UUID of the current artifact. - * @param paramType The parameter type (e.g. salinity). - */ - public static void writePolygonMeta( - CallContext context, - Document document, - Element meta, - String uuid, - String paramType - ) { - XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( - document, - ArtifactNamespaceContext.NAMESPACE_URI, - ArtifactNamespaceContext.NAMESPACE_PREFIX); - - Long time = context.getTimeToLive(); - time = time != null ? time + new Date().getTime() : null; - String ttl = time != null ? time.toString() : null; - - logger.debug("Artifacts time to live: " + ttl); - - Element layer = creator.create(LayerInfo.LAYER); - Element model = creator.create(LayerInfo.LAYER_MODEL); - Element name = creator.create(LayerInfo.LAYER_NAME); - Element title = creator.create(LayerInfo.LAYER_TITLE); - Element type = creator.create(LayerInfo.LAYER_TYPE); - Element status = creator.create(LayerInfo.LAYER_STATUS); - Element data = creator.create(LayerInfo.LAYER_DATA); - Element timeToLive = creator.create(NODE_TTL); - - model.setTextContent(paramType); - name.setTextContent(uuid); - title.setTextContent( - (String) context.getContextValue(CONTEXT_LAYER_TITLE)); - type.setTextContent("POLYGON"); - status.setTextContent("OFF"); - data.setTextContent(POLYGON_NAME); - timeToLive.setTextContent(ttl); - - layer.appendChild(model); - layer.appendChild(name); - layer.appendChild(title); - layer.appendChild(type); - layer.appendChild(status); - layer.appendChild(data); - layer.appendChild(timeToLive); - - meta.appendChild(layer); - } - - - /** - * Append isoline layer information to meta document. - * - * @param context - * @param document The meta document. - * @param meta The element where the new information need to be appended to. - * @param uuid The UUID of the current artifact. - * @param paramType The parameter type (e.g. salinity). - */ - public static void writeIsolineMeta( - CallContext context, - Document document, - Element meta, - String uuid, - String paramType - ) { - XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( - document, - ArtifactNamespaceContext.NAMESPACE_URI, - ArtifactNamespaceContext.NAMESPACE_PREFIX); - - Long time = context.getTimeToLive(); - time = time != null ? time + new Date().getTime() : null; - String ttl = time != null ? time.toString() : null; - - logger.debug("Artifacts time to live: " + ttl); - - Element layer = creator.create(LayerInfo.LAYER); - Element model = creator.create(LayerInfo.LAYER_MODEL); - Element name = creator.create(LayerInfo.LAYER_NAME); - Element title = creator.create(LayerInfo.LAYER_TITLE); - Element type = creator.create(LayerInfo.LAYER_TYPE); - Element status = creator.create(LayerInfo.LAYER_STATUS); - Element data = creator.create(LayerInfo.LAYER_DATA); - Element timeToLive = creator.create(NODE_TTL); - - model.setTextContent(paramType+"_isolines"); - name.setTextContent(uuid); - title.setTextContent( - (String) context.getContextValue(CONTEXT_LAYER_TITLE)); - type.setTextContent("LINE"); - status.setTextContent("OFF"); - data.setTextContent(ISOLINES_NAME); - timeToLive.setTextContent(ttl); - - layer.appendChild(model); - layer.appendChild(name); - layer.appendChild(title); - layer.appendChild(type); - layer.appendChild(status); - layer.appendChild(data); - layer.appendChild(timeToLive); - - meta.appendChild(layer); - } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :