view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.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 56953439dcae
children 1a57027286ce
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.WINFOArtifact;

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.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;
    }


    /**
     * 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
    ) {
        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");
        String datas[] = diffids.split("#");

        // Validate the Data-Strings.
        for (String s: datas) {
            if (!WaterlevelSelectState.isValueValid(winfo.getDataAsString("diffids"))) {
                // 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
            String[] def1  = StringUtil.unbracket(datas[i+0]).split(";");
            String[] def2  = StringUtil.unbracket(datas[i+1]).split(";");
            String   uuid1 = def1[0];
            String   uuid2 = def2[0];
            int      idx1  = Integer.parseInt(def1[2]);
            int      idx2  = Integer.parseInt(def2[2]);
    
            WINFOArtifact flys1 = (WINFOArtifact) FLYSUtils.getArtifact(
                uuid1,
                context);
            WINFOArtifact flys2 = (WINFOArtifact) FLYSUtils.getArtifact(
                uuid2,
                context);
    
            if (flys1 == null) {
                logger.warn("One of the artifacts (1) for diff calculation could not be loaded");
            }
            if (flys2 == null) {
                logger.warn("One of the artifacts (2) for diff calculation could not be loaded");
            }
            // TODO: Issue of multiple results in calculation
            String facetName = "diff ()";
    
            if (flys1 != null && flys2 != null) {
                WQKms[] minuend    = (WQKms[]) flys1.getWaterlevelData().
                                                     getData();
                WQKms[] subtrahend = (WQKms[]) flys2.getWaterlevelData().
                                                     getData();

                facetName = StringUtil.wWrap(minuend[idx1].getName()) 
                    + " - " + StringUtil.wWrap(subtrahend[idx2].getName());
                WKms wkms = WKmsOperation.SUBTRACTION.operate(minuend[idx1],
                     subtrahend[idx2]);
                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));
                facets.add(new DataFacet(CSV, "CSV data"));
            }
            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