Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory2.java @ 632:07640ab913fd
First part of storing qs in ranges
flys-artifacts/trunk@1997 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 24 May 2011 14:46:45 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
631:a9af60c84dca | 632:07640ab913fd |
---|---|
1 package de.intevation.flys.artifacts.model; | |
2 | |
3 import java.util.List; | |
4 | |
5 import net.sf.ehcache.Cache; | |
6 import net.sf.ehcache.Element; | |
7 | |
8 import de.intevation.flys.artifacts.cache.CacheFactory; | |
9 | |
10 import de.intevation.flys.backend.SessionHolder; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 | |
14 import de.intevation.flys.model.River; | |
15 import de.intevation.flys.model.Wst; | |
16 | |
17 import org.hibernate.Session; | |
18 import org.hibernate.Query; | |
19 import org.hibernate.SQLQuery; | |
20 | |
21 import org.hibernate.type.StandardBasicTypes; | |
22 | |
23 public class WstValueTableFactory2 | |
24 { | |
25 private static Logger log = Logger.getLogger(WstValueTableFactory2.class); | |
26 | |
27 public static final int DEFAULT_KIND = 0; | |
28 | |
29 // TODO: put this into a property file | |
30 | |
31 public static final String HQL_WST = | |
32 "from Wst where river=:river and kind=:kind"; | |
33 | |
34 public static final String SQL_SELECT_NAMES_POS = | |
35 "SELECT position, name FROM wst_columns " + | |
36 "WHERE wst_id = :wst_id ORDER BY position"; | |
37 | |
38 public static final String SQL_SELECT_QS = | |
39 "SELECT column_pos, q, a, b FROM wst_q_values " + | |
40 "WHERE wst_id = :wst_id"; | |
41 | |
42 public static final String SQL_SELECT_WS = | |
43 "SELECT km, w, column_pos FROM wst_w_values " + | |
44 "WHERE wst_id = :wst_id"; | |
45 | |
46 private WstValueTableFactory2() { | |
47 } | |
48 | |
49 public static WstValueTable getTable(River river) { | |
50 return getTable(river, DEFAULT_KIND); | |
51 } | |
52 | |
53 public static WstValueTable getTable(River river, int kind) { | |
54 | |
55 Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME); | |
56 | |
57 WstValueTableCacheKey cacheKey; | |
58 | |
59 if (cache != null) { | |
60 cacheKey = new WstValueTableCacheKey(river.getId(), kind); | |
61 Element element = cache.get(cacheKey); | |
62 if (element != null) { | |
63 log.debug("got wst value table from cache"); | |
64 return (WstValueTable)element.getValue(); | |
65 } | |
66 } | |
67 else { | |
68 cacheKey = null; | |
69 } | |
70 | |
71 WstValueTable valueTable = getTableUncached(river, kind); | |
72 | |
73 if (valueTable != null && cacheKey != null) { | |
74 log.debug("store wst value table in cache"); | |
75 Element element = new Element(cacheKey, valueTable); | |
76 cache.put(element); | |
77 } | |
78 | |
79 return valueTable; | |
80 } | |
81 | |
82 public static WstValueTable getTableUncached(River river) { | |
83 return getTableUncached(river, DEFAULT_KIND); | |
84 } | |
85 | |
86 public static WstValueTable getTableUncached(River river, int kind) { | |
87 | |
88 Session session = SessionHolder.HOLDER.get(); | |
89 | |
90 Query query = session.createQuery(HQL_WST); | |
91 query.setParameter("river", river); | |
92 query.setInteger("kind", kind); | |
93 | |
94 List<Wst> wsts = query.list(); | |
95 | |
96 if (wsts.isEmpty()) { | |
97 return null; | |
98 } | |
99 | |
100 Wst wst = wsts.get(0); | |
101 | |
102 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_NAMES_POS) | |
103 .addScalar("position", StandardBasicTypes.INTEGER) | |
104 .addScalar("name", StandardBasicTypes.STRING); | |
105 | |
106 sqlQuery.setInteger("wst_id", wst.getId()); | |
107 | |
108 List<Object []> columnNames = sqlQuery.list(); | |
109 | |
110 WstValueTable.Column [] columns = | |
111 new WstValueTable.Column[columnNames.size()]; | |
112 | |
113 for (int i = 0; i < columns.length; ++i) { | |
114 columns[i] = new WstValueTable.Column( | |
115 (String)columnNames.get(i)[1]); | |
116 } | |
117 | |
118 sqlQuery = session.createSQLQuery(SQL_SELECT_QS) | |
119 .addScalar("column_pos", StandardBasicTypes.INTEGER) | |
120 .addScalar("q", StandardBasicTypes.DOUBLE) | |
121 .addScalar("a", StandardBasicTypes.DOUBLE) | |
122 .addScalar("b", StandardBasicTypes.DOUBLE); | |
123 | |
124 sqlQuery.setInteger("wst_id", wst.getId()); | |
125 | |
126 Integer lastColumn = null; | |
127 | |
128 List<Object []> qRanges = sqlQuery.list(); | |
129 int start = -1; | |
130 | |
131 int Q = qRanges.size(); | |
132 | |
133 for (int i = 0; i < Q; ++i) { | |
134 Object [] qRange = qRanges.get(i); | |
135 Integer columnId = (Integer)qRange[0]; | |
136 if (lastColumn == null) { | |
137 lastColumn = columnId; | |
138 start = i; | |
139 } | |
140 else if (!lastColumn.equals(columnId)) { | |
141 QRangeTree qRangeTree = new QRangeTree(qRanges, start, i); | |
142 columns[lastColumn].setQRangeTree(qRangeTree); | |
143 lastColumn = columnId; | |
144 start = i; | |
145 } | |
146 } | |
147 | |
148 if (start != -1) { | |
149 QRangeTree qRangeTree = new QRangeTree(qRanges, start, Q); | |
150 columns[lastColumn].setQRangeTree(qRangeTree); | |
151 } | |
152 | |
153 /* This is debug code to visualize the q ranges trees | |
154 | |
155 java.io.PrintWriter out = null; | |
156 try { | |
157 out = new java.io.PrintWriter( | |
158 new java.io.FileWriter( | |
159 "/tmp/qranges" + System.currentTimeMillis() + ".dot")); | |
160 | |
161 out.println("graph \"Q ranges trees\" {"); | |
162 | |
163 for (int i = 0; i < columns.length; ++i) { | |
164 QRangeTree tree = columns[i].getQRangeTree(); | |
165 out.println(tree.toGraph()); | |
166 } | |
167 | |
168 out.println("}"); | |
169 | |
170 out.flush(); | |
171 } | |
172 catch (java.io.IOException ioe) { | |
173 log.error(ioe); | |
174 } | |
175 finally { | |
176 if (out != null) { | |
177 out.close(); | |
178 } | |
179 } | |
180 */ | |
181 // TODO: Implement me! | |
182 | |
183 return null; | |
184 } | |
185 } | |
186 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |