view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/LoadSingleEpochSelectState.java @ 9248:9e6b70cce337

pseudo epoch for salix.historical
author gernotbelger
date Thu, 12 Jul 2018 13:27:34 +0200
parents f5cff8708531
children a978b601a034
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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.salix;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
import org.dive4elements.river.artifacts.states.AddTableDataHelper;
import org.dive4elements.river.artifacts.states.DefaultState;
import org.dive4elements.river.artifacts.uinfo.UINFOArtifact;
import org.dive4elements.river.model.BedHeight;
import org.w3c.dom.Element;

public class LoadSingleEpochSelectState extends DefaultState {
    private static final long serialVersionUID = 1L;
    /** The log used in this class. */
    private static Logger log = Logger.getLogger(LoadSingleEpochSelectState.class);

    /**
     * The default constructor that initializes an empty State object.
     */
    public LoadSingleEpochSelectState() {
    }

    @Override
    protected String getUIProvider() {
        return "uinfo.salix.load_single_year_pseudo_epoch_select";
    }

    @Override
    protected void appendItems(final Artifact artifact, final ElementCreator creator, final String name, final CallContext context, final Element select) {
        final String datakey = "singleepoch";

        try {
            if (datakey.equals(name)) {
                final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact);
                final List<BedHeight> bhs = BedHeight.getBedHeightEpochs(access.getRiver(), access.getLowerKm(), access.getUpperKm());
                makeDataSourceYearEpoch(artifact, creator, select, context, bhs);
            }
        }
        catch (

        final IllegalArgumentException iae) {
            iae.printStackTrace();
        }
    }

    public static final void makeDataSourceYearEpoch(final Artifact artifact, final ElementCreator creator, final Element select, final CallContext context,
            final List<BedHeight> bedheights) { // TODO: maybe move to appropriate helper...

        final AddTableDataHelper helper = new AddTableDataHelper(creator, select, "year", context.getMeta());

        helper.addColumn(0, "pinfrom", "40", "common.client.ui.from", "ICON", "CENTER", "from");
        helper.addColumn(1, "year", "60", "year", "INTEGER", "LEFT", null);
        helper.addColumn(2, "sounding", "500", "uinfo.salix.soundings", "STRING", "LEFT", null);

        final TreeMap<Integer, String> bedHeightSorted = new TreeMap<>();

        for (final BedHeight bh : bedheights) {
            final int year = bh.getYear();
            final String soundings = bedHeightSorted.containsKey(year) ? bedHeightSorted.get(year) + ", " : "";
            bedHeightSorted.put(year, soundings + bh.getDescription());
        }
        final Iterator<Integer> iterator = bedHeightSorted.keySet().iterator();
        while (iterator.hasNext()) {
            final int year = iterator.next();
            final String soundings = bedHeightSorted.get(year);
            final Map<String, String> row = new HashMap<>();
            row.put("year", String.valueOf(year));
            row.put("sounding", soundings);
            helper.addRow(row);
        }

        helper.submitMapToXml();
    }

}

http://dive4elements.wald.intevation.org