view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/DistanceOnlyPartHistoricalSelect.java @ 9321:a978b601a034

Salix: Fixed ArrrayoutOfBoundsException; minor cleanup
author gernotbelger
date Fri, 27 Jul 2018 10:25:09 +0200
parents f48a8dc78529
children e014eca211a3
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.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
import org.dive4elements.river.artifacts.resources.Resources;
import org.dive4elements.river.artifacts.states.AddTableDataHelper;
import org.dive4elements.river.artifacts.states.DistanceOnlySelect;
import org.dive4elements.river.artifacts.uinfo.UINFOArtifact;
import org.dive4elements.river.model.BedHeight;
import org.dive4elements.river.model.River;
import org.dive4elements.river.utils.Formatter;
import org.w3c.dom.Element;

public class DistanceOnlyPartHistoricalSelect extends DistanceOnlySelect {

    private static final long serialVersionUID = 1L;

    private List<BedHeight> bhs = null;
    private Integer epoch = null;
    private Integer year = null;
    private Double lowerKm = null;
    private Double upperKm = null;
    private River river = null;

    @Override
    protected String getUIProvider() {
        return "distance_only_part_historical_panel";
    }

    @Override
    protected String getTitle(final CallContext context) {
        // REMARK: that is how it should be: return Resources.getMsg(context.getMeta(), getID());
        return Resources.getMsg(context.getMeta(), "state.title.salix.historical.distance_part_state");
    }

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

        try {
            if (datakey.equals(name)) {
                makeDataSourceYearEpoch(creator, select, context, getBedheights(artifact)); // ist nur n test
            } else if (name.equals("ld_from_part")) {

                final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact);
                final double lowerSoundings = this.getLowerUpperKmRange(getBedheights(artifact))[0];
                final double lowerKm = access.getLowerKm() > lowerSoundings ? access.getLowerKm() : lowerSoundings;

                creator.addAttr(select, "type", "options", true);

                final Element item = creator.create("item");
                creator.addAttr(item, "label", "from_test", true);
                creator.addAttr(item, "value", String.valueOf(lowerKm), true);

                select.appendChild(item);
            }

            else if (name.equals("ld_to_part")) {
                final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact);
                final double upperSoundings = this.getLowerUpperKmRange(getBedheights(artifact))[1];
                final double upperKm = access.getUpperKm() < upperSoundings ? access.getUpperKm() : upperSoundings;

                creator.addAttr(select, "type", "options", true);

                final Element item = creator.create("item");
                creator.addAttr(item, "label", "to_test", true);
                creator.addAttr(item, "value", String.valueOf(upperKm), true);

                select.appendChild(item);

            }
        }
        catch (

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

    private List<BedHeight> getBedheights(final Artifact artifact) {

        final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact); // improved lazy-loading
        final Integer year = access.getYear();
        final Integer epoch = access.getEpoch();
        final River river = access.getRiver(); // comparable? workflow does not allow return and change anyway...
        final Double lower = access.getLowerKm();
        final Double upper = access.getUpperKm();
        if (!(this.year == year && this.epoch == epoch && this.river == river && this.lowerKm == lower && this.upperKm == upper)) {
            this.bhs = null;
        }
        if (this.bhs == null) {
            final boolean isEpoch = epoch == null ? false : true;
            this.bhs = BedHeight.getBedHeightYearEpoch(isEpoch, isEpoch ? epoch : year, river, lower, upper);
            this.year = year;
            this.epoch = epoch;
            this.river = river;
            this.lowerKm = lower;
            this.upperKm = upper;
        }
        return this.bhs;

    }

    private static final void makeDataSourceYearEpoch(final ElementCreator creator, final Element select, final CallContext context,
            final List<BedHeight> bedheights) {

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

        // TODO: probably aggregating results, no sound-row, output as single row

        int year = 0; // hässlich, aber kommt vermutlich eh bald weg
        if (bedheights != null && bedheights.size() > 0)
            year = bedheights.get(0).getYear(); // das jahr/epoche ist immer dasselbe

        helper.addColumn(0, "year", "60", "year", "INTEGER", "LEFT", null);
        helper.addColumn(1, "range", "130", "state.uinfo.salix.historical.km_range_part", "STRING", "LEFT", null);
        helper.addColumn(2, "description", "500", "uinfo.salix.sounding", "STRING", "LEFT", null);

        final TreeMap<String, String> bedHeightSorted = new TreeMap<>();
        final double min = Double.MAX_VALUE;
        final double max = -Double.MAX_VALUE;
        final java.text.NumberFormat formatter = Formatter.getCalculationKm(context.getMeta());

        for (final BedHeight bh : bedheights) {
            final org.dive4elements.river.model.Range range = BedHeight.getRangeFromBedHeights(bh);
            final Double from = range.getA().doubleValue(); // NullPointer check??
            final Double to = range.getB().doubleValue();

            bedHeightSorted.put(bh.getDescription(), formatter.format(from) + " - " + formatter.format(to));
        }
        final Iterator<String> iterator = bedHeightSorted.keySet().iterator();
        while (iterator.hasNext()) {
            final String descr = iterator.next();
            final String fromTo = bedHeightSorted.get(descr);
            final Map<String, String> row = new HashMap<>();
            row.put("year", String.valueOf(year));
            row.put("range", fromTo);
            row.put("description", descr);
            helper.addRow(row);
        }

        helper.submitMapToXml();
    }

    private double[] getLowerUpperKmRange(final List<BedHeight> bedheights) {
        double min = Double.MAX_VALUE;
        double max = -Double.MAX_VALUE;

        for (final BedHeight bh : bedheights) {
            final org.dive4elements.river.model.Range range = BedHeight.getRangeFromBedHeights(bh);
            try {
                final Double from = range.getA().doubleValue(); // NullPointer check?? -> try catch
                final Double to = range.getB().doubleValue();

                final double upper = to > from ? to : from;
                final double lower = from < to ? from : to;
                if (upper > max)
                    max = upper;

                if (lower < min)
                    min = lower;
            }
            catch (final Exception e) {
                e.printStackTrace();
            }

        }
        return new double[] { min, max };
    }
}

http://dive4elements.wald.intevation.org