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