view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationSelect.java @ 658:ed7c901ee712

If Artifact.feed() fails do not store invalid values in database. flys-artifacts/trunk@2062 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 07 Jun 2011 11:43:30 +0000
parents 929137ee8154
children 9a035ef7b595
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import gnu.trove.TDoubleArrayList;

import org.apache.log4j.Logger;

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

import de.intevation.artifactdatabase.data.StateData;

import de.intevation.flys.artifacts.FLYSArtifact;


/**
 * This state is used to realize the input of multiple locations as string. The
 * string should be a whitespace separated list of double values where each
 * double value represents a location.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class LocationSelect extends LocationDistanceSelect {

    /** The logger used in this class.*/
    private static Logger logger = Logger.getLogger(LocationSelect.class);


    /** The name of the StateData object that stores the location string.*/
    public static final String FIELD_LOCATIONS = "ld_locations";


    public LocationSelect() {
    }


    @Override
    protected String getUIProvider() {
        return "location_panel";
    }


    @Override
    public boolean validate(Artifact artifact, CallContext context)
    throws IllegalArgumentException
    {
        logger.debug("LocationSelect.validate");

        FLYSArtifact flys = (FLYSArtifact) artifact;
        StateData    data = getData(flys, FIELD_LOCATIONS);

        String locationStr = data != null ? (String) data.getValue() : null;

        if (locationStr == null || locationStr.length() == 0) {
            logger.error("No locations given.");
            throw new IllegalArgumentException("error_empty_state");
        }

        double[] minmax = getMinMaxDistance(artifact);
        double[] mm     = extractLocations(locationStr);

        logger.debug("Inserted min location: " + mm[0]);
        logger.debug("Inserted max location: " + mm[mm.length-1]);

        return validateBounds(minmax[0], minmax[1], mm[0], mm[mm.length-1], 0d);
    }


    /**
     * This method takes a string that consist of whitespace separated double
     * values and returns the double values as array.
     *
     * @param locationStr The locations inserted in this state.
     *
     * @return the locations as array.
     */
    protected double[] extractLocations(String locationStr) {
        String[] tmp               = locationStr.split(" ");
        TDoubleArrayList locations = new TDoubleArrayList();

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

        locations.sort();

        return locations.toNativeArray();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org