view artifacts/src/main/java/org/dive4elements/river/artifacts/states/LocationSelect.java @ 9584:1a7cfeb1ff89

Punkt 10.8 WQ-Vorschau
author gernotbelger
date Wed, 09 Jan 2019 17:13:25 +0100
parents e4606eae8ea5
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.artifacts.states;

import org.apache.log4j.Logger;
import org.dive4elements.artifactdatabase.data.StateData;
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.w3c.dom.Element;

import gnu.trove.TDoubleArrayList;

/**
 * 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 {

    private static final long serialVersionUID = 1L;
    /** The log used in this class. */
    private static Logger log = Logger.getLogger(LocationSelect.class);

    public LocationSelect() {
    }

    /** UI Provider (which input method should the client provide to user. */
    @Override
    protected String getUIProvider() {
        return "location_panel";
    }

    @Override
    protected Element[] createItems(final XMLUtils.ElementCreator cr, final Artifact artifact, final String name, final CallContext context) {
        final double[] minmax = getMinMax(artifact);

        double minVal = Double.MIN_VALUE;
        double maxVal = Double.MAX_VALUE;

        if (minmax != null) {
            minVal = minmax[0];
            maxVal = minmax[1];
        } else {
            log.warn("Could not read min/max distance values!");
        }

        if (name.equals(LOCATIONS)) {
            final Element min = createItem(cr, new String[] { "min", new Double(minVal).toString() });

            final Element max = createItem(cr, new String[] { "max", new Double(maxVal).toString() });

            return new Element[] { min, max };
        }

        return null;
    }

    /** Validates data from artifact. */
    @Override
    public boolean validate(final Artifact artifact) throws IllegalArgumentException {
        log.debug("LocationSelect.validate");

        final D4EArtifact flys = (D4EArtifact) artifact;
        final StateData data = getData(flys, LOCATIONS);

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

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

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

        log.debug("Inserted min location: " + mm[0]);
        log.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(final String locationStr) {
        final String[] tmp = locationStr.split(" ");
        final TDoubleArrayList locations = new TDoubleArrayList();

        for (final String l : tmp) {
            try {
                locations.add(Double.parseDouble(l));
            }
            catch (final NumberFormatException nfe) {
                log.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