view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputationRangeState.java @ 1723:690037105542

Added new Artifact and Facet to access 'static' data via WKmsFactory. flys-artifacts/trunk@3005 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 18 Oct 2011 12:56:12 +0000
parents 7e19449d7826
children 9144e5a5027b
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import org.w3c.dom.Element;

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.DischargeTable;
import de.intevation.flys.model.Gauge;
import de.intevation.flys.model.River;
import de.intevation.flys.model.TimeInterval;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.WINFOArtifact;
import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.GaugesFactory;
import de.intevation.flys.artifacts.model.RiverFactory;
import de.intevation.flys.artifacts.model.WaterlevelFacet;
import de.intevation.flys.artifacts.model.WQKms;
import de.intevation.flys.artifacts.resources.Resources;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ComputationRangeState
extends    RangeState
implements FacetTypes
{
    private static Logger logger =
        Logger.getLogger(ComputationRangeState.class);


    /** 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 step width. */
    public static final int DEFAULT_STEP = 100;



    public ComputationRangeState() {
    }


    @Override
    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 = getMinMax(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", String.valueOf(getDefaultStep())});
            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
    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 = getSeriesName(context, wqkms[i].getName());
            facets.add(new WaterlevelFacet(
                i, DISCHARGE_CURVE, name, ComputeType.FEED, stateID, hash));
        }


        return res;
    }

    protected String getSeriesName(CallContext cc, String gaugeName) {
        Gauge gauge = GaugesFactory.getGauge(gaugeName);

        if (gauge == null) {
            logger.warn("Cannot determine Gauge for name: " + gaugeName);
            return gaugeName;
        }

        List<DischargeTable> dts = gauge.getDischargeTables();

        for (DischargeTable dt: dts) {
            if (dt.getKind() == 0) {
                TimeInterval ti = dt.getTimeInterval();

                Date start = ti.getStartTime();
                Date end   = ti.getStopTime();

                String name  = gauge.getName();

                if (end == null) {
                    Object[] args = new Object[] { name, start };
                    return Resources.getMsg(
                        cc.getMeta(),
                        "chart.discharge.curve.curve.valid.from",
                        "",
                        args);
                }
                else {
                    Object[] args = new Object[] { name, start, end };
                    return Resources.getMsg(
                        cc.getMeta(),
                        "chart.discharge.curve.curve.valid.range",
                        "",
                        args);
                }
            }
        }

        return gauge.getName();
    }


    @Override
    protected double[] getMinMax(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;
    }


    protected double getDefaultStep() {
        return DEFAULT_STEP;
    }


    @Override
    protected String getLowerField() {
        return FROM;
    }


    @Override
    protected String getUpperField() {
        return TO;
    }


    @Override
    protected String getStepField() {
        return STEP;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org