Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/model/Gauge.java @ 5969:abe5fe3d2d17
Fixed NPE problem in fetching extram from discharge curves.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 10 May 2013 12:22:56 +0200 |
parents | 4dd33b86dc61 |
children | 9b1eb9a52224 |
comparison
equal
deleted
inserted
replaced
5968:f0550bac4468 | 5969:abe5fe3d2d17 |
---|---|
10 | 10 |
11 import java.math.BigDecimal; | 11 import java.math.BigDecimal; |
12 | 12 |
13 import java.io.Serializable; | 13 import java.io.Serializable; |
14 | 14 |
15 import java.util.Arrays; | |
15 import java.util.List; | 16 import java.util.List; |
16 | 17 |
17 import javax.persistence.Entity; | 18 import javax.persistence.Entity; |
18 import javax.persistence.Id; | 19 import javax.persistence.Id; |
19 import javax.persistence.Table; | 20 import javax.persistence.Table; |
26 import javax.persistence.OneToMany; | 27 import javax.persistence.OneToMany; |
27 | 28 |
28 import org.hibernate.Session; | 29 import org.hibernate.Session; |
29 import org.hibernate.Query; | 30 import org.hibernate.Query; |
30 | 31 |
32 import org.apache.log4j.Logger; | |
33 | |
31 import org.dive4elements.river.backend.SessionHolder; | 34 import org.dive4elements.river.backend.SessionHolder; |
32 | 35 |
33 /** Database-mapped Gauge with all info about it. */ | 36 /** Database-mapped Gauge with all info about it. */ |
34 @Entity | 37 @Entity |
35 @Table(name = "gauges") | 38 @Table(name = "gauges") |
36 public class Gauge | 39 public class Gauge |
37 implements Serializable, Comparable<Gauge> | 40 implements Serializable, Comparable<Gauge> |
38 { | 41 { |
42 private static final Logger log = Logger.getLogger(Gauge.class); | |
43 | |
39 public static final int DEFAULT_SCALE = 100; | 44 public static final int DEFAULT_SCALE = 100; |
40 | 45 |
41 public static final int MASTER_DISCHARGE_TABLE = 0; | 46 public static final int MASTER_DISCHARGE_TABLE = 0; |
42 | 47 |
43 private Integer id; | 48 private Integer id; |
203 Query query = session.createQuery( | 208 Query query = session.createQuery( |
204 "select min(w) as min, max(w) as max from DischargeTableValue " + | 209 "select min(w) as min, max(w) as max from DischargeTableValue " + |
205 "where table_id =:table"); | 210 "where table_id =:table"); |
206 query.setParameter("table", dischargeTable.getId()); | 211 query.setParameter("table", dischargeTable.getId()); |
207 | 212 |
208 List results = query.list(); | 213 List<?> results = query.list(); |
209 Object[] result = (Object[]) results.get(0); | 214 if (results.isEmpty()) { |
210 | 215 log.error("No values in discharge table found."); |
211 return result != null | 216 return null; |
212 ? new double[] { | 217 } |
213 ((BigDecimal) result[0]).doubleValue() * scale, | 218 |
214 ((BigDecimal) result[1]).doubleValue() * scale} | 219 Object[] result = (Object[])results.get(0); |
220 | |
221 BigDecimal a = (BigDecimal)result[0]; | |
222 BigDecimal b = (BigDecimal)result[1]; | |
223 | |
224 return a != null && b != null | |
225 ? new double [] { a.doubleValue()*scale, b.doubleValue()*scale } | |
215 : null; | 226 : null; |
216 } | 227 } |
217 | 228 |
218 @OneToMany | 229 @OneToMany |
219 @JoinColumn(name = "gauge_id") | 230 @JoinColumn(name = "gauge_id") |