comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeFinderFactory.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeFinderFactory.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.model;
2
3 import org.dive4elements.river.artifacts.cache.CacheFactory;
4
5 import org.dive4elements.river.backend.SessionHolder;
6
7 import org.dive4elements.river.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 /** Get GaugeFinders. */
23 public class GaugeFinderFactory
24 {
25 private static Logger log = Logger.getLogger(GaugeFinderFactory.class);
26
27 public static final String CACHE_NAME = "gauge-finders";
28
29 public static final String SQL_GAUGES =
30 "SELECT" +
31 " g.id AS gauge_id," +
32 " g.name AS name," +
33 " r.a AS a," +
34 " r.b AS b " +
35 "FROM gauges g" +
36 " JOIN ranges r ON g.range_id = r.id " +
37 "WHERE" +
38 " g.river_id = :river_id " +
39 "ORDER BY r.a";
40
41 private static GaugeFinderFactory INSTANCE;
42
43 protected GaugeFinderFactory() {
44 }
45
46 public static synchronized GaugeFinderFactory getInstance() {
47 if (INSTANCE == null) {
48 INSTANCE = new GaugeFinderFactory();
49 }
50
51 return INSTANCE;
52 }
53
54 public GaugeFinder getGaugeFinder(String riverName) {
55 River river = RiverFactory.getRiver(riverName);
56 return river != null
57 ? getGaugeFinder(river.getId(), river.getKmUp())
58 : null;
59 }
60
61 public synchronized GaugeFinder getGaugeFinder(
62 int riverId,
63 boolean isKmUp
64 ) {
65 Cache cache = CacheFactory.getCache(CACHE_NAME);
66
67 if (cache == null) {
68 return getUncached(riverId, isKmUp);
69 }
70
71 String cacheKey = riverId + "-" + isKmUp;
72 Element element = cache.get(cacheKey);
73
74 if (element != null) {
75 return (GaugeFinder)element.getValue();
76 }
77
78 GaugeFinder finder = getUncached(riverId, isKmUp);
79
80 if (finder != null) {
81 cache.put(new Element(cacheKey, finder));
82 }
83
84 return finder;
85 }
86
87 protected GaugeFinder loadGauges(
88 Session session,
89 int riverId,
90 boolean isKmUp
91 ) {
92 SQLQuery query = session.createSQLQuery(SQL_GAUGES)
93 .addScalar("gauge_id", StandardBasicTypes.INTEGER)
94 .addScalar("name", StandardBasicTypes.STRING)
95 .addScalar("a", StandardBasicTypes.DOUBLE)
96 .addScalar("b", StandardBasicTypes.DOUBLE);
97
98 query.setInteger("river_id", riverId);
99
100 List<Object []> list = query.list();
101
102 if (list.isEmpty()) {
103 log.warn("River " + riverId + " has no gauges.");
104 return null;
105 }
106
107 List<GaugeRange> gauges = new ArrayList<GaugeRange>();
108
109 for (Object [] row: list) {
110 int gaugeId = (Integer)row[0];
111 String name = (String) row[1];
112 double start = (Double) row[2];
113 double end = (Double) row[3];
114 GaugeRange gauge = new GaugeRange(start, end, name, gaugeId);
115 gauges.add(gauge);
116 }
117
118 return new GaugeFinder(gauges, isKmUp);
119 }
120
121 protected GaugeFinder getUncached(int riverId, boolean isKmUp) {
122 Session session = SessionHolder.HOLDER.get();
123
124 GaugeFinder finder = loadGauges(session, riverId, isKmUp);
125
126 if (finder == null
127 || !finder.loadDischargeSectors(session, riverId)) {
128 return null;
129 }
130
131 return finder;
132 }
133 }
134 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org