comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/MorphologicWidthFactory.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/MorphologicWidthFactory.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.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 org.dive4elements.river.artifacts.cache.CacheFactory;
14 import org.dive4elements.river.artifacts.model.StaticMorphoWidthCacheKey;
15 import org.dive4elements.river.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 :

http://dive4elements.wald.intevation.org