Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/PorosityFactory.java @ 7846:e84726b48484
New Facet, datatype and data factory for porosities.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Wed, 30 Apr 2014 15:20:11 +0200 |
parents | |
children | 92a2b9b691e1 f4dffc7d1d60 |
comparison
equal
deleted
inserted
replaced
7845:4e264d6c6e06 | 7846:e84726b48484 |
---|---|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.artifacts.model.minfo; | |
10 | |
11 import java.util.List; | |
12 | |
13 import net.sf.ehcache.Cache; | |
14 import net.sf.ehcache.Element; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 import org.hibernate.SQLQuery; | |
18 import org.hibernate.Session; | |
19 import org.hibernate.type.StandardBasicTypes; | |
20 | |
21 import org.dive4elements.river.artifacts.cache.CacheFactory; | |
22 import org.dive4elements.river.artifacts.model.StaticMorphoWidthCacheKey; | |
23 import org.dive4elements.river.artifacts.model.StaticPorosityCacheKey; | |
24 import org.dive4elements.river.backend.SessionHolder; | |
25 | |
26 | |
27 public class PorosityFactory | |
28 { | |
29 /** Private logger to use here. */ | |
30 private static Logger log = Logger.getLogger(PorosityFactory.class); | |
31 | |
32 public static final String SQL_SELECT = | |
33 "SELECT pv.station AS station, pv.porosity AS porosity " + | |
34 " FROM porosity p" + | |
35 " JOIN porosity_values pv on pv.porosity_id = p.id" + | |
36 " WHERE p.id = :porosity_id"; | |
37 | |
38 private PorosityFactory() { | |
39 } | |
40 | |
41 | |
42 /** | |
43 * Get WKms for given column and wst_id, caring about the cache. | |
44 */ | |
45 public static Porosity getPorosity(int porosity_id) { | |
46 log.debug("PorosityFactory.getPorosity"); | |
47 Cache cache = CacheFactory.getCache(StaticPorosityCacheKey.CACHE_NAME); | |
48 | |
49 StaticPorosityCacheKey cacheKey; | |
50 | |
51 if (cache != null) { | |
52 cacheKey = new StaticPorosityCacheKey(porosity_id); | |
53 Element element = cache.get(cacheKey); | |
54 if (element != null) { | |
55 log.debug("Got static porosity values from cache"); | |
56 return (Porosity)element.getValue(); | |
57 } | |
58 } | |
59 else { | |
60 cacheKey = null; | |
61 } | |
62 | |
63 Porosity values = getPorosityUncached(porosity_id); | |
64 | |
65 if (values != null && cacheKey != null) { | |
66 log.debug("Store static porosity values in cache."); | |
67 Element element = new Element(cacheKey, values); | |
68 cache.put(element); | |
69 } | |
70 return values; | |
71 } | |
72 | |
73 private static Porosity getPorosityUncached(int porosity_id) { | |
74 if (log.isDebugEnabled()) { | |
75 log.debug("PorosityFactory.getPorosityUncached"); | |
76 } | |
77 | |
78 Session session = SessionHolder.HOLDER.get(); | |
79 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT) | |
80 .addScalar("station", StandardBasicTypes.DOUBLE) | |
81 .addScalar("porosity", StandardBasicTypes.DOUBLE); | |
82 sqlQuery.setInteger("porosity_id", porosity_id); | |
83 List<Object []> results = sqlQuery.list(); | |
84 | |
85 Porosity porosities = new Porosity(); | |
86 for (Object [] row: results) { | |
87 log.debug("got station: " + (Double)row[0]); | |
88 porosities.add( | |
89 (Double) row[0], | |
90 (Double) row[1]); | |
91 } | |
92 return porosities; | |
93 } | |
94 } | |
95 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |