Mercurial > dive4elements > river
view backend/src/main/java/org/dive4elements/river/model/Wst.java @ 9594:225e48df608c
Softwaretests...20181219 6.1: overflow day compute with 365.25 instead of 365.0
author | mschaefer |
---|---|
date | Tue, 05 Feb 2019 15:24:22 +0100 |
parents | 35afa0349782 |
children |
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.OneToMany; import javax.persistence.OneToOne; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.apache.log4j.Logger; import org.dive4elements.river.backend.SessionHolder; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.type.StandardBasicTypes; /** DB-mapped WST. */ @Entity @Table(name = "wsts") public class Wst implements Serializable { private static Logger log = Logger.getLogger(Wst.class); private Integer id; private River river; private String description; private Integer kind; private String sinfo_selection; private List<WstColumn> columns; public static final String SQL_SELECT_MINMAX = "select min(q) as minQ, max(q) as maxQ from wst_q_values " + "where wst_id = :wst and not (a > :km or b < :km)"; public Wst() { } public Wst(final River river, final String description) { this(river, description, 0); } public Wst(final River river, final String description, final Integer kind) { this.river = river; this.description = description; this.kind = kind; } @Id @SequenceGenerator( name = "SEQUENCE_WSTS_ID_SEQ", sequenceName = "WSTS_ID_SEQ", allocationSize = 1) @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_WSTS_ID_SEQ") @Column(name = "id") public Integer getId() { return this.id; } public void setId(final Integer id) { this.id = id; } @OneToOne @JoinColumn(name = "river_id" ) public River getRiver() { return this.river; } public void setRiver(final River river) { this.river = river; } @Column(name = "description") public String getDescription() { return this.description; } public void setDescription(final String description) { this.description = description; } @Column(name = "kind") public Integer getKind() { return this.kind; } public void setKind(final Integer kind) { this.kind = kind; } @Column(name = "sinfo_selection") public String getSInfoSelection() { return this.sinfo_selection; } public void setSInfoSelection(final String sinfoSelection) { this.sinfo_selection = sinfoSelection; } @OneToMany @JoinColumn(name="wst_id") public List<WstColumn> getColumns() { return this.columns; } public void setColumns(final List<WstColumn> columns) { this.columns = columns; } /** * Determines the min and max Q values of this WST. The min value is placed * in the first field of the resulting array - the max value is placed in * the second field. * * @return the min and max Q values of this WST. */ public double[] determineMinMaxQ() { final double[] ab = this.river.determineMinMaxDistance(); return determineMinMaxQ(new Range(ab[0], ab[1], this.river)); } /** * Determines the min and max Q values of this WST in the given range. The * min value is placed in the first field of the resulting array - the max * value is placed in the second field. * * @param range The range used for querying the Q values. * * @return the min and max Q values of this WST. */ public double[] determineMinMaxQ(final Range range) { if (range != null) { return determineMinMaxQ( range.getA().doubleValue(), range.getB().doubleValue()); } return null; } /** * Determines the min and max Q values of this WST in the given range. The * min value is placed in the first field of the resulting array - the max * value is placed in the second field. * * @param fromKm the lower km value. * @param toKm the upper km value. * * @return the min and max Q values of this WST. */ public double[] determineMinMaxQ(final double fromKm, final double toKm) { final Session session = SessionHolder.HOLDER.get(); final Query query = session.createQuery( "select min(q), max(q) from WstQRange where " + "id in " + " (select wstQRange.id from WstColumnQRange where " + " wstColumn.id in (select id from WstColumn where wst.id = :wst)) " + "and range.id in " + " (select id from Range where not (a > :end or b < :start))"); query.setParameter("wst", getId()); query.setParameter("start", new BigDecimal(fromKm)); query.setParameter("end", new BigDecimal(toKm)); final List<Object []> results = query.list(); if (results.isEmpty()) { return null; } if (results.get(0)[0] == null || results.get(0)[1] == null) { log.warn("Could not process result from min/maxQ query."); return null; } final Object [] result = results.get(0); return new double [] { ((BigDecimal)result[0]).doubleValue(), ((BigDecimal)result[1]).doubleValue() }; } public double[] determineMinMaxQFree(final double km) { final Session session = SessionHolder.HOLDER.get(); final SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_MINMAX) .addScalar("minQ", StandardBasicTypes.DOUBLE) .addScalar("maxQ", StandardBasicTypes.DOUBLE); sqlQuery.setInteger("wst", getId()); sqlQuery.setDouble("km", km); final List<Object[]> minmaxQ = sqlQuery.list(); if (minmaxQ.isEmpty()) { return null; } final Object[] mm = minmaxQ.get(0); if (mm[0] == null || mm[1] == null) { log.warn("No min/max Q for km " + km + " found."); return null; } return new double[] { (Double) mm[0], (Double) mm[1] }; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :