comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQSelect.java @ 1190:f514894ec2fd

merged flys-artifacts/2.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:17 +0200
parents e0243627ba62
children 17648043429f
comparison
equal deleted inserted replaced
917:b48c36076e17 1190:f514894ec2fd
1 package de.intevation.flys.artifacts.states;
2
3 import gnu.trove.TDoubleArrayList;
4
5 import org.apache.log4j.Logger;
6
7 import org.w3c.dom.Element;
8
9 import de.intevation.artifacts.Artifact;
10 import de.intevation.artifacts.CallContext;
11
12 import de.intevation.artifacts.common.utils.XMLUtils;
13
14 import de.intevation.artifactdatabase.ProtocolUtils;
15 import de.intevation.artifactdatabase.data.StateData;
16
17 import de.intevation.flys.model.Gauge;
18 import de.intevation.flys.model.River;
19 import de.intevation.flys.model.Wst;
20
21 import de.intevation.flys.artifacts.WINFOArtifact;
22
23 import de.intevation.flys.artifacts.model.WstFactory;
24 import de.intevation.flys.artifacts.resources.Resources;
25
26 import de.intevation.flys.utils.FLYSUtils;
27
28
29 /**
30 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
31 */
32 public class WQSelect extends DefaultState {
33
34 /** The logger used in this class.*/
35 private static Logger logger = Logger.getLogger(WQSelect.class);
36
37
38 /** The default step width for Qs.*/
39 public static final String DEFAULT_STEP_Q = "50";
40
41 /** The default step width for Qs.*/
42 public static final String DEFAULT_STEP_W = "30";
43
44 /** The name of the 'mode' field. */
45 public static final String WQ_MODE = "wq_mode";
46
47 /** The name of the 'selection' field.*/
48 public static final String WQ_SELECTION = "wq_selection";
49
50 /** The name of the 'from' field. */
51 public static final String WQ_FROM = "wq_from";
52
53 /** The name of the 'to' field. */
54 public static final String WQ_TO = "wq_to";
55
56 /** The name of the 'step' field. */
57 public static final String WQ_STEP = "wq_step";
58
59 /** The name of the 'single' field. */
60 public static final String WQ_SINGLE = "wq_single";
61
62
63 /**
64 * The default constructor that initializes an empty State object.
65 */
66 public WQSelect() {
67 }
68
69 protected Element createData(
70 XMLUtils.ElementCreator cr,
71 Artifact artifact,
72 StateData data,
73 CallContext context)
74 {
75 Element select = ProtocolUtils.createArtNode(
76 cr, "select", null, null);
77
78 cr.addAttr(select, "name", data.getName(), true);
79
80 Element label = ProtocolUtils.createArtNode(
81 cr, "label", null, null);
82
83 Element choices = ProtocolUtils.createArtNode(
84 cr, "choices", null, null);
85
86 label.setTextContent(Resources.getMsg(
87 context.getMeta(),
88 data.getName(),
89 data.getName()));
90
91 select.appendChild(label);
92
93 return select;
94 }
95
96
97 @Override
98 protected Element[] createItems(
99 XMLUtils.ElementCreator cr,
100 Artifact artifact,
101 String name,
102 CallContext context)
103 {
104 // TODO Insert correct min/max values!
105 double[] minmaxW = determineMinMaxW(artifact);
106 double[] minmaxQ = determineMinMaxQ(artifact);
107
108 if (name.equals("wq_from")) {
109 Element minW = createItem(
110 cr, new String[] {"minW", new Double(minmaxW[0]).toString()});
111 Element minQ = createItem(
112 cr, new String[] {"minQ", new Double(minmaxQ[0]).toString()});
113 return new Element[] { minW, minQ };
114 }
115 else if (name.equals("wq_to")) {
116 Element maxW = createItem(
117 cr, new String[] {"maxW", new Double(minmaxW[1]).toString()});
118 Element maxQ = createItem(
119 cr, new String[] {"maxQ", new Double(minmaxQ[1]).toString()});
120 return new Element[] { maxW, maxQ };
121 }
122 else {
123 Element stepW = createItem(
124 cr, new String[] {"stepW", DEFAULT_STEP_W});
125 Element stepQ = createItem(
126 cr, new String[] {"stepQ", DEFAULT_STEP_Q});
127 return new Element[] { stepW, stepQ };
128 }
129 }
130
131
132 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
133 Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
134 Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
135 Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
136
137 String[] arr = (String[]) obj;
138
139 label.setTextContent(arr[0]);
140 value.setTextContent(arr[1]);
141
142 item.appendChild(label);
143 item.appendChild(value);
144
145 return item;
146 }
147
148
149 @Override
150 protected String getUIProvider() {
151 return "wq_panel";
152 }
153
154
155 /**
156 * Determines the min and max W value for the current gauge. If no min and
157 * max values could be determined, this method will return
158 * [Double.MIN_VALUE, Double.MAX_VALUE].
159 *
160 * @param artifact The FLYSArtifact.
161 *
162 * @return the min and max W values for the current gauge.
163 */
164 protected double[] determineMinMaxW(Artifact artifact) {
165 logger.debug("WQSelect.determineCurrentGauge");
166
167 Gauge gauge = ((WINFOArtifact) artifact).getGauge();
168 double[] minmaxW = gauge != null ? gauge.determineMinMaxW() : null;
169
170 double minW = minmaxW != null ? minmaxW[0] : Double.MIN_VALUE;
171 double maxW = minmaxW != null ? minmaxW[1] : Double.MAX_VALUE;
172
173 return new double[] { minW, maxW };
174 }
175
176
177 /**
178 * Determines the min and max Q value for the current gauge. If no min and
179 * max values could be determined, this method will return
180 * [Double.MIN_VALUE, Double.MAX_VALUE].
181 *
182 * @param artifact The FLYSArtifact.
183 *
184 * @return the min and max Q values for the current gauge.
185 */
186 protected double[] determineMinMaxQ(Artifact artifact) {
187 logger.debug("WQSelect.determineMinMaxQ");
188
189 WINFOArtifact flysArtifact = (WINFOArtifact) artifact;
190
191 River river = FLYSUtils.getRiver(flysArtifact);
192 Gauge gauge = flysArtifact.getGauge();
193 Wst wst = WstFactory.getWst(river);
194
195 double[] minmaxQ = gauge != null
196 ? wst.determineMinMaxQ(gauge.getRange())
197 : null;
198
199 double minQ = minmaxQ != null ? minmaxQ[0] : Double.MIN_VALUE;
200 double maxQ = minmaxQ != null ? minmaxQ[1] : Double.MAX_VALUE;
201
202 return new double[] { minQ, maxQ };
203 }
204
205
206 @Override
207 public boolean validate(Artifact artifact)
208 throws IllegalArgumentException
209 {
210 logger.debug("WQSelect.validate");
211
212 WINFOArtifact flys = (WINFOArtifact) artifact;
213
214 StateData data = getData(flys, WQ_SELECTION);
215 String selectionMode = data != null ? (String) data.getValue() : null;
216
217 if (selectionMode == null || selectionMode.equals("single")) {
218 return validateSingle(artifact);
219 }
220 else {
221 return validateRange(artifact);
222 }
223 }
224
225
226 protected boolean validateBounds(
227 double fromValid, double toValid,
228 double from, double to, double step)
229 throws IllegalArgumentException
230 {
231 logger.debug("RangeState.validateRange");
232
233 if (from < fromValid) {
234 logger.error(
235 "Invalid 'from'. " + from + " is smaller than " + fromValid);
236 throw new IllegalArgumentException("error_feed_from_out_of_range");
237 }
238 else if (to > toValid) {
239 logger.error(
240 "Invalid 'to'. " + to + " is bigger than " + toValid);
241 throw new IllegalArgumentException("error_feed_to_out_of_range");
242 }
243
244 return true;
245 }
246
247
248 protected boolean validateSingle(Artifact artifact)
249 throws IllegalArgumentException
250 {
251 logger.debug("WQSelect.validateSingle");
252
253 WINFOArtifact flys = (WINFOArtifact) artifact;
254 StateData data = getData(flys, WQ_SINGLE);
255
256 String tmp = data != null ? (String) data.getValue() : null;
257
258 if (tmp == null || tmp.length() == 0) {
259 throw new IllegalArgumentException("error_empty_state");
260 }
261
262 String[] strValues = tmp.split(" ");
263 TDoubleArrayList all = new TDoubleArrayList();
264
265 for (String strValue: strValues) {
266 try {
267 all.add(Double.parseDouble(strValue));
268 }
269 catch (NumberFormatException nfe) {
270 logger.warn(nfe, nfe);
271 }
272 }
273
274 all.sort();
275
276 StateData dMode = getData(flys, WQ_MODE);
277 String mode = dMode != null ? (String) data.getValue() : null;
278
279 logger.debug("WQ Mode: " + mode);
280
281 double[] minmax = null;
282
283 if (mode != null && mode.trim().toLowerCase().equals("w")) {
284 minmax = determineMinMaxW(artifact);
285 }
286 else {
287 minmax = determineMinMaxQ(artifact);
288 }
289
290 double min = all.get(0);
291 double max = all.get(all.size()-1);
292
293 logger.debug("Inserted min value = " + min);
294 logger.debug("Inserted max value = " + max);
295
296 return validateBounds(minmax[0], minmax[1], min, max, 0d);
297 }
298
299
300 protected boolean validateRange(Artifact artifact)
301 throws IllegalArgumentException
302 {
303 logger.debug("WQSelect.validateRange");
304 WINFOArtifact flys = (WINFOArtifact) artifact;
305
306 StateData data = flys.getData(WQ_MODE);
307 String mode = data != null ? (String) data.getValue() : null;
308 logger.debug("WQ Mode: " + mode);
309
310 if (mode == null || mode.length() == 0) {
311 throw new IllegalArgumentException("error_feed_invalid_wq_mode");
312 }
313
314 StateData dFrom = flys.getData(WQ_FROM);
315 StateData dTo = flys.getData(WQ_TO);
316 StateData dStep = flys.getData(WQ_STEP);
317
318 String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
319 String toStr = dTo != null ? (String) dTo.getValue() : null;
320 String stepStr = dStep != null ? (String) dStep.getValue() : null;
321
322 if (fromStr == null || toStr == null || stepStr == null) {
323 throw new IllegalArgumentException("error_empty_state");
324 }
325
326 try {
327 double from = Double.parseDouble(fromStr);
328 double to = Double.parseDouble(toStr);
329 double step = Double.parseDouble(stepStr);
330
331 if (mode != null && mode.trim().toLowerCase().equals("w")) {
332 return validateW(artifact, from, to, step);
333 }
334 else if (mode != null && mode.trim().toLowerCase().equals("q")) {
335 return validateQ(artifact, from, to, step);
336 }
337 else {
338 throw new IllegalArgumentException(
339 "error_feed_invalid_wq_mode");
340 }
341 }
342 catch (NumberFormatException nfe) {
343 throw new IllegalArgumentException("error_feed_number_format");
344 }
345 }
346
347
348 /**
349 * Validates the inserted W values.
350 *
351 * @param artifact The owner artifact.
352 * @param from The lower value of the W range.
353 * @param to The upper value of the W range.
354 * @param step The step width.
355 *
356 * @return true, if everything was fine, otherwise an exception is thrown.
357 */
358 protected boolean validateW(
359 Artifact artifact,
360 double from,
361 double to,
362 double step)
363 throws IllegalArgumentException
364 {
365 logger.debug("WQSelect.validateW");
366
367 double[] minmaxW = determineMinMaxW(artifact);
368
369 return validateBounds(minmaxW[0], minmaxW[1], from, to, step);
370 }
371
372
373 /**
374 * Validates the inserted Q values.
375 *
376 * @param artifact The owner artifact.
377 * @param from The lower value of the Q range.
378 * @param to The upper value of the Q range.
379 * @param step The step width.
380 *
381 * @return true, if everything was fine, otherwise an exception is thrown.
382 */
383 protected boolean validateQ(
384 Artifact artifact,
385 double from,
386 double to,
387 double step)
388 throws IllegalArgumentException
389 {
390 logger.debug("WQSelect.validateQ");
391
392 double[] minmaxQ = determineMinMaxQ(artifact);
393
394 return validateBounds(minmaxQ[0], minmaxQ[1], from, to, step);
395 }
396 }
397 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org