annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java @ 458:523a256451cd

Use unsharp km lookup (epsilon = 0.0001). flys-artifacts/trunk@1957 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 19 May 2011 14:38:15 +0000
parents 5d65fe4c08d5
children e3ee131d5dd3
rev   line source
443
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.artifacts.model;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import java.io.Serializable;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import java.util.ArrayList;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import java.util.Comparator;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.util.List;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 import java.util.Collections;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 import org.hibernate.Session;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import org.hibernate.Query;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import org.hibernate.SQLQuery;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import org.hibernate.type.StandardBasicTypes;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import net.sf.ehcache.Cache;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import net.sf.ehcache.Element;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 import org.apache.log4j.Logger;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 import de.intevation.flys.model.River;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 import de.intevation.flys.model.Wst;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 import de.intevation.flys.model.WstColumn;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 import de.intevation.flys.artifacts.cache.CacheFactory;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 import de.intevation.flys.backend.SessionHolder;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 public class WstValueTableFactory
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 private static Logger log = Logger.getLogger(WstValueTableFactory.class);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 public static final String CACHE_NAME = "wst-value-table";
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 // TODO: put this into a property file
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 public static final String SQL_POS_WQ =
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 "SELECT position, w, q, column_pos" +
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 " FROM wst_value_table" +
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 " WHERE wst_id = :wst_id";
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 public static final class CacheKey
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 implements Serializable
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 private int riverId;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 private int kind;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 public CacheKey(int riverId, int kind) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 this.riverId = riverId;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 this.kind = kind;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 public int hashCode() {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 return (riverId << 8) | kind;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 public boolean equals(Object other) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 if (!(other instanceof CacheKey)) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 return false;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 CacheKey o = (CacheKey)other;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 return riverId == o.riverId && kind == o.kind;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 } // class CacheKey
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 private WstValueTableFactory() {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 public static WstValueTable getTable(River river) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 return getTable(river, 0);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 public static WstValueTable getTable(River river, int kind) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 Cache cache = CacheFactory.getCache(CACHE_NAME);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 CacheKey cacheKey;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 if (cache != null) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 cacheKey = new CacheKey(river.getId(), kind);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 Element element = cache.get(cacheKey);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 if (element != null) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 log.debug("got wst value table from cache");
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 return (WstValueTable)element.getValue();
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 else {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 cacheKey = null;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 WstValueTable valueTable = getTableUncached(river, kind);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 if (cacheKey != null) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 log.debug("store wst value table in cache");
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 Element element = new Element(cacheKey, valueTable);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 cache.put(element);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 return valueTable;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 public static WstValueTable getTableUncached(River river, int kind) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 Session session = SessionHolder.HOLDER.get();
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 Query query = session.createQuery(
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 "from Wst where river=:river and kind=:kind");
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 query.setParameter("river", river);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 query.setInteger("kind", kind);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 List<Wst> wsts = query.list();
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 if (wsts.isEmpty()) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 return null;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 Wst wst = wsts.get(0);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 // TODO: Do this sorting at database level
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 List<WstColumn> wstColumns = new ArrayList(wst.getColumns());
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 Collections.sort(wstColumns, new Comparator<WstColumn>() {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 public int compare(WstColumn a, WstColumn b) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 int pa = a.getPosition();
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 int pb = b.getPosition();
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 if (pa < pb) return -1;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 if (pa > pb) return +1;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 return 0;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 });
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 WstValueTable.Column [] columns =
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 new WstValueTable.Column[wstColumns.size()];
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 for (int i = 0; i < columns.length; ++i) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 columns[i] = new WstValueTable.Column(wstColumns.get(i).getName());
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 // using native SQL here to avoid myriad of small objects.
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138 SQLQuery sqlQuery = session.createSQLQuery(SQL_POS_WQ)
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 .addScalar("position", StandardBasicTypes.DOUBLE)
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 .addScalar("w", StandardBasicTypes.DOUBLE)
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 .addScalar("q", StandardBasicTypes.DOUBLE)
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 .addScalar("column_pos", StandardBasicTypes.INTEGER);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 sqlQuery.setInteger("wst_id", wst.getId());
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 WstValueTable valueTable = new WstValueTable(columns);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 int lastColumnNo = -1;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 WstValueTable.Row row = null;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 Double lastQ = -Double.MAX_VALUE;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152 boolean qSorted = true;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 for (Object r: sqlQuery.list()) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 Object[] result = (Object[]) r;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 double km = (Double) result[0];
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 Double w = (Double) result[1];
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159 Double q = (Double) result[2];
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 int columnNo = (Integer)result[3];
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 if (columnNo > lastColumnNo) { // new row
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 if (row != null) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 row.qSorted = qSorted;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 valueTable.rows.add(row);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 row = new WstValueTable.Row(
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 km,
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 new double[columnNo+1],
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 new double[columnNo+1]);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 lastQ = -Double.MAX_VALUE;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 qSorted = true;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 row.ws[columnNo] = w != null ? w : Double.NaN;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 row.qs[columnNo] = q != null ? q : Double.NaN;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 if (qSorted && (q == null || lastQ > q)) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 qSorted = false;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181 lastQ = q;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 lastColumnNo = columnNo;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
185
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 if (row != null) {
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187 valueTable.rows.add(row);
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189
458
523a256451cd Use unsharp km lookup (epsilon = 0.0001).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 443
diff changeset
190 // XXX: should not be necessary
523a256451cd Use unsharp km lookup (epsilon = 0.0001).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 443
diff changeset
191 valueTable.sortRows();
523a256451cd Use unsharp km lookup (epsilon = 0.0001).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 443
diff changeset
192
443
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 return valueTable;
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 }
5d65fe4c08d5 Separated the WST table loading logic from the calculations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org