view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationAccess.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 787fc085459b
children ba0561906f81
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.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.dive4elements.artifacts.CallContext;
import org.dive4elements.river.artifacts.access.RangeAccess;
import org.dive4elements.river.artifacts.common.EpochYearAccessHelper;
import org.dive4elements.river.artifacts.resources.Resources;
import org.dive4elements.river.artifacts.sinfo.tkhstate.TsvHelper.TsvReaderException;
import org.dive4elements.river.artifacts.uinfo.UINFOArtifact;
import org.dive4elements.river.artifacts.uinfo.UinfoCalcMode;
import org.dive4elements.river.artifacts.uinfo.inundationduration.InundationDurationCalculationResult.WmsLayer;
import org.dive4elements.river.artifacts.uinfo.inundationduration.UedauernConfiguration.YearType;

/**
 * Access to the flow depth calculation type specific SInfo artifact data.
 * REMARK: this class is NOT intended to be hold in the results (or anywhere else), in order to avoid a permanent
 * reference to the artifact instance.
 * Hence we do NOT cache any data.
 *
 * @author Gernot Belger
 */
final class InundationDurationAccess extends RangeAccess {

    // IMMER ABGLEICHEN MIT SuperVegZonesTablePanel.TABLE_CELL_SEPARATOR
    public static final String TABLE_CELL_SEPARATOR = "TABLE_CELL_SEPARATOR";
    public static final String TABLE_ROW_SEPARATOR = "TABLE_ROW_SEPARATOR";

    private final EpochYearAccessHelper helper;

    public static enum WmsClassification {
        asIs {
            @Override
            public String configureAddress(final String serverAddress, final String url, final String vegetationZoneId) {
                return url;
            }
        },
        vegetationZone {
            @Override
            public String configureAddress(final String serverAddress, final String url, final String vegetationZoneId) {

                try {
                    final String urlEncoded = URLEncoder.encode(url, "UTF-8");
                    final String vegZoneIdEncoded = URLEncoder.encode(vegetationZoneId, "UTF-8");

                    return String.format("%s/%s/%s/%s", serverAddress, VegetationWmsResource.BASE_PATH, vegZoneIdEncoded, urlEncoded);
                }
                catch (final UnsupportedEncodingException e) {
                    /* should never happen */
                    e.printStackTrace();
                    return null;
                }
            }
        };

        public abstract String configureAddress(String serverAddress, String url, String vegetationZoneId);
    }

    // Fields from state:
    //
    // calculation_mode (String)
    // ld_from, ld_to
    // ye_select (String; state.uinfo.totalepoch oder state.uinfo.year)
    // year_epoch_select
    // totalepoch (String; TODO: minmax totalEpoch herausfinden und setzen (nicht hier.. aber trotzdem die Erinnerung hier))
    // use_scenario boolean (danach kommt kein radioBtn, sondern sedimentheight-Eingabe)
    // sedimentheight String
    //
    // vegzones (String) TODO: MIT VegetationzonesAccess zusammenlegen

    public InundationDurationAccess(final UINFOArtifact uinfo) {
        super(uinfo);
        /* assert calculation mode */
        final UinfoCalcMode calculationMode = uinfo.getCalculationMode();
        this.helper = new EpochYearAccessHelper(uinfo);
        assert (calculationMode == UinfoCalcMode.uinfo_inundation_duration);
    }

    public boolean isUseYear() {
        if (this.helper.getYearEpoch().equals("state.uinfo.year"))
            return true;
        return false;
    }

    public WmsLayer createWMSLayer(final CallContext context, final String i10nKey, final WmsClassification classification, final String vegZoneId)
            throws IOException, TsvReaderException {

        final YearType type = isUseYear() ? YearType.jahre : YearType.mittel;
        final String selectedElement = getSelectedLabel();

        final String layerLabel = Resources.getMsg(context.getMeta(), i10nKey, new Object[] { selectedElement });

        return createWMSLayer(context, layerLabel, type, selectedElement, classification, vegZoneId);
    }

    public WmsLayer createScenarioWMSLayer(final CallContext context, final String i10nKey, final WmsClassification classification, final String vegZoneId)
            throws IOException, TsvReaderException {

        // FIXME: use scenario-cm as label, and scenario-type
        final YearType type = YearType.jahre;

        final String selectedElement = Integer.toString(getDwspl());
        final String layerLabel = Resources.getMsg(context.getMeta(), i10nKey, new Object[] { selectedElement });

        return createWMSLayer(context, layerLabel, type, selectedElement, classification, vegZoneId);
    }

    private WmsLayer createWMSLayer(final CallContext context, final String layerLabel, final YearType type, final String selectedElement,
            final WmsClassification classification, final String vegZoneId)
                    throws IOException, TsvReaderException {

        final String serverAddress = context.getDatabase().getServerAddress();

        final UedauernConfiguration config = UedauernConfiguration.getInstance(getRiverName(), type);
        final String url = config.getUrl(selectedElement);
        final String layer = config.getLayer(selectedElement);

        final String realUrl = classification.configureAddress(serverAddress, url, vegZoneId);

        return new WmsLayer(layerLabel, realUrl, layer, classification == WmsClassification.asIs);
    }

    private String getSelectedLabel() {
        return this.getString("year_epoch_select");
    }

    private Integer getDwspl() {
        if (getIsUseScenario())
            return super.getInteger("sedimentheight");

        return null;
    }

    public String getVegZones() {
        // mit VegetationzonesAccess zusammenlegen (eine Zeile sparen...)
        return super.getString("vegzones");
    }

    public boolean getIsUseScenario() {
        return super.getBoolean("use_scenario");
    }
}

http://dive4elements.wald.intevation.org