view gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java @ 788:e8a9e84a0328

Corrected JavaDoc errors in Chart Package. gnv-artifacts/trunk@870 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 31 Mar 2010 08:01:56 +0000
parents c4156275c1e1
children 2cea76f1112e
line wrap: on
line source
package de.intevation.gnv.utils;

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;

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

import java.util.Date;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
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 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 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);

        if (logger.isDebugEnabled()) {
            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 callContext,
        Document    document,
        Element     meta,
        String      uuid,
        String      paramType,
        String      layerType
    ) {
        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 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(LAYER_DATA_NAME);
        timeToLive.setTextContent(ttl);

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

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

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


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

        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 :

http://dive4elements.wald.intevation.org