view artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedOverview.java @ 6665:b7945db8a43b

issue1413: Only show unknown sediment loads of selected unit type. Therefore, adjusted the factory to take the units name. Unfortunately, names in db do not match values of data items. Thus do manual replacing. In Facet and Calculate, take the chosen unit via access and to the string replacement. In Facet, do not transform data (we assume it comes in unit as labeled in the db), and removed the possibility of m3/a-data of unknown yields in a t/a diagram and vice versa.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 25 Jul 2013 15:08:13 +0200
parents af13ceeba52a
children 2e11fc7f5d35
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.model.minfo;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.type.StandardBasicTypes;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.dive4elements.river.utils.KMIndex;

public class BedOverview
implements Serializable
{
    /**
     * Serial version UId.
     */
    private static final long serialVersionUID = -7967134407371364911L;

    public interface Filter {
        boolean accept(KMIndex<List<Date>> entry);

    } // interface Filter


    public static final Filter ACCEPT = new Filter() {
        public boolean accept(KMIndex<List<Date>> entry) {
            return true;
        }
    };

    public static class KmFilter implements Filter {

        protected double km;

        public KmFilter (double km) {
            this.km = km;
        }
        public boolean accept(KMIndex<List<Date>> list) {
            for (KMIndex.Entry<List<Date>> e: list){
                if (e.getKm() == km) {
                    return true;
                }
            }
            return false;
        }
    };

    public static class DateFilter implements Filter {

        protected Date date;

        public DateFilter (Date date) {
            this.date = date;
        }

        public boolean accept(KMIndex<List<Date>> list) {
            for (KMIndex.Entry<List<Date>> e: list){
                if (e.getValue().equals(this.date)) {
                    return true;
                }
            }
            return false;
        }
    };

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

    public static final double EPSILON = 1e-4;

    public static final String DATE_FORMAT = "dd.MM.yyyy";

    public static final String SQL_SQ =
        "SELECT" +
        "    so.km    AS km," +
        "    so.datum AS datum " +
        "FROM sohltest so " +
        "    JOIN station s" +
        "       ON so.stationid = s.stationid " +
        "    JOIN gewaesser g " +
        "       ON s.gewaesserid = g.gewaesserid " +
        "WHERE" +
        "    g.name = :name AND" +
        "    so.km IS NOT NULL " +
        "ORDER by" +
        "    so.km, so.datum";

    protected String       riverName;

    protected KMIndex<List<Date>> entries;

    public BedOverview() {
        entries = new KMIndex<List<Date>>();
    }

    public BedOverview(String riverName) {
        this();
        this.riverName = riverName;
    }

    private static final boolean epsilonEquals(double a, double b) {
        return Math.abs(a - b) < EPSILON;
    }

    protected void loadData(Session session) {
        SQLQuery query = session.createSQLQuery(SQL_SQ)
            .addScalar("km",    StandardBasicTypes.DOUBLE)
            .addScalar("datum", StandardBasicTypes.DATE);

        query.setString("name", riverName);

        List<Object []> list = query.list();

        if (list.isEmpty()) {
            log.warn("No river '" + riverName + "' found.");
        }

        Double prevKm = -Double.MAX_VALUE;
        List<Date> dates = new ArrayList<Date>();

        for (Object [] row: list) {
            Double km = (Double)row[0];
            if (!epsilonEquals(km, prevKm) && !dates.isEmpty()) {
                entries.add(prevKm, dates);
                dates = new ArrayList<Date>();
            }
            dates.add((Date)row[1]);
            prevKm = km;
        }

        if (!dates.isEmpty()) {
            entries.add(prevKm, dates);
        }
    }

    public boolean load(Session session) {

        loadData(session);

        return true;
    }


    public void generateOverview(Document document) {
        generateOverview(document, ACCEPT);
    }

    public KMIndex<List<Date>> filter(Filter f) {
        // TODO: Apply filter
        return entries;
    }

    public void generateOverview(
        Document document,
        Filter   filter
    ) {
        KMIndex<List<Date>> filtered = filter(ACCEPT);

        Element sqElement = document.createElement("bed");

        Element riverElement = document.createElement("river");

        riverElement.setAttribute("name", riverName);

        sqElement.appendChild(riverElement);

        SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);

        Element kmE = document.createElement("km");

        for (KMIndex.Entry<List<Date>> e: filtered) {

            List<Date> dates = e.getValue();

            if (!dates.isEmpty()) {
                Element dEs = document.createElement("dates");

                for (Date d: dates) {
                    Element dE = document.createElement("date");

                    dE.setAttribute("value", df.format(d));

                    dEs.appendChild(dE);
                }

                kmE.appendChild(dEs);
            }
        }

        sqElement.appendChild(kmE);

        document.appendChild(sqElement);
    }
}

http://dive4elements.wald.intevation.org