view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java @ 750:b24be063f8d2

Tagged RELEASE 2.4 of flys-artifacts. flys-artifacts/tags/2.4@2262 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 28 Jun 2011 09:56:52 +0000
parents c09c9e05ecfa
children 610d0e0f4f85
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import java.util.List;

import org.apache.log4j.Logger;

import org.w3c.dom.Element;

import gnu.trove.TDoubleArrayList;

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

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

import de.intevation.artifactdatabase.ProtocolUtils;
import de.intevation.artifactdatabase.data.StateData;
import de.intevation.artifactdatabase.state.Facet;

import de.intevation.flys.model.River;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.WINFOArtifact;

import de.intevation.flys.artifacts.model.RiverFactory;
import de.intevation.flys.artifacts.model.WQKms;
import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.WaterlevelFacet;
import de.intevation.flys.artifacts.model.FacetTypes;

import de.intevation.flys.artifacts.resources.Resources;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class LocationDistanceSelect
extends      RangeState
implements   FacetTypes
{

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


    /** The default step width.*/
    public static final String DEFAULT_STEP = "100";

    /** The name of the 'mode' field. */
    public static final String MODE = "ld_mode";

    /** The name of the 'locations' field.*/
    public static final String LOCATIONS = "ld_locations";

    /** The name of the 'from' field. */
    public static final String FROM = "ld_from";

    /** The name of the 'to' field. */
    public static final String TO = "ld_to";

    /** The name of the 'step' field. */
    public static final String STEP = "ld_step";

    /**
     * The default constructor that initializes an empty State object.
     */
    public LocationDistanceSelect() {
    }

    protected Element createData(
        XMLUtils.ElementCreator cr,
        Artifact    artifact,
        StateData   data,
        CallContext context)
    {
        Element select = ProtocolUtils.createArtNode(
            cr, "select", null, null);

        cr.addAttr(select, "name", data.getName(), true);

        Element label = ProtocolUtils.createArtNode(
            cr, "label", null, null);

        Element choices = ProtocolUtils.createArtNode(
            cr, "choices", null, null);

        label.setTextContent(Resources.getMsg(
            context.getMeta(),
            data.getName(),
            data.getName()));

        select.appendChild(label);

        return select;
    }


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

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

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

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

            return new Element[] { min };
        }
        else if (name.equals("ld_to")) {
            Element max = createItem(
                cr,
                new String[] {"max", new Double(maxVal).toString()});

            return new Element[] { max };
        }
        else {
            Element step = createItem(cr, new String[] {"step", DEFAULT_STEP});
            return new Element[] { step };
        }

    }


    protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
        Element item  = ProtocolUtils.createArtNode(cr, "item", null, null);
        Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
        Element value = ProtocolUtils.createArtNode(cr, "value", null, null);

        String[] arr = (String[]) obj;

        label.setTextContent(arr[0]);
        value.setTextContent(arr[1]);

        item.appendChild(label);
        item.appendChild(value);

        return item;
    }


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


    protected double[] getMinMaxDistance(Artifact artifact) {
        FLYSArtifact flysArtifact = (FLYSArtifact) artifact;
        StateData    data         = getData(flysArtifact, "river");

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

        logger.debug("Search for the min/max distances of '" + name + "'");

        River river = RiverFactory.getRiver(name);

        return river != null ? river.determineMinMaxDistance() : null;
    }


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

        FLYSArtifact flys = (FLYSArtifact) artifact;

        if (flys.isRange()) {
            return validateRange(flys, context);
        }
        else {
            return validateLocations(flys, context);
        }
    }


    protected boolean validateLocations(FLYSArtifact flys, CallContext context)
    throws    IllegalArgumentException
    {
        StateData dValues = getData(flys, LOCATIONS);
        String    values  = dValues != null ? (String)dValues.getValue() : null;

        if (values == null || values.length() == 0) {
            throw new IllegalArgumentException("error_empty_state");
        }

        double[] absMinMax = getMinMaxDistance(flys);
        double[] relMinMax = getMinMaxFromString(values);

        if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) {
            throw new IllegalArgumentException("error_feed_from_out_of_range");
        }

        if (relMinMax[1] > absMinMax[1] || relMinMax[1] < absMinMax[0]) {
            throw new IllegalArgumentException("error_feed_to_out_of_range");
        }

        return true;
    }


    protected boolean validateRange(FLYSArtifact flys, CallContext context)
    throws    IllegalArgumentException
    {
        StateData dFrom = getData(flys, FROM);
        StateData dTo   = getData(flys, TO);
        StateData dStep = getData(flys, STEP);

        String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
        String toStr   = dTo != null ? (String) dTo.getValue() : null;
        String stepStr = dStep != null ? (String) dStep.getValue() : null;

        if (fromStr == null || toStr == null || stepStr == null) {
            throw new IllegalArgumentException("error_empty_state");
        }

        try {
            double from = Double.parseDouble(fromStr);
            double to   = Double.parseDouble(toStr);
            double step = Double.parseDouble(stepStr);

            double[] minmaxDist = getMinMaxDistance(flys);

            return validateBounds(minmaxDist[0], minmaxDist[1], from, to, step);
        }
        catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("error_invalid_double_value");
        }
    }


    /**
     * Extracts the min/max values from String <i>s</i>. An
     * IllegalArgumentException is thrown if there is a value that throws a
     * NumberFormatException.
     *
     * @param s String that contains whitespace separated double values.
     *
     * @return a 2dmin array [min,max].
     */
    public static double[] getMinMaxFromString(String s)
    throws IllegalArgumentException
    {
        String[] values = s.split(" ");

        double[] minmax = new double[] {
            Double.MAX_VALUE,
            -Double.MAX_VALUE };

        for (String v: values) {
            try {
                double value = Double.valueOf(v);

                minmax[0] = minmax[0] < value ? minmax[0] : value;
                minmax[1] = minmax[1] > value ? minmax[1] : value;
            }
            catch (NumberFormatException nfe) {
                throw new IllegalArgumentException(
                    "error_invalid_double_value");
            }
        }

        return minmax;
    }


    public static double[] getLocations(FLYSArtifact flys) {
        StateData data  = flys.getData("ld_locations");
        String    value = data != null ? (String) data.getValue() : null;

        if (value == null || value.length() == 0) {
            logger.warn("No location data given.");
            return null;
        }

        String[]         splitted = value.split(" ");
        TDoubleArrayList values   = new TDoubleArrayList();

        for (String split: splitted) {
            try {
                values.add(Double.valueOf(split));
            }
            catch (NumberFormatException nfe) {
                logger.warn(nfe, nfe);
            }
        }

        return values.toNativeArray();
    }


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

        WINFOArtifact winfo = (WINFOArtifact)artifact;

        CalculationResult res = old instanceof CalculationResult
            ? (CalculationResult)old
            : winfo.getDischargeCurveData();

        if (facets == null) {
            logger.debug("generate no facets");
            return res;
        }

        WQKms [] wqkms = (WQKms [])res.getData();

        logger.debug("generate " + wqkms.length + " facets.");

        String stateID = winfo.getCurrentStateId();

        for (int i = 0; i < wqkms.length; ++i) {
            String name = wqkms[i].getName();
            facets.add(new WaterlevelFacet(
                i, DISCHARGE_CURVE, name, ComputeType.FEED, stateID, hash));
        }


        return res;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org