comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedHeightFactory.java@58bdf95df5e4
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.artifacts.model.minfo;
2
3
4 import java.util.Date;
5 import java.util.List;
6
7 import net.sf.ehcache.Cache;
8 import net.sf.ehcache.Element;
9
10 import org.apache.log4j.Logger;
11 import org.hibernate.SQLQuery;
12 import org.hibernate.Session;
13 import org.hibernate.type.StandardBasicTypes;
14
15 import org.dive4elements.river.artifacts.cache.CacheFactory;
16 import org.dive4elements.river.artifacts.model.StaticBedHeightCacheKey;
17 import org.dive4elements.river.backend.SessionHolder;
18
19 public class BedHeightFactory {
20 /** Private logger to use here. */
21 private static Logger log = Logger.getLogger(BedHeightFactory.class);
22
23 /** Query to get km and ws for wst_id and column_pos. */
24 public static final String SQL_SELECT_SINGLE =
25 "SELECT bhsv.height, bhsv.station, bhsv.data_gap, bhsv.sounding_width, bhs.year " +
26 " FROM bed_height_single bhs" +
27 " JOIN bed_height_single_values bhsv on bhsv.bed_height_single_id = bhs.id" +
28 " WHERE bhs.id = :height_id";
29
30 /** Query to get name for wst_id and column_pos. */
31 public static final String SQL_SELECT_EPOCH =
32 "SELECT bv.height, bv.station, ti.start_time, ti.stop_time" +
33 " FROM bed_height_epoch b" +
34 " JOIN bed_height_epoch_values bv ON b.id = bv.bed_height_epoch_id" +
35 " JOIN time_intervals ti ON b.time_interval_id = ti.id" +
36 " WHERE b.id = :height_id";
37
38 /** Query to get name (description) for wst_id. */
39 public static final String SQL_SELECT_DESCR_SINGLE =
40 "SELECT description FROM bed_height_single "+
41 "WHERE id = :height_id";
42
43 /** Query to get name (description) for wst_id. */
44 public static final String SQL_SELECT_DESCR_EPOCH =
45 "SELECT description from bed_height_epoch "+
46 "WHERE id = :height_id";
47
48
49 private BedHeightFactory() {
50 }
51
52
53 /**
54 * Get WKms for given column and wst_id, caring about the cache.
55 */
56 public static BedHeight getHeight(String type, int height_id, int time) {
57 log.debug("BedHeightFactory.getHeight");
58 Cache cache = CacheFactory.getCache(StaticBedHeightCacheKey.CACHE_NAME);
59
60 StaticBedHeightCacheKey cacheKey;
61
62 if (cache != null) {
63 cacheKey = new StaticBedHeightCacheKey(height_id, time);
64 Element element = cache.get(cacheKey);
65 if (element != null) {
66 log.debug("Got static bedheight values from cache");
67 return (BedHeight)element.getValue();
68 }
69 }
70 else {
71 cacheKey = null;
72 }
73
74 BedHeight values = getBedHeightUncached(type, height_id, time);
75
76 if (values != null && cacheKey != null) {
77 log.debug("Store static bed height values in cache.");
78 Element element = new Element(cacheKey, values);
79 cache.put(element);
80 }
81 return values;
82 }
83
84 /** Get name for a WKms. */
85 public static String getHeightName(String type, int height_id) {
86 log.debug("BedHeightFactory.getHeightName height_id/" + height_id);
87
88 String name = null;
89 Session session = SessionHolder.HOLDER.get();
90
91 SQLQuery nameQuery = null;
92 if (type.equals("single")) {
93 nameQuery = session.createSQLQuery(SQL_SELECT_DESCR_SINGLE)
94 .addScalar("description", StandardBasicTypes.STRING);
95 nameQuery.setInteger("height_id", height_id);
96 }
97 else if (type.equals("epoch")) {
98 nameQuery = session.createSQLQuery(SQL_SELECT_DESCR_EPOCH)
99 .addScalar("description", StandardBasicTypes.STRING);
100 nameQuery.setInteger("height_id", height_id);
101 }
102 else {
103 return "none";
104 }
105 List<String> names = nameQuery.list();
106 if (!names.isEmpty()) {
107 name = names.get(0);
108 }
109
110 return name;
111 }
112
113
114 /**
115 * Get WKms from db.
116 * @param column the position columns value
117 * @param wst_id database id of the wst
118 * @return according WKms.
119 */
120 public static BedHeight getBedHeightUncached(
121 String type,
122 int height_id,
123 int time)
124 {
125 if (log.isDebugEnabled()) {
126 log.debug("BedHeightFactory.getBedHeightUncached");
127 }
128
129 Session session = SessionHolder.HOLDER.get();
130 SQLQuery sqlQuery = null;
131 if (type.equals("single")) {
132 BedHeightSingle height =
133 new BedHeightSingle(getHeightName(type, height_id));
134 sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLE)
135 .addScalar("height", StandardBasicTypes.DOUBLE)
136 .addScalar("station", StandardBasicTypes.DOUBLE)
137 .addScalar("data_gap", StandardBasicTypes.DOUBLE)
138 .addScalar("sounding_width", StandardBasicTypes.DOUBLE)
139 .addScalar("year", StandardBasicTypes.INTEGER);
140 sqlQuery.setInteger("height_id", height_id);
141 List<Object []> results = sqlQuery.list();
142
143 for (int i = 0; i < results.size(); i++) {
144 Object[] row = results.get(i);
145 log.debug("got station: " + (Double)row[1]);
146 height.add(
147 (Double) row[0],
148 (Double) row[1],
149 (Double) row[2],
150 (Double) row[3],
151 (Integer) row[4]);
152 }
153 return height;
154 }
155 else if (type.equals("epoch")) {
156 BedHeightEpoch height =
157 new BedHeightEpoch(getHeightName(type, height_id));
158 sqlQuery = session.createSQLQuery(SQL_SELECT_EPOCH)
159 .addScalar("height", StandardBasicTypes.DOUBLE)
160 .addScalar("station", StandardBasicTypes.DOUBLE)
161 .addScalar("start_time", StandardBasicTypes.DATE)
162 .addScalar("stop_time", StandardBasicTypes.DATE);
163 sqlQuery.setInteger("height_id", height_id);
164 List<Object []> results = sqlQuery.list();
165
166 for (Object [] row: results) {
167 height.add(
168 (Double) row[0],
169 (Double) row[1],
170 (Date) row[2],
171 (Date) row[3]);
172 }
173 return height;
174 }
175 return new BedHeight();
176 }
177 }
178 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org