comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixingsColumnFactory.java @ 9415:9744ce3c3853

Rework of fixanalysis computation and dWt and WQ facets. Got rid of strange remapping and bitshifting code by explicitely saving the column information and using it in the facets. The facets also put the valid station range into their xml-metadata
author gernotbelger
date Thu, 16 Aug 2018 16:27:53 +0200
parents artifacts/src/main/java/org/dive4elements/river/artifacts/model/FixingsColumnFactory.java@af13ceeba52a
children
comparison
equal deleted inserted replaced
9414:096f151a0a9f 9415:9744ce3c3853
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.fixings;
10
11 import org.dive4elements.river.artifacts.cache.CacheFactory;
12 import org.dive4elements.river.artifacts.model.QRangeTree;
13 import org.dive4elements.river.backend.SessionHolder;
14
15 import java.util.List;
16
17 import net.sf.ehcache.Cache;
18 import net.sf.ehcache.Element;
19
20 import org.hibernate.Session;
21 import org.hibernate.SQLQuery;
22
23 import org.hibernate.type.StandardBasicTypes;
24
25 import org.apache.log4j.Logger;
26
27 public class FixingsColumnFactory
28 {
29 private static Logger log = Logger.getLogger(FixingsColumnFactory.class);
30
31 public static final String CACHE_NAME = "fixings-columns";
32
33 public static final String SQL_COLUMN_WS =
34 "SELECT wcv.position AS km, wcv.w AS w " +
35 "FROM wst_column_values wcv " +
36 "WHERE wst_column_id = :column_id " +
37 "ORDER by wcv.position";
38
39 public static final String SQL_COLUMN_QS =
40 "SELECT wqr.q AS q, r.a AS a, r.b AS b " +
41 "FROM wst_column_q_ranges wcqr " +
42 "JOIN wst_q_ranges wqr ON wcqr.wst_q_range_id = wqr.id " +
43 "JOIN ranges r ON wqr.range_id = r.id " +
44 "WHERE wcqr.wst_column_id = :column_id ORDER by r.a";
45
46 public static final FixingsColumnFactory INSTANCE =
47 new FixingsColumnFactory();
48
49 private FixingsColumnFactory() {
50 }
51
52 public static FixingsColumnFactory getInstance() {
53 return INSTANCE;
54 }
55
56 public FixingColumnData getColumnData(FixingColumn column) {
57
58 boolean debug = log.isDebugEnabled();
59
60 if (debug) {
61 log.debug("FixingsColumnFactory.getColumnData");
62 }
63
64 Cache cache = CacheFactory.getCache(CACHE_NAME);
65
66 if (cache == null) {
67 if (debug) {
68 log.debug("Cache unconfigured.");
69 }
70 return getUncached(column);
71 }
72
73 Integer cacheKey = Integer.valueOf(column.getId());
74 Element element = cache.get(cacheKey);
75
76 if (element != null) {
77 if (debug) {
78 log.debug("Column " + cacheKey + " found in cache.");
79 }
80 return (FixingColumnData)element.getValue();
81 }
82 else {
83 FixingColumnData result = getUncached(column);
84 if (result != null) {
85 if (debug) {
86 log.debug("Store column " + cacheKey + " into cache.");
87 }
88 cache.put(new Element(cacheKey, result));
89 }
90 return result;
91 }
92 }
93
94 protected FixingColumnData getUncached(FixingColumn column) {
95 Session session = SessionHolder.HOLDER.get();
96
97 SQLQuery sqlQuery = session.createSQLQuery(SQL_COLUMN_WS)
98 .addScalar("km", StandardBasicTypes.DOUBLE)
99 .addScalar("w", StandardBasicTypes.DOUBLE);
100
101 sqlQuery.setInteger("column_id", column.getId());
102
103 List<Object []> results = sqlQuery.list();
104
105 if (results.isEmpty()) {
106 return null;
107 }
108
109 double [] kms = new double[results.size()];
110 double [] ws = new double[kms.length];
111
112 for (int i = 0; i < kms.length; ++i) {
113 Object [] row = results.get(i);
114 kms[i] = ((Double)row[0]).doubleValue();
115 ws [i] = ((Double)row[1]).doubleValue();
116 }
117
118 sqlQuery = session.createSQLQuery(SQL_COLUMN_QS)
119 .addScalar("q", StandardBasicTypes.DOUBLE)
120 .addScalar("a", StandardBasicTypes.DOUBLE)
121 .addScalar("b", StandardBasicTypes.DOUBLE);
122
123 sqlQuery.setInteger("column_id", column.getId());
124
125 results = sqlQuery.list();
126
127 if (results.isEmpty()) {
128 return null;
129 }
130
131 QRangeTree qs = new QRangeTree(
132 results, QRangeTree.WITHOUT_COLUMN, 0, results.size());
133
134 return new FixingColumnData(kms, ws, qs);
135 }
136 }
137 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org