view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthState.java @ 8877:9f7a285b0ee3

Some work on SINFO FlowDepth
author gernotbelger
date Thu, 08 Feb 2018 18:48:24 +0100
parents 7bbfb24e6eec
children 64ca63f79f6f
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.sinfo.flowdepth;

import java.util.List;

import org.dive4elements.artifactdatabase.state.Facet;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.river.artifacts.ChartArtifact;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.artifacts.model.CalculationResult;
import org.dive4elements.river.artifacts.model.DataFacet;
import org.dive4elements.river.artifacts.model.EmptyFacet;
import org.dive4elements.river.artifacts.model.FacetTypes;
import org.dive4elements.river.artifacts.model.ReportFacet;
import org.dive4elements.river.artifacts.resources.Resources;
import org.dive4elements.river.artifacts.sinfo.SINFOArtifact;
import org.dive4elements.river.artifacts.states.DefaultState;

/** State in which a waterlevel has been calculated. */
public class FlowDepthState
extends      DefaultState
{
    /// ** The log that is used in this state. */
    // private static Logger log = Logger.getLogger(FlowDepthState.class);

    private static final long serialVersionUID = 1L;

    private static final String I18N_FACET_FLOW_DEPTH_FILTERED_DESCRIPTION = "sinfo.facet.flow_depth.filtered.description";

    /**
     * From this state can only be continued trivially.
     */
    @Override
    protected String getUIProvider() {
        return "continue";
    }

    @Override
    public Object computeFeed(
        final D4EArtifact artifact,
        final String       hash,
        final CallContext  context,
        final List<Facet>  facets,
        final Object       old
    ) {
    	// FIXME: why is this necessary?
        if (artifact instanceof ChartArtifact) {
            facets.add(new EmptyFacet());
            return null;
        }
        return compute((SINFOArtifact) artifact, context, hash, facets, old);
    }

    @Override
    public Object computeAdvance(
    	final D4EArtifact artifact,
    	final String       hash,
    	final CallContext  context,
    	final List<Facet>  facets,
    	final Object       old
    ) {
        if (artifact instanceof ChartArtifact) {
            facets.add(new EmptyFacet());
            return null;
        }
        return compute((SINFOArtifact) artifact, context, hash, facets, old);
    }
    
    /**
     * Compute result or returned object from cache, create facets.
     * @param old Object that was cached.
     */
    private Object compute(
    	final SINFOArtifact sinfo,
        final CallContext   context,
        final String        hash,
        final List<Facet>   facets,
        final Object        old
    ) {
        final CalculationResult res;
		if (old instanceof CalculationResult)
			res = (CalculationResult) old;
		else
			res = new FlowDepthCalculation(context).calculate(sinfo);

        if (facets == null) {
            return res;
        }

        final FlowDepthCalculationResults results =  (FlowDepthCalculationResults) res.getData();

        /* add themes for chart, for each result */
        final List<FlowDepthCalculationResult> resultList = results.getResults();
        for (int index = 0; index < resultList.size(); index++) {

			final FlowDepthCalculationResult result = resultList.get(index);
			
			/* filtered (zoom dependent mean) flow depth */
			final String facetFlowDepthFilteredDescription = Resources.getMsg( context.getMeta(), I18N_FACET_FLOW_DEPTH_FILTERED_DESCRIPTION, I18N_FACET_FLOW_DEPTH_FILTERED_DESCRIPTION, result.getLabel() );
			facets.add(new FlowDepthFilterFacet(
			        index,
			        FlowDepthProcessor.FACET_FLOW_DEPTH_FILTERED,
			        facetFlowDepthFilteredDescription,
			        ComputeType.ADVANCE,
			        id,
			        hash
			    ));
			
			// FIXME: add other themes
		}

        if (!resultList.isEmpty() ) {
            Facet csv = new DataFacet(
                FacetTypes.CSV, "CSV data", ComputeType.ADVANCE, hash, id);
            Facet pdf = new DataFacet(
            		FacetTypes.PDF, "PDF data", ComputeType.ADVANCE, hash, id);

            facets.add(csv);
            facets.add(pdf);
        }

        final Calculation report = res.getReport();

        if (report.hasProblems()) {
            facets.add(new ReportFacet(ComputeType.ADVANCE, hash, id));
        }

        return res;
    }
}

http://dive4elements.wald.intevation.org