view flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java @ 1651:a7def20539fb

flys/issue317: Removed dependency from cross section demo app. flys-artifacts/trunk@2840 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 27 Sep 2011 13:45:13 +0000
parents f58be0d661b9
children 8d08f6641372
line wrap: on
line source
package de.intevation.flys.utils;

import org.apache.log4j.Logger;
 
import java.util.HashMap;
import java.util.Map;

import javax.xml.xpath.XPathConstants;

import org.w3c.dom.Document;

import gnu.trove.TDoubleArrayList;

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

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

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.RiverFactory;
import de.intevation.flys.model.River;

public class FLYSUtils {

    /** The logger that is used in this utility. */
    private static Logger logger = Logger.getLogger(FLYSUtils.class);

    public static enum KM_MODE { RANGE, LOCATIONS, NONE };

    public static final String XPATH_RIVER_PROJECTION =
        "/artifact-database/floodmap/river[@name=$name]/srid/@value";

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

    public static final String XPATH_VELOCITY_LOGFILE =
        "/artifact-database/floodmap/velocity/logfile/@path";

    public static final String XPATH_MAPSERVER_URL =
        "/artifact-database/floodmap/mapserver/server/@path";

    public static final String XPATH_MAPFILE_PATH =
        "/artifact-database/floodmap/mapserver/mapfile/@path";

    public static final String XPATH_MAPFILE_TEMPLATE =
        "/artifact-database/floodmap/mapserver/map-template/@path";

    public static final String XPATH_MAPSERVER_TEMPLATE_PATH =
        "/artifact-database/floodmap/mapserver/templates/@path";


    private FLYSUtils() {
    }


    public static FLYSArtifact getArtifact(String uuid, CallContext context) {
        try {
            Artifact artifact = context.getDatabase().getRawArtifact(uuid);

            if (artifact == null) {
                logger.error("Artifact '" + uuid + "' does not exist.");
                return null;
            }

            if (!(artifact instanceof FLYSArtifact)) {
                logger.error("Artifact '" +uuid+ "' is no valid FLYSArtifact.");
                return null;
            }

            return (FLYSArtifact) artifact;
        }
        // TODO: catch more selective
        catch (Exception e) {
            logger.error("Cannot get FLYSArtifact " + uuid + " from database (" + e.getMessage() + ").");
            return null;
        }
    }


    /**
     * Convinience function to retrieve an XPath as string with replaced config
     * directory.
     *
     * @param xpath The XPath expression.
     *
     * @return a string with replaced config directory.
     */
    public static String getXPathString(String xpath) {
        String tmp = Config.getStringXPath(xpath);
        tmp        = Config.replaceConfigDir(tmp);

        return tmp;
    }


    public static KM_MODE getKmRangeMode(FLYSArtifact flys) {
        String mode = flys.getDataAsString("ld_mode");

        if (mode == null || mode.length() == 0) {
            return KM_MODE.NONE;
        }
        else if (mode.equals("distance"))  {
            return KM_MODE.RANGE;
        }
        else if (mode.equals("locations")) {
            return KM_MODE.LOCATIONS;
        }
        else {
            return KM_MODE.NONE;
        }
    }


    public static double[] getKmRange(FLYSArtifact flys) {
        switch (getKmRangeMode(flys)) {
            case RANGE: {
                return getKmFromTo(flys);
            }

            case LOCATIONS: {
                double[] locs = getLocations(flys);
                return new double[] { locs[0], locs[locs.length-1] };
            }

            case NONE: {
                double[] locs = getLocations(flys);
                if (locs != null) {
                    return new double[] { locs[0], locs[locs.length-1] };
                }
                else {
                    return getKmFromTo(flys);
                }
            }
        }

        return new double[] { Double.NaN, Double.NaN };
    }


    public static double[] getKmFromTo(FLYSArtifact flys) {
        String strFrom = flys.getDataAsString("ld_from");
        String strTo   = flys.getDataAsString("ld_to");

        if (strFrom == null || strTo == null) {
            return null;
        }

        try {
            return new double[] {
                Double.parseDouble(strFrom),
                Double.parseDouble(strTo) };
        }
        catch (NumberFormatException nfe) {
            return null;
        }
    }


    public static double[] getLocations(FLYSArtifact flys) {
        String locationStr = flys.getDataAsString("ld_locations");

        if (locationStr == null || locationStr.length() == 0) {
            return null;
        }

        String[] tmp               = locationStr.split(" ");
        TDoubleArrayList locations = new TDoubleArrayList();

        for (String l: tmp) {
            try {
                locations.add(Double.parseDouble(l));
            }
            catch (NumberFormatException nfe) {
            }
        }

        locations.sort();

        return locations.toNativeArray();
    }


    /**
     * Returns the selected River object based on the 'river' data that might
     * have been inserted by the user.
     *
     * @return the selected River or null if no river has been chosen yet.
     */
    public static River getRiver(FLYSArtifact flys) {
        String sRiver = flys.getDataAsString("river");

        return (sRiver != null)
            ? RiverFactory.getRiver(sRiver)
            : null;
    }


    /**
     * Extracts the SRID defined in the global configuration for the river
     * specified in <i>artifact</i>.
     *
     * @param artifact The FLYSArtifact that stores the name of the river.
     *
     * @return the SRID as string (e.g. "31466").
     */
    public static String getRiverSrid(FLYSArtifact artifact) {
        String river = artifact.getDataAsString("river");

        if (river == null || river.length() == 0) {
            return null;
        }

        Map<String, String> variables = new HashMap<String, String>(1);
        variables.put("name", river);

        Document cfg = Config.getConfig();

        return (String) XMLUtils.xpath(
            cfg,
            XPATH_RIVER_PROJECTION,
            XPathConstants.STRING,
            null,
            variables);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org