Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsColumnFactory.java @ 2619:6ed24efc80de
Loaded columns of fixings from database and store them into the cache.
flys-artifacts/trunk@4204 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 05 Apr 2012 15:29:52 +0000 |
parents | b0597a63fe70 |
children | cc0fa1798a3c |
comparison
equal
deleted
inserted
replaced
2618:3a93bbbe2ec7 | 2619:6ed24efc80de |
---|---|
1 package de.intevation.flys.artifacts.model; | 1 package de.intevation.flys.artifacts.model; |
2 | 2 |
3 import de.intevation.flys.artifacts.model.FixingsOverview.Fixing; | 3 import de.intevation.flys.artifacts.model.FixingsOverview.Fixing; |
4 | |
5 import de.intevation.flys.artifacts.cache.CacheFactory; | |
6 | |
7 import de.intevation.flys.backend.SessionHolder; | |
8 | |
9 import java.util.List; | |
10 | |
11 import net.sf.ehcache.Cache; | |
12 import net.sf.ehcache.Element; | |
13 | |
14 import org.hibernate.Session; | |
15 import org.hibernate.SQLQuery; | |
16 | |
17 import org.hibernate.type.StandardBasicTypes; | |
4 | 18 |
5 public class FixingsColumnFactory | 19 public class FixingsColumnFactory |
6 { | 20 { |
7 public static final String CACHE_NAME = "fixing-columns"; | 21 public static final String CACHE_NAME = "fixing-columns"; |
8 | 22 |
9 public static final String SQL_COLUMN_W = | 23 public static final String SQL_COLUMN_WS = |
10 "SELECT wcv.position AS km, wcv.w AS w " + | 24 "SELECT wcv.position AS km, wcv.w AS w " + |
11 "FROM wst_column_values wcv " + | 25 "FROM wst_column_values wcv " + |
12 "WHERE wst_column_id = :column_id " + | 26 "WHERE wst_column_id = :column_id " + |
13 "ORDER by wcv.position"; | 27 "ORDER by wcv.position"; |
14 | 28 |
15 public static final String SQL_COLUMN_Q = | 29 public static final String SQL_COLUMN_QS = |
16 "SELECT wqr.q AS q, r.a AS a, r.b AS b " + | 30 "SELECT wqr.q AS q, r.a AS a, r.b AS b " + |
17 "FROM wst_column_q_ranges wcqr " + | 31 "FROM wst_column_q_ranges wcqr " + |
18 "JOIN wst_q_ranges wqr ON wcqr.wst_q_range_id = wqr.id " + | 32 "JOIN wst_q_ranges wqr ON wcqr.wst_q_range_id = wqr.id " + |
19 "JOIN ranges r ON wqr.range_id = r.id " + | 33 "JOIN ranges r ON wqr.range_id = r.id " + |
20 "WHERE wcqr.wst_column_id = :column_id ORDER by r.a"; | 34 "WHERE wcqr.wst_column_id = :column_id ORDER by r.a"; |
24 | 38 |
25 private FixingsColumnFactory() { | 39 private FixingsColumnFactory() { |
26 } | 40 } |
27 | 41 |
28 public FixingsColumn getColumnData(Fixing.Column column) { | 42 public FixingsColumn getColumnData(Fixing.Column column) { |
29 // TODO: Implement me! | 43 |
30 return null; | 44 FixingsColumn result; |
45 | |
46 Cache cache = CacheFactory.getCache(CACHE_NAME); | |
47 | |
48 if (cache != null) { | |
49 Integer cacheKey = Integer.valueOf(column.getId()); | |
50 Element element = cache.get(cacheKey); | |
51 | |
52 if (element != null) { | |
53 return (FixingsColumn)element.getValue(); | |
54 } | |
55 | |
56 result = getUncached(column); | |
57 cache.put(new Element(cacheKey, result)); | |
58 } | |
59 else { | |
60 result = getUncached(column); | |
61 } | |
62 | |
63 return result; | |
64 } | |
65 | |
66 protected FixingsColumn getUncached(Fixing.Column column) { | |
67 Session session = SessionHolder.HOLDER.get(); | |
68 | |
69 SQLQuery sqlQuery = session.createSQLQuery(SQL_COLUMN_WS) | |
70 .addScalar("km", StandardBasicTypes.DOUBLE) | |
71 .addScalar("w", StandardBasicTypes.DOUBLE); | |
72 | |
73 sqlQuery.setInteger("column_id", column.getId()); | |
74 | |
75 List<Object []> results = sqlQuery.list(); | |
76 | |
77 double [] kms = new double[results.size()]; | |
78 double [] ws = new double[kms.length]; | |
79 | |
80 for (int i = 0; i < kms.length; ++i) { | |
81 Object [] row = results.get(i); | |
82 kms[i] = ((Double)row[0]).doubleValue(); | |
83 ws [i] = ((Double)row[1]).doubleValue(); | |
84 } | |
85 | |
86 sqlQuery = session.createSQLQuery(SQL_COLUMN_QS) | |
87 .addScalar("q", StandardBasicTypes.DOUBLE) | |
88 .addScalar("a", StandardBasicTypes.DOUBLE) | |
89 .addScalar("b", StandardBasicTypes.DOUBLE); | |
90 | |
91 sqlQuery.setInteger("column_id", column.getId()); | |
92 | |
93 results = sqlQuery.list(); | |
94 | |
95 QRangeTree qs = new QRangeTree( | |
96 results, QRangeTree.WITHOUT_COLUMN, 0, results.size()); | |
97 | |
98 return new FixingsColumn(kms, ws, qs); | |
31 } | 99 } |
32 } | 100 } |
33 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 101 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |