view backend/src/main/java/org/dive4elements/river/model/sinfo/CollisionValue.java @ 9160:38e8febd11b6

Added methods to select collision values and collision counts
author mschaefer
date Tue, 19 Jun 2018 14:24:41 +0200
parents 623b51bf03d7
children
line wrap: on
line source
/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * 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.sinfo;

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

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

import org.dive4elements.river.backend.SessionHolder;
import org.dive4elements.river.model.River;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.type.StandardBasicTypes;


/**
 * Hibernate binding for the DB table collision_values
 *
 * @author Matthias Schäfer
 *
 */
@Entity
@Table(name = "collision_values")
public class CollisionValue implements Serializable {

    /***** FIELDS *****/

    private static final long serialVersionUID = -1157324854470346513L;

    private Integer id;

    private Collision collision;

    private Double station;

    private CollisionType collisionType;

    private Date eventDate;

    private String gaugeName;

    private Double gaugeW;


    /***** CONSTRUCTORS *****/

    public CollisionValue() {
    }

    public CollisionValue(final Collision collision, final Double station, final CollisionType collisionType, final Date eventDate, final String gaugeName,
            final Double gaugeW) {
        this.collision = collision;
        this.station = station;
        this.collisionType = collisionType;
        this.eventDate = eventDate;
        this.gaugeName = gaugeName;
        this.gaugeW = gaugeW;
    }

    /**
     * Constructor with primitive parameter types
     */
    public CollisionValue(final Collision collision, final double km, final CollisionType collisionType, final Date eventDate, final String gaugeName,
            final double gaugeW) {
        this(collision, Double.valueOf(km), collisionType, eventDate, gaugeName, Double.valueOf(gaugeW));
    }


    /***** METHODS *****/

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

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

    @OneToOne
    @JoinColumn(name = "collision_id")
    public Collision getCollision() {
        return this.collision;
    }

    public void setCollision(final Collision collision) {
        this.collision = collision;
    }

    @Column(name = "station")
    public Double getStation() {
        return this.station;
    }

    public void setStation(final Double station) {
        this.station = station;
    }

    @OneToOne
    @JoinColumn(name = "collision_type_id")
    public CollisionType getCollisionType() {
        return this.collisionType;
    }

    public void setCollisionType(final CollisionType collisionType) {
        this.collisionType = collisionType;
    }

    @Column(name = "event_date")
    public Date getEventDate() {
        return this.eventDate;
    }

    public void setEventDate(final Date eventDate) {
        this.eventDate = eventDate;
    }

    @Column(name = "gauge_name")
    public String getGaugeName() {
        return this.gaugeName;
    }

    public void setGaugeName(final String gaugeName) {
        this.gaugeName = gaugeName;
    }

    @Column(name = "gauge_w")
    public Double getGaugeW() {
        return this.gaugeW;
    }

    public void setGaugeW(final Double gaugeW) {
        this.gaugeW = gaugeW;
    }

    /**
     * Selects the collision values of a data series in a km range from the database
     */
    public static List<CollisionValue> getValues(final Collision parent, final double kmLo, final double kmHi) {
        final Session session = SessionHolder.HOLDER.get();
        final Query query = session.createQuery("FROM CollisionValue WHERE (collision=:parent)"
                + " AND (station >= :kmLo - 0.0001) AND (station <= :kmHi + 0.0001)");
        query.setParameter("parent", parent);
        query.setParameter("kmLo", new Double(kmLo));
        query.setParameter("kmHi", new Double(kmHi));
        return query.list();
    }
    /**
     * Selects from the database the collision values of a km range of a river and a date range
     */
    public static List<CollisionValue> getValues(final River river, final double kmLo, final double kmHi, final Date fromDate, final Date toDate) {
        final Session session = SessionHolder.HOLDER.get();
        final SQLQuery query = session.createSQLQuery("SELECT station, event_date, gauge_w, gauge_name, ct.name AS collisiontype"
                + " FROM collision_values v INNER JOIN collision s ON v.collision_id=s.id"
                + " INNER JOIN collision_type ct ON v.collision_type_id=ct.id"
                + " WHERE (s.river_id=:river_id)"
                + " AND (station BETWEEN (:kmLo - 0.0001) AND (:kmHi + 0.0001))"
                + " AND (event_date BETWEEN :fromDate AND :toDate)"
                + " ORDER BY station, event_date")
                .addScalar("station", StandardBasicTypes.DOUBLE)
                .addScalar("event_date", StandardBasicTypes.DATE)
                .addScalar("gauge_w", StandardBasicTypes.DOUBLE)
                .addScalar("gauge_name", StandardBasicTypes.STRING)
                .addScalar("collisiontype", StandardBasicTypes.STRING);
        query.setInteger("river_id", river.getId());
        query.setDouble("kmLo", new Double(kmLo));
        query.setDouble("kmHi", new Double(kmHi));
        query.setDate("fromDate", fromDate);
        query.setDate("toDate", toDate);
        return buildCollisionValueList(query.list());
    }

    private static List<CollisionValue> buildCollisionValueList(final List<Object[]> rows) {
        final List<CollisionValue> values = new ArrayList<>();
        for (int i = 0; i <= rows.size() - 1; i++)
            values.add(new CollisionValue(null, (double) rows.get(i)[0], null, (Date) rows.get(i)[1], (String) rows.get(i)[3], (double) rows.get(i)[2]));
        return values;
    }
}

http://dive4elements.wald.intevation.org