view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/DistanceOnlyPartHistoricalSelect.java @ 9246:c08d5cfa4981

some hibernate queries on bedheigts for salix
author gernotbelger
date Thu, 12 Jul 2018 11:15:42 +0200
parents
children 1ec3b891ab02
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.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.w3c.dom.Element;

public class DistanceOnlyPartHistoricalSelect extends DistanceOnlySelect {

    private static final long serialVersionUID = 1L;
    private static Logger log = Logger.getLogger(DistanceOnlyPartHistoricalSelect.class);

    List<BedHeight> bhs = 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.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(artifact, 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) {
        if (this.bhs == null) {
            final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact);
            final Integer year = access.getYear();

            final Integer epoch = access.getEpoch();
            final boolean isEpoch = epoch == null ? false : true;
            this.bhs = BedHeight.getBedHeightYearEpoch(isEpoch, isEpoch ? epoch : year, access.getRiver(), access.getLowerKm(), access.getUpperKm());
        }
        return this.bhs;

    }

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

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

        helper.addColumn(0, "from_to", "100", "year", "INTEGER", "LEFT", null);
        helper.addColumn(1, "description", "500", "uinfo.salix.soundings", "STRING", "LEFT", null);

        final TreeMap<String, String> bedHeightSorted = new TreeMap<>();
        final double min = Double.MAX_VALUE;
        final double max = -Double.MAX_VALUE;

        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(), String.valueOf(from) + " - " + String.valueOf(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("from_to", String.valueOf(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