view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java @ 1096:9f88cc54570c

Set some WSPLGENJob parameters which are stored in FLYSArtifact in the Floodmap state. flys-artifacts/trunk@2599 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 26 Aug 2011 15:01:20 +0000
parents 7230e087ef8b
children e9f66d63bdd0
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;

import de.intevation.artifacts.common.utils.Config;
import de.intevation.artifacts.common.utils.FileTools;

import de.intevation.artifactdatabase.state.Facet;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.WSPLGENFacet;
import de.intevation.flys.artifacts.model.WSPLGENJob;
import de.intevation.flys.utils.FLYSUtils;


public class FloodMapState
extends      DefaultState
implements   FacetTypes
{
    /** The logger that is used in this state.*/
    private static Logger logger = Logger.getLogger(FloodMapState.class);


    public static final String XPATH_SHAPEFILE_DIR =
        "/artifact-database/floodmap/shapefile-path/@value";

    public static final String WSPLGEN_PARAMETER_FILE = "wsplgen.par";

    public static final int WSPLGEN_DEFAULT_OUTPUT = 0;



    @Override
    public Object computeAdvance(
        FLYSArtifact artifact,
        String       hash,
        CallContext  context,
        List<Facet>  facets,
        Object       old
    ) {
        logger.debug("FloodMapState.computeAdvance");

        File artifactDir = getDirectory(artifact);

        if (artifactDir == null) {
            logger.error("Could not create directory for WSPLGEN results!");
            return null;
        }

        WSPLGENJob job = prepareWSPLGENJob(artifact, artifactDir);

        if (job == null) {
            removeDirectory(artifact);

            logger.error("No WSPLGEN processing has been started!");

            return null;
        }

        facets.add(new WSPLGENFacet(0, FLOODMAP_WSPLGEN, "WSPLGEN"));

        //context.afterCall(CallContext.BACKGROUND);

        return null;
    }


    /**
     * Returns the shapefile path defined in the configuration.
     *
     * @return the shapefile path.
     */
    protected String getShapefilePath() {
        String shapePath = (String) Config.getXPath(
            XPATH_SHAPEFILE_DIR, XPathConstants.STRING);
        shapePath = Config.replaceConfigDir(shapePath);

        return shapePath;
    }


    /**
     * Returns (and creates if not existing) the directory for storing WSPLEN
     * data for the owner artifact.
     *
     * @param artifact The owner Artifact.
     *
     * @return the directory for WSPLEN data.
     */
    protected File getDirectory(FLYSArtifact artifact) {
        String shapePath = getShapefilePath();

        File artifactDir = FileTools.getDirectory(
            shapePath, artifact.identifier());

        return artifactDir;
    }


    /**
     * Removes the directory and all its content where the required data and the
     * results of WSPLGEN are stored. Should be called in endOfLife().
     */
    protected void removeDirectory(FLYSArtifact artifact) {
        String shapePath    = getShapefilePath();
        File   artifactDir = new File(shapePath, artifact.identifier());

        if (artifactDir.exists()) {
            logger.info("Delete directory: " + artifactDir.getAbsolutePath());
            boolean success = FileTools.deleteRecursive(artifactDir);
        }
        else {
            logger.debug("There is no directory to remove.");
        }
    }


    @Override
    public void endOfLife(Artifact artifact, Object callContext) {
        logger.info("FloodMapState.endOfLife: " + artifact.identifier());

        FLYSArtifact flys = (FLYSArtifact) artifact;
        removeDirectory(flys);
    }


    protected WSPLGENJob prepareWSPLGENJob(
        FLYSArtifact artifact,
        File         artifactDir
    ) {
        logger.debug("FloodMapState.prepareWSPLGENJob");

        WSPLGENJob job = new WSPLGENJob();
        File  paraFile = new File(artifactDir, WSPLGEN_PARAMETER_FILE);

        setOut(artifact, job);
        setRange(artifact, job);
        setDelta(artifact, job);
        setGel(artifact, job);
        setDist(artifact, job);

        // TODO
        // setDgm(artifact, job);    // SHP
        // setPro(artifact, job);    // SHP
        // setWsp(artifact, job);    // WSP
        // setWspTag(artifact, job);
        // setLine(artifact, job);   // SHP
        // setAxis(artifact, job);   // SHP
        // setArea(artifact, job);   // SHP
        // setOutFile(artifact, job);

        try {
            job.toFile(paraFile);

            return job;
        }
        catch (IOException ioe) {
            logger.warn("Cannot write PAR file: " + ioe.getMessage());
        }
        catch (IllegalArgumentException iae) {
            logger.warn("Cannot write PAR file: " + iae.getMessage());
        }

        return null;
    }


    protected void setOut(FLYSArtifact artifact, WSPLGENJob job) {
        job.setOut(WSPLGEN_DEFAULT_OUTPUT);
    }


    protected void setRange(FLYSArtifact artifact, WSPLGENJob job) {
        double[] range = FLYSUtils.getKmRange(artifact);

        job.setStart(range[0]);
        job.setEnd(range[1]);
    }


    protected void setDelta(FLYSArtifact artifact, WSPLGENJob job) {
        String from = artifact.getDataAsString("diff_from");
        String to   = artifact.getDataAsString("diff_to");
        String diff = artifact.getDataAsString("diff_diff");

        try {
            job.setFrom(Double.parseDouble(from));
        }
        catch (NumberFormatException nfe) {
        }

        try {
            job.setTo(Double.parseDouble(to));
        }
        catch (NumberFormatException nfe) {
        }

        try {
            job.setDiff(Double.parseDouble(diff));
        }
        catch (NumberFormatException nfe) {
        }
    }


    protected void setGel(FLYSArtifact artifact, WSPLGENJob job) {
        String gel = artifact.getDataAsString("use_floodplain");

        if (gel != null && gel.length() > 0) {
            boolean use = Boolean.parseBoolean(gel);

            if (use) {
                job.setGel(WSPLGENJob.GEL_SPERRE);
            }
            else {
                job.setGel(WSPLGENJob.GEL_NOSPERRE);
            }
        }
    }


    protected void setDist(FLYSArtifact artifact, WSPLGENJob job) {
        String dist = artifact.getDataAsString("profile_distance");

        try {
            job.setDist(Double.parseDouble(dist));
        }
        catch (NumberFormatException nfe) {
            // nothing to do here
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org