view backend/src/main/java/org/dive4elements/river/model/sinfo/InfrastructureValue.java @ 9620:26e113e8224f

Nachtrag Pos. 20: flood duration calculation for multiple infrastructure groups/types, local class FloodDurationCalculationResult.Infrastructure renamed and extracted into own class
author mschaefer
date Thu, 10 Oct 2019 17:11:54 +0200
parents d889ffe2fb05
children 02ca823ec9c6
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 java.util.Set;

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 group/type/river-side selection
     */
    public static List<InfrastructureValue> getValues(final River river, final double kmLo, final double kmHi, final AttributeKey riverside,
            final Set<String> groupTypes) {
        final Session session = SessionHolder.HOLDER.get();
        final Query query = session.createQuery("FROM InfrastructureValue"
                + " WHERE (infrastructure.river=:river)"
                + " AND (station BETWEEN :kmLo - 0.0001 AND :kmHi + 0.0001)"
                + getRiversideClause(riverside, "", "attr_id")
                + getGroupTypeClause(groupTypes, "")
                + " ORDER BY station, attribute.id");
        query.setParameter("river", river);
        query.setParameter("kmLo", new Double(kmLo));
        query.setParameter("kmHi", new Double(kmHi));
        if (!getRiversideClause(riverside, "", "attr_id").isEmpty())
            query.setParameter("attr_id", riverside.getId());
        return query.list();
    }

    /**
     * Gets a query's and-where-clause for a set of infrastructure group-type-pairs (as tab-separated strings)
     */
    private static String getGroupTypeClause(final Set<String> groupTypes, final String tableprefix) {
        if (groupTypes.size() == 0)
            return "";
        String clause = " AND (";
        String sep = "";
        for (final String groupType : groupTypes) {
            clause += sep + "(" + tableprefix + "infrastructure.group.name='" + groupType.split("\t")[0] + "'"
                    + " AND " + tableprefix + "infrastructure.type.name='" + groupType.split("\t")[1] + "')";
            sep = " OR ";
        }
        if (sep.length() >= 1)
            clause += ")";
        return clause;
    }

    /**
     * Gets a query's and-where-clause for a riverside key (tableprefix empty or starting with a dot)
     */
    public static String getRiversideClause(final AttributeKey riverside, final String tableprefix, final String variable) {
        if ((riverside == AttributeKey.LEFT) || (riverside == AttributeKey.RIGHT))
            return " AND (" + tableprefix + "attribute.id=:" + variable + ")";
        else
            return "";
    }
}

http://dive4elements.wald.intevation.org