view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java @ 4192:34337e357a54

Add message for gauge_discharge_curve_at_export Add message to display a correct info at the save at export icon when hovering.
author Björn Ricks <bjoern.ricks@intevation.de>
date Fri, 19 Oct 2012 13:59:51 +0200
parents 102050c4fc00
children
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

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

import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.Artifact;
import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.StaticWKmsArtifact;
import de.intevation.flys.artifacts.WINFOArtifact;
import de.intevation.flys.artifacts.ChartArtifact;

import de.intevation.flys.artifacts.math.WKmsOperation;

import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.artifacts.model.DifferenceCurveFacet;
import de.intevation.flys.artifacts.model.EmptyFacet;

import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.WKms;
import de.intevation.flys.artifacts.model.WQKms;

import de.intevation.flys.utils.FLYSUtils;
import de.intevation.flys.utils.StringUtil;


public class WDifferencesState
extends      DefaultState
implements   FacetTypes
{
    /** The logger that is used in this state. */
    private static Logger logger = Logger.getLogger(WDifferencesState.class);


    public WDifferencesState() {
    }


    /** Specify to display nothing (this is kind of a "final" state). */
    @Override
    protected String getUIProvider() {
        return "noinput";
    }


    @Override
    public boolean validate(Artifact artifact)
    throws IllegalArgumentException
    {
        FLYSArtifact flys = (FLYSArtifact) artifact;

        StateData data = flys.getData("diffids");

        if (data == null) {
            throw new IllegalArgumentException("diffids is empty");
        }

        // TODO: Also validate format.

        return true;
    }


    /**
     * Access the data (wkms).
     */
    protected WKms getWKms(String mingle, CallContext context) {
        String[] def  = mingle.split(";");
        String   uuid = def[0];
        String   name = def[1];
        int      idx  = Integer.parseInt(def[2]);

        if (name.startsWith("staticwkms")) {
            StaticWKmsArtifact staticWKms =
                (StaticWKmsArtifact) FLYSUtils.getArtifact(
                    uuid,
                    context);
            logger.debug("WDifferencesState obtain data from StaticWKms");
            WKms wkms = staticWKms.getWKms(idx);
            if (wkms == null)
                logger.error("No WKms from artifact.");
            return wkms;
        }

        WINFOArtifact flys = (WINFOArtifact) FLYSUtils.getArtifact(
            uuid,
            context);

        if (flys == null) {
            logger.warn("One of the artifacts (1) for diff calculation could not be loaded");
            return null;
        }
        else{
            WQKms[] wqkms = (WQKms[]) flys.getWaterlevelData().
                                              getData();
            if (wqkms == null)
            logger.warn("not  waterlevels in artifact");
            else if (wqkms.length < idx)
            logger.warn("not enough waterlevels in artifact");
            return wqkms[idx];
        }
    }


    /**
     * Return CalculationResult with Array of WKms that are difference of
     * Waterlevels. Add respective facets (DifferencesCurveFacet, DataFacet).
     */
    @Override
    public Object computeAdvance(
        FLYSArtifact artifact,
        String       hash,
        CallContext  context,
        List<Facet>  facets,
        Object       old
    ) {
        if (artifact instanceof ChartArtifact) {
            ChartArtifact chart = (ChartArtifact)artifact;
            facets.add(new EmptyFacet());
            return null;
        }
        WINFOArtifact winfo = (WINFOArtifact) artifact;
        String id = getID();

        // Load the Artifacts/facets that we want to subtract and display.
        // Expected format is:
        // [42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;0]#[1231f2-....]
        String diffids = winfo.getDataAsString("diffids");
        logger.debug("WDifferencesState has: " + diffids);
        String datas[] = diffids.split("#");

        // Validate the Data-Strings.
        for (String s: datas) {
            if (!WaterlevelSelectState.isValueValid(s)) {
                // TODO: escalate.
            }
        }

        if (datas.length < 2) {
            // TODO crash with style
        }

        List<WKms> wkmss = new ArrayList<WKms>();

        for(int i = 0; i < datas.length; i+=2) {
            // e.g.:
            // 42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;1
            WKms minuendWKms = getWKms(StringUtil.unbracket(datas[i+0]),
                context);
            WKms subtrahendWKms = getWKms(StringUtil.unbracket(datas[i+1]),
                context);

            String facetName = "diff ()";

            if (minuendWKms != null && subtrahendWKms != null) {
                facetName = StringUtil.wWrap(minuendWKms.getName())
                    + " - " + StringUtil.wWrap(subtrahendWKms.getName());
                WKms wkms = WKmsOperation.SUBTRACTION.operate(minuendWKms,
                     subtrahendWKms);
                wkms.setName(facetName);
                wkmss.add(wkms);
                logger.debug("WKMSSubtraction happened");
            }
            if (facets != null) {
                facets.add(new DifferenceCurveFacet(i/2, W_DIFFERENCES, facetName,
                    ComputeType.ADVANCE, id, hash));
            }
        }

        if (facets != null) {
            facets.add(new DataFacet(CSV, "CSV data"));
            facets.add(new DataFacet(PDF, "PDF data"));
            logger.debug("Adding facets in WDifferencesState.");
        }
        else {
            logger.debug("Not adding facets in WDifferencesState.");
        }

        // TODO Evaluate whether null is okay as reports.
        WKms[] diffs = wkmss.toArray(new WKms[wkmss.size()]);
        CalculationResult result = new CalculationResult(diffs, null);
        return result;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org