view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/UedauernPropertiesHelper.java @ 9178:2f5052835b76

uinfo inundationduration langjähr. Mittel, Uedauern.properties, Meldung
author gernotbelger
date Tue, 26 Jun 2018 19:48:35 +0200
parents
children dace17e26d33
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.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.text.StrSubstitutor;
import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.artifacts.common.utils.Config;
import org.dive4elements.river.artifacts.resources.Resources;

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

    private static UedauernPropertiesHelper instance;
    private final String CONFIG_FILE;
    private final String rivername;
    private Integer[] mittelStartEnde = null;
    private String[] einzeljahre = null;
    private String mittelUrl = null;
    private Properties properties = null;

    public UedauernPropertiesHelper(final String rivername) {
        this.rivername = rivername;
        this.CONFIG_FILE = makeFileName(rivername);
    }

    public static synchronized UedauernPropertiesHelper getInstance(final String rivername) {
        if (UedauernPropertiesHelper.instance == null || !UedauernPropertiesHelper.instance.getRivername().equals(rivername)) {

            UedauernPropertiesHelper.instance = new UedauernPropertiesHelper(rivername);
        }
        return UedauernPropertiesHelper.instance;
    }

    private String getRivername() {
        return this.rivername;
    }

    private static final String makeFileName(final String river) {
        return "uinfo_uedauern_aue_" + river + ".properties";
    }

    public LinkedHashMap<String, String> getExtraLayers(final CallMeta meta) {
        final LinkedHashMap<String, String> entries = new LinkedHashMap<>();

        final Integer[] totalEpoch = getMittelStartEnd();
        final Object[] args = new Object[] { String.valueOf(totalEpoch[0]), String.valueOf(totalEpoch[1]) };

        // final ResourceBundle rb = ResourceBundle.getBundle("Name");
        entries.put("state.uinfo.totalepoch", Resources.getMsg(meta, "state.uinfo.totalepoch", args)); //
        // String.valueOf: avoid formatting
        // (3.333,00

        return entries;
    }

    private Properties getProperties() {
        if (this.properties == null) {
            this.properties = Config.loadProperties(this.CONFIG_FILE);
        }
        return this.properties;
    }

    private final Integer[] getMittelStartEnd() {
        if (this.mittelStartEnde == null) {
            final Integer mittel_start = Integer.valueOf(getProperties().get("mittel_start").toString());
            final Integer mittel_ende = Integer.valueOf(getProperties().get("mittel_ende").toString());
            this.mittelStartEnde = new Integer[] { mittel_start, mittel_ende }; // lazy

        }
        return this.mittelStartEnde;
    }

    public String[] getEinzeljahre() {
        if (this.einzeljahre == null) { // lazy
            final Object years = getProperties().get("jahre");
            if (years != null) {
                this.einzeljahre = years.toString().split(",");
            }
        }
        return this.einzeljahre;
    }

    public final String getMittelUrl() {
        if (this.mittelUrl == null) { // lazy
            this.mittelUrl = getProperties().get("mittel_url").toString();
        }
        return this.mittelUrl;
    }

    public final String urlFromYear(final int year) {
        // besser kein lazy-loading, da der user nochmal zurück gehen und das Jahr ändern könnte...
        final String templateUrl = getProperties().get("url").toString();
        final Map<String, String> tokens = new HashMap<>();
        tokens.put("jahr", String.valueOf(year));
        final StrSubstitutor subst = new StrSubstitutor(tokens);
        final String yearUrl = subst.replace(templateUrl);
        return yearUrl;
    }

    public static boolean fileExistsForRiver(final String river) {
        final Properties properties = Config.loadProperties(makeFileName(river));
        if (properties.size() == 0) {
            return false;
        }
        return true;
    }
}

http://dive4elements.wald.intevation.org