view backend/src/main/java/org/dive4elements/river/model/sinfo/InfrastructureValue.java @ 9176:1614cb14308f

Work on calculations for S-Info flood duration workflow
author mschaefer
date Mon, 25 Jun 2018 19:21:11 +0200
parents a165cd63099f
children d889ffe2fb05
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.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 javax.persistence.Transient;

import org.dive4elements.river.backend.SessionHolder;
import org.dive4elements.river.model.Attribute;
import org.dive4elements.river.model.Attribute.AttributeKey;
import org.dive4elements.river.model.River;
import org.hibernate.Query;
import org.hibernate.Session;


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

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

    private static final long serialVersionUID = -3887269325288851829L;

    private Integer id;

    private Infrastructure infrastructure;

    private Double station;

    private Attribute attribute;

    private Double height;


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

    public InfrastructureValue() {
    }

    public InfrastructureValue(final Infrastructure infrastructure, final Double station, final Attribute attribute, final Double height) {
        this.infrastructure = infrastructure;
        this.station = station;
        this.attribute = attribute;
        this.height = height;
    }

    /**
     * Parameter constructor with primitive double km and height
     */
    public InfrastructureValue(final Infrastructure infrastructure, final double km, final Attribute attribute, final double height) {
        this(infrastructure, Double.valueOf(km), attribute, Double.valueOf(height));
    }


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

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

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

    @OneToOne
    @JoinColumn(name = "infrastructure_id")
    public Infrastructure getInfrastructure() {
        return this.infrastructure;
    }

    public void setInfrastructure(final Infrastructure infrastructure) {
        this.infrastructure = infrastructure;
    }

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

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

    @OneToOne
    @JoinColumn(name = "attribute_id")
    public Attribute getAttribute() {
        return this.attribute;
    }

    public void setAttribute(final Attribute attribute) {
        this.attribute = attribute;
    }

    @Transient
    public AttributeKey getAttributeKey() {
        return this.getAttribute().getKey();
    }

    @Column(name = "height")
    public Double getHeight() {
        return this.height;
    }

    public void setHeight(final Double height) {
        this.height = height;
    }

    /**
     * Selects from the database the infrastructure values of a data series in a km range
     */
    public static List<InfrastructureValue> getValues(final Infrastructure parent, final double kmLo, final double kmHi) {
        final Session session = SessionHolder.HOLDER.get();
        final Query query = session.createQuery("FROM InfrastructureValue WHERE (infrastructure=: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 infrastructure values of a km range of a river and a river side
     */
    public static List<InfrastructureValue> getValues(final River river, final double kmLo, final double kmHi, final AttributeKey riverside) {
        final Session session = SessionHolder.HOLDER.get();
        String riversideClause = "";
        if ((riverside == AttributeKey.LEFT) || (riverside == AttributeKey.RIGHT))
            riversideClause = " AND (v.attribute.id=:attr_id)";
        final Query query = session.createQuery("FROM InfrastructureValue v"
                + " WHERE (v.infrastructure.river=:river)"
                + " AND (v.station BETWEEN :kmLo - 0.0001 AND :kmHi + 0.0001)"
                + riversideClause
                + " ORDER BY v.station, v.attribute.id");
        query.setParameter("river", river);
        query.setParameter("kmLo", new Double(kmLo));
        query.setParameter("kmHi", new Double(kmHi));
        if (!riversideClause.isEmpty())
            query.setParameter("attr_id", riverside.getId());
        return query.list();
    }
}

http://dive4elements.wald.intevation.org