view backend/src/main/java/org/dive4elements/river/model/DischargeTable.java @ 9201:491e1a434457

Renamed getValues... to fetch...
author mschaefer
date Sun, 01 Jul 2018 15:29:40 +0200
parents 64e56a51db3f
children f2473dc34535
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.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.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.dive4elements.river.backend.SessionHolder;
import org.hibernate.Query;
import org.hibernate.Session;

@Entity
@Table(name = "discharge_tables")
public class DischargeTable
implements   Serializable, Comparable<DischargeTable>
{
    private Integer      id;
    private Gauge        gauge;
    private String       description;
    private String       bfgId;
    private Integer      kind;
    private TimeInterval timeInterval;

    private List<DischargeTableValue> dischargeTableValues;

    public DischargeTable() {
        this.kind = 0;
    }

    public DischargeTable(final Gauge gauge) {
        this(gauge, null, null, 0, null);
    }

    public DischargeTable(
            final Gauge        gauge,
            final String       description,
            final String       bfgId,
            final Integer      kind,
            final TimeInterval timeInterval
            ) {
        this.gauge        = gauge;
        this.description  = description;
        this.bfgId        = bfgId;
        this.kind         = kind;
        this.timeInterval = timeInterval;
    }

    @Id
    @SequenceGenerator(
            name           = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ",
            sequenceName   = "DISCHARGE_TABLES_ID_SEQ",
            allocationSize = 1)
    @GeneratedValue(
            strategy  = GenerationType.SEQUENCE,
            generator = "SEQUENCE_DISCHARGE_TABLES_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;
    }

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

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

    @Column(name = "bfg_id")
    public String getBfgId() {
        return this.bfgId;
    }

    public void setBfgId(final String bfgId) {
        this.bfgId = bfgId;
    }

    @Column(name = "kind")
    public Integer getKind() {
        return this.kind;
    }

    public void setKind(final Integer kind) {
        this.kind = kind;
    }

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

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

    @OneToMany
    @JoinColumn(name = "table_id")
    @OrderBy("q")
    public List<DischargeTableValue> getDischargeTableValues() {
        return this.dischargeTableValues;
    }

    public void setDischargeTableValues(
            final List<DischargeTableValue> dischargeTableValues
            ) {
        this.dischargeTableValues = dischargeTableValues;
    }

    @Transient
    public double[] getWs() {
        final double[] ws = new double[this.dischargeTableValues.size()];
        for (int i=0; i<=this.dischargeTableValues.size()-1; i++)
            ws[i] = this.dischargeTableValues.get(i).getW().doubleValue();

        return ws;
    }

    @Override
    public int compareTo(final DischargeTable o) {
        if (getKind() == 0 && o.getKind() != 0) {
            return 1;
        }

        final TimeInterval other = o.getTimeInterval();
        if (other == null && this.timeInterval == null) {
            return 1;
        }
        else if (other == null) {
            return -1;
        }
        else if (this.timeInterval == null) {
            return 1;
        }

        final Date otherStartTime = other.getStartTime();
        final Date thisStartTime  = this.timeInterval.getStartTime();

        if (otherStartTime == null) {
            return -1;
        }
        else if (thisStartTime == null) {
            return 1;
        }

        final long otherStart = otherStartTime.getTime();
        final long thisStart  = thisStartTime.getTime();

        if (otherStart < thisStart) {
            return 1;
        }
        else if (otherStart > thisStart) {
            return -1;
        }

        final Date otherStopTime  = other.getStopTime();
        final Date thisStopTime  = this.timeInterval.getStopTime();

        if (otherStopTime == null) {
            return -1;
        }
        else if (thisStopTime == null) {
            return 1;
        }

        final long otherEnd   = otherStopTime.getTime();
        final long thisEnd    = thisStopTime.getTime();

        if (otherEnd < thisEnd) {
            return 1;
        }
        else if (otherEnd > thisEnd) {
            return -1;
        }
        else {
            return 0;
        }
    }

    public static DischargeTable getDischargeTableById(final int dtId)
    {
        final Session session = SessionHolder.HOLDER.get();
        final Query query = session.createQuery("from DischargeTable where id =:dtId");
        query.setParameter("dtId", dtId);

        final List<DischargeTable> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * Selects from the database the values of a discharge table sorted by W
     */
    public static List<DischargeTableValue> fetchValuesSortedByW(final DischargeTable dischargeTable) {
        final Session session = SessionHolder.HOLDER.get();
        final Query query = session.createQuery("FROM DischargeTableValue WHERE table_id=:parentid ORDER BY w");
        query.setParameter("parentid", dischargeTable.getId());
        return query.list();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org