comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQSelect.java @ 430:7ab81ff32111 2.3

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

http://dive4elements.wald.intevation.org