ingo@647: package de.intevation.gnv.utils; ingo@647: sascha@779: import de.intevation.artifactdatabase.XMLUtils; sascha@779: sascha@779: import de.intevation.artifacts.ArtifactNamespaceContext; sascha@779: import de.intevation.artifacts.CallContext; sascha@779: sascha@779: import de.intevation.gnv.artifacts.context.GNVArtifactContext; sascha@779: sascha@779: import de.intevation.gnv.wms.LayerInfo; sascha@779: 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; sascha@779: ingo@772: import java.util.Date; ingo@647: ingo@647: import org.apache.log4j.Logger; sascha@779: ingo@647: import org.w3c.dom.Document; ingo@647: import org.w3c.dom.Element; ingo@647: ingo@647: /** sascha@780: * @author Ingo Weinzierl 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@772: public static final String NODE_TTL = "ttl"; 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: } sascha@778: 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); sascha@778: tim@655: writeAbstractMeta(context, meta, root); tim@655: writeLayerMeta(context, meta, root, uuid, paramType,layerType); tim@655: boolean success = writeMetaFile(path, meta); sascha@778: 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@772: if (logger.isDebugEnabled()) { ingo@772: logger.debug("MAPSERVER PATH: " + server); ingo@772: logger.debug("MAP PATH: " + map); ingo@772: } 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( ingo@772: CallContext callContext, 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: ingo@772: Long time = callContext.getTimeToLive(); ingo@773: time = time != null ? time + new Date().getTime() : null; ingo@772: String ttl = time != null ? time.toString() : null; ingo@772: ingo@772: logger.debug("Artifacts time to live: " + ttl); ingo@772: ingo@772: Element layer = creator.create(LayerInfo.LAYER); ingo@772: Element model = creator.create(LayerInfo.LAYER_MODEL); ingo@772: Element name = creator.create(LayerInfo.LAYER_NAME); ingo@772: Element type = creator.create(LayerInfo.LAYER_TYPE); ingo@772: Element status = creator.create(LayerInfo.LAYER_STATUS); ingo@772: Element data = creator.create(LayerInfo.LAYER_DATA); ingo@772: Element timeToLive = creator.create(NODE_TTL); 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); ingo@772: timeToLive.setTextContent(ttl); 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); ingo@772: layer.appendChild(timeToLive); tim@655: tim@655: meta.appendChild(layer); tim@655: } sascha@778: sascha@778: 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@772: Long time = context.getTimeToLive(); ingo@773: time = time != null ? time + new Date().getTime() : null; ingo@772: String ttl = time != null ? time.toString() : null; ingo@772: ingo@772: logger.debug("Artifacts time to live: " + ttl); ingo@772: 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@772: Element timeToLive = creator.create(NODE_TTL); 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@772: timeToLive.setTextContent(ttl); 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@772: layer.appendChild(timeToLive); 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@772: Long time = context.getTimeToLive(); ingo@773: time = time != null ? time + new Date().getTime() : null; ingo@772: String ttl = time != null ? time.toString() : null; ingo@772: ingo@772: logger.debug("Artifacts time to live: " + ttl); ingo@772: 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@772: Element timeToLive = creator.create(NODE_TTL); 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@772: timeToLive.setTextContent(ttl); 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@772: layer.appendChild(timeToLive); 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 :