comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/MeasurementState.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents dec4257ad570
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.gnv.state;
10
11 import de.intevation.artifacts.common.utils.XMLUtils;
12
13 import de.intevation.artifacts.CallContext;
14 import de.intevation.artifacts.CallMeta;
15
16 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
17
18 import de.intevation.gnv.geobackend.base.Result;
19 import de.intevation.gnv.geobackend.base.ResultDescriptor;
20
21 import de.intevation.gnv.state.describedata.ExtendedKeyValueData;
22 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
23 import de.intevation.gnv.state.describedata.NamedArrayList;
24 import de.intevation.gnv.state.describedata.NamedCollection;
25
26 import de.intevation.gnv.state.exception.StateException;
27
28 import de.intevation.gnv.utils.InputValidator;
29
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.TreeMap;
37
38 import org.apache.log4j.Logger;
39
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.Node;
43
44 /**
45 * This state handles input of measurements relating to a parameter. The user
46 * interface description created by this class represents a matrix - each
47 * parameter in a single row, each measurement in a column. An invalid
48 * measurement column for a specific parameter is marked as disabled and should
49 * not be selected.
50 *
51 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
52 */
53 public class MeasurementState
54 extends DefaultState
55 {
56 private static Logger logger = Logger.getLogger(MeasurementState.class);
57
58 public static final String SQL_KEY_PARAMETERID = "PARAMETERID";
59
60 public static final String SEPARATOR = ";";
61
62
63 /**
64 * This class is used to generate the Matrix in <code>MinMaxDateState</code>.
65 * Parameter and Measurements are stored in separate lists and can be
66 * requested via different methods.
67 *
68 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
69 */
70 private class ParameterMatrix {
71 private final Logger logger =
72 Logger.getLogger(ParameterMatrix.class);
73
74 private List measurements;
75 private List mDescriptions;
76 private List parameters;
77 private boolean[][] values;
78
79 /**
80 * Constructs a new matrix.
81 *
82 * @param data A collection containing the measurements.
83 * @param parameter An array of parameters.
84 */
85 public ParameterMatrix(Collection data, String[] parameter) {
86 measurements = new ArrayList(data.size());
87 mDescriptions = new ArrayList(data.size());
88 parameters = new ArrayList(parameter.length);
89
90 values = new boolean[data.size()][parameter.length];
91 for (int i = 0; i < data.size(); i++) {
92 Arrays.fill(values[i], false);
93 }
94
95 initParameters(parameter);
96 initMeasurements(data);
97 }
98
99 /**
100 * Initialize the measurements used in this matrix.
101 *
102 * @param data The measurements.
103 */
104 private void initMeasurements(Collection data) {
105 Iterator iter = data.iterator();
106 while (iter.hasNext()) {
107 ExtendedKeyValueData value = (ExtendedKeyValueData) iter.next();
108 String key = value.getKey();
109 String val = value.getValue();
110 String parameter = value.getParameter();
111
112 int i = measurements.indexOf(key);
113 int j = parameters.indexOf(parameter);
114 int tmp = mDescriptions.indexOf(val);
115
116 if (i < 0) {
117 measurements.add(key);
118 i = measurements.indexOf(key);
119
120 mDescriptions.add(val);
121 tmp = mDescriptions.indexOf(val);
122 }
123
124 if (j < 0) {
125 logger.warn("Not a valid parameter: " + parameter);
126 }
127
128 if (i >= 0 && i < measurements.size() && j >= 0
129 && j < parameters.size())
130 {
131 values[i][j] = true;
132 }
133 }
134 }
135
136 /**
137 * Initialize the parameters used in this matrix.
138 *
139 * @param parameter Parameters.
140 */
141 private void initParameters(String[] parameter) {
142 for (String param: parameter) {
143 parameters.add(param);
144 }
145 }
146
147 /**
148 * Returns the number of measurements.
149 *
150 * @return the number of measurements.
151 */
152 public int measurementSize() {
153 if (measurements != null)
154 return measurements.size();
155
156 return 0;
157 }
158
159 /**
160 * Returns the number of parameters.
161 *
162 * @return number of parameters.
163 */
164 public int parameterSize() {
165 if (parameters != null)
166 return parameters.size();
167
168 return 0;
169 }
170
171 /**
172 * Returns the measurement at idx.
173 *
174 * @param idx Index.
175 * @return the measurement.
176 */
177 public String getMeasurement(int idx) {
178 if (idx >= 0 && idx < measurements.size())
179 return (String) measurements.get(idx);
180
181 logger.warn("Index is out of bounds: " + idx);
182 return "";
183 }
184
185 /**
186 * Returns the parameter at idx.
187 *
188 * @param idx Index
189 * @return the parameter.
190 */
191 public String getParameter(int idx) {
192 if (idx >= 0 && idx < parameters.size()) {
193 return (String) parameters.get(idx);
194 }
195
196 logger.warn("Index is out of bounds: " + idx);
197 return "";
198 }
199
200 /**
201 * Returns a description text for a specific measurement.
202 *
203 * @param idx Index of a measurement.
204 * @return measurement's description.
205 */
206 public String getMDescription(int idx) {
207 if (mDescriptions != null) {
208 return (String) mDescriptions.get(idx);
209 }
210
211 return null;
212 }
213
214 /**
215 * This method returns true, if a measurement is valid for a specific
216 * parameter - otherwise false.
217 *
218 * @param i Index of a measurement column.
219 * @param j Index of a parameter row.
220 * @return true, if valid, else false.
221 */
222 public boolean isValid(int i, int j) {
223 if (i < 0 || i > measurements.size()
224 || j < 0 || j > parameters.size())
225 {
226 logger.warn("Index out of bounds: " + i + "|" + j);
227 return false;
228 }
229
230 return values[i][j];
231 }
232 } // End of ParameterMatrix
233
234
235 public MeasurementState() {
236 super();
237 }
238
239 @Override
240 protected NamedCollection<KeyValueDescibeData> extractKVP(
241 Collection<Result> result,
242 String keyid,
243 String valueid
244 ) {
245 NamedCollection<KeyValueDescibeData> kvdd =
246 new NamedArrayList<KeyValueDescibeData>(dataName, result.size());
247
248 kvdd.setMultiSelect(true);
249
250 int keyPos = -1;
251 int valPos = -1;
252 int parPos = -1;
253
254 for (Result res: result) {
255 if (keyPos < 0 || valPos < 0 || parPos < 0) {
256 ResultDescriptor rd = res.getResultDescriptor();
257
258 keyPos = rd.getColumnIndex(keyid);
259 valPos = rd.getColumnIndex(valueid);
260 parPos = rd.getColumnIndex(SQL_KEY_PARAMETERID);
261 }
262
263 kvdd.add(new ExtendedKeyValueData(
264 res.getString(keyPos),
265 res.getString(valPos),
266 getID(),
267 res.getString(parPos)));
268 }
269
270 return kvdd;
271 }
272
273
274 /**
275 * This method create the user interface description for measurement and
276 * parameters as matrix. A row for each parameter, a column for each
277 * measurement.
278 */
279 @Override
280 protected void appendToDynamicNode(
281 XMLUtils.ElementCreator artCreator,
282 XMLUtils.ElementCreator creator,
283 Document document,
284 Node dynamicNode,
285 CallMeta callMeta,
286 Object o
287 ) {
288 NamedArrayList all = (NamedArrayList) o;
289 String name = all.getName();
290 RessourceFactory factory = RessourceFactory.getInstance();
291
292 Element matrixNode = creator.create("group");
293 Element matrixLabel = creator.create("label");
294 matrixLabel.setTextContent(factory.getRessource(
295 callMeta.getLanguages(), all.getName(), all.getName()));
296 creator.addAttr(matrixNode, "mode", "matrix");
297 matrixNode.appendChild(matrixLabel);
298
299 InputData inputParam = inputData.get("parameterid");
300 ParameterMatrix matrix = new ParameterMatrix(all, inputParam.splitValue());
301
302 int measurements = matrix.measurementSize();
303 int parameters = matrix.parameterSize();
304
305 for (int i = 0; i < parameters; i++) {
306 Element select = creator.create("select");
307 String param = matrix.getParameter(i);
308 creator.addAttr(select, "label", inputParam.getDescription(param));
309 creator.addAttr(select, "ref", name);
310
311 for (int j = 0; j < measurements; j++) {
312 Element item = creator.create("item");
313 Element label = creator.create("label");
314 Element value = creator.create("value");
315
316 creator.addAttr(item, "ref", name);
317 creator.addAttr(
318 item,
319 "parameter",
320 matrix.getMDescription(j));
321
322 if (!matrix.isValid(j, i)) {
323 creator.addAttr(item, "disabled", "true");
324 }
325 else {
326 creator.addAttr(item, "disabled", "false");
327 }
328
329 String tmpValue = matrix.getMeasurement(j) + ";" + param;
330 label.setTextContent(matrix.getMDescription(j));
331 value.setTextContent(tmpValue);
332
333 item.appendChild(label);
334 item.appendChild(value);
335 select.appendChild(item);
336 }
337
338 matrixNode.appendChild(select);
339 }
340
341 dynamicNode.appendChild(matrixNode);
342 }
343
344
345 /**
346 * This feed takes some input data storing measurement ids and parameter ids
347 * and put them into ExtendedInputData objects to save the relation between
348 * a measurement and the parameter it belongs to.
349 */
350 @Override
351 public Document feed(
352 CallContext context,
353 Collection<InputData> input,
354 String uuid)
355 throws StateException
356 {
357 RessourceFactory resFactory = RessourceFactory.getInstance();
358 Locale[] serverLocales = resFactory.getLocales();
359 Locale locale = context.getMeta().getPreferredLocale(
360 serverLocales);
361
362 if (input == null) {
363 String msg = resFactory.getRessource(
364 locale,
365 EXCEPTION_NO_INPUT,
366 EXCEPTION_NO_INPUT);
367 logger.warn(msg);
368 return feedFailure(msg);
369 }
370
371 for(InputData item: input) {
372 String name = item.getName();
373 InputValue inputValue = inputValues.get(name);
374
375 String[] tupel = extractValuesAndParams(item.getValue());
376 String type = inputValue.getType();
377
378 if (inputValue == null) {
379 String msg = resFactory.getRessource(
380 locale,
381 EXCEPTION_INVALID_INPUT,
382 EXCEPTION_INVALID_INPUT);
383 logger.warn(msg);
384 return feedFailure(msg);
385 }
386
387 if (!InputValidator.isInputValid(tupel[0], type)) {
388 String msg = resFactory.getRessource(
389 locale,
390 EXCEPTION_INVALID_INPUT,
391 EXCEPTION_INVALID_INPUT);
392 logger.warn(msg);
393 return feedFailure(msg);
394 }
395
396 if (inputData == null) {
397 inputData = new TreeMap<String, InputData>();
398 }
399
400 ExtendedInputData extended = new ExtendedInputData(
401 name,
402 tupel[0],
403 item.getObject(),
404 tupel[1]);
405
406 if (name.equals(dataName)) {
407 String[] desc = getDescriptionForInputData(
408 extended, context, uuid);
409 extended.setDescription(desc);
410 }
411
412 inputData.put(name, extended);
413 }
414
415 return feedSuccess();
416 }
417
418
419 /**
420 * Extract parameter ids and measurement ids from DefaultInputData objects
421 * and return an array. In the first position of this array, the measurement
422 * ids are placed, in the second position the parameter ids - all separated
423 * by a character.
424 *
425 * @param tmp String containing measurement ids and parameter ids.
426 * @return An array with separated measurements and parameters.
427 */
428 protected String[] extractValuesAndParams(String tmp) {
429 String[] array = tmp.split(DefaultInputData.VALUE_SEPARATOR);
430
431 String[] extracted = new String[2];
432 for (String item: array) {
433 String[] tupel = item.split(ExtendedInputData.SEPARATOR);
434
435 if (extracted[0] == null) {
436 extracted[0] = tupel[0];
437 }
438 else {
439 extracted[0] +=
440 DefaultInputData.VALUE_SEPARATOR + tupel[0];
441 }
442
443 if (extracted[1] == null) {
444 extracted[1] = tupel[1];
445 }
446 else {
447 extracted[1] += DefaultInputData.VALUE_SEPARATOR + tupel[1];
448 }
449 }
450
451 logger.debug("VALUES RESULT: " + extracted[0]);
452 logger.debug("PARAMS RESULT: " + extracted[1]);
453
454 return extracted;
455 }
456 }
457 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org