Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/HYKFactory.java @ 5831:bd047b71ab37
Repaired internal references
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:06:39 +0200 |
parents | flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java@c27c4e06dd87 |
children |
comparison
equal
deleted
inserted
replaced
5830:160f53ee0870 | 5831:bd047b71ab37 |
---|---|
1 package org.dive4elements.river.artifacts.model; | |
2 | |
3 import org.dive4elements.river.artifacts.cache.CacheFactory; | |
4 import org.dive4elements.river.backend.SessionHolder; | |
5 import org.dive4elements.river.model.HYK; | |
6 import org.dive4elements.river.model.HYKFlowZone; | |
7 import org.dive4elements.river.model.HYKFormation; | |
8 | |
9 import java.io.Serializable; | |
10 import java.util.ArrayList; | |
11 import java.util.List; | |
12 | |
13 import net.sf.ehcache.Cache; | |
14 import net.sf.ehcache.Element; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 import org.hibernate.Query; | |
18 import org.hibernate.SQLQuery; | |
19 import org.hibernate.Session; | |
20 import org.hibernate.type.StandardBasicTypes; | |
21 | |
22 | |
23 /** | |
24 * Factory to access HYKs (hydrographic values). | |
25 */ | |
26 public class HYKFactory | |
27 { | |
28 private static Logger logger = Logger.getLogger(HYKFactory.class); | |
29 | |
30 public static String HYK_CACHE_NAME = "hykache"; | |
31 | |
32 | |
33 /** Do not instantiate a HYKFactory. */ | |
34 private HYKFactory() { | |
35 } | |
36 | |
37 | |
38 /** | |
39 * Get List of Zones for given river and km. | |
40 */ | |
41 public static Object getHYKs(int hykid, double km) { | |
42 logger.debug("HYKFactory.getHYKs"); | |
43 | |
44 Cache cache = CacheFactory.getCache(HYK_CACHE_NAME); | |
45 | |
46 String cacheKey; | |
47 | |
48 if (cache != null) { | |
49 cacheKey = "" + hykid + "_" + km; | |
50 Element element = cache.get(cacheKey); | |
51 if (element != null) { | |
52 logger.debug("Got hyk from cache"); | |
53 return element.getValue(); | |
54 } | |
55 } | |
56 else { | |
57 cacheKey = null; | |
58 } | |
59 | |
60 List<Zone> zones = getZonesUncached(hykid, km); | |
61 | |
62 if (zones != null && cacheKey != null) { | |
63 logger.debug("Store hykzones in cache."); | |
64 Element element = new Element(cacheKey, zones); | |
65 cache.put(element); | |
66 } | |
67 | |
68 return zones; | |
69 } | |
70 | |
71 | |
72 /** Return name for hyk with given id. */ | |
73 public static String getHykName(int hykid) { | |
74 logger.debug("HYKFactory.getHykName " + hykid); | |
75 | |
76 Session session = SessionHolder.HOLDER.get(); | |
77 | |
78 Query query = session.createQuery( | |
79 "select description from HYK where id = :hykid "); | |
80 query.setParameter("hykid", hykid); | |
81 | |
82 return (String) query.uniqueResult(); | |
83 } | |
84 | |
85 | |
86 /** | |
87 * Ask DB for hyk zones. | |
88 * @param hykid ID of the 'main' HYK to query. | |
89 * @param km for which to get the hyk-zones. | |
90 * @return according zones. | |
91 */ | |
92 public static List<Zone> getZonesUncached(int hykid, double km) { | |
93 if (logger.isDebugEnabled()) { | |
94 logger.debug("HYKFactory.getZoneUncached " + hykid + " km " + km); | |
95 } | |
96 | |
97 Session session = SessionHolder.HOLDER.get(); | |
98 | |
99 // Find out flow-direction of river. | |
100 // OPTIMIZE: 1) query kmUp directly 2) merge queries. | |
101 Query rQuery = session.createQuery("from HYK where id = :hykid"); | |
102 rQuery.setParameter("hykid", hykid); | |
103 rQuery.setMaxResults(1); | |
104 HYK hyk = (HYK) rQuery.uniqueResult(); | |
105 | |
106 double flowDir = hyk.getRiver().getKmUp() ? 1 : -1; | |
107 | |
108 List<HYKFormation> forms = getHYKFormations(hykid, km, flowDir); | |
109 List<Zone> zones = new ArrayList<Zone>(); | |
110 | |
111 // Take the first one. | |
112 if (forms.size() >= 1) { | |
113 HYKFormation form = forms.get(0); | |
114 // Create respective zones. | |
115 for (HYKFlowZone flowZone: form.getZones()) { | |
116 Zone z = new Zone(flowZone.getA().doubleValue(), | |
117 flowZone.getB().doubleValue(), | |
118 flowZone.getType().getName()); | |
119 zones.add(z); | |
120 } | |
121 } | |
122 | |
123 return zones; | |
124 } | |
125 | |
126 | |
127 protected static List<HYKFormation> getHYKFormations( | |
128 int hykid, | |
129 double km, | |
130 double flowDir | |
131 ) { | |
132 Session session = SessionHolder.HOLDER.get(); | |
133 | |
134 String SQL = "SELECT " + | |
135 " f.id AS FID, " + | |
136 " f.distance_vl AS DIST, " + | |
137 " e.hyk_id AS HID, " + | |
138 " e.km AS KM " + | |
139 " FROM hyk_formations f INNER JOIN hyk_entries e " + | |
140 " ON e.id = f.hyk_entry_id " + | |
141 " WHERE e.hyk_id = :hykid " + | |
142 " AND :km between " + | |
143 " LEAST(e.km, e.km + :flowDir*(f.distance_vl/1000.0+0.001)) " + | |
144 " AND " + | |
145 " GREATEST(e.km, e.km + :flowDir*(f.distance_vl/1000.0+0.001))"; | |
146 | |
147 SQLQuery sqlQuery = session.createSQLQuery(SQL) | |
148 .addScalar("FID", StandardBasicTypes.INTEGER) | |
149 .addScalar("DIST", StandardBasicTypes.DOUBLE) | |
150 .addScalar("HID", StandardBasicTypes.INTEGER) | |
151 .addScalar("KM", StandardBasicTypes.DOUBLE); | |
152 | |
153 sqlQuery.setInteger("hykid", hykid); | |
154 sqlQuery.setDouble("flowDir", flowDir); | |
155 sqlQuery.setDouble("km", km); | |
156 | |
157 logger.debug("HYK SQL: " + sqlQuery.getQueryString()); | |
158 | |
159 List<Object[]> results = sqlQuery.list(); | |
160 | |
161 logger.debug("Found " + results.size() + " HYKFormation IDs in DB."); | |
162 | |
163 if (results == null || results.isEmpty()) { | |
164 logger.debug("No HYK found for ID " + hykid + " at km " + km); | |
165 return new ArrayList<HYKFormation>(); | |
166 } | |
167 | |
168 Object[] resultSet = results.get(0); | |
169 Integer hykFormationId = (Integer) resultSet[0]; | |
170 | |
171 Query query = session.createQuery("from HYKFormation where id = :id"); | |
172 query.setParameter("id", hykFormationId); | |
173 query.setMaxResults(1); | |
174 | |
175 return query.list(); | |
176 } | |
177 | |
178 | |
179 /** Labeled section. */ | |
180 public static class Zone implements Serializable { | |
181 /** Lower end of segment. */ | |
182 protected double from; | |
183 /** Upper end of segment. */ | |
184 protected double to; | |
185 /** The label. */ | |
186 protected String name; | |
187 | |
188 /** Constructor for labelled section. */ | |
189 public Zone (double from, double to, String name) { | |
190 this.from = from; | |
191 this.to = to; | |
192 this.name = name; | |
193 } | |
194 | |
195 /** Get upper value. */ | |
196 public double getTo() { | |
197 return to; | |
198 } | |
199 | |
200 /** Get lower value. */ | |
201 public double getFrom() { | |
202 return from; | |
203 } | |
204 | |
205 /** Get name (type). */ | |
206 public String getName() { | |
207 return name; | |
208 } | |
209 } // public static class Zone | |
210 } | |
211 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |