Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 (2012-09-28) |
parents | 1bb6fb621167 |
children | fe29b0226faf |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
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.StateData; | |
26 | |
27 import de.intevation.artifactdatabase.state.AbstractState; | |
28 import de.intevation.artifactdatabase.state.Facet; | |
29 | |
30 import de.intevation.flys.artifacts.FLYSArtifact; | |
31 | |
32 import de.intevation.flys.artifacts.resources.Resources; | |
33 | |
34 | |
35 /** | |
36 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
37 */ | |
38 public abstract class DefaultState extends AbstractState { | |
39 | |
40 /** The logger that is used in this class. */ | |
41 private static Logger logger = Logger.getLogger(DefaultState.class); | |
42 | |
43 | |
44 /** Determines, if the DESCRIBE document should contain default values or | |
45 * not. */ | |
46 public static final boolean USE_DEFAULTS = | |
47 Boolean.getBoolean("flys.use.default.values"); | |
48 | |
49 /** The three possible compute types. */ | |
50 public static enum ComputeType { | |
51 FEED, ADVANCE, INIT | |
52 } | |
53 | |
54 | |
55 protected StateData getData(FLYSArtifact artifact, String name) { | |
56 return artifact.getData(name); | |
57 } | |
58 | |
59 | |
60 /** | |
61 * Append to a node and return xml description relevant for gui. | |
62 */ | |
63 public Element describeStatic( | |
64 Artifact artifact, | |
65 Document document, | |
66 Node root, | |
67 CallContext context, | |
68 String uuid) | |
69 { | |
70 ElementCreator creator = new ElementCreator( | |
71 document, | |
72 ArtifactNamespaceContext.NAMESPACE_URI, | |
73 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
74 | |
75 CallMeta meta = context.getMeta(); | |
76 | |
77 String helpText = Resources.getMsg(meta, getHelpText(), getHelpText()); | |
78 | |
79 String label = Resources.getMsg(meta, getID(), getID()); | |
80 Element ui = ProtocolUtils.createArtNode( | |
81 creator, "state", | |
82 new String[] { "name", "uiprovider", "label", "helpText"}, | |
83 new String[] { getID(), getUIProvider(), label, helpText }); | |
84 | |
85 Map<String, StateData> theData = getData(); | |
86 if (theData == null) { | |
87 return ui; | |
88 } | |
89 | |
90 Iterator<String> iter = theData.keySet().iterator(); | |
91 FLYSArtifact flys = (FLYSArtifact) artifact; | |
92 | |
93 while (iter.hasNext()) { | |
94 String name = iter.next(); | |
95 appendStaticData(flys, context, creator, ui, name); | |
96 } | |
97 | |
98 return ui; | |
99 } | |
100 | |
101 | |
102 protected void appendStaticData( | |
103 FLYSArtifact flys, | |
104 CallContext context, | |
105 ElementCreator cr, | |
106 Element ui, | |
107 String name | |
108 ) { | |
109 StateData data = getData(flys, name); | |
110 String value = (data != null) ? (String) data.getValue() : null; | |
111 | |
112 if (value == null) { | |
113 return; | |
114 } | |
115 | |
116 String type = data.getType(); | |
117 | |
118 logger.debug( | |
119 "Append element " + type + "'" + | |
120 name + "' (" + value + ")"); | |
121 | |
122 Element e = createStaticData(flys, cr, context, name, value, type); | |
123 | |
124 ui.appendChild(e); | |
125 | |
126 } | |
127 | |
128 | |
129 /** | |
130 * Creates a <i>data</i> element used in the static part of the DESCRIBE | |
131 * document. | |
132 * | |
133 * @param creator The ElementCreator that is used to build new Elements. | |
134 * @param cc The CallContext object used for nested i18n retrieval. | |
135 * @param name The name of the data item. | |
136 * @param value The value as string. | |
137 * | |
138 * @return an Element. | |
139 */ | |
140 protected Element createStaticData( | |
141 FLYSArtifact flys, | |
142 ElementCreator creator, | |
143 CallContext cc, | |
144 String name, | |
145 String value, | |
146 String type | |
147 ) { | |
148 Element dataElement = creator.create("data"); | |
149 creator.addAttr(dataElement, "name", name, true); | |
150 creator.addAttr(dataElement, "type", type, true); | |
151 | |
152 Element itemElement = creator.create("item"); | |
153 creator.addAttr(itemElement, "value", value, true); | |
154 | |
155 creator.addAttr( | |
156 itemElement, | |
157 "label", | |
158 getLabelFor(cc, name, value, type), | |
159 true); | |
160 | |
161 dataElement.appendChild(itemElement); | |
162 | |
163 return dataElement; | |
164 } | |
165 | |
166 | |
167 /** | |
168 * @param cc | |
169 * @param name | |
170 * @param value | |
171 * @param type | |
172 * | |
173 * @return | |
174 */ | |
175 protected String getLabelFor( | |
176 CallContext cc, | |
177 String name, | |
178 String value, | |
179 String type | |
180 ) { | |
181 CallMeta meta = cc.getMeta(); | |
182 | |
183 try { | |
184 // XXX A better way to format the output would be to use the | |
185 // 'type' value of the data objects. | |
186 double doubleVal = Double.valueOf(value); | |
187 Locale l = Resources.getLocale(meta); | |
188 NumberFormat nf = NumberFormat.getInstance(l); | |
189 | |
190 return nf.format(doubleVal); | |
191 } | |
192 catch (NumberFormatException nfe) { | |
193 return Resources.getMsg(meta, value, value); | |
194 } | |
195 } | |
196 | |
197 | |
198 /** | |
199 * This method returns the default value and description for <i>data</i>. | |
200 * Note, that this method returns the defaults only if <i>USE_DEFAULTS</i> | |
201 * is set; otherwise, null is returned. | |
202 * @param context The CallContext used for i18n. | |
203 * @param data The data objects that the defaults are for. | |
204 * @return a String[] with [default value, default label]. | |
205 */ | |
206 protected String[] getDefaultsFor(CallContext context, StateData data) { | |
207 if (USE_DEFAULTS) { | |
208 String defValue = (String) data.getValue(); | |
209 String defDesc = null; | |
210 | |
211 if (defValue != null && defValue.length() > 0) { | |
212 defDesc = Resources.getMsg( | |
213 context.getMeta(), | |
214 defValue, | |
215 defValue); | |
216 } | |
217 | |
218 if (defValue != null && defDesc != null) { | |
219 return new String[] { defValue, defDesc }; | |
220 } | |
221 } | |
222 | |
223 return null; | |
224 } | |
225 | |
226 | |
227 public Element describe( | |
228 Artifact artifact, | |
229 Document document, | |
230 Node root, | |
231 CallContext context, | |
232 String uuid) | |
233 { | |
234 ElementCreator creator = new ElementCreator( | |
235 document, | |
236 ArtifactNamespaceContext.NAMESPACE_URI, | |
237 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
238 | |
239 String helpText = Resources.getMsg( | |
240 context.getMeta(), getHelpText(), getHelpText()); | |
241 | |
242 Element ui = null; | |
243 String uiprovider = getUIProvider(); | |
244 if (uiprovider != null) { | |
245 ui = ProtocolUtils.createArtNode( | |
246 creator, "dynamic", | |
247 new String[] { "uiprovider", "helpText" }, | |
248 new String[] { uiprovider, helpText }); | |
249 } | |
250 else { | |
251 ui = ProtocolUtils.createArtNode( | |
252 creator, "dynamic", | |
253 new String[] { "helpText" }, | |
254 new String[] { helpText }); | |
255 } | |
256 | |
257 Map<String, StateData> theData = getData(); | |
258 if (theData == null) { | |
259 return ui; | |
260 } | |
261 | |
262 Iterator<String> iter = theData.keySet().iterator(); | |
263 FLYSArtifact flys = (FLYSArtifact) artifact; | |
264 | |
265 while (iter.hasNext()) { | |
266 String name = iter.next(); | |
267 StateData data = getData(flys, name); | |
268 | |
269 data = data != null ? data : getData(name); | |
270 | |
271 Element select = createData(creator, artifact, data, context); | |
272 | |
273 String[] defaults = getDefaultsFor(context, data); | |
274 if (defaults != null && defaults.length > 1) { | |
275 creator.addAttr(select, "defaultValue", defaults[0], true); | |
276 creator.addAttr(select, "defaultLabel", defaults[1], true); | |
277 } | |
278 | |
279 appendItems(artifact, creator, name, context, select); | |
280 ui.appendChild(select); | |
281 } | |
282 | |
283 return ui; | |
284 } | |
285 | |
286 | |
287 /** | |
288 * @param artifact | |
289 * @param creator | |
290 * @param name | |
291 * @param context | |
292 * @param select | |
293 */ | |
294 protected void appendItems( | |
295 Artifact artifact, | |
296 ElementCreator creator, | |
297 String name, | |
298 CallContext context, | |
299 Element select | |
300 ) { | |
301 Element choices = ProtocolUtils.createArtNode( | |
302 creator, "choices", null, null); | |
303 | |
304 select.appendChild(choices); | |
305 | |
306 Element[] items = createItems(creator, artifact, name, context); | |
307 if (items != null) { | |
308 for (Element item: items) { | |
309 choices.appendChild(item); | |
310 } | |
311 } | |
312 } | |
313 | |
314 | |
315 /** | |
316 * This method creates the root node that contains the list of selectable | |
317 * items. | |
318 * | |
319 * @param cr The ElementCreator. | |
320 * | |
321 * @return the root node of the item list. | |
322 */ | |
323 protected Element createData( | |
324 ElementCreator cr, | |
325 Artifact artifact, | |
326 StateData data, | |
327 CallContext context) | |
328 { | |
329 Element select = ProtocolUtils.createArtNode( | |
330 cr, "select", null, null); | |
331 cr.addAttr(select, "name", data.getName(), true); | |
332 | |
333 Element label = ProtocolUtils.createArtNode( | |
334 cr, "label", null, null); | |
335 | |
336 select.appendChild(label); | |
337 | |
338 label.setTextContent(Resources.getMsg( | |
339 context.getMeta(), | |
340 getID(), | |
341 getID())); | |
342 | |
343 return select; | |
344 } | |
345 | |
346 | |
347 /** | |
348 * This method creates a list of items. These items represent the amount of | |
349 * input data that is possible for this state. | |
350 * | |
351 * @param cr The ElementCreator. | |
352 * @param name The name of the amount of data. | |
353 * | |
354 * @return a list of items. | |
355 */ | |
356 protected Element[] createItems( | |
357 ElementCreator cr, | |
358 Artifact artifact, | |
359 String name, | |
360 CallContext context | |
361 ) { | |
362 return null; | |
363 } | |
364 | |
365 | |
366 /** | |
367 * This method is used to create an <i>item</i> Element that contains two | |
368 * further elements <i>label</i> and <i>value</i>. The label and value | |
369 * elements both have text nodes. | |
370 * | |
371 * @param cr The ElementCreator used to build new Elements. | |
372 * @param obj This implementation awaits a String array with [0] = label and | |
373 * [1] = value. | |
374 * | |
375 * @return an Element. | |
376 */ | |
377 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { | |
378 Element item = ProtocolUtils.createArtNode(cr, "item", null, null); | |
379 Element label = ProtocolUtils.createArtNode(cr, "label", null, null); | |
380 Element value = ProtocolUtils.createArtNode(cr, "value", null, null); | |
381 | |
382 String[] arr = (String[]) obj; | |
383 | |
384 label.setTextContent(arr[0]); | |
385 value.setTextContent(arr[1]); | |
386 | |
387 item.appendChild(label); | |
388 item.appendChild(value); | |
389 | |
390 return item; | |
391 } | |
392 | |
393 | |
394 /** | |
395 * This method transform a given value into a StateData object. | |
396 * | |
397 * @param flys The FLYSArtifact. | |
398 * @param name The name of the data object. | |
399 * @param val The value of the data object. | |
400 * | |
401 * @return a StateData object with <i>name</i> and <i>val</i>ue. | |
402 */ | |
403 public StateData transform( | |
404 FLYSArtifact flys, | |
405 CallContext cc, | |
406 StateData stateData, | |
407 String name, | |
408 String val | |
409 ) { | |
410 logger.debug("Transform data ('" + name + "','" + val + "')"); | |
411 | |
412 stateData.setValue(val); | |
413 | |
414 return stateData; | |
415 } | |
416 | |
417 | |
418 /** | |
419 * This method validates the inserted data and returns true, if everything | |
420 * was correct, otherwise an exception is thrown. | |
421 * | |
422 * @param artifact A reference to the owner artifact. | |
423 * | |
424 * @return true, if everything was fine. | |
425 */ | |
426 public boolean validate(Artifact artifact) | |
427 throws IllegalArgumentException | |
428 { | |
429 return true; | |
430 } | |
431 | |
432 | |
433 /** | |
434 * Returns which UIProvider shall be used to aid user input. | |
435 */ | |
436 protected String getUIProvider() { | |
437 return null; | |
438 } | |
439 | |
440 | |
441 public Object computeAdvance( | |
442 FLYSArtifact artifact, | |
443 String hash, | |
444 CallContext context, | |
445 List<Facet> facets, | |
446 Object old | |
447 ) { | |
448 return null; | |
449 } | |
450 | |
451 | |
452 public Object computeFeed( | |
453 FLYSArtifact artifact, | |
454 String hash, | |
455 CallContext context, | |
456 List<Facet> facets, | |
457 Object old | |
458 ) { | |
459 return null; | |
460 } | |
461 | |
462 | |
463 public Object computeInit( | |
464 FLYSArtifact artifact, | |
465 String hash, | |
466 Object context, | |
467 CallMeta meta, | |
468 List<Facet> facets) | |
469 { | |
470 return null; | |
471 } | |
472 } | |
473 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |