ingo@647: package de.intevation.gnv.utils; ingo@647: ingo@647: import java.io.File; ingo@647: import java.io.FileNotFoundException; ingo@647: import java.io.FileOutputStream; ingo@647: import java.io.IOException; ingo@647: import java.io.OutputStream; ingo@647: ingo@647: import org.apache.log4j.Logger; ingo@647: import org.w3c.dom.Document; ingo@647: import org.w3c.dom.Element; ingo@647: tim@655: import de.intevation.artifactdatabase.XMLUtils; tim@655: import de.intevation.artifacts.ArtifactNamespaceContext; tim@655: import de.intevation.artifacts.CallContext; tim@655: import de.intevation.gnv.artifacts.context.GNVArtifactContext; tim@655: import de.intevation.gnv.wms.LayerInfo; tim@655: ingo@647: /** ingo@647: * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) ingo@647: */ ingo@647: public class MetaWriter { ingo@647: ingo@647: private static Logger logger = Logger.getLogger(MetaWriter.class); ingo@647: ingo@647: public static final String NODE_MAPSERVER = "mapserver"; ingo@647: public static final String NODE_SERVER = "server"; ingo@647: public static final String NODE_MAP = "map"; ingo@647: ingo@647: public static final String META_FILE_NAME = "meta.xml"; ingo@647: public static final String ISOLINES_NAME = "isolines.shp"; ingo@647: public static final String POLYGON_NAME = "polygons.shp"; tim@655: public static final String LAYER_DATA_NAME = "data.shp"; ingo@647: ingo@730: public static final String CONTEXT_LAYER_TITLE = "wms.title"; ingo@730: ingo@647: private MetaWriter() { ingo@647: } tim@655: tim@655: public static Document writeLayerMeta(CallContext context, tim@655: String uuid, tim@655: String path, tim@655: String paramType, tim@655: String layerType){ tim@655: Document meta = XMLUtils.newDocument(); tim@655: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( tim@655: meta, tim@655: ArtifactNamespaceContext.NAMESPACE_URI, tim@655: ArtifactNamespaceContext.NAMESPACE_PREFIX); tim@655: Element root = creator.create("meta"); tim@655: meta.appendChild(root); tim@655: tim@655: writeAbstractMeta(context, meta, root); tim@655: writeLayerMeta(context, meta, root, uuid, paramType,layerType); tim@655: boolean success = writeMetaFile(path, meta); tim@655: tim@655: if (success){ tim@655: return meta; tim@655: }else{ tim@655: return null; tim@655: } tim@655: } ingo@647: ingo@647: public static Document writeHorizontalcrosssectionMeta( ingo@647: CallContext context, ingo@647: String uuid, ingo@647: String path, ingo@647: String paramType) ingo@647: { ingo@647: Document meta = XMLUtils.newDocument(); ingo@647: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@647: meta, ingo@647: ArtifactNamespaceContext.NAMESPACE_URI, ingo@647: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@647: ingo@647: Element root = creator.create("meta"); ingo@647: meta.appendChild(root); ingo@647: ingo@647: writeAbstractMeta(context, meta, root); ingo@647: writePolygonMeta(context, meta, root, uuid, path, paramType); ingo@647: writeIsolineMeta(context, meta, root, uuid, path, paramType); ingo@647: tim@655: boolean success = writeMetaFile(path, meta); tim@655: tim@655: if (success){ tim@655: return meta; tim@655: }else{ tim@655: return null; tim@655: } tim@655: } tim@655: tim@655: /** tim@655: * @param path tim@655: * @param meta tim@655: */ tim@655: private static boolean writeMetaFile(String path, Document meta) { ingo@647: try { ingo@647: File metaFile = new File(path, META_FILE_NAME); ingo@647: ingo@730: if (metaFile.exists()) { ingo@730: logger.info("Delete old meta information file."); ingo@730: metaFile.delete(); ingo@730: } ingo@730: ingo@647: if (!metaFile.createNewFile() || !metaFile.canWrite()) { ingo@647: logger.error("Error while writing meta file: "+metaFile.toString()); tim@655: return false; ingo@647: } ingo@647: ingo@647: OutputStream out = null; ingo@647: boolean success = false; ingo@647: try { ingo@647: out = new FileOutputStream(metaFile); ingo@647: success = XMLUtils.toStream(meta, out); ingo@647: } ingo@647: finally { ingo@647: if (out != null) { ingo@647: try { out.close(); } ingo@647: catch (IOException ioe) {} ingo@647: } ingo@647: } ingo@647: ingo@647: if (!success && metaFile.exists()) { ingo@647: metaFile.delete(); ingo@647: } ingo@647: tim@655: return success; ingo@647: } ingo@647: catch (FileNotFoundException fnfe) { ingo@647: logger.error(fnfe); tim@655: return false; ingo@647: } ingo@647: catch (IOException ioe) { ingo@647: logger.error(ioe, ioe); tim@655: return false; ingo@647: } ingo@647: } ingo@647: ingo@647: ingo@647: public static void writeAbstractMeta( ingo@647: CallContext callContext, ingo@647: Document document, ingo@647: Element meta ingo@647: ) { ingo@647: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@647: document, ingo@647: ArtifactNamespaceContext.NAMESPACE_URI, ingo@647: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@647: ingo@647: GNVArtifactContext context = ingo@647: (GNVArtifactContext) callContext.globalContext(); ingo@647: ingo@647: String server = (String) ingo@647: context.get(GNVArtifactContext.MAPSERVER_SERVER_PATH_KEY); ingo@647: ingo@647: String map = (String) ingo@647: context.get(GNVArtifactContext.MAPSERVER_MAP_PATH_KEY); ingo@647: ingo@647: logger.debug("MAPSERVER PATH: " + server); ingo@647: logger.debug("MAP PATH: " + map); ingo@647: ingo@647: Element mapserver = creator.create(NODE_MAPSERVER); ingo@647: Element serverPath = creator.create(NODE_SERVER); ingo@647: Element mapPath = creator.create(NODE_MAP); ingo@647: ingo@647: mapPath.setTextContent(map); ingo@647: serverPath.setTextContent(server); ingo@647: ingo@647: mapserver.appendChild(serverPath); ingo@647: mapserver.appendChild(mapPath); ingo@647: meta.appendChild(mapserver); ingo@647: } ingo@647: tim@655: protected static void writeLayerMeta( tim@655: CallContext context, tim@655: Document document, tim@655: Element meta, tim@655: String uuid, tim@655: String paramType, tim@655: String layerType tim@655: ) { tim@655: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( tim@655: document, tim@655: ArtifactNamespaceContext.NAMESPACE_URI, tim@655: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@647: tim@655: Element layer = creator.create(LayerInfo.LAYER); tim@655: Element model = creator.create(LayerInfo.LAYER_MODEL); tim@655: Element name = creator.create(LayerInfo.LAYER_NAME); tim@655: Element type = creator.create(LayerInfo.LAYER_TYPE); tim@655: Element status = creator.create(LayerInfo.LAYER_STATUS); tim@655: Element data = creator.create(LayerInfo.LAYER_DATA); tim@655: tim@655: model.setTextContent(paramType); tim@655: name.setTextContent(uuid); tim@655: type.setTextContent(layerType); tim@655: status.setTextContent("OFF"); tim@655: data.setTextContent(LAYER_DATA_NAME); tim@655: tim@655: layer.appendChild(model); tim@655: layer.appendChild(name); tim@655: layer.appendChild(type); tim@655: layer.appendChild(status); tim@655: layer.appendChild(data); tim@655: tim@655: meta.appendChild(layer); tim@655: } tim@655: tim@655: ingo@647: public static void writePolygonMeta( ingo@647: CallContext context, ingo@647: Document document, ingo@647: Element meta, ingo@647: String uuid, ingo@647: String path, ingo@647: String paramType ingo@647: ) { ingo@647: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@647: document, ingo@647: ArtifactNamespaceContext.NAMESPACE_URI, ingo@647: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@647: ingo@647: Element layer = creator.create(LayerInfo.LAYER); ingo@647: Element model = creator.create(LayerInfo.LAYER_MODEL); ingo@647: Element name = creator.create(LayerInfo.LAYER_NAME); ingo@730: Element title = creator.create(LayerInfo.LAYER_TITLE); ingo@647: Element type = creator.create(LayerInfo.LAYER_TYPE); ingo@647: Element status = creator.create(LayerInfo.LAYER_STATUS); ingo@647: Element data = creator.create(LayerInfo.LAYER_DATA); ingo@647: ingo@647: model.setTextContent(paramType); ingo@647: name.setTextContent(uuid); ingo@730: title.setTextContent( ingo@730: (String) context.getContextValue(CONTEXT_LAYER_TITLE)); ingo@647: type.setTextContent("POLYGON"); tim@655: status.setTextContent("OFF"); ingo@647: data.setTextContent(POLYGON_NAME); ingo@647: ingo@647: layer.appendChild(model); ingo@647: layer.appendChild(name); ingo@730: layer.appendChild(title); ingo@647: layer.appendChild(type); ingo@647: layer.appendChild(status); ingo@647: layer.appendChild(data); ingo@647: ingo@647: meta.appendChild(layer); ingo@647: } ingo@647: ingo@647: ingo@647: public static void writeIsolineMeta( ingo@647: CallContext context, ingo@647: Document document, ingo@647: Element meta, ingo@647: String uuid, ingo@647: String path, ingo@647: String paramType ingo@647: ) { ingo@647: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@647: document, ingo@647: ArtifactNamespaceContext.NAMESPACE_URI, ingo@647: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@647: ingo@647: Element layer = creator.create(LayerInfo.LAYER); ingo@647: Element model = creator.create(LayerInfo.LAYER_MODEL); ingo@647: Element name = creator.create(LayerInfo.LAYER_NAME); ingo@730: Element title = creator.create(LayerInfo.LAYER_TITLE); ingo@647: Element type = creator.create(LayerInfo.LAYER_TYPE); ingo@647: Element status = creator.create(LayerInfo.LAYER_STATUS); ingo@647: Element data = creator.create(LayerInfo.LAYER_DATA); ingo@647: ingo@647: model.setTextContent(paramType+"_isolines"); ingo@647: name.setTextContent(uuid); ingo@730: title.setTextContent( ingo@730: (String) context.getContextValue(CONTEXT_LAYER_TITLE)); ingo@647: type.setTextContent("LINE"); tim@655: status.setTextContent("OFF"); ingo@647: data.setTextContent(ISOLINES_NAME); ingo@647: ingo@647: layer.appendChild(model); ingo@647: layer.appendChild(name); ingo@730: layer.appendChild(title); ingo@647: layer.appendChild(type); ingo@647: layer.appendChild(status); ingo@647: layer.appendChild(data); ingo@647: ingo@647: meta.appendChild(layer); ingo@647: } ingo@647: } ingo@647: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :