comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQSelect.java @ 2617:71086a3a1c5f

Issue 499. Added validation for 'W free'. flys-artifacts/trunk@4201 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Thu, 05 Apr 2012 08:51:53 +0000
parents efb2038783f9
children 3a93bbbe2ec7
comparison
equal deleted inserted replaced
2616:27cc95e65f18 2617:71086a3a1c5f
23 23
24 import de.intevation.flys.artifacts.FLYSArtifact; 24 import de.intevation.flys.artifacts.FLYSArtifact;
25 import de.intevation.flys.artifacts.WINFOArtifact; 25 import de.intevation.flys.artifacts.WINFOArtifact;
26 26
27 import de.intevation.flys.artifacts.model.WstFactory; 27 import de.intevation.flys.artifacts.model.WstFactory;
28 import de.intevation.flys.artifacts.model.WstValueTable;
28 import de.intevation.flys.artifacts.resources.Resources; 29 import de.intevation.flys.artifacts.resources.Resources;
29 30
30 import de.intevation.flys.utils.FLYSUtils; 31 import de.intevation.flys.utils.FLYSUtils;
31 32
32 33
236 Artifact artifact, 237 Artifact artifact,
237 String name, 238 String name,
238 CallContext context) 239 CallContext context)
239 { 240 {
240 double[] minmaxW = determineMinMaxW(artifact); 241 double[] minmaxW = determineMinMaxW(artifact);
242 double[] minmaxWFree = determineMinMaxWFree(artifact);
241 double[] minmaxQ = determineMinMaxQAtGauge(artifact); 243 double[] minmaxQ = determineMinMaxQAtGauge(artifact);
242 double[] minmaxQFree = determineMinMaxQ(artifact); 244 double[] minmaxQFree = determineMinMaxQ(artifact);
243 245
244 if (name.equals("wq_from")) { 246 if (name.equals("wq_from")) {
245 Element minW = createItem(cr, new String[] { 247 Element minW = createItem(cr, new String[] {
252 254
253 Element minQFree = createItem(cr, new String[] { 255 Element minQFree = createItem(cr, new String[] {
254 "minQFree", 256 "minQFree",
255 String.valueOf(minmaxQFree[0])}); 257 String.valueOf(minmaxQFree[0])});
256 258
257 return new Element[] { minW, minQ, minQFree }; 259 Element minWFree = createItem(cr, new String[] {
260 "minWFree",
261 String.valueOf(minmaxWFree[0])});
262
263 return new Element[] { minW, minQ, minQFree, minWFree };
258 } 264 }
259 else if (name.equals("wq_to")) { 265 else if (name.equals("wq_to")) {
260 Element maxW = createItem(cr, new String[] { 266 Element maxW = createItem(cr, new String[] {
261 "maxW", 267 "maxW",
262 String.valueOf(minmaxW[1])}); 268 String.valueOf(minmaxW[1])});
267 273
268 Element maxQFree = createItem(cr, new String[] { 274 Element maxQFree = createItem(cr, new String[] {
269 "maxQFree", 275 "maxQFree",
270 String.valueOf(minmaxQFree[1])}); 276 String.valueOf(minmaxQFree[1])});
271 277
272 return new Element[] { maxW, maxQ, maxQFree }; 278 Element maxWFree = createItem(cr, new String[] {
279 "maxWFree",
280 String.valueOf(minmaxWFree[1])});
281
282 return new Element[] { maxW, maxQ, maxQFree, maxWFree };
273 } 283 }
274 else { 284 else {
275 Element stepW = createItem( 285 Element stepW = createItem(
276 cr, new String[] { 286 cr, new String[] {
277 "stepW", 287 "stepW",
282 String.valueOf(getStepsQ(minmaxQ[0], minmaxQ[1]))}); 292 String.valueOf(getStepsQ(minmaxQ[0], minmaxQ[1]))});
283 Element stepQFree = createItem( 293 Element stepQFree = createItem(
284 cr, new String[] { 294 cr, new String[] {
285 "stepQFree", 295 "stepQFree",
286 String.valueOf(getStepsQ(minmaxQFree[0], minmaxQFree[1]))}); 296 String.valueOf(getStepsQ(minmaxQFree[0], minmaxQFree[1]))});
287 297 Element stepWFree = createItem(
288 return new Element[] { stepW, stepQ, stepQFree }; 298 cr, new String[] {
299 "stepWFree",
300 String.valueOf(getStepsW(minmaxWFree[0], minmaxWFree[1]))});
301
302 return new Element[] { stepW, stepQ, stepQFree, stepWFree };
289 } 303 }
290 } 304 }
291 305
292 306
293 protected static double getStepsW(double min, double max) { 307 protected static double getStepsW(double min, double max) {
384 return new double[] { minW, maxW }; 398 return new double[] { minW, maxW };
385 } 399 }
386 400
387 401
388 /** 402 /**
403 * Determines the min and max W value. If no min and
404 * max values could be determined, this method will return
405 * [Double.MIN_VALUE, Double.MAX_VALUE].
406 *
407 * @param artifact The FLYSArtifact.
408 *
409 * @return the min and max W values.
410 */
411 protected double[] determineMinMaxWFree(Artifact artifact) {
412 logger.debug("WQSelect.determineMinMaxWFree");
413
414 WINFOArtifact winfo = (WINFOArtifact) artifact;
415 WstValueTable valueTable = winfo.getWstValueTable();
416
417 double[] minmaxW = null;
418 if(valueTable != null) {
419 double[] km = null;
420 if(winfo.isRange()) {
421 km = winfo.getFromToStep();
422 minmaxW = valueTable.getMinMaxW(km[0], km[1], km[2]);
423 }
424 else {
425 km = winfo.getKms();
426 minmaxW = valueTable.getMinMaxW(km[0]);
427 for (int i = 1; i < km.length; i++) {
428 double[] tmp = valueTable.getMinMaxW(km[i]);
429 if(tmp[0] < minmaxW[0]) {
430 minmaxW[0] = tmp[0];
431 }
432 if(tmp[1] > minmaxW[1]) {
433 minmaxW[1] = tmp[1];
434 }
435 }
436 }
437 }
438 return minmaxW != null
439 ? minmaxW
440 : new double[] { Double.MIN_VALUE, Double.MAX_VALUE };
441 }
442
443
444 /**
389 * Determines the min and max Q value for the current gauge. If no min and 445 * Determines the min and max Q value for the current gauge. If no min and
390 * max values could be determined, this method will return 446 * max values could be determined, this method will return
391 * [Double.MIN_VALUE, Double.MAX_VALUE]. 447 * [Double.MIN_VALUE, Double.MAX_VALUE].
392 * 448 *
393 * @param artifact The FLYSArtifact. 449 * @param artifact The FLYSArtifact.
423 * @return the min and max Q values for the current kilometer range. 479 * @return the min and max Q values for the current kilometer range.
424 */ 480 */
425 protected double[] determineMinMaxQ(Artifact artifact) { 481 protected double[] determineMinMaxQ(Artifact artifact) {
426 logger.debug("WQSelect.determineMinMaxQ"); 482 logger.debug("WQSelect.determineMinMaxQ");
427 483
428 FLYSArtifact flys = (FLYSArtifact) artifact; 484 WINFOArtifact winfo = (WINFOArtifact) artifact;
429 double[] kms = FLYSUtils.getKmRange(flys); 485 WstValueTable valueTable = winfo.getWstValueTable();
430 486
431 if (kms == null || kms.length == 0) { 487 double[] minmaxQ = null;
432 return new double[] { Double.MIN_VALUE, Double.MAX_VALUE }; 488 if(valueTable != null) {
433 } 489 double[] km = null;
434 490 if(winfo.isRange()) {
435 River river = FLYSUtils.getRiver(flys); 491 km = winfo.getFromToStep();
436 Wst wst = WstFactory.getWst(river); 492 minmaxQ = valueTable.getMinMaxQ(km[0], km[1], km[2]);
437 493 }
438 logger.debug("User defined KMs: " + kms[0] + " - " + kms[kms.length-1]); 494 else {
439 495 km = winfo.getKms();
440 double[] minmaxQ = wst.determineMinMaxQFree(kms[0]); 496 minmaxQ = valueTable.getMinMaxQ(km[0]);
441 497 for (int i = 1; i < km.length; i++) {
498 double[] tmp = valueTable.getMinMaxQ(km[i]);
499 if(tmp[0] < minmaxQ[0]) {
500 minmaxQ[0] = tmp[0];
501 }
502 if(tmp[1] > minmaxQ[1]) {
503 minmaxQ[1] = tmp[1];
504 }
505 }
506 }
507 }
442 return minmaxQ != null 508 return minmaxQ != null
443 ? minmaxQ 509 ? minmaxQ
444 : new double[] { Double.MIN_VALUE, Double.MAX_VALUE }; 510 : new double[] { Double.MIN_VALUE, Double.MAX_VALUE };
445 } 511 }
446 512
529 minmax = determineMinMaxW(artifact); 595 minmax = determineMinMaxW(artifact);
530 } 596 }
531 else if (mode == FLYSUtils.WQ_MODE.QGAUGE) { 597 else if (mode == FLYSUtils.WQ_MODE.QGAUGE) {
532 minmax = determineMinMaxQAtGauge(artifact); 598 minmax = determineMinMaxQAtGauge(artifact);
533 } 599 }
600 else if (mode == FLYSUtils.WQ_MODE.QFREE) {
601 minmax = determineMinMaxQ(artifact);
602 }
534 else { 603 else {
535 minmax = determineMinMaxQ(artifact); 604 minmax = determineMinMaxWFree(artifact);
536 } 605 }
537 606
538 double min = all.get(0); 607 double min = all.get(0);
539 double max = all.get(all.size()-1); 608 double max = all.get(all.size()-1);
540 609
580 else if (mode == FLYSUtils.WQ_MODE.QGAUGE) { 649 else if (mode == FLYSUtils.WQ_MODE.QGAUGE) {
581 return validateGaugeQ(artifact, from, to, step); 650 return validateGaugeQ(artifact, from, to, step);
582 } 651 }
583 else if (mode == FLYSUtils.WQ_MODE.QFREE) { 652 else if (mode == FLYSUtils.WQ_MODE.QFREE) {
584 return validateFreeQ(artifact, from, to, step); 653 return validateFreeQ(artifact, from, to, step);
654 }
655 else if (mode == FLYSUtils.WQ_MODE.WFREE) {
656 return validateFreeW(artifact, from, to, step);
585 } 657 }
586 else { 658 else {
587 throw new IllegalArgumentException( 659 throw new IllegalArgumentException(
588 "error_feed_invalid_wq_mode"); 660 "error_feed_invalid_wq_mode");
589 } 661 }
667 739
668 double[] minmaxQ = determineMinMaxQ(artifact); 740 double[] minmaxQ = determineMinMaxQ(artifact);
669 741
670 return validateBounds(minmaxQ[0], minmaxQ[1], from, to, step); 742 return validateBounds(minmaxQ[0], minmaxQ[1], from, to, step);
671 } 743 }
744
745
746 /**
747 * Validates the inserted W values based on the W range for the current
748 * kilometer range.
749 *
750 * @param artifact The owner artifact.
751 * @param from The lower value of the W range.
752 * @param to The upper value of the W range.
753 * @param step The step width.
754 *
755 * @return true, if everything was fine, otherwise an exception is thrown.
756 */
757 protected boolean validateFreeW(
758 Artifact artifact,
759 double from,
760 double to,
761 double step)
762 throws IllegalArgumentException
763 {
764 logger.debug("WQSelect.validateFreeW");
765
766 double[] minmaxW = determineMinMaxWFree(artifact);
767
768 return validateBounds(minmaxW[0], minmaxW[1], from, to, step);
769 }
770
672 } 771 }
673 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : 772 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org