comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java @ 2424:092e519ff461

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

http://dive4elements.wald.intevation.org