annotate artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java @ 5863:4897a58c8746

River artifacts: Added new copyright headers.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 28 Apr 2013 14:40:59 +0200
parents 5aa05a7a34b7
children af13ceeba52a
rev   line source
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
2 * Software engineering by Intevation GmbH
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
3 *
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
6 * documentation coming with Dive4Elements River for details.
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
7 */
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
8
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5500
diff changeset
9 package org.dive4elements.river.artifacts.model.sq;
5500
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
10
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
11 import java.math.BigDecimal;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
12 import java.util.Date;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
13 import java.util.List;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
14
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
15 import net.sf.ehcache.Cache;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
16 import net.sf.ehcache.Element;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
17
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
18 import org.apache.log4j.Logger;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
19 import org.hibernate.Query;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
20 import org.hibernate.Session;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
21
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5500
diff changeset
22 import org.dive4elements.river.artifacts.cache.CacheFactory;
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5500
diff changeset
23 import org.dive4elements.river.backend.SessionHolder;
5500
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
24
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
25
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
26 public class StaticSQFactory
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
27 {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
28 private static final Logger log =
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
29 Logger.getLogger(StaticSQFactory.class);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
30
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
31 public static final String SQL_SQ =
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
32 "SELECT " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
33 "sq.description AS description,"+
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
34 "ti.start_time AS start_time," +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
35 "ti.stop_time AS stop_time, " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
36 "ms.name AS station_name, " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
37 "ms.station AS station_km, " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
38 "ms.measurement_type AS measurement_type, " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
39 "sqv.parameter AS parameter, " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
40 "sqv.a AS a, " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
41 "sqv.b AS b, " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
42 "sqv.qmax AS qmax " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
43 "FROM sq_relation sq " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
44 "JOIN time_intervals ti ON ti.id = sq.time_interval_id " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
45 "JOIN rivers r ON r.id = sq.river_id " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
46 "JOIN sq_relation_value sqv ON sqv.sq_relation_id = sq.id " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
47 "JOIN measurement_station ms ON sqv.measurement_station_id = ms.id " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
48 "WHERE " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
49 "r.name = :river " +
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
50 "AND ms.id = :ms_id ";
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
51
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
52
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
53 private StaticSQFactory() {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
54 }
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
55
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
56 public static StaticSQContainer getSQRelations(
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
57 String river,
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
58 int measurementStation
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
59 ) {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
60 Cache cache = CacheFactory.getCache(StaticSQCacheKey.CACHE_NAME);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
61
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
62 StaticSQCacheKey cacheKey;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
63
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
64 if (cache != null) {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
65 cacheKey = new StaticSQCacheKey(river, measurementStation);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
66 Element element = cache.get(cacheKey);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
67 if (element != null) {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
68 log.debug("Got static bedheight values from cache");
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
69 return (StaticSQContainer)element.getValue();
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
70 }
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
71 }
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
72 else {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
73 cacheKey = null;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
74 }
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
75
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
76 StaticSQContainer values = getUncached(river, measurementStation);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
77
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
78 if (values != null && cacheKey != null) {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
79 log.debug("Store static sq relations in cache.");
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
80 Element element = new Element(cacheKey, values);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
81 cache.put(element);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
82 }
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
83 return values;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
84 }
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
85
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
86 private static StaticSQContainer getUncached(
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
87 String river,
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
88 int measurementStation
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
89 ) {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
90 Session session = SessionHolder.HOLDER.get();
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
91
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
92 Query query = session.createSQLQuery(SQL_SQ)
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
93 .addScalar("description")
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
94 .addScalar("start_time")
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
95 .addScalar("stop_time")
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
96 .addScalar("station_name")
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
97 .addScalar("station_km")
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
98 .addScalar("measurement_type")
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
99 .addScalar("parameter")
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
100 .addScalar("a")
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
101 .addScalar("b")
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
102 .addScalar("qmax");
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
103
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
104 query.setParameter("river", river);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
105 query.setParameter("ms_id", measurementStation);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
106
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
107 List<Object []> list = query.list();
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
108
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
109 if (list.isEmpty()) {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
110 return new StaticSQContainer();
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
111 }
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
112
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
113 StaticSQContainer sq = new StaticSQContainer();
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
114 sq.setDescription((String)list.get(0)[0]);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
115 sq.setStationName((String)list.get(0)[3]);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
116 sq.setKm(((BigDecimal)list.get(0)[4]).doubleValue());
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
117
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
118 for (Object[] row : list) {
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
119 StaticSQRelation relation = new StaticSQRelation();
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
120 relation.setStartTime((Date)row[1]);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
121 relation.setStopTime((Date)row[2]);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
122 relation.setType((String)row[5]);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
123 relation.setParameter((String)row[6]);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
124 relation.setA(((BigDecimal)row[7]).doubleValue());
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
125 relation.setB(((BigDecimal)row[8]).doubleValue());
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
126 relation.setQmax(((BigDecimal)row[9]).doubleValue());
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
127 sq.addSQRelation(relation);
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
128 }
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
129 return sq;
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
130 }
700ac898ab0b Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
131 }

http://dive4elements.wald.intevation.org