comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxState.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents dec4257ad570
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.gnv.state;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Date;
14 import java.util.GregorianCalendar;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Locale;
18
19 import org.apache.log4j.Logger;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23
24 import de.intevation.artifacts.common.utils.XMLUtils;
25 import de.intevation.artifacts.CallContext;
26 import de.intevation.artifacts.CallMeta;
27 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
28 import de.intevation.gnv.geobackend.base.Result;
29 import de.intevation.gnv.geobackend.util.DateUtils;
30 import de.intevation.gnv.state.describedata.DefaultMinMaxDescribeData;
31 import de.intevation.gnv.state.describedata.DescribeData;
32 import de.intevation.gnv.state.describedata.MinMaxDescribeData;
33 import de.intevation.gnv.state.exception.StateException;
34 import de.intevation.gnv.utils.InputValidator;
35
36 /**
37 * This state handles input of a min and max value and validates the user input.
38 * The min value needs to be equal or smaller than the max value, otherwise the
39 * input results in an error.
40 *
41 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
42 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
43 */
44 public class MinMaxState extends StateBase {
45
46 /**
47 * the logger, used to log exceptions and additonaly information
48 */
49 private static Logger log = Logger.getLogger(MinMaxState.class);
50
51 /**
52 * Key to lookup the localized exceptionmessage in the ResourceFiles.
53 */
54 public static final String EXCEPTION_INVALID_MIN_MAX_INPUT =
55 "input.is.not.valid.minmax";
56
57 /**
58 * Constructor
59 */
60 public MinMaxState() {
61 super();
62 }
63
64 /**
65 * The objects returned by the database are searched for two fields with
66 * 'MIN' and 'MAX' as names. These objects are stored and used to be
67 * displayed in the gui to give the user an orientation of the range he is
68 * able to insert.
69 */
70 @Override
71 protected List<Object> purifyResult(
72 Collection<Result> result, String uuid)
73 {
74 List<Object> describeData = new ArrayList<Object>();
75
76 if (result != null && result.size() == 1) {
77 Result value = result.iterator().next();
78 DescribeData values = new DefaultMinMaxDescribeData(
79 dataName,
80 value.getObject("MIN"),
81 value.getObject("MAX"),
82 getID());
83 describeData.add(values);
84 } else {
85 log.warn("Result cannot be handled as MinMax Resultset");
86 }
87
88 return describeData;
89 }
90
91
92 @Override
93 public Document feed(
94 CallContext context,
95 Collection<InputData> inputData,
96 String uuid)
97 throws StateException {
98 RessourceFactory resFactory = RessourceFactory.getInstance();
99
100 if (inputData == null) {
101 String msg = "No input data given.";
102 log.warn(msg);
103 return feedFailure(msg);
104 }
105
106 InputValidator iv = new InputValidator();
107 Iterator iter = inputData.iterator();
108
109 Object min = null;
110 Object max = null;
111
112 while (iter.hasNext()) {
113 InputData tmp = (InputData) iter.next();
114 InputValue meta = inputValues.get(tmp.getName());
115 String type = meta.getType();
116 String value = tmp.getValue();
117 String name = tmp.getName();
118
119 if (meta == null) {
120 String msg = "Input data not expected here. Data will be ignored.";
121 log.warn(msg);
122 return feedFailure(msg);
123 }
124
125 boolean valid = InputValidator.isInputValid(value, type);
126 if (!valid) {
127 String msg = "Input is not valid for this state.";
128 log.error(msg);
129 return feedFailure(msg);
130 }
131
132 if (name.equals(MINVALUEFIELDNAME)) {
133 min = value;
134 }
135
136 if (name.equals(MAXVALUEFIELDNAME)) {
137 max = value;
138 }
139
140 if (min != null && max != null) {
141 if (!InputValidator.isInputValid((String) min,
142 (String) max,
143 type)) {
144 Locale[] serverLocales = resFactory.getLocales();
145 Locale locale = context.getMeta()
146 .getPreferredLocale(
147 serverLocales);
148 String msg = resFactory.getRessource(locale,
149 EXCEPTION_INVALID_MIN_MAX_INPUT,
150 EXCEPTION_INVALID_MIN_MAX_INPUT);
151 log.error(msg);
152 return feedFailure(msg);
153 }
154 }
155 }
156
157
158
159 DescribeData values = new DefaultMinMaxDescribeData(
160 dataName, min, max, getID());
161
162 this.inputData.put(dataName, new DefaultInputData(dataName, values));
163
164 return feedSuccess();
165 }
166
167
168 @Override
169 protected void appendToStaticNode(
170 XMLUtils.ElementCreator artCreator,
171 XMLUtils.ElementCreator creator,
172 Document document,
173 Node staticNode,
174 CallMeta callMeta
175 ) {
176 InputData data = inputData.get(dataName);
177
178 if (data == null) {
179 return;
180 }
181
182 MinMaxDescribeData minMax = (MinMaxDescribeData) data.getObject();
183
184 Object min = minMax.getMinValue();
185 Object max = minMax.getMaxValue();
186 if (min instanceof GregorianCalendar) {
187 Date d = ((GregorianCalendar) min).getTime();
188 min = DateUtils.getPatternedDateAmer(d);
189 }
190
191 if (max instanceof GregorianCalendar) {
192 Date d = ((GregorianCalendar) max).getTime();
193 max = DateUtils.getPatternedDateAmer(d);
194 }
195
196 Element groupNode = creator.create("group");
197 artCreator.addAttr(groupNode, "state", minMax.getState(), true);
198
199 creator.addAttr(groupNode, "ref", minMax.getName());
200 Element groupNodeLableNode = creator.create("label");
201 groupNodeLableNode.setTextContent(RessourceFactory
202 .getInstance().getRessource(
203 callMeta.getLanguages(),
204 minMax.getName(),
205 minMax.getName()));
206 groupNode.appendChild(groupNodeLableNode);
207
208 Element inputMinNode = creator.create("input");
209 creator.addAttr(inputMinNode, "ref", MINVALUEFIELDNAME);
210 Element inputMinLableNode = creator.create("label");
211 inputMinLableNode.setTextContent(RessourceFactory
212 .getInstance().getRessource(
213 callMeta.getLanguages(), MINVALUEFIELDNAME,
214 MINVALUEFIELDNAME));
215 inputMinNode.appendChild(inputMinLableNode);
216
217 Element inputMinValueNode = creator.create("value");
218 inputMinValueNode.setTextContent(min.toString());
219 inputMinNode.appendChild(inputMinValueNode);
220
221 Element inputMaxNode = creator.create("input");
222 creator.addAttr(inputMaxNode, "ref", MAXVALUEFIELDNAME);
223 Element inputMaxLableNode = creator.create("label");
224 inputMaxLableNode.setTextContent(RessourceFactory
225 .getInstance().getRessource(
226 callMeta.getLanguages(), MAXVALUEFIELDNAME,
227 MAXVALUEFIELDNAME));
228 inputMaxNode.appendChild(inputMaxLableNode);
229
230 Element inputMaxValueNode = creator.create("value");
231 inputMaxValueNode.setTextContent(max.toString());
232 inputMaxNode.appendChild(inputMaxValueNode);
233
234 groupNode.appendChild(inputMinNode);
235 groupNode.appendChild(inputMaxNode);
236
237 staticNode.appendChild(groupNode);
238 }
239 }
240 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org