Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java @ 540:80630520e25a
merged gnv-artifacts/0.4
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:49 +0200 |
parents | cef17cc90fd0 |
children | 9681ac6b6527 |
comparison
equal
deleted
inserted
replaced
415:9f4a0b990d27 | 540:80630520e25a |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.state; | |
5 | |
6 import de.intevation.artifactdatabase.Config; | |
7 import de.intevation.artifactdatabase.XMLUtils; | |
8 | |
9 import de.intevation.artifacts.ArtifactNamespaceContext; | |
10 import de.intevation.artifacts.CallContext; | |
11 import de.intevation.artifacts.CallMeta; | |
12 | |
13 import de.intevation.gnv.artifacts.GNVArtifactBase; | |
14 | |
15 import de.intevation.gnv.artifacts.cache.CacheFactory; | |
16 | |
17 import de.intevation.gnv.artifacts.ressource.RessourceFactory; | |
18 | |
19 import de.intevation.gnv.geobackend.base.Result; | |
20 | |
21 import de.intevation.gnv.geobackend.base.query.QueryExecutor; | |
22 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; | |
23 | |
24 import de.intevation.gnv.geobackend.base.query.exception.QueryException; | |
25 | |
26 import de.intevation.gnv.geobackend.util.DateUtils; | |
27 | |
28 import de.intevation.gnv.state.describedata.DefaultKeyValueDescribeData; | |
29 import de.intevation.gnv.state.describedata.KeyValueDescibeData; | |
30 import de.intevation.gnv.state.describedata.MinMaxDescribeData; | |
31 import de.intevation.gnv.state.describedata.NamedArrayList; | |
32 import de.intevation.gnv.state.describedata.NamedCollection; | |
33 import de.intevation.gnv.state.describedata.SingleValueDescribeData; | |
34 | |
35 import de.intevation.gnv.state.exception.StateException; | |
36 | |
37 import de.intevation.gnv.utils.InputValidator; | |
38 | |
39 import java.util.ArrayList; | |
40 import java.util.Collection; | |
41 import java.util.Date; | |
42 import java.util.GregorianCalendar; | |
43 import java.util.HashMap; | |
44 import java.util.HashSet; | |
45 import java.util.Iterator; | |
46 import java.util.List; | |
47 import java.util.Map; | |
48 import java.util.Set; | |
49 | |
50 import javax.xml.xpath.XPathConstants; | |
51 | |
52 import org.apache.log4j.Logger; | |
53 | |
54 import org.w3c.dom.Document; | |
55 import org.w3c.dom.Element; | |
56 import org.w3c.dom.Node; | |
57 import org.w3c.dom.NodeList; | |
58 | |
59 /** | |
60 * @author Tim Englich (tim.englich@intevation.de) | |
61 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) | |
62 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) | |
63 */ | |
64 public abstract class StateBase implements State { | |
65 | |
66 /** | |
67 * The UID of this Class | |
68 */ | |
69 private static final long serialVersionUID = 2411169179001645426L; | |
70 | |
71 /** | |
72 * the logger, used to log exceptions and additonaly information | |
73 */ | |
74 private static Logger log = Logger.getLogger(GNVArtifactBase.class); | |
75 | |
76 private final static String MINVALUEFIELDNAME = "minvalue"; | |
77 private final static String MAXVALUEFIELDNAME = "maxvalue"; | |
78 | |
79 private final static String NODATASELECTIONKEY = "n/n"; | |
80 | |
81 public final static String DESCRIBEDATAKEY = "_DESCRIBEDATA"; | |
82 | |
83 public final static String XPATH_STATIC_UI = "art:static"; | |
84 public final static String XPATH_DYNAMIC_UI = "art:dynamic"; | |
85 | |
86 private String id = null; | |
87 | |
88 private String description = null; | |
89 | |
90 protected String dataName = null; | |
91 | |
92 protected boolean dataMultiSelect = false; | |
93 | |
94 protected boolean dataNoSelect = false; | |
95 | |
96 protected String queryID = null; | |
97 | |
98 protected Collection<String> inputValueNames = null; | |
99 | |
100 private Map<String, InputValue> inputValues = null; | |
101 | |
102 private State parent = null; | |
103 | |
104 protected Map<String, InputData> inputData = null; | |
105 | |
106 | |
107 /** | |
108 * Constructor | |
109 */ | |
110 public StateBase() { | |
111 super(); | |
112 } | |
113 | |
114 /** | |
115 * @see de.intevation.gnv.state.State#getID() | |
116 */ | |
117 public String getID() { | |
118 return this.id; | |
119 } | |
120 | |
121 /** | |
122 * @see de.intevation.gnv.state.State#getDescription() | |
123 */ | |
124 public String getDescription() { | |
125 return this.description; | |
126 } | |
127 | |
128 | |
129 | |
130 /** | |
131 * @see de.intevation.gnv.state.State#getRequiredInputValues() | |
132 */ | |
133 public Collection<InputValue> getRequiredInputValues() { | |
134 return this.inputValues.values(); | |
135 } | |
136 | |
137 | |
138 public void reset(String uuid) { | |
139 | |
140 if (parent != null) { | |
141 inputData = parent.inputData(); | |
142 } | |
143 } | |
144 | |
145 | |
146 /** | |
147 * @see de.intevation.gnv.state.State#setup(org.w3c.dom.Node) | |
148 */ | |
149 public void setup(Node configuration) { | |
150 log.debug("StateBase.setup"); | |
151 this.id = ((Element)configuration).getAttribute("id"); | |
152 this.description = ((Element)configuration).getAttribute("description"); | |
153 | |
154 log.info("State-ID = " + this.id); | |
155 | |
156 NodeList inputValuesNodes = Config.getNodeSetXPath(configuration, | |
157 "inputvalues/inputvalue"); | |
158 this.inputValues = new HashMap<String, InputValue>(inputValuesNodes | |
159 .getLength()); | |
160 this.inputValueNames = new ArrayList<String>(inputValuesNodes | |
161 .getLength()); | |
162 for (int i = 0; i < inputValuesNodes.getLength(); i++) { | |
163 Element inputValueNode = (Element)inputValuesNodes.item(i); | |
164 String usedinQueryValue = inputValueNode.getAttribute("usedinquery"); | |
165 int usedinQuery = 1; | |
166 if (usedinQueryValue != null) { | |
167 try { | |
168 usedinQuery = Integer.parseInt(usedinQueryValue); | |
169 } catch (NumberFormatException e) { | |
170 log | |
171 .warn("Used in Query Value cannot be transformed into a Number"); | |
172 } | |
173 } | |
174 InputValue inputValue = new DefaultInputValue(inputValueNode.getAttribute("name"), | |
175 inputValueNode.getAttribute("type"), | |
176 Boolean.parseBoolean(inputValueNode. | |
177 getAttribute("multiselect")), usedinQuery); | |
178 log.debug(inputValue.toString()); | |
179 this.inputValues.put(inputValue.getName(), inputValue); | |
180 this.inputValueNames.add(inputValue.getName()); | |
181 } | |
182 | |
183 this.queryID = Config.getStringXPath(configuration, "queryID"); | |
184 log.info("QueryID ==> " + this.queryID); | |
185 | |
186 this.dataName = Config.getStringXPath(configuration, "dataname"); | |
187 | |
188 String dataMultiSelectValue = Config.getStringXPath(configuration, | |
189 "data-multiselect"); | |
190 if (dataMultiSelectValue != null) { | |
191 this.dataMultiSelect = Boolean.parseBoolean(dataMultiSelectValue); | |
192 } | |
193 | |
194 String dataNoSelectValue =Config.getStringXPath(configuration, | |
195 "data-noselect"); | |
196 if (dataNoSelectValue != null) { | |
197 this. dataNoSelect = Boolean.parseBoolean(dataNoSelectValue); | |
198 } | |
199 | |
200 } | |
201 | |
202 /** | |
203 * @see de.intevation.gnv.state.State#getParent() | |
204 */ | |
205 public State getParent() { | |
206 return this.parent; | |
207 } | |
208 | |
209 /** | |
210 * @see de.intevation.gnv.state.State#setParent(de.intevation.gnv.state.State) | |
211 */ | |
212 public void setParent(State state) { | |
213 this.parent = state; | |
214 } | |
215 | |
216 /** | |
217 * @see de.intevation.gnv.state.State#putInputData(java.util.Collection) | |
218 */ | |
219 public void putInputData(Collection<InputData> inputData, String uuid) | |
220 throws StateException { | |
221 log.debug("StateBase.putInputData"); | |
222 if (inputData != null) { | |
223 Iterator<InputData> it = inputData.iterator(); | |
224 InputValidator iv = new InputValidator(); | |
225 while (it.hasNext()) { | |
226 InputData tmpItem = it.next(); | |
227 InputValue inputValue = this.inputValues.get(tmpItem.getName()); | |
228 if (inputValue != null) { | |
229 if (this.inputData == null) { | |
230 this.inputData = new HashMap<String, InputData>( | |
231 inputData.size()); | |
232 } | |
233 | |
234 boolean valid = iv.isInputValid(tmpItem.getValue(), | |
235 inputValue.getType()); | |
236 if (valid) { | |
237 if (tmpItem.getName().equals(MINVALUEFIELDNAME)){ | |
238 String minValue = tmpItem.getValue(); | |
239 String maxValue = this.getInputValue4ID(inputData, MAXVALUEFIELDNAME); | |
240 valid = iv.isInputValid(maxValue,inputValue.getType()); | |
241 if (!valid){ | |
242 String errMsg = "Wrong input for " + tmpItem.getValue() | |
243 + " is not an " + inputValue.getType() | |
244 + " Value."; | |
245 log.warn(errMsg); | |
246 throw new StateException(errMsg); | |
247 } | |
248 | |
249 valid = iv.isInputValid(minValue, | |
250 maxValue, | |
251 inputValue.getType()); | |
252 if (!valid){ | |
253 String errMsg = "MaxValue-Input is less than MinValue-Input "; | |
254 log.warn(errMsg); | |
255 throw new StateException(errMsg); | |
256 } | |
257 }else if (tmpItem.getName().equals(MAXVALUEFIELDNAME)){ | |
258 String minValue = this.getInputValue4ID(inputData, MINVALUEFIELDNAME); | |
259 String maxValue = tmpItem.getValue(); | |
260 valid = iv.isInputValid(minValue,inputValue.getType()); | |
261 if (!valid){ | |
262 String errMsg = "Wrong input for " + tmpItem.getValue() | |
263 + " is not an " + inputValue.getType() | |
264 + " Value."; | |
265 log.warn(errMsg); | |
266 throw new StateException(errMsg); | |
267 } | |
268 | |
269 valid = iv.isInputValid(minValue, | |
270 maxValue, | |
271 inputValue.getType()); | |
272 if (!valid){ | |
273 String errMsg = "MaxValue-Input is less than MinValue-Input "; | |
274 log.warn(errMsg); | |
275 throw new StateException(errMsg); | |
276 } | |
277 } | |
278 this.setSelection(tmpItem, uuid); | |
279 this.inputData.put(tmpItem.getName(), tmpItem); | |
280 } else { | |
281 String errMsg = "Wrong input for " + tmpItem.getValue() | |
282 + " is not an " + inputValue.getType() | |
283 + " Value."; | |
284 log.warn(errMsg); | |
285 throw new StateException(errMsg); | |
286 } | |
287 | |
288 } else { | |
289 String errMsg = "No Inputvalue given for Inputdata " | |
290 + tmpItem.getName(); | |
291 log.warn(errMsg + "Value will be ignored"); | |
292 | |
293 } | |
294 } | |
295 } else { | |
296 log.warn("No Inputdata given"); | |
297 } | |
298 } | |
299 | |
300 private String getInputValue4ID(Collection<InputData> inputData, String inputName){ | |
301 Iterator<InputData> it = inputData.iterator(); | |
302 while (it.hasNext()) { | |
303 InputData tmpItem = it.next(); | |
304 if (tmpItem.getName().equals(inputName)){ | |
305 return tmpItem.getValue(); | |
306 } | |
307 } | |
308 return null; | |
309 } | |
310 | |
311 private void setSelection(InputData inputData, String uuid) { | |
312 log.debug("StateBase.setSelection"); | |
313 | |
314 Object o = this.getDescribeData(inputData.getName(),uuid); | |
315 if (o != null) { | |
316 if (o instanceof Collection<?>) { | |
317 Collection<KeyValueDescibeData> values = (Collection<KeyValueDescibeData>) o; | |
318 | |
319 String value = inputData.getValue(); | |
320 String[] selectedValues = value.split(","); | |
321 Set<String> selectedItems = new HashSet<String>( | |
322 selectedValues.length); | |
323 for (int i = 0; i < selectedValues.length; i++) { | |
324 selectedItems.add(selectedValues[i].trim()); | |
325 } | |
326 // Selektion umsetzen | |
327 Iterator<KeyValueDescibeData> it = values.iterator(); | |
328 while (it.hasNext()) { | |
329 KeyValueDescibeData data = it.next(); | |
330 String key = "" + data.getKey(); | |
331 boolean selected = selectedItems.contains(key); | |
332 data.setSelected(selected); | |
333 } | |
334 } else if (o instanceof MinMaxDescribeData) { | |
335 MinMaxDescribeData data = (MinMaxDescribeData) o; | |
336 if (inputData.getName().equals(MINVALUEFIELDNAME)) { | |
337 data.setMinValue(inputData.getValue()); | |
338 } | |
339 if (inputData.getName().equals(MAXVALUEFIELDNAME)) { | |
340 data.setMaxValue(inputData.getValue()); | |
341 } | |
342 } else if (o instanceof SingleValueDescribeData) { | |
343 ((SingleValueDescribeData)o).setValue(inputData.getValue()); | |
344 } | |
345 } | |
346 } | |
347 | |
348 private Object getDescribeData(String name, String uuid) { | |
349 log.debug("StateBase.getDescribeData"); | |
350 Collection<Object> descibeData = this.getDescibeData(uuid); | |
351 if (descibeData != null) { | |
352 Iterator<Object> it = descibeData.iterator(); | |
353 while (it.hasNext()) { | |
354 Object o = it.next(); | |
355 if (o instanceof NamedCollection<?>) { | |
356 if (name.equals(((NamedCollection<?>) o).getName())) { | |
357 return o; | |
358 } | |
359 } else if (o instanceof MinMaxDescribeData) { | |
360 if (name.equals(((MinMaxDescribeData) o).getMinName())) { | |
361 return o; | |
362 } | |
363 if (name.equals(((MinMaxDescribeData) o).getMaxName())) { | |
364 return o; | |
365 } | |
366 }else if (o instanceof SingleValueDescribeData) { | |
367 if (name.equals(((SingleValueDescribeData)o).getName())){ | |
368 return o; | |
369 } | |
370 } | |
371 } | |
372 } | |
373 return null; | |
374 } | |
375 | |
376 /** | |
377 * @see de.intevation.gnv.state.State#advance(java.lang.String, | |
378 * de.intevation.artifacts.CallMeta) | |
379 */ | |
380 public void advance(String uuid, CallContext context) | |
381 throws StateException | |
382 { | |
383 } | |
384 | |
385 public void initialize(String uuid, CallContext context) | |
386 throws StateException | |
387 { | |
388 log.debug("StateBase.initialize"); | |
389 try { | |
390 String[] filterValues = this | |
391 .generateFilterValuesFromInputData(); | |
392 Collection<Result> result = null; | |
393 try { | |
394 if (this.queryID != null) { | |
395 QueryExecutor queryExecutor = QueryExecutorFactory | |
396 .getInstance().getQueryExecutor(); | |
397 result = queryExecutor.executeQuery(this.queryID, | |
398 filterValues); | |
399 } | |
400 this.purifyResult(result, uuid); | |
401 } catch (RuntimeException e) { | |
402 log.error(e, e); | |
403 } | |
404 } catch (QueryException e) { | |
405 log.error(e, e); | |
406 throw new StateException(e); | |
407 } | |
408 } | |
409 | |
410 /** | |
411 * @return | |
412 */ | |
413 protected String[] generateFilterValuesFromInputData() { | |
414 List<String> list = new ArrayList<String>(); | |
415 Iterator<String> it = this.inputValueNames.iterator(); | |
416 while (it.hasNext()) { | |
417 String value = it.next(); | |
418 InputData data = this.inputData.get(value); | |
419 if (data != null | |
420 && this.inputValues.containsKey(data.getName())) { | |
421 int size = this.inputValues.get(data.getName()) | |
422 .usedInQueries(); | |
423 String type = this.inputValues.get(data.getName()) | |
424 .getType(); | |
425 String requestValue = data.getValue(); | |
426 if (type.equalsIgnoreCase("string")) { | |
427 requestValue = this | |
428 .prepareInputData4DBQuery(requestValue); | |
429 } else if (type.equalsIgnoreCase("date")) { | |
430 requestValue = this | |
431 .prepareInputData4DateDBQuery(requestValue); | |
432 } else if (type.equalsIgnoreCase("coordinate")){ | |
433 requestValue = this | |
434 .prepareInputData4RegionDBQuery(requestValue); | |
435 } | |
436 for (int j = 0; j < size; j++) { | |
437 list.add(requestValue); | |
438 } | |
439 } | |
440 } | |
441 String[] filterValues = list.toArray(new String[list.size()]); | |
442 return filterValues; | |
443 } | |
444 | |
445 protected String prepareInputData4RegionDBQuery(String value){ | |
446 return value; | |
447 } | |
448 | |
449 private String prepareInputData4DateDBQuery(String value) { | |
450 log.debug("StateBase.prepareInputData4DateDBQuery"); | |
451 if (value != null) { | |
452 String[] values = value.split(","); | |
453 String newValue = ""; | |
454 for (int i = 0; i < values.length; i++) { | |
455 if (newValue.length() > 0) { | |
456 newValue = newValue + " , "; | |
457 } | |
458 // TODO JUST HACK FIND A BETTER RESOLUTION | |
459 newValue = newValue + "to_date('" + values[i].trim() | |
460 + "', 'YYYY.MM.DD HH24:MI:SS')"; | |
461 } | |
462 return newValue; | |
463 } | |
464 | |
465 return value; | |
466 } | |
467 | |
468 private String prepareInputData4DBQuery(String value) { | |
469 log.debug("StateBase.prepareInputData4DBQuery"); | |
470 if (value != null) { | |
471 String[] values = value.split(","); | |
472 String newValue = ""; | |
473 for (int i = 0; i < values.length; i++) { | |
474 if (newValue.length() > 0) { | |
475 newValue = newValue + " , "; | |
476 } | |
477 newValue = newValue + "'" + values[i].trim() + "'"; | |
478 } | |
479 return newValue; | |
480 } | |
481 | |
482 return value; | |
483 | |
484 } | |
485 | |
486 /** | |
487 * @param result | |
488 */ | |
489 protected void purifyResult(Collection<Result> result, String uuid) { | |
490 log.debug("StateBase.purifyResult"); | |
491 List<Object> describeData = this.getDescibeData(uuid); | |
492 if (describeData == null) { | |
493 describeData = new ArrayList<Object>(); | |
494 } | |
495 NamedCollection<KeyValueDescibeData> keyValueDescibeData = extractKVP(result, "KEY", "VALUE"); | |
496 describeData.add(keyValueDescibeData); | |
497 this.setDescibeData(uuid, describeData); | |
498 } | |
499 | |
500 /** | |
501 * @param result | |
502 * @return | |
503 */ | |
504 protected NamedCollection<KeyValueDescibeData> extractKVP(Collection<Result> result, | |
505 String keyid, | |
506 String valueid) { | |
507 Iterator<Result> rit = result.iterator(); | |
508 int dataSize = (this.dataNoSelect ? result.size()+1 : result.size()); | |
509 | |
510 NamedCollection<KeyValueDescibeData> keyValueDescibeData = new NamedArrayList<KeyValueDescibeData>( | |
511 this.dataName, dataSize); | |
512 keyValueDescibeData.setMultiSelect(this.dataMultiSelect); | |
513 | |
514 if (this.dataNoSelect){ | |
515 keyValueDescibeData.add(new DefaultKeyValueDescribeData( | |
516 NODATASELECTIONKEY, | |
517 "No Selection", | |
518 getID() | |
519 )); | |
520 } | |
521 | |
522 boolean initialized = false; | |
523 int keyPos = 0; | |
524 int valuePos = 1; | |
525 String previousKey = null; | |
526 | |
527 while (rit.hasNext()) { | |
528 Result resultValue = rit.next(); | |
529 if (!initialized){ | |
530 keyPos = resultValue.getResultDescriptor().getColumnIndex(keyid); | |
531 valuePos = resultValue.getResultDescriptor().getColumnIndex(valueid); | |
532 if (valuePos < 0){ | |
533 valuePos = 1; | |
534 } | |
535 initialized = true; | |
536 } | |
537 String tmpKey = resultValue.getString(keyPos); | |
538 // TODO: FIXME: We have to do that because the arcsde does not support a distinct Query on Layers. | |
539 if (previousKey == null || !tmpKey.equals(previousKey)){ | |
540 previousKey = tmpKey; | |
541 keyValueDescibeData.add(new DefaultKeyValueDescribeData(tmpKey, resultValue.getString(valuePos), getID())); | |
542 } | |
543 } | |
544 return keyValueDescibeData; | |
545 } | |
546 | |
547 /** | |
548 * @see de.intevation.gnv.state.State#describe(org.w3c.dom.Document, | |
549 * org.w3c.dom.Node, de.intevation.artifacts.CallMeta, | |
550 * java.lang.String) | |
551 */ | |
552 public void describe( | |
553 Document document, | |
554 Node rootNode, | |
555 CallContext context, | |
556 String uuid | |
557 ) { | |
558 log.debug("StateBase.describe"); | |
559 CallMeta callMeta = context.getMeta(); | |
560 | |
561 List<Object> descibeData = this.getDescibeData(uuid); | |
562 if (descibeData != null) { | |
563 Iterator<Object> it = descibeData.iterator(); | |
564 | |
565 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
566 document, | |
567 ArtifactNamespaceContext.NAMESPACE_URI, | |
568 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
569 ); | |
570 | |
571 Node staticNode = (Node) XMLUtils.xpath( | |
572 rootNode, | |
573 XPATH_STATIC_UI, | |
574 XPathConstants.NODE, | |
575 ArtifactNamespaceContext.INSTANCE | |
576 ); | |
577 | |
578 Node dynamic = (Node) XMLUtils.xpath( | |
579 rootNode, | |
580 XPATH_DYNAMIC_UI, | |
581 XPathConstants.NODE, | |
582 ArtifactNamespaceContext.INSTANCE | |
583 ); | |
584 | |
585 XMLUtils.ElementCreator xCreator = new XMLUtils.ElementCreator( | |
586 document, | |
587 XMLUtils.XFORM_URL, | |
588 XMLUtils.XFORM_PREFIX | |
589 ); | |
590 | |
591 while (it.hasNext()) { | |
592 Object o = it.next(); | |
593 if ((!it.hasNext() && dataName != null)) { | |
594 appendToDynamicNode( | |
595 creator, xCreator, document, dynamic, callMeta, o); | |
596 } | |
597 else { | |
598 appendToStaticNode( | |
599 creator, xCreator, document, staticNode, callMeta, o); | |
600 } | |
601 } | |
602 } | |
603 } | |
604 | |
605 | |
606 protected void appendToStaticNode( | |
607 XMLUtils.ElementCreator artCreator, | |
608 XMLUtils.ElementCreator creator, | |
609 Document document, | |
610 Node staticNode, | |
611 CallMeta callMeta, | |
612 Object o | |
613 ) { | |
614 if (o instanceof Collection<?>) { | |
615 String name = null; | |
616 boolean multiselect = false; | |
617 if (o instanceof NamedCollection<?>) { | |
618 NamedCollection<?> nc = ((NamedCollection<?>) o); | |
619 name = nc.getName(); | |
620 multiselect = nc.isMultiSelect(); | |
621 } else { | |
622 Object[] names = this.inputValueNames.toArray(); | |
623 name = names[names.length - 1].toString(); | |
624 } | |
625 | |
626 Element selectNode = creator.create(multiselect?"select":"select1"); | |
627 creator.addAttr(selectNode, "ref", name); | |
628 | |
629 Element lableNode = creator.create("label"); | |
630 lableNode.setTextContent(RessourceFactory.getInstance() | |
631 .getRessource(callMeta.getLanguages(), name, name)); | |
632 Element choiceNode = creator.create("choices"); | |
633 | |
634 Collection<KeyValueDescibeData> values = (Collection<KeyValueDescibeData>) o; | |
635 Iterator<KeyValueDescibeData> resultIt = values.iterator(); | |
636 while (resultIt.hasNext()) { | |
637 KeyValueDescibeData result = resultIt.next(); | |
638 | |
639 if (result.isSelected()) { | |
640 artCreator.addAttr( | |
641 selectNode, "state", result.getState(), true | |
642 ); | |
643 | |
644 Element itemNode = creator.create("item"); | |
645 | |
646 creator.addAttr(itemNode, "selected", "true"); | |
647 | |
648 Element choiceLableNode = creator.create("label"); | |
649 choiceLableNode.setTextContent(result.getValue()); | |
650 itemNode.appendChild(choiceLableNode); | |
651 | |
652 Element choiceValueNode = creator.create("value"); | |
653 choiceValueNode.setTextContent("" + result.getKey()); | |
654 itemNode.appendChild(choiceValueNode); | |
655 choiceNode.appendChild(itemNode); | |
656 } | |
657 } | |
658 selectNode.appendChild(lableNode); | |
659 selectNode.appendChild(choiceNode); | |
660 | |
661 staticNode.appendChild(selectNode); | |
662 } | |
663 else if (o instanceof MinMaxDescribeData) { | |
664 appendMinMaxDescribeData( | |
665 artCreator, | |
666 creator, | |
667 document, | |
668 staticNode, | |
669 callMeta, | |
670 o); | |
671 } | |
672 else if (o instanceof SingleValueDescribeData) { | |
673 appendSingleValueDescribeData( | |
674 artCreator, | |
675 creator, | |
676 document, | |
677 staticNode, | |
678 callMeta, | |
679 o); | |
680 } | |
681 } | |
682 | |
683 | |
684 protected void appendToDynamicNode( | |
685 XMLUtils.ElementCreator artCreator, | |
686 XMLUtils.ElementCreator creator, | |
687 Document document, | |
688 Node dynamicNode, | |
689 CallMeta callMeta, | |
690 Object o | |
691 ) { | |
692 if (o instanceof Collection<?>) { | |
693 String name = null; | |
694 boolean multiselect = false; | |
695 if (o instanceof NamedCollection<?>) { | |
696 NamedCollection<?> nc = ((NamedCollection<?>) o); | |
697 name = nc.getName(); | |
698 multiselect = nc.isMultiSelect(); | |
699 } else { | |
700 Object[] names = this.inputValueNames.toArray(); | |
701 name = names[names.length - 1].toString(); | |
702 } | |
703 | |
704 Element selectNode = creator.create(multiselect?"select":"select1"); | |
705 creator.addAttr(selectNode, "ref", name); | |
706 | |
707 Element lableNode = creator.create("label"); | |
708 lableNode.setTextContent(RessourceFactory.getInstance() | |
709 .getRessource(callMeta.getLanguages(), name, name)); | |
710 Element choiceNode = creator.create("choices"); | |
711 | |
712 Collection<KeyValueDescibeData> values = (Collection<KeyValueDescibeData>) o; | |
713 Iterator<KeyValueDescibeData> resultIt = values.iterator(); | |
714 while (resultIt.hasNext()) { | |
715 KeyValueDescibeData result = resultIt.next(); | |
716 Element itemNode = creator.create("item"); | |
717 | |
718 if (result.isSelected()) { | |
719 itemNode.setAttribute("selected", "true"); | |
720 } | |
721 | |
722 Element choiceLableNode = creator.create("label"); | |
723 choiceLableNode.setTextContent(result.getValue()); | |
724 itemNode.appendChild(choiceLableNode); | |
725 | |
726 Element choicValueNode = creator.create("value"); | |
727 choicValueNode.setTextContent("" + result.getKey()); | |
728 itemNode.appendChild(choicValueNode); | |
729 choiceNode.appendChild(itemNode); | |
730 } | |
731 selectNode.appendChild(lableNode); | |
732 selectNode.appendChild(choiceNode); | |
733 | |
734 dynamicNode.appendChild(selectNode); | |
735 } | |
736 else if (o instanceof MinMaxDescribeData) { | |
737 appendMinMaxDescribeData( | |
738 artCreator, | |
739 creator, | |
740 document, | |
741 dynamicNode, | |
742 callMeta, | |
743 o); | |
744 } | |
745 else if (o instanceof SingleValueDescribeData) { | |
746 appendSingleValueDescribeData( | |
747 artCreator, | |
748 creator, | |
749 document, | |
750 dynamicNode, | |
751 callMeta, | |
752 o); | |
753 } | |
754 } | |
755 | |
756 | |
757 protected void appendMinMaxDescribeData( | |
758 XMLUtils.ElementCreator artCreator, | |
759 XMLUtils.ElementCreator creator, | |
760 Document document, | |
761 Node node, | |
762 CallMeta callMeta, | |
763 Object o | |
764 ) { | |
765 MinMaxDescribeData minMaxDescibeData = (MinMaxDescribeData) o; | |
766 Object min = minMaxDescibeData.getMinValue(); | |
767 Object max = minMaxDescibeData.getMaxValue(); | |
768 if (min instanceof GregorianCalendar) { | |
769 Date d = ((GregorianCalendar) min).getTime(); | |
770 min = DateUtils.getPatternedDateAmer(d); | |
771 } | |
772 | |
773 if (max instanceof GregorianCalendar) { | |
774 Date d = ((GregorianCalendar) max).getTime(); | |
775 max = DateUtils.getPatternedDateAmer(d); | |
776 } | |
777 | |
778 Element groupNode = creator.create("group"); | |
779 artCreator.addAttr(groupNode, "state", minMaxDescibeData.getState(), true); | |
780 | |
781 creator.addAttr(groupNode, "ref", minMaxDescibeData.getName()); | |
782 Element groupNodeLableNode = creator.create("label"); | |
783 groupNodeLableNode.setTextContent(RessourceFactory | |
784 .getInstance().getRessource( | |
785 callMeta.getLanguages(), | |
786 minMaxDescibeData.getName(), | |
787 minMaxDescibeData.getName())); | |
788 groupNode.appendChild(groupNodeLableNode); | |
789 | |
790 Element inputMinNode = creator.create("input"); | |
791 creator.addAttr(inputMinNode, "ref", MINVALUEFIELDNAME); | |
792 Element inputMinLableNode = creator.create("label"); | |
793 inputMinLableNode.setTextContent(RessourceFactory | |
794 .getInstance().getRessource( | |
795 callMeta.getLanguages(), MINVALUEFIELDNAME, | |
796 MINVALUEFIELDNAME)); | |
797 inputMinNode.appendChild(inputMinLableNode); | |
798 | |
799 Element inputMinValueNode = creator.create("value"); | |
800 inputMinValueNode.setTextContent(min.toString()); | |
801 inputMinNode.appendChild(inputMinValueNode); | |
802 | |
803 Element inputMaxNode = creator.create("input"); | |
804 creator.addAttr(inputMaxNode, "ref", MAXVALUEFIELDNAME); | |
805 Element inputMaxLableNode = creator.create("label"); | |
806 inputMaxLableNode.setTextContent(RessourceFactory | |
807 .getInstance().getRessource( | |
808 callMeta.getLanguages(), MAXVALUEFIELDNAME, | |
809 MAXVALUEFIELDNAME)); | |
810 inputMaxNode.appendChild(inputMaxLableNode); | |
811 | |
812 Element inputMaxValueNode = creator.create("value"); | |
813 inputMaxValueNode.setTextContent(max.toString()); | |
814 inputMaxNode.appendChild(inputMaxValueNode); | |
815 | |
816 groupNode.appendChild(inputMinNode); | |
817 groupNode.appendChild(inputMaxNode); | |
818 | |
819 node.appendChild(groupNode); | |
820 } | |
821 | |
822 | |
823 protected void appendSingleValueDescribeData( | |
824 XMLUtils.ElementCreator artCreator, | |
825 XMLUtils.ElementCreator creator, | |
826 Document document, | |
827 Node node, | |
828 CallMeta callMeta, | |
829 Object o | |
830 ) { | |
831 SingleValueDescribeData svdb = (SingleValueDescribeData) o; | |
832 | |
833 Element groupNode = creator.create("group"); | |
834 artCreator.addAttr(groupNode, "state", svdb.getState(), true); | |
835 creator.addAttr(groupNode, "ref", svdb.getName()); | |
836 | |
837 Element groupNodeLableNode = creator.create("label"); | |
838 groupNodeLableNode.setTextContent(RessourceFactory | |
839 .getInstance().getRessource( | |
840 callMeta.getLanguages(), | |
841 svdb.getName(), | |
842 svdb.getName())); | |
843 groupNode.appendChild(groupNodeLableNode); | |
844 | |
845 Element inputNode = creator.create("input"); | |
846 creator.addAttr(inputNode, "ref", svdb.getName()); | |
847 | |
848 Element inputLableNode = creator.create("label"); | |
849 inputLableNode.setTextContent(""); | |
850 inputNode.appendChild(inputLableNode); | |
851 | |
852 Element inputValueNode = creator.create("value"); | |
853 inputValueNode.setTextContent(svdb.getValue()); | |
854 inputNode.appendChild(inputValueNode); | |
855 | |
856 groupNode.appendChild(inputNode); | |
857 | |
858 node.appendChild(groupNode); | |
859 } | |
860 | |
861 /** | |
862 * @see de.intevation.gnv.state.State#getDescibeData() | |
863 */ | |
864 protected List<Object> getDescibeData(String uuid) { | |
865 if (CacheFactory.getInstance().isInitialized()) { | |
866 String key = uuid + DESCRIBEDATAKEY; | |
867 log.debug("Hash for Queryelements: " + key); | |
868 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key); | |
869 if (value != null) { | |
870 return (List<Object>) (value.getObjectValue()); | |
871 } | |
872 } | |
873 return null; | |
874 } | |
875 | |
876 /** | |
877 * @see de.intevation.gnv.state.State#getDescibeData() | |
878 */ | |
879 protected void setDescibeData(String uuid, List<Object> describeData) { | |
880 | |
881 if (CacheFactory.getInstance().isInitialized()) { | |
882 String key = uuid + DESCRIBEDATAKEY; | |
883 log.debug("Hash for Queryelements: " + key); | |
884 CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, describeData)); | |
885 } | |
886 } | |
887 | |
888 public Map<String, InputData> inputData() { | |
889 return inputData; | |
890 } | |
891 | |
892 /** | |
893 * @see de.intevation.gnv.state.State#getInputData() | |
894 */ | |
895 public Collection<InputData> getInputData() throws StateException { | |
896 return this.inputData != null ? this.inputData.values() : null; | |
897 } | |
898 | |
899 public void endOfLife(Object globalContext) { | |
900 } | |
901 } | |
902 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |