view flys-backend/src/main/java/de/intevation/flys/model/River.java @ 504:ee693b8fbb55

Fixed flys/issue11 and flys51. flys-backend/trunk@1890 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 10 May 2011 17:31:10 +0000
parents b316d2106598
children ce9c6f05f464
line wrap: on
line source
package de.intevation.flys.model;

import java.io.Serializable;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
import javax.persistence.SequenceGenerator;
import javax.persistence.OneToMany;
import javax.persistence.JoinColumn;
import javax.persistence.GenerationType;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Query;

import de.intevation.flys.backend.SessionHolder;


@Entity
@Table(name = "rivers")
public class River
implements   Serializable
{
    private Integer id;

    private String  name;

    private List<Gauge> gauges;

    @Id
    @SequenceGenerator(
        name           = "SEQUENCE_RIVERS_ID_SEQ",
        sequenceName   = "RIVERS_ID_SEQ",
        allocationSize = 1)
    @GeneratedValue(
        strategy  = GenerationType.SEQUENCE,
        generator = "SEQUENCE_RIVERS_ID_SEQ")
    @Column(name = "id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public River() {
    }

    public River(String name) {
        this.name = name;
    }

    @OneToMany
    @JoinColumn(name="river_id")
    public List<Gauge> getGauges() {
        return gauges;
    }

    public void setGauges(List<Gauge> gauges) {
        this.gauges = gauges;
    }

    public String toString() {
        return name != null ? name : "";
    }


    /**
     * This method returns the gauges that intersect with <i>a</i> and
     * <i>b</i>,
     *
     * @param a A start point.
     * @param b An end point.
     *
     * @return the intersecting gauges.
     */
    public List<Gauge> determineGauges(double a, double b) {
        Session session = SessionHolder.HOLDER.get();

        Query query = session.createQuery(
            "from Gauge where river=:river " +
            "and not (range.a > :b or range.b < :a) order by a");
        query.setParameter("river", this);
        query.setParameter("a", new BigDecimal(a));
        query.setParameter("b", new BigDecimal(b));

        return query.list();
    }


    /**
     * This method returns the first gauge that is intersected by <i>a</i> and
     * <i>b</i>,
     *
     * @param a A start point.
     * @param b An end point.
     *
     * @return the first intersecting gauge.
     */
    public Gauge determineGauge(double a, double b) {
        List<Gauge> gauges = determineGauges(a, b);

        return gauges != null && gauges.size() > 0 ? gauges.get(0) : null;
    }

    /**
     * Returns the min and max distance of this river. The first position in the
     * resulting array contains the min distance, the second position the max
     * distance.
     *
     * @return the min and max distance of this river.
     */
    public double[] determineMinMaxDistance() {
        if (gauges == null) {
            return null;
        }

        double minmax[] = new double[] { Double.MAX_VALUE, Double.MIN_VALUE };

        for (Gauge g: gauges) {
            Range r = g.getRange();

            double a  = r.getA().doubleValue();
            minmax[0] = minmax[0] < a ? minmax[0] : a;

            BigDecimal bigB = r.getB();
            if (bigB != null) {
                double b  = bigB.doubleValue();
                minmax[1] = minmax[1] > b ? minmax[1] : b;
            }
        }

        return minmax;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org