view backend/src/main/java/org/dive4elements/river/model/BedHeightEpoch.java @ 6229:3fea9701d58d

Fix gauge determination. If we look upstream ( a > b ) we still have to compare the start value of our range against the gauge and not the end value. The start value is always the relevant value as we handle the direction by checking this against A or B of the gauge. Also: I will never compare doubles with == again. I will never compare doubles with == again. I will never compare doubles with == again. I will never compare doubles with == again. I will never compare doubles with == again.
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 06 Jun 2013 18:23:17 +0200
parents 4c3ccf2b0304
children
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.model;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;

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.GenerationType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.OneToMany;

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

import org.dive4elements.river.backend.SessionHolder;


@Entity
@Table(name = "bed_height_epoch")
public class BedHeightEpoch implements Serializable {

    private Integer id;

    private River river;

    private TimeInterval timeInterval;

    private ElevationModel curElevationModel;
    private ElevationModel oldElevationModel;

    private Range range;

    private String evaluationBy;
    private String description;

    private List<BedHeightEpochValue> values;


    public BedHeightEpoch() {
    }


    public BedHeightEpoch(
        River          river,
        TimeInterval   timeInterval,
        Range          range,
        ElevationModel curElevationModel,
        ElevationModel oldElevationModel,
        String         evaluationBy,
        String         description
    ) {
        this.river             = river;
        this.timeInterval      = timeInterval;
        this.range             = range;
        this.curElevationModel = curElevationModel;
        this.oldElevationModel = oldElevationModel;
        this.evaluationBy      = evaluationBy;
        this.description       = description;
        this.values            = new ArrayList<BedHeightEpochValue>();
    }


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

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

    @OneToOne
    @JoinColumn(name = "river_id" )
    public River getRiver() {
        return river;
    }

    public void setRiver(River river) {
        this.river = river;
    }

    @OneToOne
    @JoinColumn(name = "time_interval_id")
    public TimeInterval getTimeInterval() {
        return timeInterval;
    }

    public void setTimeInterval(TimeInterval timeInterval) {
        this.timeInterval = timeInterval;
    }

    @OneToOne
    @JoinColumn(name = "cur_elevation_model_id")
    public ElevationModel getCurElevationModel() {
        return curElevationModel;
    }

    public void setCurElevationModel(ElevationModel curElevationModel) {
        this.curElevationModel = curElevationModel;
    }

    @OneToOne
    @JoinColumn(name = "old_elevation_model_id")
    public ElevationModel getOldElevationModel() {
        return oldElevationModel;
    }

    public void setOldElevationModel(ElevationModel oldElevationModel) {
        this.oldElevationModel = oldElevationModel;
    }

    @OneToOne
    @JoinColumn(name = "range_id")
    public Range getRange() {
        return range;
    }

    public void setRange(Range range) {
        this.range = range;
    }

    @Column(name = "evaluation_by")
    public String getEvaluationBy() {
        return evaluationBy;
    }

    public void setEvaluationBy(String evaluationBy) {
        this.evaluationBy = evaluationBy;
    }

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

    public void setDescription(String description) {
        this.description = description;
    }

    @OneToMany
    @JoinColumn(name = "bed_height_epoch_id")
    public List<BedHeightEpochValue> getValues() {
        return values;
    }

    public void setValues(List<BedHeightEpochValue> values) {
        this.values = values;
    }


    public static List<BedHeightEpoch> getBedHeightEpochs(
        River  river,
        double kmLo,
        double kmHi
    ) {
        Session session = SessionHolder.HOLDER.get();

        Query query = session.createQuery(
            "from BedHeightEpoch where river=:river");

        query.setParameter("river", river);

        // TODO Do km range filtering in SQL statement

        List<BedHeightEpoch> epochs = query.list();
        List<BedHeightEpoch> good   = new ArrayList<BedHeightEpoch>();

        OUTER: for (BedHeightEpoch e: epochs) {
            for (BedHeightEpochValue value: e.getValues()) {
                double station = value.getStation().doubleValue();

                if (station >= kmLo && station <= kmHi) {
                    good.add(e);
                    continue OUTER;
                }
            }
        }

        return good;
    }


    public static BedHeightEpoch getBedHeightEpochById(int id) {
        Session session = SessionHolder.HOLDER.get();

        Query query = session.createQuery(
            "from BedHeightEpoch where id=:id");

        query.setParameter("id", id);

        List<BedHeightEpoch> singles = query.list();

        return singles != null ? singles.get(0) : null;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org