Mercurial > dive4elements > river
view backend/src/main/java/org/dive4elements/river/model/sinfo/CollisionAggregateValue.java @ 9405:34cd4faf43f4
minMax queries
author | gernotbelger |
---|---|
date | Wed, 15 Aug 2018 14:34:23 +0200 |
parents | 38e8febd11b6 |
children |
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.ArrayList; import java.util.Date; import java.util.List; import org.dive4elements.river.backend.SessionHolder; import org.dive4elements.river.backend.utils.DateUtil; import org.dive4elements.river.model.River; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.type.StandardBasicTypes; /** * Aggregation of collision_values * * @author Matthias Schäfer * */ public class CollisionAggregateValue implements Serializable { /***** FIELDS *****/ private static final long serialVersionUID = 1; private Collision parent; private double station; // private CollisionType collisionType; private Date minDate; private Date maxDate; private int count; private double gaugeW; /***** CONSTRUCTORS *****/ public CollisionAggregateValue() { } public CollisionAggregateValue(final Collision collision, final double station, final Date minDate, final Date maxDate, final int count, final double gaugeW) { this.parent = collision; this.station = station; // this.collisionType = collisionType; this.minDate = minDate; this.maxDate = maxDate; this.count = count; this.gaugeW = gaugeW; } /***** METHODS *****/ public int getCount() { return this.count; } public Collision getCollision() { return this.parent; } public double getStation() { return this.station; } // public CollisionType getCollisionType() { // return this.collisionType; // } public Date getMinDate() { return this.minDate; } public Date getMaxDate() { return this.maxDate; } public Double getGaugeW() { return this.gaugeW; } /** * Selects from the database the collision aggregate values of a data series in a km range */ public static List<CollisionAggregateValue> getValuesByKm(final Collision parent, final double kmLo, final double kmHi) { final Session session = SessionHolder.HOLDER.get(); final SQLQuery query = session.createSQLQuery("SELECT FLOOR(station+0.4999) AS station, COUNT(*) AS count," + " MIN(event_date) AS date_min, MAX(event_date) AS date_max," + " AVG(gauge_w) AS gauge_w" + " FROM collision_values v INNER JOIN collision s ON v.collision_id=s.id" + " WHERE (collision_id=:parent)" + " AND (station BETWEEN (:kmLo - 0.0001) AND (:kmHi + 0.0001))" + " GROUP BY FLOOR(station+0.4999)" + " ORDER BY FLOOR(station+0.4999)") .addScalar("station", StandardBasicTypes.DOUBLE).addScalar("count", StandardBasicTypes.INTEGER) .addScalar("date_min", StandardBasicTypes.DATE).addScalar("date_max", StandardBasicTypes.DATE) .addScalar("gauge_w", StandardBasicTypes.DOUBLE); query.setInteger("parent", parent.getId()); query.setDouble("kmLo", new Double(kmLo)); query.setDouble("kmHi", new Double(kmHi)); final List<CollisionAggregateValue> values = new ArrayList(); final List<Object[]> rows = query.list(); if (rows != null) { for (int i = 0; i <= rows.size() - 1; i++) { values.add(new CollisionAggregateValue(parent, (double) rows.get(i)[0], (Date) rows.get(i)[2], (Date) rows.get(i)[3], (int) rows.get(i)[1], (double) rows.get(i)[4])); } } return values; } /** * Selects from the database the collision aggregate values of a km range of a river and a range of years */ public static List<CollisionAggregateValue> getValuesByKm(final River river, final double kmLo, final double kmHi, final int fromYear, final int toYear) { final Session session = SessionHolder.HOLDER.get(); final SQLQuery query = session.createSQLQuery("SELECT FLOOR(station+0.4999) AS station, COUNT(*) AS count," + " MIN(event_date) AS date_min, MAX(event_date) AS date_max," + " AVG(gauge_w) AS gauge_w" + " FROM collision_values v INNER JOIN collision s ON v.collision_id=s.id" + " WHERE (s.river_id=:river_id)" + " AND (station BETWEEN (:kmLo - 0.0001) AND (:kmHi + 0.0001))" + " AND (event_date BETWEEN :fromDate AND :toDate)" + " GROUP BY FLOOR(station+0.4999)" + " ORDER BY FLOOR(station+0.4999)") .addScalar("station", StandardBasicTypes.DOUBLE).addScalar("count", StandardBasicTypes.INTEGER) .addScalar("date_min", StandardBasicTypes.DATE).addScalar("date_max", StandardBasicTypes.DATE) .addScalar("gauge_w", StandardBasicTypes.DOUBLE); query.setInteger("river_id", river.getId()); query.setDouble("kmLo", new Double(kmLo)); query.setDouble("kmHi", new Double(kmHi)); query.setDate("fromDate", DateUtil.getStartDateFromYear(fromYear)); query.setDate("toDate", DateUtil.getEndDateFromYear(toYear)); final List<CollisionAggregateValue> values = new ArrayList(); final List<Object[]> rows = query.list(); if (rows != null) { for (int i = 0; i <= rows.size() - 1; i++) { values.add(new CollisionAggregateValue(null, (double) rows.get(i)[0], (Date) rows.get(i)[2], (Date) rows.get(i)[3], (int) rows.get(i)[1], (double) rows.get(i)[4])); } } return values; } }