comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/SingleInputState.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 f94a95009423
children 9981452c7e75
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.state;
2
3 import de.intevation.artifacts.CallContext;
4
5 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
6 import de.intevation.gnv.geobackend.base.Result;
7 import de.intevation.gnv.state.describedata.DefaultSingleValueDescribeData;
8 import de.intevation.gnv.state.exception.StateException;
9 import de.intevation.gnv.utils.InputValidator;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Locale;
16
17 import org.apache.log4j.Logger;
18
19 import org.w3c.dom.Document;
20
21 /**
22 * This state handles single user input. The user is allowed to select just one
23 * value.
24 *
25 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
26 *
27 */
28 public class SingleInputState extends StateBase {
29
30 /**
31 * the logger, used to log exceptions and additonaly information
32 */
33 private static Logger log = Logger.getLogger(SingleInputState.class);
34
35 private static final long serialVersionUID = -6169497306324917318L;
36
37 /**
38 * Constructor
39 */
40 public SingleInputState() {
41 }
42
43
44 @Override
45 protected List<Object> purifyResult(Collection<Result> result, String uuid) {
46 log.debug("SingleInputState.purifyResult");
47 List<Object> describeData = new ArrayList<Object>();
48
49 String value = null;
50 if (result != null && result.size() == 1) {
51 Result tmpItem = result.iterator().next();
52 value = tmpItem.getObject("MAX").toString();
53 } else {
54 value = "";
55 }
56
57 describeData.add(new DefaultSingleValueDescribeData(
58 this.dataName, value, getID()));
59
60 return describeData;
61 }
62
63 /**
64 * This feed method needs a collection of two InputData objects. These
65 * objects' values need to be a datetime string which is turned into a Date
66 * object. Afterwards, the given dates are validated. Min and max date need
67 * to be in range of the min and max date retrieved by
68 * {@link #getDescibeData(java.lang.String)}.
69 */
70 @Override
71 public Document feed(
72 CallContext context,
73 Collection<InputData> inputData,
74 String uuid)
75 throws StateException {
76 RessourceFactory resFactory = RessourceFactory.getInstance();
77 Locale[] serverLocales = resFactory.getLocales();
78 Locale locale = context.getMeta().getPreferredLocale(
79 serverLocales);
80
81 if (inputData == null) {
82 String msg = "No input data given.";
83 log.warn(msg);
84 return feedFailure(msg);
85 }
86
87 Iterator<InputData> it = inputData.iterator();
88 InputData tmpItem = it.next();
89 InputValue inputValue = inputValues.get(tmpItem.getName());
90
91 if (inputValue == null) {
92 String msg = resFactory.getRessource(
93 locale,
94 EXCEPTION_INVALID_INPUT,
95 EXCEPTION_INVALID_INPUT);
96 log.warn(msg);
97 return feedFailure(msg);
98 }
99
100 boolean valid = InputValidator.isInputValid(
101 tmpItem.getValue(), inputValue.getType());
102
103 if (valid) {
104 String[] desc = getDescriptionForInputData(tmpItem, uuid);
105 tmpItem.setDescription(desc);
106
107 this.inputData.put(tmpItem.getName(), tmpItem);
108 return feedSuccess();
109 }
110
111 else {
112 String msg = resFactory.getRessource(
113 locale,
114 EXCEPTION_INVALID_INPUT,
115 EXCEPTION_INVALID_INPUT);
116 log.warn(msg);
117 return feedFailure(msg);
118 }
119 }
120 }
121 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org