view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationState.java @ 9537:bf6b63208f34

Work on uinfo inundation duration calculation. Using proxy-wms to induce additional style information (work in progress).
author gernotbelger
date Wed, 17 Oct 2018 11:23:17 +0200
parents 992c188b7330
children 3955ecc1a516
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * 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.uinfo.inundationduration;

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.MapArtifact.MapState;
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.ReportFacet;
import org.dive4elements.river.artifacts.model.map.WMSLayerFacet;
import org.dive4elements.river.artifacts.uinfo.UINFOArtifact;
import org.dive4elements.river.artifacts.uinfo.inundationduration.InundationDurationCalculationResult.WmsLayer;

/**
 * @author Domenico Nardi Tironi
 */
public class InundationDurationState extends MapState {

    /// ** 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 LABEL_URL_SEPARATOR = ";LABEL_URL_SEPARATOR;";// always sync with client (ExportPanel)

    /**
     * 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((UINFOArtifact) artifact, context, ComputeType.FEED, 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;
        }

        if (facets != null)
            super.computeAdvance(artifact, hash, context, facets, old);

        return compute((UINFOArtifact) artifact, context, ComputeType.ADVANCE, hash, facets, old);

    }

    /**
     * Compute result or returned object from cache, create facets.
     *
     * @param old
     *            Object that was cached.
     */
    private Object compute(final UINFOArtifact sinfo, final CallContext context, final ComputeType type, final String hash, final List<Facet> facets,
            final Object old) {

        final CalculationResult res = doCompute(sinfo, context, old);

        if (facets == null)
            return res;

        final InundationDurationCalculationResult result = (InundationDurationCalculationResult) res.getData();
        final List<WmsLayer> layers = result.getLayers();

        int index = 1; // 1 because super.computeAdvance adds the river theme with index 0
        for (final WmsLayer layer : layers) {

            final String label = layer.getLabel();
            final String url = layer.getUrl();

            final WMSLayerFacet wmsFacet = new WMSLayerFacet(index, FLOODMAP_EXTERNAL_WMS_INUNDATIONDUR + index, label, type, getID(), hash, url);
            wmsFacet.addLayer(layer.getLayer());

            facets.add(wmsFacet);
            // TODO:
            // wmsFacet.setExtent(getExtent(false));
            // wmsFacet.setOriginalExtent(getExtent(true));
            wmsFacet.setSrid(getSrid());

            if (layer.isShowLayerLink())
                facets.add(new DataFacet("wms_url", label + LABEL_URL_SEPARATOR + url, ComputeType.ADVANCE, hash, this.id));

            index++; // because super.computeAdvance adds the river theme with index 0
        }

        final Calculation report = res.getReport();
        if (report.hasProblems())
            facets.add(new ReportFacet(ComputeType.ADVANCE, hash, this.id));

        return res;
    }

    private CalculationResult doCompute(final UINFOArtifact artifact, final CallContext context, final Object old) {
        if (old instanceof CalculationResult)
            return (CalculationResult) old;

        return new InundationDurationCalculation(context).calculate(artifact);
    }
}

http://dive4elements.wald.intevation.org