Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java @ 608:74dfb9346574
Added descriptions to the selected input data and use them for rendering the GUI.
gnv-artifacts/trunk@674 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 10 Feb 2010 15:35:26 +0000 |
parents | 292fbcd5e9ac |
children | 22e65fb4c64a |
comparison
equal
deleted
inserted
replaced
607:292fbcd5e9ac | 608:74dfb9346574 |
---|---|
224 } | 224 } |
225 | 225 |
226 boolean valid = iv.isInputValid(tmpItem.getValue(), | 226 boolean valid = iv.isInputValid(tmpItem.getValue(), |
227 inputValue.getType()); | 227 inputValue.getType()); |
228 if (valid) { | 228 if (valid) { |
229 String desc = getDescriptionForInputData(tmpItem, uuid); | |
230 tmpItem.setDescription(desc); | |
231 this.inputData.put(tmpItem.getName(), tmpItem); | |
232 } else { | |
233 String errMsg = "Wrong input for " + tmpItem.getValue() | |
234 + " is not an " + inputValue.getType() | |
235 + " Value."; | |
236 log.warn(errMsg); | |
237 throw new StateException(errMsg); | |
238 } | |
239 | |
240 } else { | |
241 String errMsg = "No Inputvalue given for Inputdata " | |
242 + tmpItem.getName(); | |
243 log.warn(errMsg + "Value will be ignored"); | |
244 | |
245 } | |
246 } | |
247 } else { | |
248 log.warn("No Inputdata given"); | |
249 } | |
250 } | |
251 | |
252 | |
253 private String getDescriptionForInputData(InputData data, String uuid) { | |
254 // there is only one element in the list, so take the first | |
255 Object obj = getDescibeData(uuid).get(0); | |
256 | |
257 if (obj instanceof NamedArrayList) { | |
258 NamedArrayList list = (NamedArrayList) obj; | |
259 int size = list.size(); | |
260 | |
261 for (int i = 0; i < size; i++) { | |
262 KeyValueDescibeData kv = (KeyValueDescibeData) list.get(i); | |
263 | |
264 if (kv.getKey().equals(data.getValue())) { | |
265 return kv.getValue(); | |
266 } | |
267 } | |
268 } | |
269 | |
270 return data.getValue(); | |
271 } | |
272 | |
273 | |
274 /** | |
275 * @see de.intevation.gnv.state.State#putInputData(java.util.Collection) | |
276 */ | |
277 public void putInputData(Collection<InputData> inputData, String uuid) | |
278 throws StateException { | |
279 if (inputData != null) { | |
280 Iterator<InputData> it = inputData.iterator(); | |
281 InputValidator iv = new InputValidator(); | |
282 while (it.hasNext()) { | |
283 InputData tmpItem = it.next(); | |
284 InputValue inputValue = this.inputValues.get(tmpItem.getName()); | |
285 if (inputValue != null) { | |
286 if (this.inputData == null) { | |
287 this.inputData = new HashMap<String, InputData>( | |
288 inputData.size()); | |
289 } | |
290 | |
291 boolean valid = iv.isInputValid(tmpItem.getValue(), | |
292 inputValue.getType()); | |
293 if (valid) { | |
229 if (tmpItem.getName().equals(MINVALUEFIELDNAME)){ | 294 if (tmpItem.getName().equals(MINVALUEFIELDNAME)){ |
230 String minValue = tmpItem.getValue(); | 295 String minValue = tmpItem.getValue(); |
231 String maxValue = this.getInputValue4ID(inputData, MAXVALUEFIELDNAME); | 296 String maxValue = this.getInputValue4ID(inputData, MAXVALUEFIELDNAME); |
232 valid = iv.isInputValid(maxValue,inputValue.getType()); | 297 valid = iv.isInputValid(maxValue,inputValue.getType()); |
233 if (!valid){ | 298 if (!valid){ |
284 } | 349 } |
285 } | 350 } |
286 } else { | 351 } else { |
287 log.warn("No Inputdata given"); | 352 log.warn("No Inputdata given"); |
288 } | 353 } |
289 } | 354 |
290 | 355 } |
291 /** | 356 |
292 * @see de.intevation.gnv.state.State#putInputData(java.util.Collection) | 357 |
293 */ | 358 /** |
294 public void putInputData(Collection<InputData> inputData, String uuid) | 359 * |
295 throws StateException { | |
296 if (inputData != null) { | |
297 Iterator<InputData> it = inputData.iterator(); | |
298 InputValidator iv = new InputValidator(); | |
299 while (it.hasNext()) { | |
300 InputData tmpItem = it.next(); | |
301 InputValue inputValue = this.inputValues.get(tmpItem.getName()); | |
302 if (inputValue != null) { | |
303 if (this.inputData == null) { | |
304 this.inputData = new HashMap<String, InputData>( | |
305 inputData.size()); | |
306 } | |
307 | |
308 boolean valid = iv.isInputValid(tmpItem.getValue(), | |
309 inputValue.getType()); | |
310 if (valid) { | |
311 if (tmpItem.getName().equals(MINVALUEFIELDNAME)){ | |
312 String minValue = tmpItem.getValue(); | |
313 String maxValue = this.getInputValue4ID(inputData, MAXVALUEFIELDNAME); | |
314 valid = iv.isInputValid(maxValue,inputValue.getType()); | |
315 if (!valid){ | |
316 String errMsg = "Wrong input for " + tmpItem.getValue() | |
317 + " is not an " + inputValue.getType() | |
318 + " Value."; | |
319 log.warn(errMsg); | |
320 throw new StateException(errMsg); | |
321 } | |
322 | |
323 valid = iv.isInputValid(minValue, | |
324 maxValue, | |
325 inputValue.getType()); | |
326 if (!valid){ | |
327 String errMsg = "MaxValue-Input is less than MinValue-Input "; | |
328 log.warn(errMsg); | |
329 throw new StateException(errMsg); | |
330 } | |
331 }else if (tmpItem.getName().equals(MAXVALUEFIELDNAME)){ | |
332 String minValue = this.getInputValue4ID(inputData, MINVALUEFIELDNAME); | |
333 String maxValue = tmpItem.getValue(); | |
334 valid = iv.isInputValid(minValue,inputValue.getType()); | |
335 if (!valid){ | |
336 String errMsg = "Wrong input for " + tmpItem.getValue() | |
337 + " is not an " + inputValue.getType() | |
338 + " Value."; | |
339 log.warn(errMsg); | |
340 throw new StateException(errMsg); | |
341 } | |
342 | |
343 valid = iv.isInputValid(minValue, | |
344 maxValue, | |
345 inputValue.getType()); | |
346 if (!valid){ | |
347 String errMsg = "MaxValue-Input is less than MinValue-Input "; | |
348 log.warn(errMsg); | |
349 throw new StateException(errMsg); | |
350 } | |
351 } | |
352 this.inputData.put(tmpItem.getName(), tmpItem); | |
353 } else { | |
354 String errMsg = "Wrong input for " + tmpItem.getValue() | |
355 + " is not an " + inputValue.getType() | |
356 + " Value."; | |
357 log.warn(errMsg); | |
358 throw new StateException(errMsg); | |
359 } | |
360 | |
361 } else { | |
362 String errMsg = "No Inputvalue given for Inputdata " | |
363 + tmpItem.getName(); | |
364 log.warn(errMsg + "Value will be ignored"); | |
365 | |
366 } | |
367 } | |
368 } else { | |
369 log.warn("No Inputdata given"); | |
370 } | |
371 | |
372 debugInputData(); | |
373 } | |
374 | |
375 private void debugInputData() { | |
376 if (log.isDebugEnabled()) { | |
377 Iterator iter = inputData.keySet().iterator(); | |
378 while (iter.hasNext()) { | |
379 String key = (String) iter.next(); | |
380 } | |
381 } | |
382 } | |
383 | |
384 /** | |
385 * | |
386 * @see de.intevation.gnv.state.State#setPreSettings(java.util.Map) | 360 * @see de.intevation.gnv.state.State#setPreSettings(java.util.Map) |
387 */ | 361 */ |
388 public void setPreSettings(Map<String, InputData> preSettings) { | 362 public void setPreSettings(Map<String, InputData> preSettings) { |
389 this.preSettings = preSettings; | 363 this.preSettings = preSettings; |
390 } | 364 } |
398 } | 372 } |
399 } | 373 } |
400 return null; | 374 return null; |
401 } | 375 } |
402 | 376 |
403 private void setSelection(InputData inputData, String uuid) { | |
404 | |
405 Object o = this.getDescribeData(inputData.getName(),uuid); | |
406 if (o != null) { | |
407 if (o instanceof Collection<?>) { | |
408 Collection<KeyValueDescibeData> values = (Collection<KeyValueDescibeData>) o; | |
409 | |
410 String value = inputData.getValue(); | |
411 String[] selectedValues = value.split(","); | |
412 Set<String> selectedItems = new HashSet<String>( | |
413 selectedValues.length); | |
414 for (int i = 0; i < selectedValues.length; i++) { | |
415 selectedItems.add(selectedValues[i].trim()); | |
416 } | |
417 // Selektion umsetzen | |
418 Iterator<KeyValueDescibeData> it = values.iterator(); | |
419 while (it.hasNext()) { | |
420 KeyValueDescibeData data = it.next(); | |
421 String key = "" + data.getKey(); | |
422 boolean selected = selectedItems.contains(key); | |
423 data.setSelected(selected); | |
424 } | |
425 } else if (o instanceof MinMaxDescribeData) { | |
426 MinMaxDescribeData data = (MinMaxDescribeData) o; | |
427 if (inputData.getName().equals(MINVALUEFIELDNAME)) { | |
428 data.setMinValue(inputData.getValue()); | |
429 } | |
430 if (inputData.getName().equals(MAXVALUEFIELDNAME)) { | |
431 data.setMaxValue(inputData.getValue()); | |
432 } | |
433 } else if (o instanceof SingleValueDescribeData) { | |
434 ((SingleValueDescribeData)o).setValue(inputData.getValue()); | |
435 } | |
436 } | |
437 } | |
438 | |
439 private Object getDescribeData(String name, String uuid) { | |
440 Collection<Object> descibeData = this.getDescibeData(uuid); | |
441 if (descibeData != null) { | |
442 Iterator<Object> it = descibeData.iterator(); | |
443 while (it.hasNext()) { | |
444 Object o = it.next(); | |
445 if (o instanceof NamedCollection<?>) { | |
446 if (name.equals(((NamedCollection<?>) o).getName())) { | |
447 return o; | |
448 } | |
449 } else if (o instanceof MinMaxDescribeData) { | |
450 if (name.equals(((MinMaxDescribeData) o).getMinName())) { | |
451 return o; | |
452 } | |
453 if (name.equals(((MinMaxDescribeData) o).getMaxName())) { | |
454 return o; | |
455 } | |
456 }else if (o instanceof SingleValueDescribeData) { | |
457 if (name.equals(((SingleValueDescribeData)o).getName())){ | |
458 return o; | |
459 } | |
460 } | |
461 } | |
462 } | |
463 return null; | |
464 } | |
465 | 377 |
466 /** | 378 /** |
467 * @see de.intevation.gnv.state.State#advance(java.lang.String, | 379 * @see de.intevation.gnv.state.State#advance(java.lang.String, |
468 * de.intevation.artifacts.CallMeta) | 380 * de.intevation.artifacts.CallMeta) |
469 */ | 381 */ |
473 } | 385 } |
474 | 386 |
475 public void initialize(String uuid, CallContext context) | 387 public void initialize(String uuid, CallContext context) |
476 throws StateException | 388 throws StateException |
477 { | 389 { |
390 /* | |
478 try { | 391 try { |
479 getDescibeData(uuid); | 392 getDescibeData(uuid); |
480 } | 393 } |
481 catch (RuntimeException e) { | 394 catch (RuntimeException e) { |
482 log.error(e, e); | 395 log.error(e, e); |
483 throw new StateException(e); | 396 throw new StateException(e); |
484 } | 397 } |
398 */ | |
485 } | 399 } |
486 | 400 |
487 /** | 401 /** |
488 * @return | 402 * @return |
489 */ | 403 */ |
636 } | 550 } |
637 return keyValueDescibeData; | 551 return keyValueDescibeData; |
638 } | 552 } |
639 | 553 |
640 | 554 |
641 public String findNewValue() { | |
642 Iterator iter = inputValueNames.iterator(); | |
643 | |
644 while (iter.hasNext()) { | |
645 String key = (String) iter.next(); | |
646 boolean old = false; | |
647 | |
648 StateBase parent = (StateBase) getParent(); | |
649 if (parent == null) { | |
650 // first state should always render a dynamic part | |
651 old = true; | |
652 } | |
653 | |
654 while (parent != null) { | |
655 if (parent.inputValueNames.contains(key) || inBlackList(key)) { | |
656 old = true; | |
657 break; | |
658 } | |
659 | |
660 parent = (StateBase) parent.getParent(); | |
661 } | |
662 | |
663 if (!old) { | |
664 return key; | |
665 } | |
666 } | |
667 | |
668 return null; | |
669 } | |
670 | |
671 | |
672 public static boolean inBlackList(String key) { | 555 public static boolean inBlackList(String key) { |
673 int length = BLACKLIST.length; | 556 int length = BLACKLIST.length; |
674 for (int i = 0; i < length; i++) { | 557 for (int i = 0; i < length; i++) { |
675 if (BLACKLIST[i].equals(key)) { | 558 if (BLACKLIST[i].equals(key)) { |
676 return true; | 559 return true; |
735 CallContext context, | 618 CallContext context, |
736 String uuid) | 619 String uuid) |
737 { | 620 { |
738 CallMeta callMeta = context.getMeta(); | 621 CallMeta callMeta = context.getMeta(); |
739 | 622 |
740 String newInput = findNewValue(); | 623 if (dataName == null) |
741 if (newInput == null && parent != null) | |
742 return; | 624 return; |
743 | 625 |
744 List<Object> descibeData = getDescibeData(uuid); | 626 List<Object> descibeData = getDescibeData(uuid); |
745 if (descibeData != null) { | 627 if (descibeData != null) { |
746 Iterator<Object> it = descibeData.iterator(); | 628 Iterator<Object> it = descibeData.iterator(); |
769 ((StateBase) parent).describeStatic( | 651 ((StateBase) parent).describeStatic( |
770 artCreator, creator, document, staticNode, context, uuid); | 652 artCreator, creator, document, staticNode, context, uuid); |
771 } | 653 } |
772 | 654 |
773 CallMeta callMeta = context.getMeta(); | 655 CallMeta callMeta = context.getMeta(); |
774 String inputKey = findNewValue(); | 656 appendToStaticNode(artCreator, creator, document, staticNode, callMeta); |
775 appendToStaticNode( | |
776 artCreator, creator, document, staticNode, callMeta, inputKey); | |
777 } | 657 } |
778 | 658 |
779 | 659 |
780 protected void appendToStaticNode( | 660 protected void appendToStaticNode( |
781 XMLUtils.ElementCreator artCreator, | 661 XMLUtils.ElementCreator artCreator, |
782 XMLUtils.ElementCreator creator, | 662 XMLUtils.ElementCreator creator, |
783 Document document, | 663 Document document, |
784 Node staticNode, | 664 Node staticNode, |
785 CallMeta callMeta, | 665 CallMeta callMeta |
786 String inputKey | |
787 ) { | 666 ) { |
788 InputValue meta = inputValues.get(inputKey); | 667 InputData data = inputData.get(dataName); |
789 InputData data = inputData.get(inputKey); | 668 |
790 | 669 if (data == null) { |
791 if (meta == null || data == null) { | |
792 return; | 670 return; |
793 } | 671 } |
794 | 672 |
795 boolean multiselect = meta.isMultiselect(); | 673 Element selectNode = creator.create("select1"); |
796 | 674 creator.addAttr(selectNode, "ref", dataName); |
797 Element selectNode = creator.create(multiselect?"select":"select1"); | |
798 creator.addAttr(selectNode, "ref", inputKey); | |
799 | 675 |
800 Element lableNode = creator.create("label"); | 676 Element lableNode = creator.create("label"); |
801 lableNode.setTextContent(RessourceFactory.getInstance() | 677 lableNode.setTextContent(RessourceFactory.getInstance() |
802 .getRessource(callMeta.getLanguages(), inputKey, inputKey)); | 678 .getRessource(callMeta.getLanguages(), dataName, dataName)); |
803 Element choiceNode = creator.create("choices"); | 679 Element choiceNode = creator.create("choices"); |
804 | 680 |
805 /* TODO InputData can have more than 1 value if multiselect, implement a | 681 /* TODO InputData can have more than 1 value if multiselect, implement a |
806 * loop over these values | 682 * loop over these values |
807 | 683 |
816 Element itemNode = creator.create("item"); | 692 Element itemNode = creator.create("item"); |
817 | 693 |
818 creator.addAttr(itemNode, "selected", "true"); | 694 creator.addAttr(itemNode, "selected", "true"); |
819 | 695 |
820 Element choiceLableNode = creator.create("label"); | 696 Element choiceLableNode = creator.create("label"); |
821 choiceLableNode.setTextContent(data.getValue()); | 697 choiceLableNode.setTextContent(data.getDescription()); |
822 itemNode.appendChild(choiceLableNode); | 698 itemNode.appendChild(choiceLableNode); |
823 | 699 |
824 Element choiceValueNode = creator.create("value"); | 700 Element choiceValueNode = creator.create("value"); |
825 choiceValueNode.setTextContent(inputKey); | 701 choiceValueNode.setTextContent(dataName); |
826 itemNode.appendChild(choiceValueNode); | 702 itemNode.appendChild(choiceValueNode); |
827 choiceNode.appendChild(itemNode); | 703 choiceNode.appendChild(itemNode); |
828 /* | 704 /* |
829 } | 705 } |
830 */ | 706 */ |