view gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java @ 856:c9996913ff4b

Adjusted the subtitle creation for charts in 'Horizontalen Schnittprofilen' (see issue138). gnv-artifacts/trunk@974 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 23 Apr 2010 12:26:31 +0000
parents 164d102b0af5
children 3fbabd4803d7
line wrap: on
line source
package de.intevation.gnv.state.layer;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Iterator;

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

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

import de.intevation.artifactdatabase.Config;
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.geobackend.base.Result;
import de.intevation.gnv.geobackend.base.query.QueryExecutor;
import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
import de.intevation.gnv.geobackend.base.query.exception.QueryException;
import de.intevation.gnv.state.InputData;
import de.intevation.gnv.state.OutputStateBase;
import de.intevation.gnv.state.exception.StateException;
import de.intevation.gnv.utils.ArtifactXMLUtilities;
import de.intevation.gnv.utils.ExclusiveExec;
import de.intevation.gnv.utils.FileUtils;
import de.intevation.gnv.utils.MapfileGenerator;
import de.intevation.gnv.utils.MetaWriter;
import de.intevation.gnv.utils.ShapeFileWriter;

/**
 * This <code>OutputState</code> is used for Layer-Products.
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 *
 */
public class LayerOutputState extends OutputStateBase {

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger.getLogger(LayerOutputState.class);

    /**
     * The UID of this Class.
     */
    private static final long serialVersionUID = 9180957321704424049L;

    /**
     * The Basename of the Templates for the WMS-Exports
     */
    public static final String LAYER_MODEL    = "layer";

    /**
     * The Name of the Shapefile which will be generated.
     */
    public static final String SHAPEFILE_NAME   = "data.shp";

    /**
     * The ID for the Query fetching the Layer from the DB
     */
    private String dataQueryID = null;

    /**
     * The ID for the Query fetching the Geometry from the DB
     * which should be used to Clip the Layerdata
     */
    private String geometryQueryID = null;

    /**
     * The ID of the Query for fetching the Columnnames of the Table which
     * should put into the Shapefile.
     */
    private String columnQueryID = null;

    /**
     * The ID of the Query for fetching the Information which kind of Geometry
     * should be put into the Shapefile.
     */
    private String geometryTypeQueryID = null;

    /**
     * The ID for the Value which will hold the Geometry-Value
     */
    private String geometryID = null;

    /**
     * Flag for synchronized Access of the Shapefile.
     */
    private Boolean shapeFileLock = new Boolean(true);

    /**
     * The Path where the Shapefile is stored.
     */
    private String shapeFilePath;

    /**
     * The ID of the Template which should be used to generate the Layerentry
     * in the Mapfile.
     */
    private String templateID = null;

    /**
     * The Kind of Geometry which should be used to generate the Shapefile.
     */
    private String geometryType = null;

    /**
     * Constructor
     */
    public LayerOutputState() {
        super();
    }

    public void out(Document format, Collection<InputData> inputData,
                    OutputStream outputStream, String uuid,
                    CallContext callContext) throws StateException {

        log.debug("LayerOutputState.out");
        String outputMode = XMLUtils.xpathString(
                format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE);
        if (outputMode.equalsIgnoreCase("wms")) {
            Collection<Result> data = this.fetchData();
            if (data != null && !data.isEmpty()){
                XMLUtils.toStream(this.getWMS(uuid, callContext,
                                              data, geometryType,
                                              inputData),
                                  outputStream);
            }else{
                this.writeExceptionReport2Stream(outputStream);
            }
        }else if (outputMode.equalsIgnoreCase("zip")){
            Collection<Result> data = this.fetchData();
            if (data != null && !data.isEmpty()){
                this.writeZip(uuid, callContext, outputStream,
                              data,geometryType);
            }else{
                this.writeExceptionReport2Stream(outputStream);
            }

        }
    }

    /**
     * Writes an exception to an output stream.
     *
     * @param outputStream The output stream used to write the exception to.
     */
    private void writeExceptionReport2Stream(OutputStream outputStream) {
        Document document = XMLUtils.newDocument();
        ArtifactXMLUtilities.
        createExceptionReport("No Data to Export", document);
        XMLUtils.toStream(document,outputStream);
    }


    /**
     * Fetches the Data from the Databasebackend.
     *
     * @return the resultdata.
     */
    protected Collection<Result> fetchData(){
        log.debug("LayerOutputState.fetchData");
        Collection<Result> result = this.getData(this.queryID);
        Collection<Result> data = null;
        String geometryWKT = null;
        if (result != null){
            QueryExecutor queryExecutor = QueryExecutorFactory.getInstance()
                                                              .getQueryExecutor();
            Iterator<Result> it = result.iterator();
            String[] queryValues = null;
            if (it.hasNext()){
                Result resultValue = it.next();
                String table = resultValue.getString(0);

                this.geometryType = this.getGeometryType(table, queryExecutor);

                String where = resultValue.getString(1);
                String columns = this.fetchColumns(table);

                templateID = resultValue.getString(2);
                if (this.geometryID != null){
                    InputData geometryInputData =
                         this.inputData.get(this.geometryID);
                    if (geometryInputData != null){

                        try {
                            Collection<Result> geometryData = queryExecutor
                                                      .executeQuery(this.geometryQueryID,
                                                                    new String[]{geometryInputData.getValue()});
                            Iterator<Result> git = geometryData.iterator();
                            if (git.hasNext()){
                                Result geometryValue = git.next();
                                geometryWKT = geometryValue.getString(0);
                            }
                        } catch (QueryException e) {
                            log.error(e,e);
                        }
                       queryValues = new String[]{columns,
                                                  table,
                                                  where,
                                                  geometryWKT};
                    }else{
                        //Look into the presetting for an WKT
                        InputData geometryWKTData = this.preSettings != null ?
                                                    this.preSettings.get("geometry") :
                                                    null ;
                        if (geometryWKTData != null){
                            queryValues = new String[]{columns,
                                                       table,
                                                       where,
                                                       geometryWKTData.getValue()};
                        }else{
                            queryValues = new String[]{columns,table,where};
                        }
                    }
                }else{
                    //Look into the presetting for an WKT
                    InputData geometryWKTData = this.preSettings != null ?
                                                this.preSettings.get("geometry") :
                                                null ;
                    if (geometryWKTData != null){
                        queryValues = new String[]{columns,
                                                   table,
                                                   where,
                                                   geometryWKTData.getValue()};
                    }else{
                        queryValues = new String[]{columns,table,where};
                    }
                }
            }

            try {
                data  = queryExecutor.executeQuery(dataQueryID,
                                                   queryValues);
                if (data != null && geometryWKT != null){
                    WKTReader wktReader = new WKTReader();
                    Geometry border = wktReader.read(geometryWKT);

                    Iterator<Result> dataIt = data.iterator();
                    while (dataIt.hasNext()){
                        // Trim the Geometries using the
                        // Geometry if on is available.
                        Result current = dataIt.next();
                        String currentWKT = current.getString(0);
                        Geometry currentGeometry = null;
                        try {
                            currentGeometry = wktReader.read(currentWKT);
                        } catch (Exception e) {
                            log.error("Error parsing Geometry "+ currentWKT);
                            log.error(e,e);
                        }

                        if (currentGeometry != null){
                            Geometry newGeometry = currentGeometry.intersection(border);
                            current.addColumnValue(0, newGeometry.toText());
                        }
                    }
                }
            } catch (QueryException e) {
                log.error(e,e);
            } catch (ParseException e){
                log.error(e,e);
            }
        }
        return data;
    }


    /**
     * This method determines the geometry type on basis of a table name.
     *
     * @param tableName Name of the table in the database.
     * @param queryExecutor The QueryExecutor.
     * @return the geometry type as string (e.g. MultiPolygon, Polygon, etc).
     */
    private String getGeometryType(String tableName,
                                   QueryExecutor queryExecutor){
        String returnValue = null;
        String[] tables = tableName.toUpperCase().split(",");
        String[] filter = tables[0].split("\\.");

        try {
            Collection<Result> result =
                queryExecutor.executeQuery(this.geometryTypeQueryID, filter);
            if (result != null && !result.isEmpty())
            {
                int geometryCode = result.iterator().next().getInteger(0);
                if (geometryCode == 11 ||
                    geometryCode == 10){
                    returnValue = "MultiPolygon";
                }else if (geometryCode == 9 ||
                          geometryCode == 8){
                    returnValue = "MultiLineString";
                }else if (geometryCode == 7){
                    returnValue = "MultiPoint";
                }else if (geometryCode == 6){
                    returnValue = "GeometryCollection";
                }else if (geometryCode == 5 ||
                          geometryCode == 4){
                    returnValue = "Polygon";
                }else if (geometryCode == 3 ||
                          geometryCode == 2){
                    returnValue = "LineString";
                }else if (geometryCode == 1){
                    returnValue = "Point";
                }else if (geometryCode == 0){
                    returnValue = "Geometry";
                }
            }
        } catch (QueryException e) {
            log.error(e,e);
        }

        return returnValue;
    }


    /**
     * Fetch the columns of a specific table.
     *
     * @param tableName The name of the table.
     * @return the columns as string.
     */
    private String fetchColumns(String tableName){
        String returnValue = null;
        try {
            String[] tables = tableName.toUpperCase().split(",");
            String[] filter = tables[0].split("\\.");
            // Only use the first Table the second one will be ignored.
            QueryExecutor queryExecutor = QueryExecutorFactory.getInstance()
            .getQueryExecutor();

            Collection<Result> columnData = queryExecutor.
                                                executeQuery(this.columnQueryID,
                                                             filter);
            if (columnData != null && !columnData.isEmpty()){
                StringBuffer sb = new StringBuffer();
                synchronized (sb) {
                    Iterator<Result> it = columnData.iterator();
                    while(it.hasNext()){
                        Result current = it.next();
                        sb.append(current.getString(0));
                        if (it.hasNext()){
                            sb.append(" , ");
                        }
                    }
                }
                returnValue = sb.toString();
            }

        } catch (QueryException e) {
            log.error(e,e);
        }
        return returnValue;
    }


    @Override
    public void setup(Node configuration) {
        log.debug("LayerOutputState.setup");
        super.setup(configuration);
        this.dataQueryID = Config.getStringXPath(configuration,
                                                 "queryID-layerdata");
        this.geometryID = Config.getStringXPath(configuration,
                                                "inputvalue-geometry");
        this.geometryQueryID =  Config.getStringXPath(configuration,
                                                "queryID-geometry");

        this.columnQueryID =   "layer_colums"; //Config.getStringXPath(configuration,
                                               // "queryID-columns");
        this.geometryTypeQueryID = "geometry_type";
    }


    /**
     * Write the resultdata to shapefiles.
     *
     * @param uuid The UUID of the current artifact.
     * @param data The finalized data used for shapefile creation.
     * @param callContext The CallContext object.
     * @param geometryType The geometry type.
     * @return the shapefile path.
     */
    protected String writeToShapeFile(
                                      String             uuid,
                                      Collection<Result> data,
                                      CallContext        callContext,
                                      String geometryType
                                  ) {
        File baseDir = shapefileDirectory(callContext);

        File shapeDir = new File(baseDir, uuid);
        boolean success    = false;
        boolean createdDir = false;

        try {
            synchronized (shapeFileLock) {
                if (shapeDir.exists()) {
                    FileUtils.deleteContent(shapeDir);
                }
                else if (!shapeDir.mkdirs()) {
                    log.error("cannot create directory '"
                        + shapeDir.getAbsolutePath() + "'");
                    return null;
                }
                createdDir = true;
            }

            File shapeFile = new File(shapeDir, SHAPEFILE_NAME);
            if (!ShapeFileWriter.writeDataToFile(shapeFile, "data", data,geometryType)){
                log.error("writing data into shapefile failed");
                return null;
            }

            shapeFilePath = shapeDir.getAbsolutePath();
            success = true;

            callContext.afterCall(CallContext.STORE);

            return shapeFilePath;
        }
        finally {
            if (!success && createdDir) {
                FileUtils.deleteRecursive(shapeDir);
            }
        }
    }


    /**
     * Create a zip archive with the shapefiles of the given shapefiles path and
     * write it to <code>output</code>.
     *
     * @param uuid The UUID of the current artifact.
     * @param callContext The CallContext object.
     * @param output The output stream.
     * @param data The data to be written to shapefile.
     * @param geometryType The geometry type.
     * @throws StateException if an error occured while zipfile creation.
     */
    protected void writeZip(
            String       uuid,
            CallContext  callContext,
            OutputStream output,
            Collection<Result> data,
            String geometryType
        )
        throws StateException
        {
            try {
                String p = getShapeFilePath();
                if (p != null) {
                    File dir = new File(p);
                    if (dir.isDirectory()) {
                        FileUtils.createZipArchive(dir, output);
                    }
                }
                else {

                    if ((p = writeToShapeFile(uuid, data, callContext,geometryType)) != null) {
                        FileUtils.createZipArchive(new File(p), output);
                    }
                }
            }
            catch (IOException ioe) {
                log.error(ioe.getLocalizedMessage(), ioe);
            }
        }

    /**
     * Returns the shapefile path.
     *
     * @return the shapefile path.
     */
    public String getShapeFilePath() {
        synchronized (shapeFileLock) {
            return shapeFilePath;
        }
    }


    private static File shapefileDirectory(CallContext callContext) {
        // TODO: Refactoring nessessary it should be used only one Shapefilepath
        //       for alle Modes. Code was taken from HorizontalCrossSectionMeshOutputState
        GNVArtifactContext context =
            (GNVArtifactContext)callContext.globalContext();
        File dir = (File)context.get(
            GNVArtifactContext.HORIZONTAL_CROSS_SECTION_RESULT_SHAPEFILE_PATH_KEY);
        return dir != null
            ? dir
            : GNVArtifactContext.DEFAULT_HORIZONTAL_CROSS_SECTION_PROFILE_SHAPEFILE_PATH;
    }


    @Override
    public void endOfLife(Object globalContext) {
        super.endOfLife(globalContext);

        // do it in background
        new Thread() {
            @Override
            public void run() {
                String path = resetShapeFilePath();

                if (path == null) {
                    return;
                }

                File dir = new File(path);

                for (int i = 0; i < 10; ++i) {
                    if (!dir.exists() || FileUtils.deleteRecursive(dir)) {
                        MapfileGenerator.getInstance().update();
                        return;
                    }

                    try {
                        Thread.sleep(10000L);
                    }
                    catch (InterruptedException ie) {
                    }
                }

                log.error("failed to remove directory '" + path + "'");
            } // run
        }.start();
    }

    /**
     * Resets the Settings e.g shapeFilePath and templateID
     * @return
     */
    private String resetShapeFilePath() {
        synchronized (shapeFileLock) {
            String path = shapeFilePath;
            shapeFilePath = null;
            templateID = null;
            return path;
        }
    }


    /**
     * Write data to shapefiles and feed a map service with information about
     * these shapefiles. The map service can be queried for displaying
     * corresponding layers as WMS.
     *
     * @param uuid The UUID of the current artifact.
     * @param callContext The CallContext object.
     * @param data A collection with some input data.
     * @param geometryType The geometry type.
     * @param inputData the Parameters which are send by the out-Call.
     * @return a document with some meta information (shapefile path, geometry
     * type, time to live of the current artifact, etc).
     * @throws StateException if an error occured while shapefile writing.
     */
    protected Document getWMS(String uuid,
                              CallContext callContext,
                              Collection<Result> data,
                              String geometryType,
                              Collection<InputData> inputData)
    throws StateException
    {
        Document document = XMLUtils.newDocument();

        Element pathElement = document.createElement("path");
        document.appendChild(pathElement);

        String path = getShapeFilePath();

        if (path != null && new File(path).isDirectory()) {
            String title = getLayerTitle(inputData);
            if (title == null) {
                title = uuid;
            }

            callContext.putContextValue(
                MetaWriter.CONTEXT_LAYER_TITLE, title);

            String paramType = findParameterTitle(geometryType);

            if (log.isDebugEnabled()) {
                log.debug("Layer title: " + title);
                log.debug("Layer type: " + paramType);
            }

            Document meta = MetaWriter.writeLayerMeta(callContext, uuid,
                                                        path, paramType,
                                                        this.determineGeometryType(geometryType),
                                                        SHAPEFILE_NAME,
                                                        title);
            if (meta != null) {
                MapfileGenerator.getInstance().update();
                return meta;
            }

            pathElement.setTextContent(path);
        }
        else {
            ExclusiveExec.UniqueKey key = ExclusiveExec.INSTANCE.acquire(uuid);
            try{
                if (data != null &&
                    (path = writeToShapeFile(uuid, data, callContext,geometryType)) != null) {
                    String paramType = findParameterTitle(geometryType);
                    String    title      = getLayerTitle(inputData);
                    if (title == null) {
                        title = uuid;
                    }
                    Document meta = MetaWriter.writeLayerMeta(callContext, uuid,
                                                              path, paramType,
                                                              this.determineGeometryType(geometryType),
                                                              SHAPEFILE_NAME,
                                                              title);
                    if (meta != null) {
                        MapfileGenerator.getInstance().update();
                        return meta;
                    }
                    pathElement.setTextContent(path);
                }
            }finally{
                ExclusiveExec.INSTANCE.release(key);
            }
        }

        return document;
    }

    /**
     * Returns the parameterType for the Layer.
     * @param geometryType
     * @return
     */
    private String findParameterTitle(String geometryType) {
        String paramType = LAYER_MODEL+"_"+templateID;

        if (!MapfileGenerator.getInstance().templateExists(paramType)){
            // If the template doesn't exist the Defaulttemplates will be used.
            paramType = LAYER_MODEL+"_"+
                        this.determineDefaultTemplateName(geometryType);
        }
        return paramType;
    }

    /**
     * Find the title for a wms layer specified by the user.
     *
     * @param inputData A collection with InputData objects.
     * @return the title.
     */
    protected String getLayerTitle(Collection<InputData> inputData) {
        for (InputData data: inputData) {
            String name = data.getName();
            if (name != null && name.equals("title")) {
                return (String) data.getValue();
            }
        }
        return null;
    }


    /**
     * Turns the geometry type into a form used for the templating mechanism
     * while mapfile generation.
     *
     * @param geometryType The original geometry type.
     * @return a valid geometry type fpr the template mechanism.
     */
    private String determineGeometryType(String geometryType){

        String returnValue = geometryType.toLowerCase();

        if (returnValue.equalsIgnoreCase("linestring")){
            returnValue = "Line";
        }else if (returnValue.equalsIgnoreCase("multilinestring")){
            returnValue ="Line";
        }else if (returnValue.equalsIgnoreCase("multipolygon")){
            returnValue = "Polygon";
        }
        return returnValue;
    }


    /**
     * Determine the default template name if no special template is existing
     * for this layer.
     *
     * @param geometryType The geometry type.
     * @return a default geometry fitting to the original geometry type.
     */
    private String determineDefaultTemplateName(String geometryType){

        String returnValue = geometryType.toLowerCase();

        if (returnValue.equalsIgnoreCase("multilinestring")){
            returnValue ="linestring";
        }else if (returnValue.equalsIgnoreCase("multipolygon")){
            returnValue = "polygon";
        }else if (returnValue.equalsIgnoreCase("multipoint")){
            returnValue = "point";
        }
        return returnValue;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org