comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxDateState.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 499cfbbb61bc
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.state;
2
3 import java.util.Collection;
4 import java.util.Date;
5 import java.util.GregorianCalendar;
6 import java.util.Iterator;
7 import java.util.Locale;
8
9 import org.apache.log4j.Logger;
10 import org.w3c.dom.Document;
11
12 import de.intevation.artifacts.CallContext;
13 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
14 import de.intevation.gnv.geobackend.util.DateUtils;
15 import de.intevation.gnv.state.describedata.DefaultMinMaxDescribeData;
16 import de.intevation.gnv.state.describedata.DescribeData;
17 import de.intevation.gnv.state.describedata.MinMaxDescribeData;
18 import de.intevation.gnv.state.exception.StateException;
19 import de.intevation.gnv.utils.InputValidator;
20
21 /**
22 * This state handles date input. The resulting describe document of this state
23 * contains two fields in the user interface description to define a time range.
24 *
25 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
26 */
27 public class MinMaxDateState extends MinMaxState {
28
29
30 /**
31 *
32 */
33 public static final String EXCEPTION_DATE_REQUIRED =
34 "input.is.not.valid.date.required";
35
36 /**
37 *
38 */
39 public static final String EXCEPTION_START_AFTER_END =
40 "start.date.after.end.date";
41
42 /**
43 *
44 */
45 public static final String EXCEPTION_DATE_OUT_OF_RANGE =
46 "date.out.of.range";
47
48 /**
49 *
50 */
51 public static final String EXCEPTION_MISSING_DATE =
52 "missing.data.field";
53
54 private static Logger logger = Logger.getLogger(MinMaxDateState.class);
55
56
57 /**
58 *
59 */
60 public MinMaxDateState() {
61 super();
62 }
63
64
65 /**
66 * This feed method needs a collection of two InputData objects. These
67 * objects' values need to be a datetime string which is turned into a Date
68 * object. Afterwards, the given dates are validated. Min and max date need
69 * to be in range of the min and max date retrieved by
70 * {@link #getDescibeData(java.lang.String)}.
71 */
72 @Override
73 public Document feed(
74 CallContext context,
75 Collection<InputData> inputData,
76 String uuid)
77 throws StateException {
78 RessourceFactory resFactory = RessourceFactory.getInstance();
79 Locale[] serverLocales = resFactory.getLocales();
80 Locale locale = context.getMeta().getPreferredLocale(
81 serverLocales);
82
83 if (inputData == null) {
84 String msg = "No input data given.";
85 logger.warn(msg);
86 return feedFailure(msg);
87 }
88
89 Iterator iter = inputData.iterator();
90
91 MinMaxDescribeData data =
92 (MinMaxDescribeData) getDescibeData(uuid).get(0);
93 Object min = data.getMinValue();
94 Object max = data.getMaxValue();
95
96 Object tmpMin = null;
97 Object tmpMax = null;
98
99 while (iter.hasNext()) {
100 InputData tmp = (InputData) iter.next();
101 InputValue meta = inputValues.get(tmp.getName());
102 String type = meta.getType();
103 String value = tmp.getValue();
104 String name = tmp.getName();
105
106 if (meta == null) {
107 String msg = "Input data not expected here. Data will be ignored.";
108 logger.warn(msg);
109 return feedFailure(msg);
110 }
111
112 if (!InputValidator.isInputValid(value, type)) {
113 String msg = resFactory.getRessource(
114 locale, EXCEPTION_DATE_REQUIRED, EXCEPTION_DATE_REQUIRED);
115 logger.error(msg);
116 return feedFailure(msg);
117 }
118
119 Date lower = null;
120 if (min instanceof GregorianCalendar) {
121 lower = ((GregorianCalendar)min).getTime();
122 }
123
124 Date upper = null;
125 if (max instanceof GregorianCalendar) {
126 upper = ((GregorianCalendar)max).getTime();
127 }
128
129 Date d = null;
130 try {
131 d = DateUtils.getDateFromString(value,DateUtils.DATE_PATTERN);
132 }
133 catch (Exception e) {
134 logger.warn(e, e);
135 }
136
137 if (d == null || lower == null || upper == null) {
138 String msg = resFactory.getRessource(
139 locale,
140 EXCEPTION_MISSING_DATE,
141 EXCEPTION_MISSING_DATE);
142 logger.warn(msg);
143 }
144 else {
145 if (logger.isDebugEnabled()) {
146 logger.debug("Date to validate: " + d.toString());
147 logger.debug("Lower date bound: " + lower.toString());
148 logger.debug("Upper date bound: " + upper.toString());
149 }
150
151 if (!InputValidator.isDateValid(d, lower, upper)) {
152 String msg = resFactory.getRessource(
153 locale,
154 EXCEPTION_DATE_OUT_OF_RANGE,
155 EXCEPTION_DATE_OUT_OF_RANGE);
156 logger.error(msg);
157 return feedFailure(msg);
158 }
159 }
160
161 if (name.equals(MINVALUEFIELDNAME)) {
162 tmpMin = value;
163 }
164
165 if (name.equals(MAXVALUEFIELDNAME)) {
166 tmpMax = value;
167 }
168
169 if (tmpMin != null && tmpMax != null) {
170 if (!InputValidator.isInputValid((String) tmpMin, (String) tmpMax, type)) {
171 String msg = resFactory.getRessource(
172 locale,
173 EXCEPTION_START_AFTER_END,
174 EXCEPTION_START_AFTER_END);
175 logger.error(msg);
176 return feedFailure(msg);
177 }
178 }
179 }
180
181 DescribeData values = new DefaultMinMaxDescribeData(
182 dataName, tmpMin, tmpMax, getID());
183
184 this.inputData.put(dataName, new DefaultInputData(dataName, values));
185
186 return feedSuccess();
187 }
188 }
189 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org