view gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java @ 745:d194939ecbaf

Improvement: Values of Type Geometry which are Points will be prepared as Regions for DB-Query. gnv-artifacts/trunk@785 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 16 Mar 2010 12:53:37 +0000
parents 452fad121560
children cd64ab8a46a7
line wrap: on
line source
package de.intevation.gnv.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import de.intevation.artifactdatabase.XMLUtils;
import de.intevation.artifacts.ArtifactNamespaceContext;
import de.intevation.artifacts.CallContext;
import de.intevation.gnv.artifacts.context.GNVArtifactContext;
import de.intevation.gnv.wms.LayerInfo;

/**
 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
 */
public class MetaWriter {

    private static Logger logger = Logger.getLogger(MetaWriter.class);

    public static final String NODE_MAPSERVER = "mapserver";
    public static final String NODE_SERVER    = "server";
    public static final String NODE_MAP       = "map";

    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 LAYER_DATA_NAME   = "data.shp";

    public static final String CONTEXT_LAYER_TITLE = "wms.title";

    private MetaWriter() {
    }
    
    public static Document writeLayerMeta(CallContext context,
                                          String      uuid,
                                          String      path,
                                          String      paramType,
                                          String      layerType){
        Document meta = XMLUtils.newDocument();
        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);
        writeLayerMeta(context, meta, root, uuid, paramType,layerType);
        boolean success = writeMetaFile(path, meta);
       
        if (success){
            return meta;
        }else{
            return null;
        }
    }

    public static Document writeHorizontalcrosssectionMeta(
        CallContext context,
        String      uuid,
        String      path,
        String      paramType)
    {
        Document meta = XMLUtils.newDocument();
        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);
        writePolygonMeta(context, meta, root, uuid, path, paramType);
        writeIsolineMeta(context, meta, root, uuid, path, paramType);

        boolean success = writeMetaFile(path, meta);

        if (success){
            return meta;
        }else{
            return null;
        }
    }

    /**
     * @param path
     * @param meta
     */
    private static boolean writeMetaFile(String path, Document meta) {
        try {
            File metaFile = new File(path, META_FILE_NAME);

            if (metaFile.exists()) {
                logger.info("Delete old meta information file.");
                metaFile.delete();
            }

            if (!metaFile.createNewFile() || !metaFile.canWrite()) {
                logger.error("Error while writing meta file: "+metaFile.toString());
                return false;
            }

            OutputStream out = null;
            boolean success = false;
            try {
                out = new FileOutputStream(metaFile);
                success = XMLUtils.toStream(meta, out);
            }
            finally {
                if (out != null) {
                    try { out.close(); }
                    catch (IOException ioe) {}
                }
            }

            if (!success && metaFile.exists()) {
                metaFile.delete();
            }

            return success;
        }
        catch (FileNotFoundException fnfe) {
            logger.error(fnfe);
            return false;
        }
        catch (IOException ioe) {
            logger.error(ioe, ioe);
            return false;
        }
    }


    public static void writeAbstractMeta(
        CallContext callContext,
        Document    document,
        Element     meta
    ) {
        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
            document,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        GNVArtifactContext context =
            (GNVArtifactContext) callContext.globalContext();

        String server = (String)
            context.get(GNVArtifactContext.MAPSERVER_SERVER_PATH_KEY);

        String map = (String)
            context.get(GNVArtifactContext.MAPSERVER_MAP_PATH_KEY);

        logger.debug("MAPSERVER PATH: " + server);
        logger.debug("MAP PATH: " + map);

        Element mapserver  = creator.create(NODE_MAPSERVER);
        Element serverPath = creator.create(NODE_SERVER);
        Element mapPath    = creator.create(NODE_MAP);

        mapPath.setTextContent(map);
        serverPath.setTextContent(server);

        mapserver.appendChild(serverPath);
        mapserver.appendChild(mapPath);
        meta.appendChild(mapserver);
    }

    protected static void writeLayerMeta(
        CallContext context,
        Document    document,
        Element     meta,
        String      uuid,
        String      paramType,
        String      layerType
    ) {
        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
            document,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Element layer  = creator.create(LayerInfo.LAYER);
        Element model  = creator.create(LayerInfo.LAYER_MODEL);
        Element name   = creator.create(LayerInfo.LAYER_NAME);
        Element type   = creator.create(LayerInfo.LAYER_TYPE);
        Element status = creator.create(LayerInfo.LAYER_STATUS);
        Element data   = creator.create(LayerInfo.LAYER_DATA);

        model.setTextContent(paramType);
        name.setTextContent(uuid);
        type.setTextContent(layerType);
        status.setTextContent("OFF");
        data.setTextContent(LAYER_DATA_NAME);

        layer.appendChild(model);
        layer.appendChild(name);
        layer.appendChild(type);
        layer.appendChild(status);
        layer.appendChild(data);

        meta.appendChild(layer);
    }
    
    
    public static void writePolygonMeta(
        CallContext context,
        Document    document,
        Element     meta,
        String      uuid,
        String      path,
        String      paramType
    ) {
        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
            document,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        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);

        model.setTextContent(paramType);
        name.setTextContent(uuid);
        title.setTextContent(
            (String) context.getContextValue(CONTEXT_LAYER_TITLE));
        type.setTextContent("POLYGON");
        status.setTextContent("OFF");
        data.setTextContent(POLYGON_NAME);

        layer.appendChild(model);
        layer.appendChild(name);
        layer.appendChild(title);
        layer.appendChild(type);
        layer.appendChild(status);
        layer.appendChild(data);

        meta.appendChild(layer);
    }


    public static void writeIsolineMeta(
        CallContext context,
        Document    document,
        Element     meta,
        String      uuid,
        String      path,
        String      paramType
    ) {
        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
            document,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        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);

        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);

        layer.appendChild(model);
        layer.appendChild(name);
        layer.appendChild(title);
        layer.appendChild(type);
        layer.appendChild(status);
        layer.appendChild(data);

        meta.appendChild(layer);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org