comparison backend/src/main/java/org/dive4elements/river/importer/ImportSQRelationValue.java @ 7730:e1b831fe435a slt-simplify-cross-sections

Merged default into slt-simplify-cross-sections branch and updated package and class names.
author Tom Gottfried <tom@intevation.de>
date Mon, 20 Jan 2014 14:04:20 +0100
parents 4c3ccf2b0304
children 17db08570637
comparison
equal deleted inserted replaced
5084:ca45dd039b54 7730:e1b831fe435a
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details.
7 */
8
9 package org.dive4elements.river.importer;
10
11 import java.math.BigDecimal;
12 import java.sql.SQLException;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16
17 import org.hibernate.Query;
18 import org.hibernate.Session;
19 import org.hibernate.exception.ConstraintViolationException;
20
21 import org.dive4elements.river.model.MeasurementStation;
22 import org.dive4elements.river.model.SQRelation;
23 import org.dive4elements.river.model.SQRelationValue;
24
25
26 public class ImportSQRelationValue {
27
28 private static Logger log = Logger.getLogger(ImportSQRelationValue.class);
29
30
31 private SQRelationValue peer;
32
33 private String parameter;
34
35 private Double km;
36 private Double a;
37 private Double b;
38 private Double qMax;
39 private Double rSQ;
40 private Integer nTot;
41 private Integer nOutlier;
42 private Double cFerguson;
43 private Double cDuan;
44
45
46 public ImportSQRelationValue(
47 String parameter,
48 Double km,
49 Double a,
50 Double b,
51 Double qMax,
52 Double rSQ,
53 Integer nTot,
54 Integer nOutlier,
55 Double cFerguson,
56 Double cDuan
57 ) {
58 this.parameter = parameter;
59 this.km = km;
60 this.a = a;
61 this.b = b;
62 this.qMax = qMax;
63 this.rSQ = rSQ;
64 this.nTot = nTot;
65 this.nOutlier = nOutlier;
66 this.cFerguson = cFerguson;
67 this.cDuan = cDuan;
68 }
69
70
71 public void storeDependencies(SQRelation owner)
72 throws SQLException, ConstraintViolationException
73 {
74 getPeer(owner);
75 }
76
77
78 public SQRelationValue getPeer(SQRelation owner) {
79 if (peer == null) {
80 Session session = ImporterSession.getInstance().getDatabaseSession();
81
82 Query query = session.createQuery(
83 "from MeasurementStation " +
84 " where station between :kml and :kmh");
85 query.setDouble("kml", km - 1e-4);
86 query.setDouble("kmh", km + 1e-4);
87
88 List<MeasurementStation> result = query.list();
89
90 if (result.isEmpty()) {
91 log.error("No measurement stations found at km " + km);
92 return null;
93 }
94
95 Query query2 = session.createQuery(
96 "from SQRelationValue " +
97 " where sqRelation=:owner " +
98 " and parameter=:parameter" +
99 " and measurementStation=:measurementStation" +
100 " and a=:a" +
101 " and b=:b" +
102 " and qMax=:qMax" +
103 " and rSQ=:rSQ" +
104 " and cFerguson=:cFerguson" +
105 " and cDuan=:cDuan");
106
107 query2.setParameter("owner", owner);
108 query2.setString("parameter", parameter);
109 query2.setParameter("measurementStation", result.get(0));
110 query2.setBigDecimal("a", toBigDecimal(a));
111 query2.setBigDecimal("b", toBigDecimal(b));
112 query2.setBigDecimal("qMax", toBigDecimal(qMax));
113 query2.setBigDecimal("rSQ", toBigDecimal(rSQ));
114 query2.setBigDecimal("cFerguson", toBigDecimal(cFerguson));
115 query2.setBigDecimal("cDuan", toBigDecimal(cDuan));
116
117 List<SQRelationValue> values = query2.list();
118
119 if (values.isEmpty()) {
120 peer = new SQRelationValue(
121 owner,
122 parameter,
123 result.get(0),
124 a,
125 b,
126 qMax,
127 rSQ,
128 nTot,
129 nOutlier,
130 cFerguson,
131 cDuan
132 );
133
134 session.save(peer);
135 }
136 else {
137 peer = values.get(0);
138 }
139 }
140 return peer;
141 }
142
143 private static final BigDecimal toBigDecimal(Double x) {
144 if (x == null) return null;
145 return new BigDecimal(x);
146 }
147 }
148 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org