Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java @ 1190:f514894ec2fd
merged flys-artifacts/2.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:17 +0200 |
parents | 1aba1a75beb2 |
children | 17648043429f |
comparison
equal
deleted
inserted
replaced
917:b48c36076e17 | 1190:f514894ec2fd |
---|---|
1 package de.intevation.flys.artifacts.states; | |
2 | |
3 import java.text.NumberFormat; | |
4 import java.util.Iterator; | |
5 import java.util.Locale; | |
6 import java.util.Map; | |
7 import java.util.List; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 | |
11 import org.w3c.dom.Document; | |
12 import org.w3c.dom.Element; | |
13 import org.w3c.dom.Node; | |
14 | |
15 import de.intevation.artifacts.Artifact; | |
16 import de.intevation.artifacts.ArtifactNamespaceContext; | |
17 import de.intevation.artifacts.CallContext; | |
18 import de.intevation.artifacts.CallMeta; | |
19 | |
20 import de.intevation.artifacts.common.utils.XMLUtils; | |
21 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; | |
22 | |
23 import de.intevation.artifactdatabase.ProtocolUtils; | |
24 | |
25 import de.intevation.artifactdatabase.data.DefaultStateData; | |
26 import de.intevation.artifactdatabase.data.StateData; | |
27 | |
28 import de.intevation.artifactdatabase.state.AbstractState; | |
29 import de.intevation.artifactdatabase.state.Facet; | |
30 | |
31 import de.intevation.flys.artifacts.FLYSArtifact; | |
32 | |
33 import de.intevation.flys.artifacts.resources.Resources; | |
34 | |
35 | |
36 /** | |
37 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
38 */ | |
39 public abstract class DefaultState extends AbstractState { | |
40 | |
41 /** The logger that is used in this class. */ | |
42 private static Logger logger = Logger.getLogger(DefaultState.class); | |
43 | |
44 public static enum ComputeType { | |
45 FEED, ADVANCE, INIT | |
46 } | |
47 | |
48 protected StateData getData(FLYSArtifact artifact, String name) { | |
49 return artifact.getData(name); | |
50 } | |
51 | |
52 | |
53 /** | |
54 * Append to a node and return xml description relevant for gui. | |
55 */ | |
56 public Element describeStatic( | |
57 Artifact artifact, | |
58 Document document, | |
59 Node root, | |
60 CallContext context, | |
61 String uuid) | |
62 { | |
63 ElementCreator creator = new ElementCreator( | |
64 document, | |
65 ArtifactNamespaceContext.NAMESPACE_URI, | |
66 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
67 | |
68 CallMeta meta = context.getMeta(); | |
69 | |
70 String label = Resources.getMsg(meta, getID(), getID()); | |
71 Element ui = ProtocolUtils.createArtNode( | |
72 creator, "state", | |
73 new String[] { "name", "uiprovider", "label" }, | |
74 new String[] { getID(), getUIProvider(), label }); | |
75 | |
76 Map<String, StateData> theData = getData(); | |
77 if (theData == null) { | |
78 return ui; | |
79 } | |
80 | |
81 Iterator<String> iter = theData.keySet().iterator(); | |
82 FLYSArtifact flys = (FLYSArtifact) artifact; | |
83 | |
84 while (iter.hasNext()) { | |
85 String name = iter.next(); | |
86 appendStaticData(flys, context, creator, ui, name); | |
87 } | |
88 | |
89 return ui; | |
90 } | |
91 | |
92 | |
93 protected void appendStaticData( | |
94 FLYSArtifact flys, | |
95 CallContext context, | |
96 ElementCreator cr, | |
97 Element ui, | |
98 String name | |
99 ) { | |
100 StateData data = getData(flys, name); | |
101 String value = (data != null) ? (String) data.getValue() : null; | |
102 | |
103 if (value == null) { | |
104 return; | |
105 } | |
106 | |
107 logger.debug("Append element '" + name + "' (" + value + ")"); | |
108 | |
109 Element e = createStaticData(cr, context, name, value, data.getType()); | |
110 | |
111 ui.appendChild(e); | |
112 | |
113 } | |
114 | |
115 | |
116 /** | |
117 * Creates a <i>data</i> element used in the static part of the DESCRIBE | |
118 * document. | |
119 * | |
120 * @param creator The ElementCreator that is used to build new Elements. | |
121 * @param meta The CallMeta object used for i18n. | |
122 * @param name The name of the data item. | |
123 * @param value The value as string. | |
124 * | |
125 * @return an Element. | |
126 */ | |
127 protected Element createStaticData( | |
128 ElementCreator creator, | |
129 CallContext cc, | |
130 String name, | |
131 String value, | |
132 String type | |
133 ) { | |
134 CallMeta meta = cc.getMeta(); | |
135 | |
136 Element dataElement = creator.create("data"); | |
137 creator.addAttr(dataElement, "name", name, true); | |
138 creator.addAttr(dataElement, "type", type, true); | |
139 | |
140 Element itemElement = creator.create("item"); | |
141 creator.addAttr(itemElement, "value", value, true); | |
142 | |
143 String attrValue = ""; | |
144 try { | |
145 // XXX A better way to format the output would be to use the | |
146 // 'type' value of the data objects. | |
147 double doubleVal = Double.valueOf(value); | |
148 Locale l = Resources.getLocale(meta); | |
149 NumberFormat nf = NumberFormat.getInstance(l); | |
150 | |
151 attrValue = nf.format(doubleVal); | |
152 } | |
153 catch (NumberFormatException nfe) { | |
154 attrValue = Resources.getMsg(meta, value, value); | |
155 } | |
156 | |
157 creator.addAttr(itemElement, "label", attrValue, true); | |
158 dataElement.appendChild(itemElement); | |
159 | |
160 return dataElement; | |
161 } | |
162 | |
163 | |
164 public Element describe( | |
165 Artifact artifact, | |
166 Document document, | |
167 Node root, | |
168 CallContext context, | |
169 String uuid) | |
170 { | |
171 ElementCreator creator = new ElementCreator( | |
172 document, | |
173 ArtifactNamespaceContext.NAMESPACE_URI, | |
174 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
175 | |
176 Element ui = null; | |
177 String uiprovider = getUIProvider(); | |
178 if (uiprovider != null) { | |
179 ui = ProtocolUtils.createArtNode( | |
180 creator, "dynamic", | |
181 new String[] { "uiprovider" }, | |
182 new String[] { uiprovider }); | |
183 } | |
184 else { | |
185 ui = ProtocolUtils.createArtNode(creator, "dynamic", null, null); | |
186 } | |
187 | |
188 Map<String, StateData> theData = getData(); | |
189 if (theData == null) { | |
190 return ui; | |
191 } | |
192 | |
193 Iterator<String> iter = theData.keySet().iterator(); | |
194 FLYSArtifact flys = (FLYSArtifact) artifact; | |
195 | |
196 while (iter.hasNext()) { | |
197 String name = iter.next(); | |
198 StateData data = getData(flys, name); | |
199 | |
200 data = data != null ? data : getData(name); | |
201 | |
202 Element select = createData(creator, artifact, data, context); | |
203 | |
204 String defValue = (String) data.getValue(); | |
205 String defDesc = null; | |
206 | |
207 if (defValue != null && defValue.length() > 0) { | |
208 defDesc = Resources.getMsg( | |
209 context.getMeta(), | |
210 defValue, | |
211 defValue); | |
212 } | |
213 | |
214 if (defValue != null && defDesc != null) { | |
215 creator.addAttr(select, "defaultValue", defValue, true); | |
216 creator.addAttr(select, "defaultLabel", defDesc, true); | |
217 } | |
218 | |
219 Element choices = ProtocolUtils.createArtNode( | |
220 creator, "choices", null, null); | |
221 | |
222 select.appendChild(choices); | |
223 ui.appendChild(select); | |
224 | |
225 Element[] items = createItems(creator, artifact, name, context); | |
226 if (items != null) { | |
227 for (Element item: items) { | |
228 choices.appendChild(item); | |
229 } | |
230 } | |
231 } | |
232 | |
233 return ui; | |
234 } | |
235 | |
236 | |
237 /** | |
238 * This method creates the root node that contains the list of selectable | |
239 * items. | |
240 * | |
241 * @param cr The ElementCreator. | |
242 * @param name The name of the amount of data. | |
243 * | |
244 * @return the root node of the item list. | |
245 */ | |
246 protected Element createData( | |
247 ElementCreator cr, | |
248 Artifact artifact, | |
249 StateData data, | |
250 CallContext context) | |
251 { | |
252 Element select = ProtocolUtils.createArtNode( | |
253 cr, "select", null, null); | |
254 cr.addAttr(select, "name", data.getName(), true); | |
255 | |
256 Element label = ProtocolUtils.createArtNode( | |
257 cr, "label", null, null); | |
258 | |
259 select.appendChild(label); | |
260 | |
261 label.setTextContent(Resources.getMsg( | |
262 context.getMeta(), | |
263 getID(), | |
264 getID())); | |
265 | |
266 return select; | |
267 } | |
268 | |
269 | |
270 /** | |
271 * This method creates a list of items. These items represent the amount of | |
272 * input data that is possible for this state. | |
273 * | |
274 * @param cr The ElementCreator. | |
275 * @param name The name of the amount of data. | |
276 * | |
277 * @return a list of items. | |
278 */ | |
279 protected Element[] createItems( | |
280 ElementCreator cr, | |
281 Artifact artifact, | |
282 String name, | |
283 CallContext context | |
284 ) { | |
285 return null; | |
286 } | |
287 | |
288 | |
289 /** | |
290 * This method is used to create an <i>item</i> Element that contains two | |
291 * further elements <i>label</i> and <i>value</i>. The label and value | |
292 * elements both have text nodes. | |
293 * | |
294 * @param cr The ElementCreator used to build new Elements. | |
295 * @param obj This implementation awaits a String array with [0] = label and | |
296 * [1] = value. | |
297 * | |
298 * @return an Element. | |
299 */ | |
300 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { | |
301 Element item = ProtocolUtils.createArtNode(cr, "item", null, null); | |
302 Element label = ProtocolUtils.createArtNode(cr, "label", null, null); | |
303 Element value = ProtocolUtils.createArtNode(cr, "value", null, null); | |
304 | |
305 String[] arr = (String[]) obj; | |
306 | |
307 label.setTextContent(arr[0]); | |
308 value.setTextContent(arr[1]); | |
309 | |
310 item.appendChild(label); | |
311 item.appendChild(value); | |
312 | |
313 return item; | |
314 } | |
315 | |
316 | |
317 /** | |
318 * This method transform a given value into a StateData object. | |
319 * | |
320 * @param flys The FLYSArtifact. | |
321 * @param name The name of the data object. | |
322 * @param val The value of the data object. | |
323 * | |
324 * @return a StateData object with <i>name</i> and <i>val</i>ue. | |
325 */ | |
326 public StateData transform( | |
327 FLYSArtifact flys, | |
328 CallContext cc, | |
329 String name, | |
330 String val | |
331 ) { | |
332 logger.debug("Transform data ('" + name + "','" + val + "')"); | |
333 return new DefaultStateData(name, null, null, val); | |
334 } | |
335 | |
336 | |
337 /** | |
338 * This method validates the inserted data and returns true, if everything | |
339 * was correct, otherwise an exception is thrown. | |
340 * | |
341 * @param artifact A reference to the owner artifact. | |
342 * | |
343 * @return true, if everything was fine. | |
344 */ | |
345 public boolean validate(Artifact artifact) | |
346 throws IllegalArgumentException | |
347 { | |
348 return true; | |
349 } | |
350 | |
351 | |
352 protected String getUIProvider() { | |
353 return null; | |
354 } | |
355 | |
356 | |
357 public Object computeAdvance( | |
358 FLYSArtifact artifact, | |
359 String hash, | |
360 CallContext context, | |
361 List<Facet> facets, | |
362 Object old | |
363 ) { | |
364 return null; | |
365 } | |
366 | |
367 | |
368 public Object computeFeed( | |
369 FLYSArtifact artifact, | |
370 String hash, | |
371 CallContext context, | |
372 List<Facet> facets, | |
373 Object old | |
374 ) { | |
375 return null; | |
376 } | |
377 | |
378 | |
379 public Object computeInit( | |
380 FLYSArtifact artifact, | |
381 String hash, | |
382 Object context, | |
383 CallMeta meta, | |
384 List<Facet> facets) | |
385 { | |
386 return null; | |
387 } | |
388 } | |
389 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |