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