Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedHeightFactory.java @ 3614:68beaa827751
MINFO: Implemented UI and facet/artifact stack for bed height differences.
flys-artifacts/trunk@5276 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Tue, 28 Aug 2012 11:45:23 +0000 |
parents | |
children | 633fbb61a0cc |
comparison
equal
deleted
inserted
replaced
3613:dd6e25980c91 | 3614:68beaa827751 |
---|---|
1 package de.intevation.flys.artifacts.model.minfo; | |
2 | |
3 | |
4 import java.util.List; | |
5 | |
6 import net.sf.ehcache.Cache; | |
7 import net.sf.ehcache.Element; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 import org.hibernate.SQLQuery; | |
11 import org.hibernate.Session; | |
12 import org.hibernate.type.StandardBasicTypes; | |
13 | |
14 import de.intevation.flys.artifacts.cache.CacheFactory; | |
15 import de.intevation.flys.artifacts.model.StaticBedHeightCacheKey; | |
16 import de.intevation.flys.backend.SessionHolder; | |
17 | |
18 public class BedHeightFactory { | |
19 /** Private logger to use here. */ | |
20 private static Logger log = Logger.getLogger(BedHeightFactory.class); | |
21 | |
22 /** Query to get km and ws for wst_id and column_pos. */ | |
23 public static final String SQL_SELECT_SINGLE = | |
24 "SELECT height, station, data_gap FROM bed_height_single_values " + | |
25 "WHERE id = :height_id"; | |
26 | |
27 /** Query to get name for wst_id and column_pos. */ | |
28 public static final String SQL_SELECT_EPOCH = | |
29 "SELECT height, station FROM bed_height_epoch_values "+ | |
30 "WHERE id = :height_id"; | |
31 | |
32 /** Query to get name (description) for wst_id. */ | |
33 public static final String SQL_SELECT_DESCR_SINGLE = | |
34 "SELECT description FROM bed_height_single "+ | |
35 "WHERE id = :height_id"; | |
36 | |
37 /** Query to get name (description) for wst_id. */ | |
38 public static final String SQL_SELECT_DESCR_EPOCH = | |
39 "SELECT description from bed_height_epoch "+ | |
40 "WHERE id = :height_id"; | |
41 | |
42 | |
43 private BedHeightFactory() { | |
44 } | |
45 | |
46 | |
47 /** | |
48 * Get WKms for given column and wst_id, caring about the cache. | |
49 */ | |
50 public static BedHeight getHeight(String type, int height_id, int time) { | |
51 log.debug("BedHeightFactory.getHeight"); | |
52 Cache cache = CacheFactory.getCache(StaticBedHeightCacheKey.CACHE_NAME); | |
53 | |
54 StaticBedHeightCacheKey cacheKey; | |
55 | |
56 if (cache != null) { | |
57 cacheKey = new StaticBedHeightCacheKey(height_id, time); | |
58 Element element = cache.get(cacheKey); | |
59 if (element != null) { | |
60 log.debug("Got static bedheight values from cache"); | |
61 return (BedHeight)element.getValue(); | |
62 } | |
63 } | |
64 else { | |
65 cacheKey = null; | |
66 } | |
67 | |
68 BedHeight values = getBedHeightUncached(type, height_id, time); | |
69 | |
70 if (values != null && cacheKey != null) { | |
71 log.debug("Store static bed height values in cache."); | |
72 Element element = new Element(cacheKey, values); | |
73 cache.put(element); | |
74 } | |
75 return values; | |
76 } | |
77 | |
78 /** Get name for a WKms. */ | |
79 public static String getHeightName(String type, int height_id) { | |
80 log.debug("BedHeightFactory.getHeightName height_id/" + height_id); | |
81 | |
82 String name = null; | |
83 Session session = SessionHolder.HOLDER.get(); | |
84 | |
85 SQLQuery nameQuery = null; | |
86 if (type.equals("single")) { | |
87 nameQuery = session.createSQLQuery(SQL_SELECT_DESCR_SINGLE) | |
88 .addScalar("description", StandardBasicTypes.STRING); | |
89 nameQuery.setInteger("height_id", height_id); | |
90 } | |
91 else if (type.equals("epoch")) { | |
92 nameQuery = session.createSQLQuery(SQL_SELECT_DESCR_EPOCH) | |
93 .addScalar("description", StandardBasicTypes.STRING); | |
94 nameQuery.setInteger("height_id", height_id); | |
95 } | |
96 else { | |
97 return "none"; | |
98 } | |
99 List<String> names = nameQuery.list(); | |
100 if (names.size() >= 1) { | |
101 name = names.get(0); | |
102 } | |
103 | |
104 return name; | |
105 } | |
106 | |
107 | |
108 /** | |
109 * Get WKms from db. | |
110 * @param column the position columns value | |
111 * @param wst_id database id of the wst | |
112 * @return according WKms. | |
113 */ | |
114 public static BedHeight getBedHeightUncached( | |
115 String type, | |
116 int height_id, | |
117 int time) | |
118 { | |
119 if (log.isDebugEnabled()) { | |
120 log.debug("BedHeightFactory.getBedHeightUncached"); | |
121 } | |
122 | |
123 BedHeight height = new BedHeight(getHeightName(type, height_id)); | |
124 | |
125 Session session = SessionHolder.HOLDER.get(); | |
126 SQLQuery sqlQuery = null; | |
127 if (type.equals("single")) { | |
128 sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLE) | |
129 .addScalar("height", StandardBasicTypes.DOUBLE) | |
130 .addScalar("station", StandardBasicTypes.DOUBLE) | |
131 .addScalar("data_gap", StandardBasicTypes.DOUBLE); | |
132 sqlQuery.setInteger("height_id", height_id); | |
133 List<Object []> results = sqlQuery.list(); | |
134 | |
135 for (int i = 0; i <= results.size(); i++) { | |
136 Object[] row = results.get(i); | |
137 height.add((Double) row[0], (Double) row[1], (Double) row[2]); | |
138 } | |
139 } | |
140 else if (type.equals("epoch")) { | |
141 sqlQuery = session.createSQLQuery(SQL_SELECT_EPOCH) | |
142 .addScalar("height", StandardBasicTypes.DOUBLE) | |
143 .addScalar("station", StandardBasicTypes.DOUBLE); | |
144 sqlQuery.setInteger("height_id", height_id); | |
145 List<Object []> results = sqlQuery.list(); | |
146 | |
147 for (int i = 0; i <= results.size(); i++) { | |
148 Object[] row = results.get(i); | |
149 height.add((Double) row[0], (Double) row[1], 0); | |
150 } | |
151 } | |
152 | |
153 return height; | |
154 } | |
155 } | |
156 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |