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