Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java @ 3812:f788d2d901d6
merged flys-artifacts/pre2.6-2011-12-05
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:53 +0200 |
parents | 06c157848c8f |
children | e384d78ff78b |
comparison
equal
deleted
inserted
replaced
3808:5fab0fe3c445 | 3812:f788d2d901d6 |
---|---|
1 package de.intevation.flys.utils; | |
2 | |
3 import org.apache.log4j.Logger; | |
4 | |
5 import java.text.NumberFormat; | |
6 import java.util.HashMap; | |
7 import java.util.List; | |
8 import java.util.Map; | |
9 | |
10 import javax.xml.xpath.XPathConstants; | |
11 | |
12 import org.w3c.dom.Document; | |
13 | |
14 import org.hibernate.SessionFactory; | |
15 import org.hibernate.impl.SessionFactoryImpl; | |
16 | |
17 import gnu.trove.TDoubleArrayList; | |
18 | |
19 import de.intevation.artifacts.Artifact; | |
20 import de.intevation.artifacts.CallContext; | |
21 | |
22 import de.intevation.artifacts.common.utils.Config; | |
23 import de.intevation.artifacts.common.utils.XMLUtils; | |
24 | |
25 import de.intevation.flys.backend.SessionFactoryProvider; | |
26 | |
27 import de.intevation.flys.artifacts.context.FLYSContext; | |
28 import de.intevation.flys.artifacts.FLYSArtifact; | |
29 import de.intevation.flys.artifacts.WINFOArtifact; | |
30 import de.intevation.flys.artifacts.model.RiverFactory; | |
31 import de.intevation.flys.model.Gauge; | |
32 import de.intevation.flys.model.MainValue; | |
33 import de.intevation.flys.model.River; | |
34 | |
35 public class FLYSUtils { | |
36 | |
37 /** The logger that is used in this utility. */ | |
38 private static Logger logger = Logger.getLogger(FLYSUtils.class); | |
39 | |
40 public static enum KM_MODE { RANGE, LOCATIONS, NONE }; | |
41 | |
42 public static final String XPATH_RIVER_PROJECTION = | |
43 "/artifact-database/floodmap/river[@name=$name]/srid/@value"; | |
44 | |
45 public static final String XPATH_SHAPEFILE_DIR = | |
46 "/artifact-database/floodmap/shapefile-path/@value"; | |
47 | |
48 public static final String XPATH_VELOCITY_LOGFILE = | |
49 "/artifact-database/floodmap/velocity/logfile/@path"; | |
50 | |
51 public static final String XPATH_MAPSERVER_URL = | |
52 "/artifact-database/floodmap/mapserver/server/@path"; | |
53 | |
54 public static final String XPATH_MAPFILE_PATH = | |
55 "/artifact-database/floodmap/mapserver/mapfile/@path"; | |
56 | |
57 public static final String XPATH_MAPFILE_TEMPLATE = | |
58 "/artifact-database/floodmap/mapserver/map-template/@path"; | |
59 | |
60 public static final String XPATH_MAPSERVER_TEMPLATE_PATH = | |
61 "/artifact-database/floodmap/mapserver/templates/@path"; | |
62 | |
63 | |
64 private FLYSUtils() { | |
65 } | |
66 | |
67 | |
68 public static FLYSArtifact getArtifact(String uuid, CallContext context) { | |
69 try { | |
70 Artifact artifact = context.getDatabase().getRawArtifact(uuid); | |
71 | |
72 if (artifact == null) { | |
73 logger.error("Artifact '" + uuid + "' does not exist."); | |
74 return null; | |
75 } | |
76 | |
77 if (!(artifact instanceof FLYSArtifact)) { | |
78 logger.error("Artifact '" +uuid+ "' is no valid FLYSArtifact."); | |
79 return null; | |
80 } | |
81 | |
82 return (FLYSArtifact) artifact; | |
83 } | |
84 // TODO: catch more selective | |
85 catch (Exception e) { | |
86 logger.error("Cannot get FLYSArtifact " + uuid + " from database (" + e.getMessage() + ")."); | |
87 return null; | |
88 } | |
89 } | |
90 | |
91 | |
92 /** | |
93 * Returns the FLYSContext from context object. | |
94 * | |
95 * @param context The CallContext or the FLYSContext. | |
96 * | |
97 * @return the FLYSContext. | |
98 */ | |
99 public static FLYSContext getFlysContext(Object context) { | |
100 return context instanceof FLYSContext | |
101 ? (FLYSContext) context | |
102 : (FLYSContext) ((CallContext) context).globalContext(); | |
103 } | |
104 | |
105 | |
106 /** | |
107 * Convinience function to retrieve an XPath as string with replaced config | |
108 * directory. | |
109 * | |
110 * @param xpath The XPath expression. | |
111 * | |
112 * @return a string with replaced config directory. | |
113 */ | |
114 public static String getXPathString(String xpath) { | |
115 String tmp = Config.getStringXPath(xpath); | |
116 tmp = Config.replaceConfigDir(tmp); | |
117 | |
118 return tmp; | |
119 } | |
120 | |
121 | |
122 public static boolean isUsingOracle() { | |
123 SessionFactory sf = SessionFactoryProvider.getSessionFactory(); | |
124 | |
125 String d = SessionFactoryProvider.getDriver((SessionFactoryImpl) sf); | |
126 | |
127 return d != null ? d.indexOf("Oracle") >= 0 : false; | |
128 } | |
129 | |
130 | |
131 public static KM_MODE getKmRangeMode(FLYSArtifact flys) { | |
132 String mode = flys.getDataAsString("ld_mode"); | |
133 | |
134 if (mode == null || mode.length() == 0) { | |
135 return KM_MODE.NONE; | |
136 } | |
137 else if (mode.equals("distance")) { | |
138 return KM_MODE.RANGE; | |
139 } | |
140 else if (mode.equals("locations")) { | |
141 return KM_MODE.LOCATIONS; | |
142 } | |
143 else { | |
144 return KM_MODE.NONE; | |
145 } | |
146 } | |
147 | |
148 | |
149 public static double[] getKmRange(FLYSArtifact flys) { | |
150 switch (getKmRangeMode(flys)) { | |
151 case RANGE: { | |
152 return getKmFromTo(flys); | |
153 } | |
154 | |
155 case LOCATIONS: { | |
156 double[] locs = getLocations(flys); | |
157 return new double[] { locs[0], locs[locs.length-1] }; | |
158 } | |
159 | |
160 case NONE: { | |
161 double[] locs = getLocations(flys); | |
162 if (locs != null) { | |
163 return new double[] { locs[0], locs[locs.length-1] }; | |
164 } | |
165 else { | |
166 return getKmFromTo(flys); | |
167 } | |
168 } | |
169 } | |
170 | |
171 return new double[] { Double.NaN, Double.NaN }; | |
172 } | |
173 | |
174 | |
175 public static double[] getKmFromTo(FLYSArtifact flys) { | |
176 String strFrom = flys.getDataAsString("ld_from"); | |
177 String strTo = flys.getDataAsString("ld_to"); | |
178 | |
179 if (strFrom == null || strTo == null) { | |
180 return null; | |
181 } | |
182 | |
183 try { | |
184 return new double[] { | |
185 Double.parseDouble(strFrom), | |
186 Double.parseDouble(strTo) }; | |
187 } | |
188 catch (NumberFormatException nfe) { | |
189 return null; | |
190 } | |
191 } | |
192 | |
193 | |
194 public static double[] getLocations(FLYSArtifact flys) { | |
195 String locationStr = flys.getDataAsString("ld_locations"); | |
196 | |
197 if (locationStr == null || locationStr.length() == 0) { | |
198 return null; | |
199 } | |
200 | |
201 String[] tmp = locationStr.split(" "); | |
202 TDoubleArrayList locations = new TDoubleArrayList(); | |
203 | |
204 for (String l: tmp) { | |
205 try { | |
206 locations.add(Double.parseDouble(l)); | |
207 } | |
208 catch (NumberFormatException nfe) { | |
209 } | |
210 } | |
211 | |
212 locations.sort(); | |
213 | |
214 return locations.toNativeArray(); | |
215 } | |
216 | |
217 | |
218 /** | |
219 * Returns the selected River object based on the 'river' data that might | |
220 * have been inserted by the user. | |
221 * | |
222 * @return the selected River or null if no river has been chosen yet. | |
223 */ | |
224 public static River getRiver(FLYSArtifact flys) { | |
225 String sRiver = flys.getDataAsString("river"); | |
226 | |
227 return (sRiver != null) | |
228 ? RiverFactory.getRiver(sRiver) | |
229 : null; | |
230 } | |
231 | |
232 | |
233 /** | |
234 * Extracts the SRID defined in the global configuration for the river | |
235 * specified in <i>artifact</i>. | |
236 * | |
237 * @param artifact The FLYSArtifact that stores the name of the river. | |
238 * | |
239 * @return the SRID as string (e.g. "31466"). | |
240 */ | |
241 public static String getRiverSrid(FLYSArtifact artifact) { | |
242 String river = artifact.getDataAsString("river"); | |
243 | |
244 if (river == null || river.length() == 0) { | |
245 return null; | |
246 } | |
247 | |
248 return getRiverSrid(river); | |
249 } | |
250 | |
251 | |
252 public static String getRiverSrid(String rivername) { | |
253 Map<String, String> variables = new HashMap<String, String>(1); | |
254 variables.put("name", rivername); | |
255 | |
256 Document cfg = Config.getConfig(); | |
257 | |
258 return (String) XMLUtils.xpath( | |
259 cfg, | |
260 XPATH_RIVER_PROJECTION, | |
261 XPathConstants.STRING, | |
262 null, | |
263 variables); | |
264 } | |
265 | |
266 | |
267 public static String createWspWTitle( | |
268 WINFOArtifact winfo, | |
269 CallContext cc, | |
270 String name | |
271 ) { | |
272 String[] parts = name.split("="); | |
273 | |
274 NumberFormat nf = Formatter.getWaterlevelW(cc); | |
275 | |
276 String namedMainValue = null; | |
277 | |
278 boolean isQ = winfo.isQ(); | |
279 boolean isFree = winfo.isFreeQ(); | |
280 | |
281 double v; | |
282 | |
283 try { | |
284 v = Double.valueOf(parts[1]); | |
285 | |
286 namedMainValue = getNamedMainValue(winfo.getGauge(), v); | |
287 } | |
288 catch (NumberFormatException nfe) { | |
289 logger.warn("Cannot parse Double of: '" + parts[1] + "'"); | |
290 return name; | |
291 } | |
292 | |
293 String prefix = null; | |
294 | |
295 if (isQ && !isFree && namedMainValue != null) { | |
296 return "W (" + namedMainValue + ")"; | |
297 } | |
298 | |
299 if (isQ) { | |
300 prefix = "Q="; | |
301 } | |
302 | |
303 return prefix == null | |
304 ? "W(" + nf.format(v) + ")" | |
305 : "W(" + prefix + nf.format(v) + ")"; | |
306 } | |
307 | |
308 | |
309 public static String createWspQTitle( | |
310 WINFOArtifact winfo, | |
311 CallContext cc, | |
312 String name | |
313 ) { | |
314 String[] parts = name.split("="); | |
315 | |
316 NumberFormat nf = Formatter.getWaterlevelQ(cc); | |
317 | |
318 String namedMainValue = null; | |
319 | |
320 boolean isQ = winfo.isQ(); | |
321 boolean isFree = winfo.isFreeQ(); | |
322 | |
323 double v; | |
324 | |
325 try { | |
326 v = Double.valueOf(parts[1]); | |
327 | |
328 namedMainValue = getNamedMainValue(winfo.getGauge(), v); | |
329 } | |
330 catch (NumberFormatException nfe) { | |
331 logger.warn("Cannot parse Double of: '" + parts[1] + "'"); | |
332 return name; | |
333 } | |
334 | |
335 String prefix = null; | |
336 | |
337 if (isQ && !isFree && namedMainValue != null) { | |
338 return namedMainValue; | |
339 } | |
340 | |
341 if (!isQ) { | |
342 prefix = "W="; | |
343 } | |
344 | |
345 return prefix == null | |
346 ? "Q(" + nf.format(v) + ")" | |
347 : "Q(" + prefix + nf.format(v) + ")"; | |
348 } | |
349 | |
350 | |
351 public static String getNamedMainValue(Gauge gauge, double value) { | |
352 List<MainValue> mainValues = gauge.getMainValues(); | |
353 logger.debug("Search named main value for: " + value); | |
354 | |
355 for (MainValue mv: mainValues) { | |
356 if (mv.getValue().doubleValue() == value) { | |
357 logger.debug("Found named main value: " + mv.getMainValue().getName()); | |
358 return mv.getMainValue().getName(); | |
359 } | |
360 } | |
361 | |
362 logger.debug("Did not find a named main value for: " + value); | |
363 return null; | |
364 } | |
365 | |
366 | |
367 /** | |
368 * Returns the URL of user mapfile for the owner of Artifact | |
369 * <i>artifactId</i>. | |
370 * | |
371 * @param artifactId The UUID of an artifact. | |
372 * | |
373 * @return the URL of the user wms. | |
374 */ | |
375 public static String getUserWMSUrl(String artifactId) { | |
376 String url = getXPathString(XPATH_MAPSERVER_URL); | |
377 url = url + "user-wms"; | |
378 | |
379 return url; | |
380 } | |
381 } | |
382 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |