comparison flys-backend/src/main/java/org/dive4elements/river/importer/ImportSQRelationValue.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java@bc140668db82
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
1 package de.intevation.flys.importer;
2
3 import java.math.BigDecimal;
4 import java.sql.SQLException;
5 import java.util.List;
6
7 import org.apache.log4j.Logger;
8
9 import org.hibernate.Query;
10 import org.hibernate.Session;
11 import org.hibernate.exception.ConstraintViolationException;
12
13 import de.intevation.flys.model.MeasurementStation;
14 import de.intevation.flys.model.SQRelation;
15 import de.intevation.flys.model.SQRelationValue;
16
17
18 public class ImportSQRelationValue {
19
20 private static Logger log = Logger.getLogger(ImportSQRelationValue.class);
21
22
23 private SQRelationValue peer;
24
25 private String parameter;
26
27 private Double km;
28 private Double a;
29 private Double b;
30 private Double qMax;
31 private Double rSQ;
32 private Integer nTot;
33 private Integer nOutlier;
34 private Double cFerguson;
35 private Double cDuan;
36
37
38 public ImportSQRelationValue(
39 String parameter,
40 Double km,
41 Double a,
42 Double b,
43 Double qMax,
44 Double rSQ,
45 Integer nTot,
46 Integer nOutlier,
47 Double cFerguson,
48 Double cDuan
49 ) {
50 this.parameter = parameter;
51 this.km = km;
52 this.a = a;
53 this.b = b;
54 this.qMax = qMax;
55 this.rSQ = rSQ;
56 this.nTot = nTot;
57 this.nOutlier = nOutlier;
58 this.cFerguson = cFerguson;
59 this.cDuan = cDuan;
60 }
61
62
63 public void storeDependencies(SQRelation owner)
64 throws SQLException, ConstraintViolationException
65 {
66 getPeer(owner);
67 }
68
69
70 public SQRelationValue getPeer(SQRelation owner) {
71 if (peer == null) {
72 Session session = ImporterSession.getInstance().getDatabaseSession();
73
74 Query query = session.createQuery(
75 "from MeasurementStation " +
76 " where station between :kml and :kmh");
77 query.setDouble("kml", km - 1e-4);
78 query.setDouble("kmh", km + 1e-4);
79
80 List<MeasurementStation> result = query.list();
81
82 if (result.isEmpty()) {
83 log.error("No measurement stations found at km " + km);
84 return null;
85 }
86
87 Query query2 = session.createQuery(
88 "from SQRelationValue " +
89 " where sqRelation=:owner " +
90 " and parameter=:parameter" +
91 " and measurementStation=:measurementStation" +
92 " and a=:a" +
93 " and b=:b" +
94 " and qMax=:qMax" +
95 " and rSQ=:rSQ" +
96 " and cFerguson=:cFerguson" +
97 " and cDuan=:cDuan");
98
99 query2.setParameter("owner", owner);
100 query2.setString("parameter", parameter);
101 query2.setParameter("measurementStation", result.get(0));
102 query2.setBigDecimal("a", toBigDecimal(a));
103 query2.setBigDecimal("b", toBigDecimal(b));
104 query2.setBigDecimal("qMax", toBigDecimal(qMax));
105 query2.setBigDecimal("rSQ", toBigDecimal(rSQ));
106 query2.setBigDecimal("cFerguson", toBigDecimal(cFerguson));
107 query2.setBigDecimal("cDuan", toBigDecimal(cDuan));
108
109 List<SQRelationValue> values = query2.list();
110
111 if (values.isEmpty()) {
112 peer = new SQRelationValue(
113 owner,
114 parameter,
115 result.get(0),
116 a,
117 b,
118 qMax,
119 rSQ,
120 nTot,
121 nOutlier,
122 cFerguson,
123 cDuan
124 );
125
126 session.save(peer);
127 }
128 else {
129 peer = values.get(0);
130 }
131 }
132 return peer;
133 }
134
135 private static final BigDecimal toBigDecimal(Double x) {
136 if (x == null) return null;
137 return new BigDecimal(x);
138 }
139 }
140 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org