Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/utils/FLYSUtils.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/utils/FLYSUtils.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.utils; | |
2 | |
3 import org.dive4elements.artifactdatabase.state.State; | |
4 import org.dive4elements.artifacts.Artifact; | |
5 import org.dive4elements.artifacts.CallContext; | |
6 import org.dive4elements.artifacts.common.utils.Config; | |
7 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
8 import org.dive4elements.river.artifacts.FLYSArtifact; | |
9 import org.dive4elements.river.artifacts.StaticWKmsArtifact; | |
10 import org.dive4elements.river.artifacts.WINFOArtifact; | |
11 import org.dive4elements.river.artifacts.access.RangeAccess; | |
12 import org.dive4elements.river.artifacts.context.FLYSContext; | |
13 import org.dive4elements.river.artifacts.model.LocationProvider; | |
14 import org.dive4elements.river.artifacts.model.RiverFactory; | |
15 import org.dive4elements.river.artifacts.model.WKms; | |
16 import org.dive4elements.river.artifacts.model.WQ; | |
17 import org.dive4elements.river.artifacts.model.WQKms; | |
18 import org.dive4elements.river.artifacts.states.WDifferencesState; | |
19 import org.dive4elements.river.artifacts.states.WaterlevelSelectState; | |
20 import org.dive4elements.river.backend.SessionFactoryProvider; | |
21 import org.dive4elements.river.model.Gauge; | |
22 import org.dive4elements.river.model.MainValue; | |
23 import org.dive4elements.river.model.River; | |
24 | |
25 import gnu.trove.TDoubleArrayList; | |
26 import gnu.trove.TIntArrayList; | |
27 import gnu.trove.TLongArrayList; | |
28 | |
29 import java.text.NumberFormat; | |
30 import java.util.HashMap; | |
31 import java.util.List; | |
32 import java.util.Map; | |
33 import java.util.regex.Matcher; | |
34 import java.util.regex.Pattern; | |
35 | |
36 import javax.xml.xpath.XPathConstants; | |
37 | |
38 import org.apache.log4j.Logger; | |
39 import org.hibernate.SessionFactory; | |
40 import org.hibernate.impl.SessionFactoryImpl; | |
41 import org.w3c.dom.Document; | |
42 | |
43 | |
44 /** | |
45 * Static helper methods to e.g. access FLYSArtifacts data. | |
46 * | |
47 * @deprecated Don't use FLYSUtils to get data from an {@link Artifact} anymore. | |
48 * Instead use and/or create a {@link Access} class hierarchy. | |
49 **/ | |
50 @Deprecated | |
51 public class FLYSUtils { | |
52 | |
53 /** The logger that is used in this utility. */ | |
54 private static Logger logger = Logger.getLogger(FLYSUtils.class); | |
55 | |
56 /** | |
57 * An enum that represents the 5 possible WQ modes in FLYS. The 5 values are | |
58 * <i>QFREE</i> <i>QGAUGE</i> <i>WGAUGE</i> <i>WFREE</i> and <i>NONE</i>. | |
59 */ | |
60 public static enum WQ_MODE { QFREE, QGAUGE, WFREE, WGAUGE, NONE }; | |
61 | |
62 /** | |
63 * An enum that represents the 4 possible WQ input modes in FLYS. The 4 | |
64 * values are | |
65 * <i>ADAPTED</i> <i>SINGLE</i> <i>RANGE</i> and <i>NONE</i>. | |
66 */ | |
67 public static enum WQ_INPUT { ADAPTED, SINGLE, RANGE, NONE }; | |
68 | |
69 public static final Pattern NUMBERS_PATTERN = | |
70 Pattern.compile("\\D*(\\d++.\\d*)\\D*"); | |
71 | |
72 public static final String XPATH_FLOODMAP_RIVER_PROJECTION = | |
73 "/artifact-database/floodmap/river[@name=$name]/srid/@value"; | |
74 | |
75 public static final String XPATH_FLOODMAP_DGM_PROJECTION = | |
76 "/artifact-database/floodmap/river[@name=$name]/dgm-srid/@value"; | |
77 | |
78 public static final String XPATH_FLOODMAP_SHAPEFILE_DIR = | |
79 "/artifact-database/floodmap/shapefile-path/@value"; | |
80 | |
81 public static final String XPATH_FLOODMAP_VELOCITY_LOGFILE = | |
82 "/artifact-database/floodmap/velocity/logfile/@path"; | |
83 | |
84 public static final String XPATH_FLOODMAP_MAPSERVER_URL = | |
85 "/artifact-database/floodmap/mapserver/server/@path"; | |
86 | |
87 public static final String XPATH_RIVERMAP_MAPSERVER_URL = | |
88 "/artifact-database/rivermap/mapserver/server/@path"; | |
89 | |
90 public static final String XPATH_FLOODMAP_MAPFILE_PATH = | |
91 "/artifact-database/floodmap/mapserver/mapfile/@path"; | |
92 | |
93 public static final String XPATH_FLOODMAP_MAPFILE_TEMPLATE = | |
94 "/artifact-database/floodmap/mapserver/map-template/@path"; | |
95 | |
96 public static final String XPATH_FLOODMAP_MAPSERVER_TEMPLATE_PATH = | |
97 "/artifact-database/floodmap/mapserver/templates/@path"; | |
98 | |
99 | |
100 private FLYSUtils() { | |
101 } | |
102 | |
103 | |
104 /** | |
105 * Pulls Artifact with given UUID fromm database. | |
106 * @return FLYSArtifact with given UUID or null (in case of errors). | |
107 */ | |
108 public static FLYSArtifact getArtifact(String uuid, CallContext context) { | |
109 try { | |
110 Artifact artifact = context.getDatabase().getRawArtifact(uuid); | |
111 | |
112 if (artifact == null) { | |
113 logger.error("Artifact '" + uuid + "' does not exist."); | |
114 return null; | |
115 } | |
116 | |
117 if (!(artifact instanceof FLYSArtifact)) { | |
118 logger.error("Artifact '" +uuid+ "' is no valid FLYSArtifact."); | |
119 return null; | |
120 } | |
121 | |
122 return (FLYSArtifact) artifact; | |
123 } | |
124 // TODO: catch more selective | |
125 catch (Exception e) { | |
126 logger.error("Cannot get FLYSArtifact " + uuid | |
127 + " from database (" + e.getMessage() + ")."); | |
128 return null; | |
129 } | |
130 } | |
131 | |
132 | |
133 /** | |
134 * Returns the FLYSContext from context object. | |
135 * | |
136 * @param context The CallContext or the FLYSContext. | |
137 * | |
138 * @return the FLYSContext. | |
139 */ | |
140 public static FLYSContext getFlysContext(Object context) { | |
141 return context instanceof FLYSContext | |
142 ? (FLYSContext) context | |
143 : (FLYSContext) ((CallContext) context).globalContext(); | |
144 } | |
145 | |
146 | |
147 /** | |
148 * Convinience function to retrieve an XPath as string with replaced config | |
149 * directory. | |
150 * | |
151 * @param xpath The XPath expression. | |
152 * | |
153 * @return a string with replaced config directory. | |
154 */ | |
155 public static String getXPathString(String xpath) { | |
156 String tmp = Config.getStringXPath(xpath); | |
157 tmp = Config.replaceConfigDir(tmp); | |
158 | |
159 return tmp; | |
160 } | |
161 | |
162 | |
163 public static boolean isUsingOracle() { | |
164 SessionFactory sf = SessionFactoryProvider.getSessionFactory(); | |
165 | |
166 String d = SessionFactoryProvider.getDriver((SessionFactoryImpl) sf); | |
167 | |
168 return d != null ? d.indexOf("Oracle") >= 0 : false; | |
169 } | |
170 | |
171 | |
172 /** | |
173 * This method returns an WQ_MODE enum which is based on the parameters | |
174 * stored in <i>flys</i> Artifact. If there is no <i>wq_isq</i> parameter | |
175 * existing, WQ_MODE.NONE is returned. | |
176 * | |
177 * @param flys The FLYSArtifact that stores wq mode relevant parameters. | |
178 * | |
179 * @return an enum WQ_MODE. | |
180 */ | |
181 public static WQ_MODE getWQMode(FLYSArtifact flys) { | |
182 if (flys == null) { | |
183 return WQ_MODE.NONE; | |
184 } | |
185 | |
186 String values = flys.getDataAsString("wq_values"); | |
187 Boolean isQ = flys.getDataAsBoolean("wq_isq"); | |
188 | |
189 if (values != null) { | |
190 return isQ ? WQ_MODE.QGAUGE : WQ_MODE.WGAUGE; | |
191 } | |
192 | |
193 Boolean isFree = flys.getDataAsBoolean("wq_isfree"); | |
194 | |
195 if (isQ != null && isQ) { | |
196 return isFree ? WQ_MODE.QFREE : WQ_MODE.QGAUGE; | |
197 } | |
198 else if (isQ != null && !isQ) { | |
199 return isFree ? WQ_MODE.WFREE : WQ_MODE.WGAUGE; | |
200 } | |
201 else { | |
202 return WQ_MODE.NONE; | |
203 } | |
204 } | |
205 | |
206 | |
207 public static WQ_INPUT getWQInputMode(FLYSArtifact flys) { | |
208 if (flys == null) { | |
209 return WQ_INPUT.NONE; | |
210 } | |
211 | |
212 Boolean selection = flys.getDataAsBoolean("wq_isrange"); | |
213 String adapted = flys.getDataAsString("wq_values"); | |
214 | |
215 if(adapted != null && adapted.length() > 0) { | |
216 return WQ_INPUT.ADAPTED; | |
217 } | |
218 | |
219 if (selection != null && selection) { | |
220 return WQ_INPUT.RANGE; | |
221 } | |
222 else { | |
223 return WQ_INPUT.SINGLE; | |
224 } | |
225 } | |
226 | |
227 | |
228 /** | |
229 * Get bounds for river of artifact. | |
230 * @param flysArtifact artifact which has a "river" data. | |
231 * @return double array. min is at[0], max at[1]. null if given artifact is null | |
232 */ | |
233 public static double[] getRiverMinMax(FLYSArtifact flysArtifact) { | |
234 if (flysArtifact == null) { | |
235 return null; | |
236 } | |
237 | |
238 String riverName = flysArtifact.getDataAsString("river"); | |
239 | |
240 if (riverName == null) { | |
241 riverName = ""; | |
242 } | |
243 | |
244 logger.debug("Search for the min/max distances of '" + riverName + "'"); | |
245 | |
246 River river = RiverFactory.getRiver(riverName); | |
247 | |
248 return river != null | |
249 ? river.determineMinMaxDistance() | |
250 : null; | |
251 } | |
252 | |
253 | |
254 public static double[] getKmFromTo(FLYSArtifact flys) { | |
255 String strFrom = flys.getDataAsString("ld_from"); | |
256 String strTo = flys.getDataAsString("ld_to"); | |
257 | |
258 if (strFrom == null) { | |
259 strFrom = flys.getDataAsString("from"); | |
260 } | |
261 | |
262 if (strTo == null) { | |
263 strTo = flys.getDataAsString("to"); | |
264 } | |
265 | |
266 if (strFrom == null || strTo == null) { | |
267 return null; | |
268 } | |
269 | |
270 try { | |
271 return new double[] { | |
272 Double.parseDouble(strFrom), | |
273 Double.parseDouble(strTo) }; | |
274 } | |
275 catch (NumberFormatException nfe) { | |
276 return null; | |
277 } | |
278 } | |
279 | |
280 | |
281 /** | |
282 * Return sorted array of locations at which stuff was calculated | |
283 * (from ld_locations data), null if not parameterized this way. | |
284 */ | |
285 // TODO moved to RangeAccess. Resolve remaining calls. | |
286 private static double[] getLocations(FLYSArtifact flys) { | |
287 String locationStr = flys.getDataAsString("ld_locations"); | |
288 | |
289 if (locationStr == null || locationStr.length() == 0) { | |
290 if (flys instanceof WINFOArtifact) { | |
291 WINFOArtifact winfo = (WINFOArtifact) flys; | |
292 if (winfo.getReferenceStartKm() != null && winfo.getReferenceEndKms() != null) { | |
293 return new double[] | |
294 { | |
295 winfo.getReferenceStartKm().doubleValue(), | |
296 winfo.getReferenceEndKms()[0] | |
297 }; | |
298 } | |
299 } | |
300 return null; | |
301 } | |
302 | |
303 String[] tmp = locationStr.split(" "); | |
304 TDoubleArrayList locations = new TDoubleArrayList(); | |
305 | |
306 for (String l: tmp) { | |
307 try { | |
308 locations.add(Double.parseDouble(l)); | |
309 } | |
310 catch (NumberFormatException nfe) { | |
311 logger.debug(nfe.getLocalizedMessage(), nfe); | |
312 } | |
313 } | |
314 | |
315 locations.sort(); | |
316 | |
317 return locations.toNativeArray(); | |
318 } | |
319 | |
320 | |
321 /** | |
322 * Returns the Qs for a given FLYSArtifact. This method currently accepts | |
323 * only instances of WINFOArtifact. | |
324 * | |
325 * @param flys A FLYSArtifact. | |
326 * | |
327 * @return the Qs. | |
328 */ | |
329 public static double[] getQs(FLYSArtifact flys) { | |
330 // XXX this is not nice! | |
331 if (flys instanceof WINFOArtifact) { | |
332 return ((WINFOArtifact) flys).getQs(); | |
333 } | |
334 | |
335 logger.warn("This method currently supports WINFOArtifact only!"); | |
336 | |
337 return null; | |
338 } | |
339 | |
340 | |
341 /** | |
342 * Returns the Ws for a given FLYSArtifact. This method currently accepts | |
343 * only instances of WINFOArtifact. | |
344 * | |
345 * @param flys A FLYSArtifact. | |
346 * | |
347 * @return the Ws. | |
348 */ | |
349 public static double[] getWs(FLYSArtifact flys) { | |
350 // XXX this is not nice! | |
351 if (flys instanceof WINFOArtifact) { | |
352 return ((WINFOArtifact) flys).getWs(); | |
353 } | |
354 | |
355 logger.warn("This method currently supports WINFOArtifact only!"); | |
356 | |
357 return null; | |
358 } | |
359 | |
360 | |
361 /** | |
362 * Returns the selected River object based on the 'river' data that might | |
363 * have been inserted by the user. | |
364 * | |
365 * @return the selected River or null if no river has been chosen yet. | |
366 */ | |
367 public static River getRiver(FLYSArtifact flys) { | |
368 String sRiver = getRivername(flys); | |
369 | |
370 return (sRiver != null) | |
371 ? RiverFactory.getRiver(sRiver) | |
372 : null; | |
373 } | |
374 | |
375 | |
376 /** | |
377 * Returns the name of the river specified in the given <i>flys</i> | |
378 * Artifact. | |
379 * | |
380 * @param flys The FLYSArtifact that stores a river relevant information. | |
381 * | |
382 * @return the name of the specified river or null. | |
383 */ | |
384 public static String getRivername(FLYSArtifact flys) { | |
385 return flys != null ? flys.getDataAsString("river") : null; | |
386 } | |
387 | |
388 | |
389 /** | |
390 * Extracts the SRID defined in the global configuration for the river | |
391 * specified in <i>artifact</i>. | |
392 * | |
393 * @param artifact The FLYSArtifact that stores the name of the river. | |
394 * | |
395 * @return the SRID as string (e.g. "31466"). | |
396 */ | |
397 public static String getRiverSrid(FLYSArtifact artifact) { | |
398 String river = artifact.getDataAsString("river"); | |
399 | |
400 if (river == null || river.length() == 0) { | |
401 return null; | |
402 } | |
403 | |
404 return getRiverSrid(river); | |
405 } | |
406 | |
407 | |
408 public static String getRiverSrid(String rivername) { | |
409 Map<String, String> variables = new HashMap<String, String>(1); | |
410 variables.put("name", rivername); | |
411 | |
412 Document cfg = Config.getConfig(); | |
413 | |
414 return (String) XMLUtils.xpath( | |
415 cfg, | |
416 XPATH_FLOODMAP_RIVER_PROJECTION, | |
417 XPathConstants.STRING, | |
418 null, | |
419 variables); | |
420 } | |
421 | |
422 public static String getRiverDGMSrid(String rivername) { | |
423 Map<String, String> variables = new HashMap<String, String>(1); | |
424 variables.put("name", rivername); | |
425 | |
426 Document cfg = Config.getConfig(); | |
427 | |
428 String dgm = (String) XMLUtils.xpath( | |
429 cfg, | |
430 XPATH_FLOODMAP_DGM_PROJECTION, | |
431 XPathConstants.STRING, | |
432 null, | |
433 variables); | |
434 if (logger.isDebugEnabled()) { | |
435 logger.debug("Use EPSG:" + dgm + " for DGM"); | |
436 } | |
437 return dgm; | |
438 } | |
439 | |
440 /** | |
441 * Return the (first) Gauge corresponding to the given location(s) of | |
442 * the artifact. | |
443 * @param flys the artifact in question. | |
444 * @return (First) gauge of locations of river of artifact. | |
445 */ | |
446 public static Gauge getGauge(FLYSArtifact flys) { | |
447 River river = getRiver(flys); | |
448 | |
449 if (river == null) { | |
450 logger.debug("no river found"); | |
451 return null; | |
452 } | |
453 | |
454 RangeAccess rangeAccess = new RangeAccess(flys, null); | |
455 double[] dist = rangeAccess.getKmRange(); | |
456 | |
457 if (dist == null) { | |
458 logger.debug("no range found"); | |
459 return null; | |
460 } | |
461 | |
462 if (logger.isDebugEnabled()) { | |
463 logger.debug("Determine gauge for:"); | |
464 logger.debug("... river: " + river.getName()); | |
465 logger.debug("... distance: " + dist[0] + " - " + dist[1]); | |
466 } | |
467 | |
468 Gauge gauge = river.determineGauge(dist[0], dist[1]); | |
469 | |
470 String name = gauge != null ? gauge.getName() : "'n/a"; | |
471 logger.debug("Found gauge: " + name); | |
472 | |
473 return gauge; | |
474 } | |
475 | |
476 | |
477 public static String getGaugename(FLYSArtifact flys) { | |
478 Gauge gauge = getGauge(flys); | |
479 | |
480 return gauge != null ? gauge.getName() : null; | |
481 } | |
482 | |
483 | |
484 public static Gauge getReferenceGauge(FLYSArtifact flys) { | |
485 Long officialNumber = flys.getDataAsLong("reference_gauge"); | |
486 | |
487 return officialNumber != null | |
488 ? Gauge.getGaugeByOfficialNumber(officialNumber) | |
489 : null; | |
490 } | |
491 | |
492 | |
493 public static String getReferenceGaugeName(FLYSArtifact flys) { | |
494 Gauge refGauge = getReferenceGauge(flys); | |
495 | |
496 return refGauge != null | |
497 ? refGauge.getName() | |
498 : "-- not found --"; | |
499 } | |
500 | |
501 | |
502 public static Double getValueFromWQ(WQ wq) { | |
503 if (wq == null) { | |
504 return null; | |
505 } | |
506 | |
507 Matcher m = NUMBERS_PATTERN.matcher(wq.getName()); | |
508 | |
509 if (m.matches()) { | |
510 logger.debug("Found a number."); | |
511 | |
512 String raw = m.group(1); | |
513 | |
514 try { | |
515 return Double.valueOf(raw); | |
516 } | |
517 catch (NumberFormatException nfe) { | |
518 } | |
519 } | |
520 | |
521 return null; | |
522 } | |
523 | |
524 | |
525 /** Creates human-readable name for a wsp (waterlevel/longitudinal section). | |
526 * @param name will be split at '='s. | |
527 */ | |
528 public static String createWspWTitle( | |
529 WINFOArtifact winfo, | |
530 CallContext cc, | |
531 String name | |
532 ) { | |
533 String[] parts = name.split("="); | |
534 | |
535 NumberFormat nf = Formatter.getWaterlevelW(cc); | |
536 | |
537 String namedMainValue = null; | |
538 | |
539 boolean isQ = winfo.isQ(); | |
540 boolean isFree = winfo.isFreeQ(); | |
541 | |
542 double v; | |
543 | |
544 try { | |
545 v = Double.valueOf(parts[1]); | |
546 | |
547 namedMainValue = getNamedMainValue(winfo.getGauge(), v); | |
548 } | |
549 catch (NumberFormatException nfe) { | |
550 logger.warn("Cannot parse Double of: '" + parts[1] + "'"); | |
551 return name; | |
552 } | |
553 | |
554 String prefix = null; | |
555 | |
556 if (isQ && !isFree && namedMainValue != null) { | |
557 return "W (" + namedMainValue + ")"; | |
558 } | |
559 | |
560 if (isQ) { | |
561 prefix = "Q="; | |
562 } | |
563 | |
564 return prefix == null | |
565 ? "W(" + nf.format(v) + ")" | |
566 : "W(" + prefix + nf.format(v) + ")"; | |
567 } | |
568 | |
569 | |
570 public static String createWspQTitle( | |
571 WINFOArtifact winfo, | |
572 CallContext cc, | |
573 String name | |
574 ) { | |
575 String[] parts = name.split("="); | |
576 | |
577 NumberFormat nf = Formatter.getWaterlevelQ(cc); | |
578 | |
579 String namedMainValue = null; | |
580 | |
581 boolean isQ = winfo.isQ(); | |
582 boolean isFree = winfo.isFreeQ(); | |
583 | |
584 double v; | |
585 | |
586 try { | |
587 v = Double.valueOf(parts[1]); | |
588 | |
589 namedMainValue = getNamedMainValue(winfo.getGauge(), v); | |
590 } | |
591 catch (NumberFormatException nfe) { | |
592 logger.warn("Cannot parse Double of: '" + parts[1] + "'"); | |
593 return name; | |
594 } | |
595 | |
596 String prefix = null; | |
597 | |
598 if (isQ && !isFree && namedMainValue != null) { | |
599 return namedMainValue; | |
600 } | |
601 | |
602 if (!isQ) { | |
603 prefix = "W="; | |
604 } | |
605 | |
606 return prefix == null | |
607 ? "Q(" + nf.format(v) + ")" | |
608 : "Q(" + prefix + nf.format(v) + ")"; | |
609 } | |
610 | |
611 | |
612 /** | |
613 * Returns the named main value if a Q was selected and if this Q fits to a | |
614 * named main value. Otherwise, this function returns null. | |
615 * | |
616 * @param winfo The WINFO Artifact. | |
617 * @param value The Q (or W) value. | |
618 * | |
619 * @return a named main value or null. | |
620 */ | |
621 public static String getNamedMainValue(WINFOArtifact winfo, double value) { | |
622 WQ_MODE wqmode = getWQMode(winfo); | |
623 | |
624 if (wqmode != WQ_MODE.QGAUGE) { | |
625 return null; | |
626 } | |
627 else { | |
628 return getNamedMainValue(winfo.getGauge(), value); | |
629 } | |
630 } | |
631 | |
632 | |
633 public static String getNamedMainValue(Gauge gauge, double value) { | |
634 List<MainValue> mainValues = gauge.getMainValues(); | |
635 logger.debug("Search named main value for: " + value); | |
636 | |
637 for (MainValue mv: mainValues) { | |
638 if (mv.getValue().doubleValue() == value) { | |
639 logger.debug("Found named main value: " + mv.getMainValue().getName()); | |
640 return mv.getMainValue().getName(); | |
641 } | |
642 } | |
643 | |
644 logger.debug("Did not find a named main value for: " + value); | |
645 return null; | |
646 } | |
647 | |
648 | |
649 /** | |
650 * | |
651 * @param nmv A string that represents a named main value. | |
652 * | |
653 * @throws NullPointerException if nmv is null. | |
654 */ | |
655 public static String stripNamedMainValue(String nmv) { | |
656 int startIndex = nmv.indexOf("("); | |
657 int endIndex = nmv.indexOf(")"); | |
658 | |
659 if (startIndex > 0 && endIndex > 0 && startIndex < endIndex) { | |
660 return nmv.substring(0, startIndex); | |
661 } | |
662 | |
663 return nmv; | |
664 } | |
665 | |
666 | |
667 /** | |
668 * Returns the URL of user mapfile for the owner of Artifact | |
669 * <i>artifactId</i>. | |
670 * | |
671 * @param artifactId The UUID of an artifact. | |
672 * | |
673 * @return the URL of the user wms. | |
674 */ | |
675 public static String getUserWMSUrl(String artifactId) { | |
676 String url = getXPathString(XPATH_FLOODMAP_MAPSERVER_URL); | |
677 url = url.endsWith("/") ? url + "user-wms" : url + "/" + "user-wms"; | |
678 | |
679 return url; | |
680 } | |
681 | |
682 | |
683 public static String getRiverWMSUrl() { | |
684 String url = getXPathString(XPATH_RIVERMAP_MAPSERVER_URL); | |
685 url = url.endsWith("/") ? url + "river-wms" : url + "/" + "river-wms"; | |
686 | |
687 return url; | |
688 } | |
689 | |
690 | |
691 /** | |
692 * This method returns the description for a given <i>km</i> for a specific | |
693 * river. The river is provided by the FLYSArtifact <i>flys</i>. | |
694 * | |
695 * @param flys The FLYSArtifact that provides a river. | |
696 * @param km The kilometer. | |
697 * | |
698 * @return the description for <i>km</i> or an empty string if no | |
699 * description was found. | |
700 */ | |
701 public static String getLocationDescription(FLYSArtifact flys, double km) { | |
702 String river = getRivername(flys); | |
703 | |
704 if (river == null) { | |
705 return ""; | |
706 } | |
707 | |
708 return LocationProvider.getLocation(river, km); | |
709 } | |
710 | |
711 | |
712 /** | |
713 * This method returns the differences for a w-differences calculation. | |
714 * | |
715 * @param winfo The WINFOArtifact. | |
716 * @param context The context. | |
717 * | |
718 * @return The differences as string separated by semicolon and linebreak. | |
719 */ | |
720 public static String getWDifferences( | |
721 WINFOArtifact winfo, | |
722 CallContext context) | |
723 { | |
724 State state = winfo.getCurrentState(context); | |
725 if(state instanceof WDifferencesState) { | |
726 String diffids = winfo.getDataAsString("diffids"); | |
727 String datas[] = diffids.split("#"); | |
728 | |
729 // Validate the Data-Strings. | |
730 for (String s: datas) { | |
731 if (!WaterlevelSelectState.isValueValid(s)) { | |
732 return ""; | |
733 } | |
734 } | |
735 | |
736 if (datas.length < 2) { | |
737 return ""; | |
738 } | |
739 | |
740 String diffs = ""; | |
741 for(int i = 0; i < datas.length; i+=2) { | |
742 // e.g.: | |
743 // 42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;1 | |
744 WKms minuendWKms = getWKms(StringUtil.unbracket(datas[i+0]), | |
745 context); | |
746 WKms subtrahendWKms = getWKms(StringUtil.unbracket(datas[i+1]), | |
747 context); | |
748 if (minuendWKms != null && subtrahendWKms != null) { | |
749 diffs += StringUtil.wWrap(minuendWKms.getName()) | |
750 + " - " + StringUtil.wWrap(subtrahendWKms.getName()); | |
751 } | |
752 diffs += ";\n"; | |
753 } | |
754 return diffs; | |
755 } | |
756 else { | |
757 logger.warn("Not a valid state for differences."); | |
758 return ""; | |
759 } | |
760 } | |
761 | |
762 | |
763 protected static WKms getWKms(String mingle, CallContext context) { | |
764 String[] def = mingle.split(";"); | |
765 String uuid = def[0]; | |
766 String name = def[1]; | |
767 int idx = Integer.parseInt(def[2]); | |
768 | |
769 if (name.startsWith("staticwkms")) { | |
770 StaticWKmsArtifact staticWKms = | |
771 (StaticWKmsArtifact) FLYSUtils.getArtifact( | |
772 uuid, | |
773 context); | |
774 WKms wkms = staticWKms.getWKms(idx); | |
775 if (wkms == null) | |
776 logger.error("No WKms from artifact."); | |
777 return wkms; | |
778 } | |
779 | |
780 WINFOArtifact flys = (WINFOArtifact) FLYSUtils.getArtifact( | |
781 uuid, | |
782 context); | |
783 | |
784 if (flys == null) { | |
785 logger.warn("One of the artifacts (1) for diff calculation could not be loaded"); | |
786 return null; | |
787 } | |
788 else{ | |
789 WQKms[] wqkms = (WQKms[]) flys.getWaterlevelData(). | |
790 getData(); | |
791 if (wqkms == null) | |
792 logger.warn("not waterlevels in artifact"); | |
793 else if (wqkms.length < idx) | |
794 logger.warn("not enough waterlevels in artifact"); | |
795 return wqkms[idx]; | |
796 } | |
797 } | |
798 | |
799 | |
800 /** | |
801 * This method transform a string into an int array. Therefore, the string | |
802 * <i>raw</i> must consist of int values separated by a <i>';'</i>. | |
803 * | |
804 * @param raw The raw integer array as string separated by a ';'. | |
805 * | |
806 * @return an array of int values. | |
807 */ | |
808 public static int[] intArrayFromString(String raw) { | |
809 String[] splitted = raw != null ? raw.split(";") : null; | |
810 | |
811 if (splitted == null || splitted.length == 0) { | |
812 logger.warn("No integer values found in '" + raw + "'"); | |
813 return new int[0]; | |
814 } | |
815 | |
816 TIntArrayList integers = new TIntArrayList(splitted.length); | |
817 | |
818 for (String value: splitted) { | |
819 try { | |
820 integers.add(Integer.parseInt(value)); | |
821 } | |
822 catch (NumberFormatException nfe) { | |
823 logger.warn("Parsing integer failed: " + nfe); | |
824 } | |
825 } | |
826 | |
827 return integers.toNativeArray(); | |
828 } | |
829 | |
830 | |
831 /** | |
832 * This method transform a string into a long array. Therefore, the string | |
833 * <i>raw</i> must consist of int values separated by a <i>';'</i>. | |
834 * | |
835 * @param raw The raw long array as string separated by a ';'. | |
836 * | |
837 * @return an array of int values. | |
838 */ | |
839 public static long[] longArrayFromString(String raw) { | |
840 String[] splitted = raw != null ? raw.split(";") : null; | |
841 | |
842 if (splitted == null || splitted.length == 0) { | |
843 logger.warn("No long values found in '" + raw + "'"); | |
844 return new long[0]; | |
845 } | |
846 | |
847 TLongArrayList longs = new TLongArrayList(splitted.length); | |
848 | |
849 for (String value: splitted) { | |
850 try { | |
851 longs.add(Long.valueOf(value)); | |
852 } | |
853 catch (NumberFormatException nfe) { | |
854 logger.warn("Parsing long failed: " + nfe); | |
855 } | |
856 } | |
857 | |
858 return longs.toNativeArray(); | |
859 } | |
860 | |
861 | |
862 /** | |
863 * This method transform a string into an double array. Therefore, the | |
864 * string <i>raw</i> must consist of double values separated by a | |
865 * <i>';'</i>. | |
866 * | |
867 * @param raw The raw double array as string separated by a ';'. | |
868 * | |
869 * @return an array of double values. | |
870 */ | |
871 public static double[] doubleArrayFromString(String raw) { | |
872 String[] splitted = raw != null ? raw.split(";") : null; | |
873 | |
874 if (splitted == null || splitted.length == 0) { | |
875 logger.warn("No double values found in '" + raw + "'"); | |
876 return new double[0]; | |
877 } | |
878 | |
879 TDoubleArrayList doubles = new TDoubleArrayList(splitted.length); | |
880 | |
881 for (String value: splitted) { | |
882 try { | |
883 doubles.add(Double.valueOf(value)); | |
884 } | |
885 catch (NumberFormatException nfe) { | |
886 logger.warn("Parsing double failed: " + nfe); | |
887 } | |
888 } | |
889 | |
890 return doubles.toNativeArray(); | |
891 } | |
892 | |
893 | |
894 /** | |
895 * Returns the gauges that match the selected kilometer range. | |
896 * | |
897 * @param flys the flys artifact. | |
898 * | |
899 * @return the gauges based on the selected kilometer range (null if | |
900 * none/no range set). | |
901 */ | |
902 public static List<Gauge> getGauges(FLYSArtifact flys) { | |
903 | |
904 River river = getRiver(flys); | |
905 if (river == null) { | |
906 logger.debug("getGauges: no river!"); | |
907 return null; | |
908 } | |
909 | |
910 RangeAccess rangeAccess = new RangeAccess(flys, null); | |
911 double[] dist = rangeAccess.getKmRange(); | |
912 if (dist == null) { | |
913 logger.debug("getGauges: no dist!"); | |
914 return null; | |
915 } | |
916 logger.debug("getGauges: " + dist[0] + " - " + dist[1]); | |
917 | |
918 return river.determineGauges(dist[0], dist[1]); | |
919 } | |
920 } | |
921 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |