mschaefer@8971: /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde mschaefer@8971: * Software engineering by mschaefer@8971: * Björnsen Beratende Ingenieure GmbH mschaefer@8971: * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt mschaefer@8971: * mschaefer@8971: * This file is Free Software under the GNU AGPL (>=v3) mschaefer@8971: * and comes with ABSOLUTELY NO WARRANTY! Check out the mschaefer@8971: * documentation coming with Dive4Elements River for details. mschaefer@8971: */ mschaefer@8971: mschaefer@8971: package org.dive4elements.river.model.sinfo; mschaefer@8971: mschaefer@8971: import java.io.Serializable; mschaefer@9115: import java.util.List; mschaefer@8971: mschaefer@8971: import javax.persistence.Column; mschaefer@8971: import javax.persistence.Entity; mschaefer@8971: import javax.persistence.GeneratedValue; mschaefer@8971: import javax.persistence.GenerationType; mschaefer@8971: import javax.persistence.Id; mschaefer@8971: import javax.persistence.JoinColumn; mschaefer@8971: import javax.persistence.OneToOne; mschaefer@8971: import javax.persistence.SequenceGenerator; mschaefer@8971: import javax.persistence.Table; mschaefer@9176: import javax.persistence.Transient; mschaefer@8971: mschaefer@9115: import org.dive4elements.river.backend.SessionHolder; mschaefer@8971: import org.dive4elements.river.model.Attribute; mschaefer@9176: import org.dive4elements.river.model.Attribute.AttributeKey; mschaefer@9176: import org.dive4elements.river.model.River; mschaefer@9115: import org.hibernate.Query; mschaefer@9115: import org.hibernate.Session; mschaefer@8971: mschaefer@8971: mschaefer@8971: /** mschaefer@8971: * Hibernate binding for the DB table infrastructure_values mschaefer@8971: * mschaefer@8971: * @author Matthias Schäfer mschaefer@8971: * mschaefer@8971: */ mschaefer@8971: @Entity mschaefer@8971: @Table(name = "infrastructure_values") mschaefer@8971: public class InfrastructureValue implements Serializable { mschaefer@8971: mschaefer@8971: /***** FIELDS *****/ mschaefer@8971: mschaefer@8971: private static final long serialVersionUID = -3887269325288851829L; mschaefer@8971: mschaefer@8971: private Integer id; mschaefer@8971: mschaefer@8971: private Infrastructure infrastructure; mschaefer@8971: mschaefer@8971: private Double station; mschaefer@8971: mschaefer@8971: private Attribute attribute; mschaefer@8971: mschaefer@8971: private Double height; mschaefer@8971: mschaefer@8971: mschaefer@8971: /***** CONSTRUCTORS *****/ mschaefer@8971: mschaefer@8971: public InfrastructureValue() { mschaefer@8971: } mschaefer@8971: mschaefer@8971: public InfrastructureValue(final Infrastructure infrastructure, final Double station, final Attribute attribute, final Double height) { mschaefer@8971: this.infrastructure = infrastructure; mschaefer@8971: this.station = station; mschaefer@8971: this.attribute = attribute; mschaefer@8971: this.height = height; mschaefer@8971: } mschaefer@8971: mschaefer@8971: /** mschaefer@8971: * Parameter constructor with primitive double km and height mschaefer@8971: */ mschaefer@8971: public InfrastructureValue(final Infrastructure infrastructure, final double km, final Attribute attribute, final double height) { mschaefer@8971: this(infrastructure, Double.valueOf(km), attribute, Double.valueOf(height)); mschaefer@8971: } mschaefer@8971: mschaefer@8971: mschaefer@8971: /***** METHODS *****/ mschaefer@8971: mschaefer@8971: @Id mschaefer@8971: @SequenceGenerator(name = "SEQUENCE_INFRASTRUCTURE_VALUE_ID_SEQ", sequenceName = "INFRASTRUCTURE_VALUES_ID_SEQ", allocationSize = 1) mschaefer@8971: @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_INFRASTRUCTURE_VALUE_ID_SEQ") mschaefer@8971: @Column(name = "id") mschaefer@8971: public Integer getId() { mschaefer@8971: return this.id; mschaefer@8971: } mschaefer@8971: mschaefer@8971: public void setId(final Integer id) { mschaefer@8971: this.id = id; mschaefer@8971: } mschaefer@8971: mschaefer@8971: @OneToOne mschaefer@8971: @JoinColumn(name = "infrastructure_id") mschaefer@8971: public Infrastructure getInfrastructure() { mschaefer@8971: return this.infrastructure; mschaefer@8971: } mschaefer@8971: mschaefer@8971: public void setInfrastructure(final Infrastructure infrastructure) { mschaefer@8971: this.infrastructure = infrastructure; mschaefer@8971: } mschaefer@8971: mschaefer@8971: @Column(name = "station") mschaefer@8971: public Double getStation() { mschaefer@8971: return this.station; mschaefer@8971: } mschaefer@8971: mschaefer@8971: public void setStation(final Double station) { mschaefer@8971: this.station = station; mschaefer@8971: } mschaefer@8971: mschaefer@8971: @OneToOne mschaefer@8971: @JoinColumn(name = "attribute_id") mschaefer@8971: public Attribute getAttribute() { mschaefer@8971: return this.attribute; mschaefer@8971: } mschaefer@8971: mschaefer@8971: public void setAttribute(final Attribute attribute) { mschaefer@8971: this.attribute = attribute; mschaefer@8971: } mschaefer@8971: mschaefer@9176: @Transient mschaefer@9176: public AttributeKey getAttributeKey() { mschaefer@9176: return this.getAttribute().getKey(); mschaefer@9176: } mschaefer@9176: mschaefer@8971: @Column(name = "height") mschaefer@8971: public Double getHeight() { mschaefer@8971: return this.height; mschaefer@8971: } mschaefer@8971: mschaefer@8971: public void setHeight(final Double height) { mschaefer@8971: this.height = height; mschaefer@8971: } mschaefer@9115: mschaefer@9115: /** mschaefer@9176: * Selects from the database the infrastructure values of a data series in a km range mschaefer@9115: */ mschaefer@9115: public static List getValues(final Infrastructure parent, final double kmLo, final double kmHi) { mschaefer@9115: final Session session = SessionHolder.HOLDER.get(); mschaefer@9115: final Query query = session.createQuery("FROM InfrastructureValue WHERE (infrastructure=:parent)" mschaefer@9115: + " AND (station >= :kmLo - 0.0001) AND (station <= :kmHi + 0.0001)"); mschaefer@9115: query.setParameter("parent", parent); mschaefer@9115: query.setParameter("kmLo", new Double(kmLo)); mschaefer@9115: query.setParameter("kmHi", new Double(kmHi)); mschaefer@9115: return query.list(); mschaefer@9115: } mschaefer@9176: mschaefer@9176: /** mschaefer@9176: * Selects from the database the infrastructure values of a km range of a river and a river side mschaefer@9176: */ mschaefer@9176: public static List getValues(final River river, final double kmLo, final double kmHi, final AttributeKey riverside) { mschaefer@9176: final Session session = SessionHolder.HOLDER.get(); mschaefer@9176: final Query query = session.createQuery("FROM InfrastructureValue v" mschaefer@9176: + " WHERE (v.infrastructure.river=:river)" mschaefer@9176: + " AND (v.station BETWEEN :kmLo - 0.0001 AND :kmHi + 0.0001)" mschaefer@9614: + getRiversideClause(riverside, "v", "attr_id") mschaefer@9176: + " ORDER BY v.station, v.attribute.id"); mschaefer@9176: query.setParameter("river", river); mschaefer@9176: query.setParameter("kmLo", new Double(kmLo)); mschaefer@9176: query.setParameter("kmHi", new Double(kmHi)); mschaefer@9614: if (!getRiversideClause(riverside, "v", "attr_id").isEmpty()) mschaefer@9176: query.setParameter("attr_id", riverside.getId()); mschaefer@9176: return query.list(); mschaefer@9176: } mschaefer@9614: mschaefer@9614: /** mschaefer@9614: * Gets a query's and-where-clause for a riverside key mschaefer@9614: */ mschaefer@9614: public static String getRiversideClause(final AttributeKey riverside, final String tablealias, final String variable) { mschaefer@9614: if ((riverside == AttributeKey.LEFT) || (riverside == AttributeKey.RIGHT)) mschaefer@9614: return " AND (" + tablealias + ".attribute.id=:" + variable + ")"; mschaefer@9614: else mschaefer@9614: return ""; mschaefer@9614: } mschaefer@8971: }