comparison backend/src/main/java/org/dive4elements/river/model/sinfo/DailyDischargeValue.java @ 9405:34cd4faf43f4

minMax queries
author gernotbelger
date Wed, 15 Aug 2018 14:34:23 +0200
parents bc9a45d2b1fa
children 38201f5b0dd9
comparison
equal deleted inserted replaced
9404:bc9a45d2b1fa 9405:34cd4faf43f4
9 */ 9 */
10 10
11 package org.dive4elements.river.model.sinfo; 11 package org.dive4elements.river.model.sinfo;
12 12
13 import java.io.Serializable; 13 import java.io.Serializable;
14 import java.util.ArrayList;
15 import java.util.Date; 14 import java.util.Date;
16 import java.util.List; 15 import java.util.List;
17 16
18 import javax.persistence.Column; 17 import javax.persistence.Column;
19 import javax.persistence.Entity; 18 import javax.persistence.Entity;
46 * Field to use in a query's order-by clause 45 * Field to use in a query's order-by clause
47 * 46 *
48 */ 47 */
49 public enum OrderByField { 48 public enum OrderByField {
50 DAY, DISCHARGE; 49 DAY, DISCHARGE;
50 }
51
52 public enum MinMax {
53 min, max
51 } 54 }
52 55
53 /***** FIELDS *****/ 56 /***** FIELDS *****/
54 57
55 private static final long serialVersionUID = -6192738825193230784L; 58 private static final long serialVersionUID = -6192738825193230784L;
137 } 140 }
138 141
139 /** 142 /**
140 * Selects from the database the daily discharge, puts the first and the last into a List (min, max day) 143 * Selects from the database the daily discharge, puts the first and the last into a List (min, max day)
141 */ 144 */
142 public static List<DailyDischargeValue> getGlobalMinMax(final Gauge gauge) { 145
146 public static Date getGlobalMinMax(final Gauge gauge, final MinMax minmax) {
143 final Session session = SessionHolder.HOLDER.get(); 147 final Session session = SessionHolder.HOLDER.get();
144 final Query query = session 148 final Query query = session.createQuery(
145 .createQuery("SELECT v" + " FROM DailyDischargeValue AS v JOIN v.dailyDischarge AS s" + " WHERE (s.gauge.id=:gaugeid) ORDER BY day"); 149 "SELECT " + minmax.toString() + "(v.day) " + " FROM DailyDischargeValue AS v JOIN v.dailyDischarge AS s" + " WHERE (s.gauge.id=:gaugeid)");
146 query.setParameter("gaugeid", gauge.getId()); 150 query.setParameter("gaugeid", gauge.getId());
147 return minMaxFromDbResultHelper(query.list()); 151 final List<Date> list = query.list();
152 if (list != null && list.size() == 1) {
153 return list.get(0);
154 }
155 return null;
148 } 156 }
149 157
150 private static List<DailyDischargeValue> minMaxFromDbResultHelper(final List<DailyDischargeValue> values) {
151 if (values != null && values.size() > 1) {
152 final List<DailyDischargeValue> newList = new ArrayList<>();
153 newList.add(values.get(0));
154 newList.add(values.get(values.size() - 1));
155 return newList;
156 }
157 return null;
158
159 }
160 } 158 }

http://dive4elements.wald.intevation.org