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

Work on calculations for S-Info flood duration workflow
author mschaefer
date Mon, 25 Jun 2018 19:21:11 +0200
parents 64e56a51db3f
children 491e1a434457
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.math.BigDecimal;
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.MainValueType.MainValueTypeKey;
import org.hibernate.Query;
import org.hibernate.Session;


/** A Main or Extreme value of a rivers gauge. */
@Entity
@Table(name = "main_values")
public class MainValue
implements   Serializable
{
    private Integer        id;

    private Gauge          gauge;

    private NamedMainValue mainValue;

    private BigDecimal     value;

    private TimeInterval   timeInterval;

    public MainValue() {
    }

    public MainValue(
            final Gauge          gauge,
            final NamedMainValue mainValue,
            final BigDecimal     value,
            final TimeInterval   timeInterval
            ) {
        this.gauge        = gauge;
        this.mainValue    = mainValue;
        this.value        = value;
        this.timeInterval = timeInterval;
    }

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

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

    @OneToOne
    @JoinColumn(name = "gauge_id")
    public Gauge getGauge() {
        return this.gauge;
    }

    public void setGauge(final Gauge gauge) {
        this.gauge = gauge;
    }

    @OneToOne
    @JoinColumn(name = "named_value_id")
    public NamedMainValue getMainValue() {
        return this.mainValue;
    }

    public void setMainValue(final NamedMainValue mainValue) {
        this.mainValue = mainValue;
    }

    @Column(name = "value") // FIXME: type mapping needed?
    public BigDecimal getValue() {
        return this.value;
    }

    public void setValue(final BigDecimal value) {
        this.value = value;
    }

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

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

    /**
     * Selects from the database the main values of a gauge and a type, in ascending value order
     */
    public static List<MainValue> getValuesOfGaugeAndType(final Gauge gauge, final MainValueTypeKey typekey) {
        final Session session = SessionHolder.HOLDER.get();
        final Query query = session.createQuery("SELECT mv"
                + " FROM MainValue AS mv JOIN mv.mainValue AS nmv"
                + " WHERE mv.gauge.id=:gaugeid AND nmv.type.id=:typeid"
                + " ORDER BY value");
        query.setParameter("gaugeid", gauge.getId());
        query.setParameter("typeid", typekey.getId());
        return query.list();
    }

    /**
     * Selects from the database the discharge-duration main values of a gauge sorted by duration
     */
    public static List<MainValue> getDurationDischargesOfGauge(final Gauge gauge) {
        final Session session = SessionHolder.HOLDER.get();
        final Query query = session.createQuery("SELECT mv"
                + " FROM MainValue AS mv JOIN mv.mainValue AS nmv"
                + " WHERE mv.gauge.id=:gaugeid AND nmv.type.id=:typeid"
                + " ORDER BY CAST(nmv.name AS int)");
        query.setParameter("gaugeid", gauge.getId());
        query.setParameter("typeid", MainValueTypeKey.DURATION.getId());
        return query.list();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org