view artifacts/src/main/java/org/dive4elements/river/artifacts/common/AccessHelper.java @ 9069:1ffd38826175

access uinfo.vegetationzones+inundation_duration
author gernotbelger
date Tue, 15 May 2018 12:00:26 +0200
parents 2ed3824a3d53
children
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.common;

import org.apache.log4j.Logger;
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.Access;

import gnu.trove.TIntArrayList;

/**
 * @author Domenico Nardi Tironi
 *
 */
public class AccessHelper extends Access {

    private static Logger log = Logger.getLogger(AccessHelper.class);

    // private final Artifact artifact;

    public AccessHelper(final Artifact artifact) {
        this.artifact = (D4EArtifact) artifact;
    }

    public String getYearEpoch() {

        return getString("ye_select"); // ACHTUNG, Ergebniswerte wurden geändert in state.sinfo.epoch und state.sinfo.year

    }

    /** [year1, years2,..] if its about years. */
    public int[] getYears(final String yearSelectedValue, final String yearKey) {
        int[] years = null;
        // if (years != null) {
        // return years;
        // }
        if (getYearEpoch().equals(yearSelectedValue)) {
            final TIntArrayList ints = new TIntArrayList();
            final String yearsData = getString(yearKey);
            if (yearsData == null || yearsData.isEmpty()) {
                log.warn("No years provided");
                return null;
            }
            for (final String sValue : yearsData.split(" ")) {
                try {
                    ints.add(Integer.parseInt(sValue));
                }
                catch (final NumberFormatException e) {
                    /* Client should prevent this */
                    log.warn("Invalid year value: " + sValue);
                    continue;
                }
            }

            if (!ints.isEmpty()) {
                ints.sort();
                years = ints.toNativeArray();
            }
            return years;
        }
        return null;
    }

    public int[][] getEpochs(final String epochSelectedValue, final String epochKey) {
        int epochs[][] = null;
        // if (epochs != null) {
        // return epochs;
        // }

        if (!getYearEpoch().equals(epochSelectedValue)) {
            return null;
        }

        final String data = getString(epochKey);

        if (data == null) {
            log.warn("No 'epochs' parameter specified!");
            return null;
        }

        final String[] parts = data.split(";");

        epochs = new int[parts.length][];

        for (int i = 0; i < parts.length; i++) {
            final String[] values = parts[i].split(",");
            final TIntArrayList ints = new TIntArrayList();
            try {
                ints.add(Integer.parseInt(values[0]));
                ints.add(Integer.parseInt(values[1]));
                epochs[i] = ints.toNativeArray();
            }
            catch (final NumberFormatException nfe) {
                log.warn("Cannot parse int from string: '" + values + "'");
            }
        }
        return epochs;
    }

}

http://dive4elements.wald.intevation.org