view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/UedauernConfiguration.java @ 9559:ba0561906f81

Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
author gernotbelger
date Wed, 24 Oct 2018 18:40:38 +0200
parents 787fc085459b
children 63bbd5e45839
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.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.dive4elements.river.artifacts.sinfo.tkhstate.TsvHelper;
import org.dive4elements.river.artifacts.sinfo.tkhstate.TsvHelper.TsvReaderException;

/**
 * @author Domenico Nardi Tironi
 *
 */
public class UedauernConfiguration {

    public final static class WmsConfig {
        private final String label;
        private final String url;
        private final String layer_default;

        public WmsConfig(final String label, final String url, final String layer) {
            this.label = label;
            this.url = url;
            this.layer_default = layer;
        }

        public String getLabel() {
            return this.label;
        }

        public String getUrl() {
            return this.url;
        }

        public String getLayer() {
            return this.layer_default;
        }
    }

    public static enum YearType {
        jahre, mittel, szenario
    }

    private final int DEFAULT_WMS_INDEX = 0;
    private final int VEGETATIONZONE_WMS_INDEX = 1;

    private static Map<String, UedauernConfiguration> cache = new HashMap<>();

    private final Map<String, WmsConfig[]> wmsConfigs;

    public static synchronized UedauernConfiguration getInstance(final String rivername, final YearType type) throws IOException, TsvReaderException {

        final String cacheKey = type.name() + "#" + rivername;
        if (!cache.containsKey(cacheKey)) {
            final UedauernConfiguration instance = loadConfiguration(rivername, type);
            cache.put(cacheKey, instance);
        }

        return cache.get(cacheKey);
    }

    private UedauernConfiguration(final String rivername, final YearType type, final Map<String, WmsConfig[]> wmsConfigs) {
        this.wmsConfigs = wmsConfigs;
    }

    private static UedauernConfiguration loadConfiguration(final String rivername, final YearType type) throws IOException, TsvReaderException {

        final String configFile = makeFileName(rivername, type);
        final File file = TsvHelper.makeFile2(configFile, rivername);
        final List<String[]> tsv = TsvHelper.readTsv(file, 5);

        final Map<String, WmsConfig[]> wmsConfigs = new LinkedHashMap<>(tsv.size());

        for (final String[] line : tsv) {
            if (line != null && line.length > 0 && !StringUtils.isEmpty(line[0])) {
                final String label = line[0];
                WmsConfig defaultConfig = null;
                WmsConfig vegZoneConfig = null;

                if (line.length > 2 && !StringUtils.isEmpty(line[1]) && !StringUtils.isEmpty(line[2])) {
                    defaultConfig = new WmsConfig(label, line[2], line[1]);

                    if (line.length > 4 && !StringUtils.isEmpty(line[3]) && !StringUtils.isEmpty(line[4])) {
                        vegZoneConfig = new WmsConfig(label, line[4], line[3]);
                    }
                }
                wmsConfigs.put(label, new WmsConfig[] { defaultConfig, vegZoneConfig });
            }
        }
        return new UedauernConfiguration(rivername, type, wmsConfigs);
    }

    private static final String makeFileName(final String river, final YearType type) {
        return "wms_uedauern_" + String.valueOf(type) + "_" + river + ".tsv";
    }

    public Collection<String> getLabels() throws UnsupportedEncodingException {
        return this.wmsConfigs.keySet();
    }

    public WmsConfig getDefaultWmsConfig(final String label) {
        return this.getConfig(label, this.DEFAULT_WMS_INDEX);
    }

    public WmsConfig getVegWmsConfig(final String label) {
        return this.getConfig(label, this.VEGETATIONZONE_WMS_INDEX);
    }

    private WmsConfig getConfig(final String label, final int index) {
        if (this.wmsConfigs.containsKey(label))
            if (this.wmsConfigs.get(label) != null && index < this.wmsConfigs.get(label).length)
                return this.wmsConfigs.get(label)[index];

        return null;
    }

    public static boolean filesExistsForRiver(final String river) {

        for (final YearType t : YearType.values()) {
            final String configFile = makeFileName(river, t);
            final File file = TsvHelper.makeFile2(configFile, river);

            if (TsvHelper.checkFile(file) == null)
                return false;
        }

        return true;
    }

}

http://dive4elements.wald.intevation.org