comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeFinderFactory.java @ 3143:29022c93027d

FixA: Create a cached GaugeFinderFactory to access the gauge along a river flys-artifacts/trunk@4751 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 21 Jun 2012 16:46:05 +0000
parents
children 368d1837ce5d
comparison
equal deleted inserted replaced
3142:9aed2e4de3ca 3143:29022c93027d
1 package de.intevation.flys.artifacts.model;
2
3 import de.intevation.flys.artifacts.cache.CacheFactory;
4
5 import de.intevation.flys.backend.SessionHolder;
6
7 import de.intevation.flys.model.River;
8
9 import java.util.ArrayList;
10 import java.util.List;
11
12 import net.sf.ehcache.Cache;
13 import net.sf.ehcache.Element;
14
15 import org.apache.log4j.Logger;
16
17 import org.hibernate.SQLQuery;
18 import org.hibernate.Session;
19
20 import org.hibernate.type.StandardBasicTypes;
21
22 public class GaugeFinderFactory
23 {
24 private static Logger log = Logger.getLogger(GaugeFinderFactory.class);
25
26 public static final String CACHE_NAME = "gauge-finders";
27
28 public static final String SQL_GAUGES =
29 "SELECT" +
30 " g.id AS gauge_id," +
31 " r.a AS a," +
32 " r.b AS b " +
33 "FROM gauges g" +
34 " JOIN ranges r ON g.range_id = r.id " +
35 "WHERE" +
36 " g.river_id = :river_id " +
37 "ORDER BY r.a";
38
39 private static GaugeFinderFactory INSTANCE;
40
41 protected GaugeFinderFactory() {
42 }
43
44 public static synchronized GaugeFinderFactory getInstance() {
45 if (INSTANCE == null) {
46 INSTANCE = new GaugeFinderFactory();
47 }
48
49 return INSTANCE;
50 }
51
52 public GaugeFinder getGaugeFinder(String riverName) {
53 River river = RiverFactory.getRiver(riverName);
54 return river != null
55 ? getGaugeFinder(river.getId(), river.getKmUp())
56 : null;
57 }
58
59 public synchronized GaugeFinder getGaugeFinder(
60 int riverId,
61 boolean isKmUp
62 ) {
63 Cache cache = CacheFactory.getCache(CACHE_NAME);
64
65 if (cache == null) {
66 return getUncached(riverId, isKmUp);
67 }
68
69 String cacheKey = riverId + "-" + isKmUp;
70 Element element = cache.get(cacheKey);
71
72 if (element != null) {
73 return (GaugeFinder)element.getValue();
74 }
75
76 GaugeFinder finder = getUncached(riverId, isKmUp);
77
78 if (finder != null) {
79 cache.put(new Element(cacheKey, finder));
80 }
81
82 return finder;
83 }
84
85 protected GaugeFinder loadGauges(
86 Session session,
87 int riverId,
88 boolean isKmUp
89 ) {
90 SQLQuery query = session.createSQLQuery(SQL_GAUGES)
91 .addScalar("gauge_id", StandardBasicTypes.INTEGER)
92 .addScalar("a", StandardBasicTypes.DOUBLE)
93 .addScalar("b", StandardBasicTypes.DOUBLE);
94
95 query.setInteger("river_id", riverId);
96
97 List<Object []> list = query.list();
98
99 if (list.isEmpty()) {
100 log.warn("River " + riverId + " has no gauges.");
101 return null;
102 }
103
104 List<GaugeRange> gauges = new ArrayList<GaugeRange>();
105
106 for (Object [] row: list) {
107 int gaugeId = (Integer)row[0];
108 double start = (Double) row[1];
109 double end = (Double) row[2];
110 GaugeRange gauge = new GaugeRange(start, end, gaugeId);
111 gauges.add(gauge);
112 }
113
114 return new GaugeFinder(gauges, isKmUp);
115 }
116
117 protected GaugeFinder getUncached(int riverId, boolean isKmUp) {
118 Session session = SessionHolder.HOLDER.get();
119
120 GaugeFinder finder = loadGauges(session, riverId, isKmUp);
121
122 if (finder == null
123 || !finder.loadDischargeSectors(session, riverId)) {
124 return null;
125 }
126
127 return finder;
128 }
129 }
130 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org