comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQSelect.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents c4591312f3d3
children 8e66293c5369
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.states;
2
3 import java.text.NumberFormat;
4
5 import gnu.trove.TDoubleArrayList;
6
7 import org.apache.log4j.Logger;
8
9 import org.w3c.dom.Element;
10
11 import de.intevation.artifacts.Artifact;
12 import de.intevation.artifacts.CallContext;
13
14 import de.intevation.artifacts.common.utils.XMLUtils;
15 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
16
17 import de.intevation.artifactdatabase.ProtocolUtils;
18 import de.intevation.artifactdatabase.data.StateData;
19
20 import de.intevation.flys.model.Gauge;
21 import de.intevation.flys.model.River;
22 import de.intevation.flys.model.Wst;
23
24 import de.intevation.flys.artifacts.FLYSArtifact;
25 import de.intevation.flys.artifacts.WINFOArtifact;
26
27 import de.intevation.flys.artifacts.model.WstFactory;
28 import de.intevation.flys.artifacts.model.WstValueTable;
29 import de.intevation.flys.artifacts.resources.Resources;
30
31 import de.intevation.flys.utils.FLYSUtils;
32
33
34 /**
35 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
36 */
37 public class WQSelect extends DefaultState {
38
39 /** The logger used in this class. */
40 private static Logger logger = Logger.getLogger(WQSelect.class);
41
42 /** The default step width for Qs. */
43 public static final String DEFAULT_STEP_Q = "50";
44
45 /** The default step width for Qs. */
46 public static final String DEFAULT_STEP_W = "30";
47
48 /** The max number of steps for Qs and Ws. */
49 public static final int MAX_STEPS = 30;
50
51 /** The name of the 'mode' field. */
52 public static final String WQ_MODE = "wq_isq";
53
54 /** Them name fo the 'free' field. */
55 public static final String WQ_FREE = "wq_isfree";
56
57 /** The name of the 'selection' field. */
58 public static final String WQ_SELECTION = "wq_isrange";
59
60 /** The name of the 'from' field. */
61 public static final String WQ_FROM = "wq_from";
62
63 /** The name of the 'to' field. */
64 public static final String WQ_TO = "wq_to";
65
66 /** The name of the 'step' field. */
67 public static final String WQ_STEP = "wq_step";
68
69 /** The name of the 'single' field. */
70 public static final String WQ_SINGLE = "wq_single";
71
72
73 /**
74 * The default constructor that initializes an empty State object.
75 */
76 public WQSelect() {
77 }
78
79
80 @Override
81 protected Element createStaticData(
82 FLYSArtifact flys,
83 ElementCreator creator,
84 CallContext cc,
85 String name,
86 String value,
87 String type
88 ) {
89 if (!name.equals(WQ_SINGLE)) {
90 return super.createStaticData(flys, creator, cc, name, value, type);
91 }
92
93 Boolean isQ = flys.getDataAsBoolean(WQ_MODE);
94 Boolean isFree = flys.getDataAsBoolean(WQ_FREE);
95
96 WINFOArtifact winfo = (WINFOArtifact) flys;
97
98 Element dataElement = creator.create("data");
99 creator.addAttr(dataElement, "name", name, true);
100 creator.addAttr(dataElement, "type", type, true);
101
102 Element itemElement = creator.create("item");
103 creator.addAttr(itemElement, "value", value, true);
104
105 String label;
106
107 if (!isQ || isFree) {
108 label = getLabel(winfo, cc, value);
109 }
110 else {
111 label = getSpecialLabel(winfo, cc, value);
112 }
113
114 creator.addAttr(itemElement, "label", label, true);
115
116 dataElement.appendChild(itemElement);
117
118 return dataElement;
119 }
120
121
122 protected static String getLabel(
123 WINFOArtifact winfo,
124 CallContext cc,
125 String raw
126 ) {
127 String[] values = raw.split(" ");
128
129 if (values.length < 1) {
130 return null;
131 }
132
133 StringBuilder label = new StringBuilder();
134
135 NumberFormat nf = NumberFormat.getInstance(
136 Resources.getLocale(cc.getMeta()));
137
138 for (String value: values) {
139 try {
140 double v = Double.parseDouble(value.trim());
141
142 String formatted = nf.format(v);
143
144 if (label.length() > 0) {
145 label.append(';');
146 }
147 label.append(formatted);
148 }
149 catch (NumberFormatException nfe) {
150 // do nothing here
151 }
152 }
153
154 return label.toString();
155 }
156
157
158 protected static String getSpecialLabel(
159 WINFOArtifact winfo,
160 CallContext cc,
161 String raw
162 ) {
163 String[] values = raw.split(" ");
164
165 if (values.length < 1) {
166 return null;
167 }
168
169 NumberFormat nf = NumberFormat.getInstance(
170 Resources.getLocale(cc.getMeta()));
171
172 Gauge gauge = winfo.getGauge();
173
174 boolean debug = logger.isDebugEnabled();
175
176 StringBuilder label = new StringBuilder();
177
178 for (String value: values) {
179 try {
180 double v = Double.parseDouble(value.trim());
181
182 String tmp = nf.format(v);
183 String mv = FLYSUtils.getNamedMainValue(gauge, v);
184
185 if (mv != null && mv.length() > 0) {
186 tmp = mv + ": " + tmp;
187 if (debug) {
188 logger.debug("Add main value: '" + mv + "'");
189 }
190 }
191 if (label.length() > 0) {
192 label.append(';');
193 }
194 label.append(tmp);
195 }
196 catch (NumberFormatException nfe) {
197 // do nothing here
198 }
199 }
200
201 return label.toString();
202 }
203
204
205 @Override
206 protected Element createData(
207 XMLUtils.ElementCreator cr,
208 Artifact artifact,
209 StateData data,
210 CallContext context)
211 {
212 Element select = ProtocolUtils.createArtNode(
213 cr, "select", null, null);
214
215 cr.addAttr(select, "name", data.getName(), true);
216
217 Element label = ProtocolUtils.createArtNode(
218 cr, "label", null, null);
219
220 Element choices = ProtocolUtils.createArtNode(
221 cr, "choices", null, null);
222
223 label.setTextContent(Resources.getMsg(
224 context.getMeta(),
225 data.getName(),
226 data.getName()));
227
228 select.appendChild(label);
229
230 return select;
231 }
232
233
234 @Override
235 protected Element[] createItems(
236 XMLUtils.ElementCreator cr,
237 Artifact artifact,
238 String name,
239 CallContext context)
240 {
241 double[] minmaxW = determineMinMaxW(artifact);
242 double[] minmaxWFree = determineMinMaxWFree(artifact);
243 double[] minmaxQ = determineMinMaxQAtGauge(artifact);
244 double[] minmaxQFree = determineMinMaxQ(artifact);
245
246 if (name.equals("wq_from")) {
247 Element minW = createItem(cr, new String[] {
248 "minW",
249 String.valueOf(minmaxW[0])});
250
251 Element minQ = createItem(cr, new String[] {
252 "minQ",
253 String.valueOf(minmaxQ[0])});
254
255 Element minQFree = createItem(cr, new String[] {
256 "minQFree",
257 String.valueOf(minmaxQFree[0])});
258
259 Element minWFree = createItem(cr, new String[] {
260 "minWFree",
261 String.valueOf(minmaxWFree[0])});
262
263 return new Element[] { minW, minQ, minQFree, minWFree };
264 }
265 else if (name.equals("wq_to")) {
266 Element maxW = createItem(cr, new String[] {
267 "maxW",
268 String.valueOf(minmaxW[1])});
269
270 Element maxQ = createItem(cr, new String[] {
271 "maxQ",
272 String.valueOf(minmaxQ[1])});
273
274 Element maxQFree = createItem(cr, new String[] {
275 "maxQFree",
276 String.valueOf(minmaxQFree[1])});
277
278 Element maxWFree = createItem(cr, new String[] {
279 "maxWFree",
280 String.valueOf(minmaxWFree[1])});
281
282 return new Element[] { maxW, maxQ, maxQFree, maxWFree };
283 }
284 else {
285 Element stepW = createItem(
286 cr, new String[] {
287 "stepW",
288 String.valueOf(getStepsW(minmaxW[0], minmaxW[1]))});
289 Element stepQ = createItem(
290 cr, new String[] {
291 "stepQ",
292 String.valueOf(getStepsQ(minmaxQ[0], minmaxQ[1]))});
293 Element stepQFree = createItem(
294 cr, new String[] {
295 "stepQFree",
296 String.valueOf(getStepsQ(minmaxQFree[0], minmaxQFree[1]))});
297 Element stepWFree = createItem(
298 cr, new String[] {
299 "stepWFree",
300 String.valueOf(getStepsW(minmaxWFree[0], minmaxWFree[1]))});
301
302 return new Element[] { stepW, stepQ, stepQFree, stepWFree };
303 }
304 }
305
306
307 protected static double getStepsW(double min, double max) {
308 double diff = min < max ? max - min : min - max;
309 double step = diff / MAX_STEPS;
310
311 if (step < 10) {
312 return getSteps(step, 1);
313 }
314 else if (step < 100) {
315 return getSteps(step, 10);
316 }
317 else if (step < 1000) {
318 return getSteps(step, 100);
319 }
320 else {
321 return step;
322 }
323 }
324
325
326 protected static double getStepsQ(double min, double max) {
327 double diff = min < max ? max - min : min - max;
328 double step = diff / MAX_STEPS;
329
330 if (step < 10) {
331 return getSteps(step, 1);
332 }
333 else if (step < 100) {
334 return getSteps(step, 10);
335 }
336 else if (step < 1000) {
337 return getSteps(step, 100);
338 }
339 else {
340 return step;
341 }
342 }
343
344
345 protected static double getSteps(double steps, double factor) {
346 int fac = (int) (steps / factor);
347 double diff = steps - fac * factor;
348
349 if (diff == 0) {
350 return steps;
351 }
352
353 return factor * (fac + 1);
354 }
355
356
357 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
358 Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
359 Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
360 Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
361
362 String[] arr = (String[]) obj;
363
364 label.setTextContent(arr[0]);
365 value.setTextContent(arr[1]);
366
367 item.appendChild(label);
368 item.appendChild(value);
369
370 return item;
371 }
372
373
374 @Override
375 protected String getUIProvider() {
376 return "wq_panel";
377 }
378
379
380 /**
381 * Determines the min and max W value for the current gauge. If no min and
382 * max values could be determined, this method will return
383 * [Double.MIN_VALUE, Double.MAX_VALUE].
384 *
385 * @param artifact The FLYSArtifact.
386 *
387 * @return the min and max W values for the current gauge.
388 */
389 protected double[] determineMinMaxW(Artifact artifact) {
390 logger.debug("WQSelect.determineCurrentGauge");
391
392 Gauge gauge = ((WINFOArtifact) artifact).getGauge();
393 double[] minmaxW = gauge != null ? gauge.determineMinMaxW() : null;
394
395 double minW = minmaxW != null ? minmaxW[0] : Double.MIN_VALUE;
396 double maxW = minmaxW != null ? minmaxW[1] : Double.MAX_VALUE;
397
398 return new double[] { minW, maxW };
399 }
400
401
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 // Use the start km to determine the min max values.
423 minmaxW = valueTable.getMinMaxW(km[0]);
424 }
425 else {
426 km = winfo.getKms();
427 minmaxW = valueTable.getMinMaxW(km[0]);
428 }
429 }
430 return minmaxW != null
431 ? minmaxW
432 : new double[] { Double.MIN_VALUE, Double.MAX_VALUE };
433 }
434
435
436 /**
437 * Determines the min and max Q value for the current gauge. If no min and
438 * max values could be determined, this method will return
439 * [Double.MIN_VALUE, Double.MAX_VALUE].
440 *
441 * @param artifact The FLYSArtifact.
442 *
443 * @return the min and max Q values for the current gauge.
444 */
445 protected double[] determineMinMaxQAtGauge(Artifact artifact) {
446 logger.debug("WQSelect.determineMinMaxQAtGauge");
447
448 WINFOArtifact flysArtifact = (WINFOArtifact) artifact;
449
450 River river = FLYSUtils.getRiver(flysArtifact);
451 Gauge gauge = flysArtifact.getGauge();
452 Wst wst = WstFactory.getWst(river);
453
454 double[] minmaxQ = gauge != null
455 ? wst.determineMinMaxQ(gauge.getRange())
456 : null;
457
458 double minQ = minmaxQ != null ? minmaxQ[0] : Double.MIN_VALUE;
459 double maxQ = minmaxQ != null ? minmaxQ[1] : Double.MAX_VALUE;
460
461 return new double[] { minQ, maxQ };
462 }
463
464
465 /**
466 * Determines the min and max Q value for the current kilometer range. If no
467 * min and max values could be determined, this method will return
468 *
469 * @param artifact The FLYSArtifact.
470 *
471 * @return the min and max Q values for the current kilometer range.
472 */
473 protected double[] determineMinMaxQ(Artifact artifact) {
474 logger.debug("WQSelect.determineMinMaxQ");
475
476 WINFOArtifact winfo = (WINFOArtifact) artifact;
477 WstValueTable valueTable = winfo.getWstValueTable();
478
479 double[] minmaxQ = null;
480 if(valueTable != null) {
481 double[] km = null;
482 if(winfo.isRange()) {
483 km = winfo.getFromToStep();
484 minmaxQ = valueTable.getMinMaxQ(km[0], km[1], km[2]);
485 }
486 else {
487 km = winfo.getKms();
488 minmaxQ = valueTable.getMinMaxQ(km[0]);
489 for (int i = 1; i < km.length; i++) {
490 double[] tmp = valueTable.getMinMaxQ(km[i]);
491 if(tmp[0] < minmaxQ[0]) {
492 minmaxQ[0] = tmp[0];
493 }
494 if(tmp[1] > minmaxQ[1]) {
495 minmaxQ[1] = tmp[1];
496 }
497 }
498 }
499 }
500 return minmaxQ != null
501 ? minmaxQ
502 : new double[] { Double.MIN_VALUE, Double.MAX_VALUE };
503 }
504
505
506 @Override
507 public boolean validate(Artifact artifact)
508 throws IllegalArgumentException
509 {
510 logger.debug("WQSelect.validate");
511
512 WINFOArtifact flys = (WINFOArtifact) artifact;
513
514 StateData data = getData(flys, WQ_SELECTION);
515 boolean isRange = data != null
516 ? Boolean.valueOf((String) data.getValue())
517 : false;
518
519
520
521 if (!isRange) {
522 return validateSingle(artifact);
523 }
524 else {
525 return validateRange(artifact);
526 }
527 }
528
529
530 protected boolean validateBounds(
531 double fromValid, double toValid,
532 double from, double to, double step)
533 throws IllegalArgumentException
534 {
535 logger.debug("RangeState.validateRange");
536
537 if (from < fromValid) {
538 logger.error(
539 "Invalid 'from'. " + from + " is smaller than " + fromValid);
540 throw new IllegalArgumentException("error_feed_from_out_of_range");
541 }
542 else if (to > toValid) {
543 logger.error(
544 "Invalid 'to'. " + to + " is bigger than " + toValid);
545 throw new IllegalArgumentException("error_feed_to_out_of_range");
546 }
547
548 return true;
549 }
550
551
552 protected boolean validateSingle(Artifact artifact)
553 throws IllegalArgumentException
554 {
555 logger.debug("WQSelect.validateSingle");
556
557 WINFOArtifact flys = (WINFOArtifact) artifact;
558 StateData data = getData(flys, WQ_SINGLE);
559
560 String tmp = data != null ? (String) data.getValue() : null;
561
562 if (tmp == null || tmp.length() == 0) {
563 throw new IllegalArgumentException("error_empty_state");
564 }
565
566 String[] strValues = tmp.split(" ");
567 TDoubleArrayList all = new TDoubleArrayList();
568
569 for (String strValue: strValues) {
570 try {
571 all.add(Double.parseDouble(strValue));
572 }
573 catch (NumberFormatException nfe) {
574 logger.warn(nfe, nfe);
575 }
576 }
577
578 all.sort();
579
580 FLYSUtils.WQ_MODE mode = FLYSUtils.getWQMode(flys);
581
582 logger.debug("WQ Mode: " + mode);
583
584 double[] minmax = null;
585
586 if (mode == FLYSUtils.WQ_MODE.WGAUGE) {
587 minmax = determineMinMaxW(artifact);
588 }
589 else if (mode == FLYSUtils.WQ_MODE.QGAUGE) {
590 minmax = determineMinMaxQAtGauge(artifact);
591 }
592 else if (mode == FLYSUtils.WQ_MODE.QFREE) {
593 minmax = determineMinMaxQ(artifact);
594 }
595 else {
596 minmax = determineMinMaxWFree(artifact);
597 }
598
599 double min = all.get(0);
600 double max = all.get(all.size()-1);
601
602 logger.debug("Inserted min value = " + min);
603 logger.debug("Inserted max value = " + max);
604
605 return validateBounds(minmax[0], minmax[1], min, max, 0d);
606 }
607
608
609 protected boolean validateRange(Artifact artifact)
610 throws IllegalArgumentException
611 {
612 logger.debug("WQSelect.validateRange");
613
614 WINFOArtifact flys = (WINFOArtifact) artifact;
615 FLYSUtils.WQ_MODE mode = FLYSUtils.getWQMode(flys);
616
617 if (mode == null) {
618 throw new IllegalArgumentException("error_feed_invalid_wq_mode");
619 }
620
621 StateData dFrom = flys.getData(WQ_FROM);
622 StateData dTo = flys.getData(WQ_TO);
623 StateData dStep = flys.getData(WQ_STEP);
624
625 String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
626 String toStr = dTo != null ? (String) dTo.getValue() : null;
627 String stepStr = dStep != null ? (String) dStep.getValue() : null;
628
629 if (fromStr == null || toStr == null || stepStr == null) {
630 throw new IllegalArgumentException("error_empty_state");
631 }
632
633 try {
634 double from = Double.parseDouble(fromStr);
635 double to = Double.parseDouble(toStr);
636 double step = Double.parseDouble(stepStr);
637
638 if (mode == FLYSUtils.WQ_MODE.WGAUGE) {
639 return validateGaugeW(artifact, from, to, step);
640 }
641 else if (mode == FLYSUtils.WQ_MODE.QGAUGE) {
642 return validateGaugeQ(artifact, from, to, step);
643 }
644 else if (mode == FLYSUtils.WQ_MODE.QFREE) {
645 return validateFreeQ(artifact, from, to, step);
646 }
647 else if (mode == FLYSUtils.WQ_MODE.WFREE) {
648 return validateFreeW(artifact, from, to, step);
649 }
650 else {
651 throw new IllegalArgumentException(
652 "error_feed_invalid_wq_mode");
653 }
654 }
655 catch (NumberFormatException nfe) {
656 throw new IllegalArgumentException("error_feed_number_format");
657 }
658 }
659
660
661 /**
662 * Validates the inserted W values.
663 *
664 * @param artifact The owner artifact.
665 * @param from The lower value of the W range.
666 * @param to The upper value of the W range.
667 * @param step The step width.
668 *
669 * @return true, if everything was fine, otherwise an exception is thrown.
670 */
671 protected boolean validateGaugeW(
672 Artifact artifact,
673 double from,
674 double to,
675 double step)
676 throws IllegalArgumentException
677 {
678 logger.debug("WQSelect.validateGaugeW");
679
680 double[] minmaxW = determineMinMaxW(artifact);
681
682 return validateBounds(minmaxW[0], minmaxW[1], from, to, step);
683 }
684
685
686 /**
687 * Validates the inserted Q values based on the Q range for the current
688 * gauge.
689 *
690 * @param artifact The owner artifact.
691 * @param from The lower value of the Q range.
692 * @param to The upper value of the Q range.
693 * @param step The step width.
694 *
695 * @return true, if everything was fine, otherwise an exception is thrown.
696 */
697 protected boolean validateGaugeQ(
698 Artifact artifact,
699 double from,
700 double to,
701 double step)
702 throws IllegalArgumentException
703 {
704 logger.debug("WQSelect.validateGaugeQ");
705
706 double[] minmaxQ = determineMinMaxQAtGauge(artifact);
707
708 return validateBounds(minmaxQ[0], minmaxQ[1], from, to, step);
709 }
710
711
712 /**
713 * Validates the inserted Q values based on the Q range for the current
714 * kilometer range.
715 *
716 * @param artifact The owner artifact.
717 * @param from The lower value of the Q range.
718 * @param to The upper value of the Q range.
719 * @param step The step width.
720 *
721 * @return true, if everything was fine, otherwise an exception is thrown.
722 */
723 protected boolean validateFreeQ(
724 Artifact artifact,
725 double from,
726 double to,
727 double step)
728 throws IllegalArgumentException
729 {
730 logger.debug("WQSelect.validateFreeQ");
731
732 double[] minmaxQ = determineMinMaxQ(artifact);
733
734 return validateBounds(minmaxQ[0], minmaxQ[1], from, to, step);
735 }
736
737
738 /**
739 * Validates the inserted W values based on the W range for the current
740 * kilometer range.
741 *
742 * @param artifact The owner artifact.
743 * @param from The lower value of the W range.
744 * @param to The upper value of the W range.
745 * @param step The step width.
746 *
747 * @return true, if everything was fine, otherwise an exception is thrown.
748 */
749 protected boolean validateFreeW(
750 Artifact artifact,
751 double from,
752 double to,
753 double step)
754 throws IllegalArgumentException
755 {
756 logger.debug("WQSelect.validateFreeW");
757
758 double[] minmaxW = determineMinMaxWFree(artifact);
759
760 return validateBounds(minmaxW[0], minmaxW[1], from, to, step);
761 }
762
763 }
764 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org