comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.model.minfo;
2
3 import gnu.trove.TDoubleArrayList;
4
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.List;
8
9 import net.sf.ehcache.Cache;
10 import net.sf.ehcache.Element;
11
12 import org.apache.log4j.Logger;
13 import org.hibernate.SQLQuery;
14 import org.hibernate.Session;
15 import org.hibernate.type.StandardBasicTypes;
16
17 import org.dive4elements.river.artifacts.cache.CacheFactory;
18 import org.dive4elements.river.artifacts.model.StaticSedimentLoadCacheKey;
19 import org.dive4elements.river.backend.SessionHolder;
20
21 /** Pull Sediment Loads out of db. */
22 public class SedimentLoadFactory
23 {
24 /** Private logger to use here. */
25 private static Logger log = Logger.getLogger(SedimentLoadFactory.class);
26
27 public static final String LOADS_CACHE_NAME = "sedimentloads";
28 public static final String LOAD_DATA_CACHE_NAME = "sedimentload-data";
29
30 /** Query to get km and ws for wst_id and column_pos. */
31 public static final String SQL_SELECT_SINGLES =
32 "SELECT DISTINCT " +
33 " sy.description AS description, " +
34 " ti.start_time AS year " +
35 " FROM sediment_yield sy " +
36 " JOIN rivers r ON sy.river_id = r.id " +
37 " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " +
38 " JOIN time_intervals ti ON sy.time_interval_id = ti.id " +
39 " WHERE r.name = :name " +
40 " AND ti.stop_time IS NULL " +
41 " AND syv.station BETWEEN :startKm AND :endKm";
42
43 /** Query to get name for wst_id and column_pos. */
44 public static final String SQL_SELECT_EPOCHS =
45 "SELECT DISTINCT " +
46 " sy.description AS description, " +
47 " ti.start_time AS start, " +
48 " ti.stop_time AS end " +
49 " FROM sediment_yield sy " +
50 " JOIN rivers r ON sy.river_id = r.id " +
51 " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " +
52 " JOIN time_intervals ti ON sy.time_interval_id = ti.id " +
53 " WHERE r.name = :name " +
54 " AND ti.stop_time IS NOT NULL " +
55 " AND syv.station BETWEEN :startKm AND :endKm";
56
57 public static final String SQL_SELECT_SINGLES_DATA =
58 "SELECT" +
59 " sy.description AS description, " +
60 " ti.start_time AS year, " +
61 " syv.value AS load, " +
62 " syv.station AS km " +
63 " FROM sediment_yield sy " +
64 " JOIN rivers r ON sy.river_id = r.id " +
65 " JOIN time_intervals ti ON sy.time_interval_id = ti.id " +
66 " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " +
67 " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " +
68 " WHERE r.name = :name " +
69 " AND ti.start_time BETWEEN :begin AND :end " +
70 " AND ti.stop_time IS NULL " +
71 " AND gf.name = :grain " +
72 " AND syv.station BETWEEN :startKm AND :endKm";
73
74 public static final String SQL_SELECT_EPOCHS_DATA =
75 "SELECT" +
76 " sy.description AS description, " +
77 " ti.start_time AS startYear, " +
78 " syv.value AS load, " +
79 " syv.station AS km," +
80 " ti.stop_time AS endYear " +
81 " FROM sediment_yield sy " +
82 " JOIN rivers r ON sy.river_id = r.id " +
83 " JOIN time_intervals ti ON sy.time_interval_id = ti.id " +
84 " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " +
85 " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " +
86 " WHERE r.name = :name " +
87 " AND ti.start_time BETWEEN :sbegin AND :send " +
88 " AND ti.stop_time BETWEEN :ebegin AND :eend " +
89 " AND gf.name = :grain " +
90 " AND syv.station BETWEEN :startKm AND :endKm";
91
92 private SedimentLoadFactory() {
93 }
94
95 /**
96 *
97 */
98 public static SedimentLoad[] getLoads(
99 String river,
100 String type,
101 double startKm,
102 double endKm
103 ) {
104 log.debug("SedimentLoadFactory.getLoads");
105 Cache cache = CacheFactory.getCache(LOADS_CACHE_NAME);
106
107 if (cache == null) {
108 log.debug("Cache not configured.");
109 return getSedimentLoadsUncached(river, type, startKm, endKm);
110 }
111
112 StaticSedimentLoadCacheKey key =
113 new StaticSedimentLoadCacheKey(river, startKm, endKm, 0, 0);
114
115 Element element = cache.get(key);
116
117 if (element != null) {
118 log.debug("SedimentLoad found in cache");
119 return (SedimentLoad[])element.getValue();
120 }
121
122 SedimentLoad[] values =
123 getSedimentLoadsUncached(river, type, startKm, endKm);
124
125 if (values != null && key != null) {
126 log.debug("Store static sediment load values in cache.");
127 element = new Element(key, values);
128 cache.put(element);
129 }
130 return values;
131 }
132
133 public static SedimentLoad getLoadWithData(
134 String river,
135 String type,
136 double startKm,
137 double endKm,
138 int syear,
139 int eyear
140 ) {
141 log.debug("SedimentLoadFactory.getLoadWithData");
142 Cache cache = CacheFactory.getCache(LOAD_DATA_CACHE_NAME);
143
144 if (cache == null) {
145 log.debug("Cache not configured.");
146 return getSedimentLoadWithDataUncached(
147 river,
148 type,
149 startKm,
150 endKm,
151 syear,
152 eyear);
153 }
154
155 StaticSedimentLoadCacheKey key =
156 new StaticSedimentLoadCacheKey(river, startKm, endKm, syear, eyear);
157
158 Element element = cache.get(key);
159
160 if (element != null) {
161 log.debug("SedimentLoad found in cache");
162 return (SedimentLoad)element.getValue();
163 }
164
165 SedimentLoad values = getSedimentLoadWithDataUncached(
166 river,
167 type,
168 startKm,
169 endKm,
170 syear,
171 eyear);
172
173 if (values != null && key != null) {
174 log.debug("Store static bed height values in cache.");
175 element = new Element(key, values);
176 cache.put(element);
177 }
178 return values;
179 }
180
181 /**
182 * Get sediment loads from db.
183 * @param river the river
184 * @param type the sediment load type (year or epoch)
185 * @return according sediment loads.
186 */
187 public static SedimentLoad[] getSedimentLoadsUncached(
188 String river,
189 String type,
190 double startKm,
191 double endKm
192 ) {
193 log.debug("SedimentLoadFactory.getSedimentLoadsUncached");
194
195 Session session = SessionHolder.HOLDER.get();
196 SQLQuery sqlQuery = null;
197
198 if (type.equals("single")) {
199 sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLES)
200 .addScalar("description", StandardBasicTypes.STRING)
201 .addScalar("year", StandardBasicTypes.DATE);
202 sqlQuery.setString("name", river);
203 sqlQuery.setDouble("startKm", startKm);
204 sqlQuery.setDouble("endKm", endKm);
205 List<Object []> results = sqlQuery.list();
206 SedimentLoad[] loads = new SedimentLoad[results.size()];
207 for (int i = 0; i < results.size(); i++) {
208 Object[] row = results.get(i);
209 loads[i] = new SedimentLoad(
210 (String) row[0],
211 (Date) row[1],
212 null,
213 false);
214 }
215 return loads;
216 }
217 else if (type.equals("epoch")) {
218 sqlQuery = session.createSQLQuery(SQL_SELECT_EPOCHS)
219 .addScalar("description", StandardBasicTypes.STRING)
220 .addScalar("start", StandardBasicTypes.DATE)
221 .addScalar("end", StandardBasicTypes.DATE);
222 sqlQuery.setString("name", river);
223 sqlQuery.setDouble("startKm", startKm);
224 sqlQuery.setDouble("endKm", endKm);
225 List<Object []> results = sqlQuery.list();
226
227 SedimentLoad[] loads = new SedimentLoad[results.size()];
228 for (int i = 0; i < results.size(); i++) {
229 Object[] row = results.get(i);
230 loads[i] = new SedimentLoad(
231 (String) row[0],
232 (Date) row[1],
233 (Date) row[2],
234 true);
235 }
236 return loads;
237 }
238 return new SedimentLoad[0];
239 }
240
241 /**
242 * Get sediment loads from db.
243 * @param river the river
244 * @param type the sediment load type (year or epoch)
245 * @return according sediment loads.
246 */
247 public static SedimentLoad getSedimentLoadWithDataUncached(
248 String river,
249 String type,
250 double startKm,
251 double endKm,
252 int syear,
253 int eyear
254 ) {
255 log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached");
256 Session session = SessionHolder.HOLDER.get();
257 SQLQuery sqlQuery = null;
258
259 Calendar start = Calendar.getInstance();
260 start.set(syear - 1, 11, 31);
261 Calendar end = Calendar.getInstance();
262 end.set(syear, 11, 30);
263
264 if (type.equals("year") || type.equals("epoch")) {
265 sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLES_DATA)
266 .addScalar("description", StandardBasicTypes.STRING)
267 .addScalar("year", StandardBasicTypes.DATE)
268 .addScalar("load", StandardBasicTypes.DOUBLE)
269 .addScalar("km", StandardBasicTypes.DOUBLE);
270 sqlQuery.setString("name", river);
271 sqlQuery.setDouble("startKm", startKm);
272 sqlQuery.setDouble("endKm", endKm);
273 sqlQuery.setDate("begin", start.getTime());
274 sqlQuery.setDate("end", end.getTime());
275 sqlQuery.setString("grain", "total");
276 List<Object []> results = sqlQuery.list();
277 SedimentLoad load = new SedimentLoad();
278 Object[] row = results.get(0);
279 load = new SedimentLoad(
280 (String) row[0],
281 (Date) row[1],
282 null,
283 false);
284 getValues("coarse", sqlQuery, load);
285 getValues("fine_middle", sqlQuery, load);
286 getValues("sand", sqlQuery, load);
287 getValues("suspended_sediment", sqlQuery, load);
288 getValues("susp_sand_bed", sqlQuery, load);
289 getValues("susp_sand", sqlQuery, load);
290
291 return load;
292 }
293 else if (type.equals("off_epoch")) {
294 Calendar toStart = Calendar.getInstance();
295 toStart.set(eyear - 1, 11, 31);
296 Calendar toEnd = Calendar.getInstance();
297 toEnd.set(eyear, 11, 30);
298 sqlQuery = session.createSQLQuery(SQL_SELECT_EPOCHS_DATA)
299 .addScalar("description", StandardBasicTypes.STRING)
300 .addScalar("startYear", StandardBasicTypes.DATE)
301 .addScalar("load", StandardBasicTypes.DOUBLE)
302 .addScalar("km", StandardBasicTypes.DOUBLE)
303 .addScalar("endYear", StandardBasicTypes.DATE);
304 sqlQuery.setString("name", river);
305 sqlQuery.setDouble("startKm", startKm);
306 sqlQuery.setDouble("endKm", endKm);
307 sqlQuery.setDate("sbegin", start.getTime());
308 sqlQuery.setDate("send", end.getTime());
309 sqlQuery.setDate("ebegin",toStart.getTime());
310 sqlQuery.setDate("eend", toEnd.getTime());
311 sqlQuery.setString("grain", "total");
312
313 List<Object[]> results = null;
314 results = sqlQuery.list();
315
316 SedimentLoad load = new SedimentLoad();
317 Object[] row = results.get(0);
318 load = new SedimentLoad(
319 (String) row[0],
320 (Date) row[1],
321 (Date) row[4],
322 true);
323 TDoubleArrayList kms = new TDoubleArrayList();
324 for (int i = 0; i < results.size(); i++) {
325 row = results.get(i);
326 kms.add((Double)row[3]);
327 load.setLoadTotal((Double)row[3], (Double)row[2]);
328 }
329 getValues("coarse", sqlQuery, load);
330 getValues("fine_middle", sqlQuery, load);
331 getValues("sand", sqlQuery, load);
332 getValues("suspended_sediment", sqlQuery, load);
333 getValues("susp_sand_bed", sqlQuery, load);
334 getValues("susp_sand", sqlQuery, load);
335 return load;
336 }
337 return new SedimentLoad();
338 }
339
340
341 /**
342 * Run query with grain parameter set to fraction, feed result into
343 * load.
344 * @param fraction value to set 'grain' parameter in query to.
345 * @param query query in which to set 'grain' parameter and run.
346 * @param load[out] SedimentLoad which to populate with values.
347 */
348 protected static void getValues (
349 String fraction,
350 SQLQuery query,
351 SedimentLoad load
352 ) {
353 query.setString("grain", fraction);
354 List<Object[]> results = query.list();
355 for (int i = 0; i < results.size(); i++) {
356 Object[] row = results.get(i);
357 double km = (Double)row[3];
358 double v = -1;
359 if (row[2] != null) {
360 v = ((Double)row[2]).doubleValue();
361 }
362 if (fraction.equals("coarse")) {
363 load.setCoarse(km, v);
364 }
365 else if (fraction.equals("sand")) {
366 load.setSand(km, v);
367 }
368 else if (fraction.equals("fine_middle")) {
369 load.setFineMiddle(km, v);
370 }
371 else if (fraction.equals("suspended_sediment")) {
372 load.setSuspSediment(km, v);
373 }
374 else if (fraction.equals("susp_sand")) {
375 load.setSuspSand(km, v);
376 }
377 else if (fraction.equals("susp_sand_bed")) {
378 load.setSuspSandBed(km, v);
379 }
380 }
381 }
382 }
383 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org