comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java @ 8694:9bb7f19cbb6f

(issue1750) Use new MeasuremenStation-methods to find companion station and do less checking for inconsistent data (schema or at least importer do the job now).
author Tom Gottfried <tom@intevation.de>
date Mon, 20 Apr 2015 10:23:40 +0200
parents b0e5a2ce0b09
children 0a5239a1e46e
comparison
equal deleted inserted replaced
8693:851ea37d35f3 8694:9bb7f19cbb6f
19 import org.hibernate.Query; 19 import org.hibernate.Query;
20 import org.hibernate.Session; 20 import org.hibernate.Session;
21 21
22 import org.dive4elements.river.artifacts.cache.CacheFactory; 22 import org.dive4elements.river.artifacts.cache.CacheFactory;
23 import org.dive4elements.river.backend.SessionHolder; 23 import org.dive4elements.river.backend.SessionHolder;
24 import org.dive4elements.river.model.MeasurementStation;
24 25
25 26
26 public class StaticSQFactory 27 public class StaticSQFactory
27 { 28 {
28 private static final Logger log = 29 private static final Logger log =
29 Logger.getLogger(StaticSQFactory.class); 30 Logger.getLogger(StaticSQFactory.class);
30
31 public static final String SQL_STATIONS_AT_RANGE =
32 "SELECT "+
33 "ms.id AS ms_id " +
34 "FROM measurement_station ms " +
35 "JOIN ranges ra ON ra.id = ms.range_id " +
36 "JOIN rivers r ON r.id = ra.river_id " +
37 "WHERE r.name = :river AND " +
38 "CASE WHEN r.km_up = 1 AND ra.b IS NOT NULL " +
39 "THEN ra.b " +
40 "ELSE ra.a " +
41 "END = (SELECT " +
42 "CASE WHEN r.km_up = 1 AND ra.b IS NOT NULL " +
43 "THEN ra.b " +
44 "ELSE ra.a " +
45 "END " +
46 "FROM measurement_station ms " +
47 "JOIN ranges ra ON ra.id = ms.range_id " +
48 "JOIN rivers r ON r.id = ra.river_id " +
49 "WHERE r.name = :river AND " +
50 "ms.id = :ms_id)";
51 31
52 public static final String SQL_SQ = 32 public static final String SQL_SQ =
53 "SELECT " + 33 "SELECT " +
54 "sq.description AS description,"+ 34 "sq.description AS description,"+
55 "ti.start_time AS start_time," + 35 "ti.start_time AS start_time," +
88 * Returns all SQRelations for the location of the station and 68 * Returns all SQRelations for the location of the station and
89 * not just for the station. E.g. for a "Geschiebemessstelle" 69 * not just for the station. E.g. for a "Geschiebemessstelle"
90 * and a "Schwebstoffmesstelle" at the same place.*/ 70 * and a "Schwebstoffmesstelle" at the same place.*/
91 public static StaticSQContainer getSQRelationsForLocation( 71 public static StaticSQContainer getSQRelationsForLocation(
92 String river, 72 String river,
93 int measurementStation 73 int mStationId
94 ) { 74 ) {
95 Session session = SessionHolder.HOLDER.get(); 75 Session session = SessionHolder.HOLDER.get();
96 Query query = session.createSQLQuery(SQL_STATIONS_AT_RANGE) 76 Query query = session.createQuery(
97 .setParameter("river", river) 77 "from MeasurementStation " +
98 .setParameter("ms_id", measurementStation); 78 "where id=:ms_id")
79 .setParameter("ms_id", mStationId);
80 MeasurementStation mStation = (MeasurementStation)query.list().get(0);
81
99 /* Take the first container for the station requested. */ 82 /* Take the first container for the station requested. */
100 StaticSQContainer retval = getSQRelations(river, measurementStation); 83 StaticSQContainer retval = getSQRelations(river, mStationId);
101 84
102 /* And some others */ 85 /* And it's companion station */
103 List<Object> list = query.list(); 86 MeasurementStation companion = mStation.findCompanionStation();
104 if (list == null || list.isEmpty()) { 87 if (companion != null) {
105 log.error("Did not even find one measurement station. Broken Query?"); 88 int stationId = companion.getId();
106 log.debug("River: " + river);
107 log.debug("Mesurement station id: " + measurementStation);
108 return retval;
109 }
110
111 if (list.size() > 2) {
112 log.error("More then two measurement stations found at the same range. Bad Data!");
113 return retval;
114 }
115
116 for (Object stationIdO: list) {
117 Integer stationId;
118 if (stationIdO instanceof BigDecimal) {
119 stationId = ((BigDecimal)stationIdO).intValue();
120 } else {
121 /* If it is something else entirely we die here. */
122 stationId = (Integer) stationIdO;
123 }
124 log.debug("Collecting SQ Relations for: "+ stationId);
125 if (stationId == measurementStation) {
126 /* Skip the same station */
127 continue;
128 }
129
130 StaticSQContainer additional = getSQRelations(river, stationId); 89 StaticSQContainer additional = getSQRelations(river, stationId);
131 if (additional == null || additional.getSQRelations() == null) { 90 if (additional == null || additional.getSQRelations() == null) {
132 continue; 91 /* New one is empty, just take the old one. */
133 } 92 return retval;
134 93 }
135 if (retval == null || retval.getSQRelations() == null || retval.getSQRelations().isEmpty()) { 94 if (retval == null ||
95 retval.getSQRelations() == null ||
96 retval.getSQRelations().isEmpty()) {
136 /* Old one is empty, just take the new one. */ 97 /* Old one is empty, just take the new one. */
137 retval = additional; 98 return additional;
138 continue; 99 }
139 } 100 for (StaticSQRelation add: additional.getSQRelations()) {
140 101 /* Add SQ relations from new one to old one */
141 for (StaticSQRelation rel: additional.getSQRelations()) { 102 retval.addSQRelation(add);
142 /* Check if we already have one for this parameter. 103 }
143 * This is highly unlikely in the data scheme of things. */
144 List<StaticSQRelation> old = retval.getRelationsByParameter(
145 rel.getParameter());
146 if (old != null || !old.isEmpty()) {
147 log.warn("Multiple SQ relation Parameters found for different " +
148 "measurement_stations at the same range. This should not happen.");
149 continue;
150 }
151 retval.addSQRelation(rel);
152 }
153
154 } 104 }
155 return retval; 105 return retval;
156 } 106 }
157 107
158 108
159 public static StaticSQContainer getSQRelations( 109 public static StaticSQContainer getSQRelations(
160 String river, 110 String river,
161 int measurementStation 111 int mStationId
162 ) { 112 ) {
163 Cache cache = CacheFactory.getCache(StaticSQCacheKey.CACHE_NAME); 113 Cache cache = CacheFactory.getCache(StaticSQCacheKey.CACHE_NAME);
164 114
165 StaticSQCacheKey cacheKey; 115 StaticSQCacheKey cacheKey;
166 116
167 if (cache != null) { 117 if (cache != null) {
168 cacheKey = new StaticSQCacheKey(river, measurementStation); 118 cacheKey = new StaticSQCacheKey(river, mStationId);
169 Element element = cache.get(cacheKey); 119 Element element = cache.get(cacheKey);
170 if (element != null) { 120 if (element != null) {
171 log.debug("Got static sq relations from cache"); 121 log.debug("Got static sq relations from cache");
172 return (StaticSQContainer)element.getValue(); 122 return (StaticSQContainer)element.getValue();
173 } 123 }
174 } 124 }
175 else { 125 else {
176 cacheKey = null; 126 cacheKey = null;
177 } 127 }
178 128
179 StaticSQContainer values = getUncached(river, measurementStation); 129 StaticSQContainer values = getUncached(river, mStationId);
180 130
181 if (values != null && cacheKey != null) { 131 if (values != null && cacheKey != null) {
182 log.debug("Store static sq relations in cache."); 132 log.debug("Store static sq relations in cache.");
183 Element element = new Element(cacheKey, values); 133 Element element = new Element(cacheKey, values);
184 cache.put(element); 134 cache.put(element);
229 return sq; 179 return sq;
230 } 180 }
231 181
232 private static StaticSQContainer getUncached( 182 private static StaticSQContainer getUncached(
233 String river, 183 String river,
234 int measurementStation 184 int mStationId
235 ) { 185 ) {
236 Session session = SessionHolder.HOLDER.get(); 186 Session session = SessionHolder.HOLDER.get();
237 187
238 Query query = session.createSQLQuery(SQL_SQ + STATION_CLAUSE) 188 Query query = session.createSQLQuery(SQL_SQ + STATION_CLAUSE)
239 .addScalar("description") 189 .addScalar("description")
246 .addScalar("a") 196 .addScalar("a")
247 .addScalar("b") 197 .addScalar("b")
248 .addScalar("qmax"); 198 .addScalar("qmax");
249 199
250 query.setParameter("river", river); 200 query.setParameter("river", river);
251 query.setParameter("ms_id", measurementStation); 201 query.setParameter("ms_id", mStationId);
252 202
253 List<Object []> list = query.list(); 203 List<Object []> list = query.list();
254 204
255 if (list.isEmpty()) { 205 if (list.isEmpty()) {
256 log.debug("Query returened empty"); 206 log.debug("Query returned empty");
257 return new StaticSQContainer(); 207 return new StaticSQContainer();
258 } 208 }
259 209
260 StaticSQContainer sq = new StaticSQContainer(); 210 StaticSQContainer sq = new StaticSQContainer();
261 sq.setDescription((String)list.get(0)[0]); 211 sq.setDescription((String)list.get(0)[0]);

http://dive4elements.wald.intevation.org