Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/MorphologicWidthFactory.java @ 5764:7ac111db8a07
Model, factory, facet and static artifact for morphologic width.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Thu, 18 Apr 2013 14:14:15 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5763:dd652fcc9f0d | 5764:7ac111db8a07 |
---|---|
1 package de.intevation.flys.artifacts.model.minfo; | |
2 | |
3 import java.util.List; | |
4 | |
5 import net.sf.ehcache.Cache; | |
6 import net.sf.ehcache.Element; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 import org.hibernate.SQLQuery; | |
10 import org.hibernate.Session; | |
11 import org.hibernate.type.StandardBasicTypes; | |
12 | |
13 import de.intevation.flys.artifacts.cache.CacheFactory; | |
14 import de.intevation.flys.artifacts.model.StaticMorphoWidthCacheKey; | |
15 import de.intevation.flys.backend.SessionHolder; | |
16 | |
17 | |
18 public class MorphologicWidthFactory | |
19 { | |
20 /** Private logger to use here. */ | |
21 private static Logger log = Logger.getLogger(MorphologicWidthFactory.class); | |
22 | |
23 public static final String SQL_SELECT = | |
24 "SELECT mwv.station AS station, mwv.width AS width " + | |
25 " FROM morphologic_width mw" + | |
26 " JOIN morphologic_width_values mwv on mwv.morphologic_width_id = mw.id" + | |
27 " WHERE mw.id = :width_id"; | |
28 | |
29 private MorphologicWidthFactory() { | |
30 } | |
31 | |
32 | |
33 /** | |
34 * Get WKms for given column and wst_id, caring about the cache. | |
35 */ | |
36 public static MorphologicWidth getWidth(int width_id) { | |
37 log.debug("MorphologicWidthFactory.getWidth"); | |
38 Cache cache = CacheFactory.getCache(StaticMorphoWidthCacheKey.CACHE_NAME); | |
39 | |
40 StaticMorphoWidthCacheKey cacheKey; | |
41 | |
42 if (cache != null) { | |
43 cacheKey = new StaticMorphoWidthCacheKey(width_id); | |
44 Element element = cache.get(cacheKey); | |
45 if (element != null) { | |
46 log.debug("Got static bedheight values from cache"); | |
47 return (MorphologicWidth)element.getValue(); | |
48 } | |
49 } | |
50 else { | |
51 cacheKey = null; | |
52 } | |
53 | |
54 MorphologicWidth values = getWidthUncached(width_id); | |
55 | |
56 if (values != null && cacheKey != null) { | |
57 log.debug("Store static morphologic width values in cache."); | |
58 Element element = new Element(cacheKey, values); | |
59 cache.put(element); | |
60 } | |
61 return values; | |
62 } | |
63 | |
64 private static MorphologicWidth getWidthUncached(int width_id) { | |
65 if (log.isDebugEnabled()) { | |
66 log.debug("MorphologicWidthFactory.getWidthUncached"); | |
67 } | |
68 | |
69 Session session = SessionHolder.HOLDER.get(); | |
70 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT) | |
71 .addScalar("station", StandardBasicTypes.DOUBLE) | |
72 .addScalar("width", StandardBasicTypes.DOUBLE); | |
73 sqlQuery.setInteger("width_id", width_id); | |
74 List<Object []> results = sqlQuery.list(); | |
75 | |
76 MorphologicWidth widths = new MorphologicWidth(); | |
77 for (int i = 0; i < results.size(); i++) { | |
78 Object[] row = results.get(i); | |
79 log.debug("got station: " + (Double)row[0]); | |
80 widths.add( | |
81 (Double) row[0], | |
82 (Double) row[1]); | |
83 } | |
84 return widths; | |
85 } | |
86 } | |
87 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |