comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/WQSelect.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/artifacts/states/WQSelect.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.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 org.dive4elements.artifacts.Artifact;
12 import org.dive4elements.artifacts.CallContext;
13
14 import org.dive4elements.artifacts.common.utils.XMLUtils;
15 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
16
17 import org.dive4elements.artifactdatabase.ProtocolUtils;
18 import org.dive4elements.artifactdatabase.data.StateData;
19
20 import org.dive4elements.river.model.Gauge;
21 import org.dive4elements.river.model.River;
22 import org.dive4elements.river.model.Wst;
23
24 import org.dive4elements.river.artifacts.FLYSArtifact;
25 import org.dive4elements.river.artifacts.WINFOArtifact;
26
27 import org.dive4elements.river.artifacts.model.WstFactory;
28 import org.dive4elements.river.artifacts.model.WstValueTable;
29 import org.dive4elements.river.artifacts.resources.Resources;
30
31 import org.dive4elements.river.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 // XXX: DEAD CODE
221 /*
222 Element choices = ProtocolUtils.createArtNode(
223 cr, "choices", null, null);
224 */
225
226 label.setTextContent(Resources.getMsg(
227 context.getMeta(),
228 data.getName(),
229 data.getName()));
230
231 select.appendChild(label);
232
233 return select;
234 }
235
236
237 @Override
238 protected Element[] createItems(
239 XMLUtils.ElementCreator cr,
240 Artifact artifact,
241 String name,
242 CallContext context)
243 {
244 double[] minmaxW = determineMinMaxW(artifact);
245 double[] minmaxWFree = determineMinMaxWFree(artifact);
246 double[] minmaxQ = determineMinMaxQAtGauge(artifact);
247 double[] minmaxQFree = determineMinMaxQ(artifact);
248
249 if (name.equals("wq_from")) {
250 Element minW = createItem(cr, new String[] {
251 "minW",
252 String.valueOf(minmaxW[0])});
253
254 Element minQ = createItem(cr, new String[] {
255 "minQ",
256 String.valueOf(minmaxQ[0])});
257
258 Element minQFree = createItem(cr, new String[] {
259 "minQFree",
260 String.valueOf(minmaxQFree[0])});
261
262 Element minWFree = createItem(cr, new String[] {
263 "minWFree",
264 String.valueOf(minmaxWFree[0])});
265
266 return new Element[] { minW, minQ, minQFree, minWFree };
267 }
268 else if (name.equals("wq_to")) {
269 Element maxW = createItem(cr, new String[] {
270 "maxW",
271 String.valueOf(minmaxW[1])});
272
273 Element maxQ = createItem(cr, new String[] {
274 "maxQ",
275 String.valueOf(minmaxQ[1])});
276
277 Element maxQFree = createItem(cr, new String[] {
278 "maxQFree",
279 String.valueOf(minmaxQFree[1])});
280
281 Element maxWFree = createItem(cr, new String[] {
282 "maxWFree",
283 String.valueOf(minmaxWFree[1])});
284
285 return new Element[] { maxW, maxQ, maxQFree, maxWFree };
286 }
287 else {
288 Element stepW = createItem(
289 cr, new String[] {
290 "stepW",
291 String.valueOf(getStepsW(minmaxW[0], minmaxW[1]))});
292 Element stepQ = createItem(
293 cr, new String[] {
294 "stepQ",
295 String.valueOf(getStepsQ(minmaxQ[0], minmaxQ[1]))});
296 Element stepQFree = createItem(
297 cr, new String[] {
298 "stepQFree",
299 String.valueOf(getStepsQ(minmaxQFree[0], minmaxQFree[1]))});
300 Element stepWFree = createItem(
301 cr, new String[] {
302 "stepWFree",
303 String.valueOf(getStepsW(minmaxWFree[0], minmaxWFree[1]))});
304
305 return new Element[] { stepW, stepQ, stepQFree, stepWFree };
306 }
307 }
308
309
310 protected static double getStepsW(double min, double max) {
311 double diff = min < max ? max - min : min - max;
312 double step = diff / MAX_STEPS;
313
314 if (step < 10) {
315 return getSteps(step, 1);
316 }
317 else if (step < 100) {
318 return getSteps(step, 10);
319 }
320 else if (step < 1000) {
321 return getSteps(step, 100);
322 }
323 else {
324 return step;
325 }
326 }
327
328
329 protected static double getStepsQ(double min, double max) {
330 double diff = min < max ? max - min : min - max;
331 double step = diff / MAX_STEPS;
332
333 if (step < 10) {
334 return getSteps(step, 1);
335 }
336 else if (step < 100) {
337 return getSteps(step, 10);
338 }
339 else if (step < 1000) {
340 return getSteps(step, 100);
341 }
342 else {
343 return step;
344 }
345 }
346
347
348 protected static double getSteps(double steps, double factor) {
349 int fac = (int) (steps / factor);
350 double diff = steps - fac * factor;
351
352 if (diff == 0) {
353 return steps;
354 }
355
356 return factor * (fac + 1);
357 }
358
359
360 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
361 Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
362 Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
363 Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
364
365 String[] arr = (String[]) obj;
366
367 label.setTextContent(arr[0]);
368 value.setTextContent(arr[1]);
369
370 item.appendChild(label);
371 item.appendChild(value);
372
373 return item;
374 }
375
376
377 @Override
378 protected String getUIProvider() {
379 return "wq_panel";
380 }
381
382
383 /**
384 * Determines the min and max W value for the current gauge. If no min and
385 * max values could be determined, this method will return
386 * [Double.MIN_VALUE, Double.MAX_VALUE].
387 *
388 * @param artifact The FLYSArtifact.
389 *
390 * @return the min and max W values for the current gauge.
391 */
392 protected double[] determineMinMaxW(Artifact artifact) {
393 logger.debug("WQSelect.determineCurrentGauge");
394
395 Gauge gauge = ((WINFOArtifact) artifact).getGauge();
396 double[] minmaxW = gauge != null ? gauge.determineMinMaxW() : null;
397
398 double minW = minmaxW != null ? minmaxW[0] : Double.MIN_VALUE;
399 double maxW = minmaxW != null ? minmaxW[1] : Double.MAX_VALUE;
400
401 return new double[] { minW, maxW };
402 }
403
404
405 /**
406 * Determines the min and max W value. If no min and
407 * max values could be determined, this method will return
408 * [Double.MIN_VALUE, Double.MAX_VALUE].
409 *
410 * @param artifact The FLYSArtifact.
411 *
412 * @return the min and max W values.
413 */
414 protected double[] determineMinMaxWFree(Artifact artifact) {
415 logger.debug("WQSelect.determineMinMaxWFree");
416
417 WINFOArtifact winfo = (WINFOArtifact) artifact;
418 WstValueTable valueTable = winfo.getWstValueTable();
419
420 double[] minmaxW = null;
421 if(valueTable != null) {
422 double[] km = null;
423 if(winfo.isRange()) {
424 km = winfo.getFromToStep();
425 // Use the start km to determine the min max values.
426 minmaxW = valueTable.getMinMaxW(km[0]);
427 }
428 else {
429 km = winfo.getKms();
430 minmaxW = valueTable.getMinMaxW(km[0]);
431 }
432 }
433 return minmaxW != null
434 ? minmaxW
435 : new double[] { Double.MIN_VALUE, Double.MAX_VALUE };
436 }
437
438
439 /**
440 * Determines the min and max Q value for the current gauge. If no min and
441 * max values could be determined, this method will return
442 * [Double.MIN_VALUE, Double.MAX_VALUE].
443 *
444 * @param artifact The FLYSArtifact.
445 *
446 * @return the min and max Q values for the current gauge.
447 */
448 protected double[] determineMinMaxQAtGauge(Artifact artifact) {
449 logger.debug("WQSelect.determineMinMaxQAtGauge");
450
451 WINFOArtifact flysArtifact = (WINFOArtifact) artifact;
452
453 River river = FLYSUtils.getRiver(flysArtifact);
454 Gauge gauge = flysArtifact.getGauge();
455 Wst wst = WstFactory.getWst(river);
456
457 double[] minmaxQ = gauge != null
458 ? wst.determineMinMaxQ(gauge.getRange())
459 : null;
460
461 double minQ = minmaxQ != null ? minmaxQ[0] : Double.MIN_VALUE;
462 double maxQ = minmaxQ != null ? minmaxQ[1] : Double.MAX_VALUE;
463
464 return new double[] { minQ, maxQ };
465 }
466
467
468 /**
469 * Determines the min and max Q value for the current kilometer range. If no
470 * min and max values could be determined, this method will return
471 *
472 * @param artifact The FLYSArtifact.
473 *
474 * @return the min and max Q values for the current kilometer range.
475 */
476 protected double[] determineMinMaxQ(Artifact artifact) {
477 logger.debug("WQSelect.determineMinMaxQ");
478
479 WINFOArtifact winfo = (WINFOArtifact) artifact;
480 WstValueTable valueTable = winfo.getWstValueTable();
481
482 double[] minmaxQ = null;
483 if(valueTable != null) {
484 double[] km = null;
485 if(winfo.isRange()) {
486 km = winfo.getFromToStep();
487 minmaxQ = valueTable.getMinMaxQ(km[0], km[1], km[2]);
488 }
489 else {
490 km = winfo.getKms();
491 minmaxQ = valueTable.getMinMaxQ(km[0]);
492 for (int i = 1; i < km.length; i++) {
493 double[] tmp = valueTable.getMinMaxQ(km[i]);
494 if(tmp[0] < minmaxQ[0]) {
495 minmaxQ[0] = tmp[0];
496 }
497 if(tmp[1] > minmaxQ[1]) {
498 minmaxQ[1] = tmp[1];
499 }
500 }
501 }
502 }
503 return minmaxQ != null
504 ? minmaxQ
505 : new double[] { Double.MIN_VALUE, Double.MAX_VALUE };
506 }
507
508
509 @Override
510 public boolean validate(Artifact artifact)
511 throws IllegalArgumentException
512 {
513 logger.debug("WQSelect.validate");
514
515 WINFOArtifact flys = (WINFOArtifact) artifact;
516
517 StateData data = getData(flys, WQ_SELECTION);
518 boolean isRange = data != null
519 ? Boolean.valueOf((String) data.getValue())
520 : false;
521
522
523
524 if (!isRange) {
525 return validateSingle(artifact);
526 }
527 else {
528 return validateRange(artifact);
529 }
530 }
531
532
533 protected boolean validateBounds(
534 double fromValid, double toValid,
535 double from, double to, double step)
536 throws IllegalArgumentException
537 {
538 logger.debug("RangeState.validateRange");
539
540 if (from < fromValid) {
541 logger.error(
542 "Invalid 'from'. " + from + " is smaller than " + fromValid);
543 throw new IllegalArgumentException("error_feed_from_out_of_range");
544 }
545 else if (to > toValid) {
546 logger.error(
547 "Invalid 'to'. " + to + " is bigger than " + toValid);
548 throw new IllegalArgumentException("error_feed_to_out_of_range");
549 }
550
551 return true;
552 }
553
554
555 protected boolean validateSingle(Artifact artifact)
556 throws IllegalArgumentException
557 {
558 logger.debug("WQSelect.validateSingle");
559
560 WINFOArtifact flys = (WINFOArtifact) artifact;
561 StateData data = getData(flys, WQ_SINGLE);
562
563 String tmp = data != null ? (String) data.getValue() : null;
564
565 if (tmp == null || tmp.length() == 0) {
566 throw new IllegalArgumentException("error_empty_state");
567 }
568
569 String[] strValues = tmp.split(" ");
570 TDoubleArrayList all = new TDoubleArrayList();
571
572 for (String strValue: strValues) {
573 try {
574 all.add(Double.parseDouble(strValue));
575 }
576 catch (NumberFormatException nfe) {
577 logger.warn(nfe, nfe);
578 }
579 }
580
581 all.sort();
582
583 FLYSUtils.WQ_MODE mode = FLYSUtils.getWQMode(flys);
584
585 logger.debug("WQ Mode: " + mode);
586
587 double[] minmax = null;
588
589 if (mode == FLYSUtils.WQ_MODE.WGAUGE) {
590 minmax = determineMinMaxW(artifact);
591 }
592 else if (mode == FLYSUtils.WQ_MODE.QGAUGE) {
593 minmax = determineMinMaxQAtGauge(artifact);
594 }
595 else if (mode == FLYSUtils.WQ_MODE.QFREE) {
596 minmax = determineMinMaxQ(artifact);
597 }
598 else {
599 minmax = determineMinMaxWFree(artifact);
600 }
601
602 double min = all.get(0);
603 double max = all.get(all.size()-1);
604
605 logger.debug("Inserted min value = " + min);
606 logger.debug("Inserted max value = " + max);
607
608 return validateBounds(minmax[0], minmax[1], min, max, 0d);
609 }
610
611
612 protected boolean validateRange(Artifact artifact)
613 throws IllegalArgumentException
614 {
615 logger.debug("WQSelect.validateRange");
616
617 WINFOArtifact flys = (WINFOArtifact) artifact;
618 FLYSUtils.WQ_MODE mode = FLYSUtils.getWQMode(flys);
619
620 if (mode == null) {
621 throw new IllegalArgumentException("error_feed_invalid_wq_mode");
622 }
623
624 StateData dFrom = flys.getData(WQ_FROM);
625 StateData dTo = flys.getData(WQ_TO);
626 StateData dStep = flys.getData(WQ_STEP);
627
628 String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
629 String toStr = dTo != null ? (String) dTo.getValue() : null;
630 String stepStr = dStep != null ? (String) dStep.getValue() : null;
631
632 if (fromStr == null || toStr == null || stepStr == null) {
633 throw new IllegalArgumentException("error_empty_state");
634 }
635
636 try {
637 double from = Double.parseDouble(fromStr);
638 double to = Double.parseDouble(toStr);
639 double step = Double.parseDouble(stepStr);
640
641 if (mode == FLYSUtils.WQ_MODE.WGAUGE) {
642 return validateGaugeW(artifact, from, to, step);
643 }
644 else if (mode == FLYSUtils.WQ_MODE.QGAUGE) {
645 return validateGaugeQ(artifact, from, to, step);
646 }
647 else if (mode == FLYSUtils.WQ_MODE.QFREE) {
648 return validateFreeQ(artifact, from, to, step);
649 }
650 else if (mode == FLYSUtils.WQ_MODE.WFREE) {
651 return validateFreeW(artifact, from, to, step);
652 }
653 else {
654 throw new IllegalArgumentException(
655 "error_feed_invalid_wq_mode");
656 }
657 }
658 catch (NumberFormatException nfe) {
659 throw new IllegalArgumentException("error_feed_number_format");
660 }
661 }
662
663
664 /**
665 * Validates the inserted W values.
666 *
667 * @param artifact The owner artifact.
668 * @param from The lower value of the W range.
669 * @param to The upper value of the W range.
670 * @param step The step width.
671 *
672 * @return true, if everything was fine, otherwise an exception is thrown.
673 */
674 protected boolean validateGaugeW(
675 Artifact artifact,
676 double from,
677 double to,
678 double step)
679 throws IllegalArgumentException
680 {
681 logger.debug("WQSelect.validateGaugeW");
682
683 double[] minmaxW = determineMinMaxW(artifact);
684
685 return validateBounds(minmaxW[0], minmaxW[1], from, to, step);
686 }
687
688
689 /**
690 * Validates the inserted Q values based on the Q range for the current
691 * gauge.
692 *
693 * @param artifact The owner artifact.
694 * @param from The lower value of the Q range.
695 * @param to The upper value of the Q range.
696 * @param step The step width.
697 *
698 * @return true, if everything was fine, otherwise an exception is thrown.
699 */
700 protected boolean validateGaugeQ(
701 Artifact artifact,
702 double from,
703 double to,
704 double step)
705 throws IllegalArgumentException
706 {
707 logger.debug("WQSelect.validateGaugeQ");
708
709 double[] minmaxQ = determineMinMaxQAtGauge(artifact);
710
711 return validateBounds(minmaxQ[0], minmaxQ[1], from, to, step);
712 }
713
714
715 /**
716 * Validates the inserted Q values based on the Q range for the current
717 * kilometer range.
718 *
719 * @param artifact The owner artifact.
720 * @param from The lower value of the Q range.
721 * @param to The upper value of the Q range.
722 * @param step The step width.
723 *
724 * @return true, if everything was fine, otherwise an exception is thrown.
725 */
726 protected boolean validateFreeQ(
727 Artifact artifact,
728 double from,
729 double to,
730 double step)
731 throws IllegalArgumentException
732 {
733 logger.debug("WQSelect.validateFreeQ");
734
735 double[] minmaxQ = determineMinMaxQ(artifact);
736
737 return validateBounds(minmaxQ[0], minmaxQ[1], from, to, step);
738 }
739
740
741 /**
742 * Validates the inserted W values based on the W range for the current
743 * kilometer range.
744 *
745 * @param artifact The owner artifact.
746 * @param from The lower value of the W range.
747 * @param to The upper value of the W range.
748 * @param step The step width.
749 *
750 * @return true, if everything was fine, otherwise an exception is thrown.
751 */
752 protected boolean validateFreeW(
753 Artifact artifact,
754 double from,
755 double to,
756 double step)
757 throws IllegalArgumentException
758 {
759 logger.debug("WQSelect.validateFreeW");
760
761 double[] minmaxW = determineMinMaxWFree(artifact);
762
763 return validateBounds(minmaxW[0], minmaxW[1], from, to, step);
764 }
765
766 }
767 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org