comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents d9af29a4bb85
children 70e9d56e21fc
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
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 public Element describe(
199 Artifact artifact,
200 Document document,
201 Node root,
202 CallContext context,
203 String uuid)
204 {
205 ElementCreator creator = new ElementCreator(
206 document,
207 ArtifactNamespaceContext.NAMESPACE_URI,
208 ArtifactNamespaceContext.NAMESPACE_PREFIX);
209
210 String helpText = Resources.getMsg(
211 context.getMeta(), getHelpText(), getHelpText());
212
213 Element ui = null;
214 String uiprovider = getUIProvider();
215 if (uiprovider != null) {
216 ui = ProtocolUtils.createArtNode(
217 creator, "dynamic",
218 new String[] { "uiprovider", "helpText" },
219 new String[] { uiprovider, helpText });
220 }
221 else {
222 ui = ProtocolUtils.createArtNode(
223 creator, "dynamic",
224 new String[] { "helpText" },
225 new String[] { helpText });
226 }
227
228 Map<String, StateData> theData = getData();
229 if (theData == null) {
230 return ui;
231 }
232
233 Iterator<String> iter = theData.keySet().iterator();
234 FLYSArtifact flys = (FLYSArtifact) artifact;
235
236 while (iter.hasNext()) {
237 String name = iter.next();
238 StateData data = getData(flys, name);
239
240 data = data != null ? data : getData(name);
241
242 Element select = createData(creator, artifact, data, context);
243
244 if (USE_DEFAULTS) {
245 String defValue = (String) data.getValue();
246 String defDesc = null;
247
248 if (defValue != null && defValue.length() > 0) {
249 defDesc = Resources.getMsg(
250 context.getMeta(),
251 defValue,
252 defValue);
253 }
254
255 if (defValue != null && defDesc != null) {
256 creator.addAttr(select, "defaultValue", defValue, true);
257 creator.addAttr(select, "defaultLabel", defDesc, true);
258 }
259 }
260
261 appendItems(artifact, creator, name, context, select);
262 ui.appendChild(select);
263 }
264
265 return ui;
266 }
267
268
269 /**
270 * @param artifact
271 * @param creator
272 * @param name
273 * @param context
274 * @param select
275 */
276 protected void appendItems(
277 Artifact artifact,
278 ElementCreator creator,
279 String name,
280 CallContext context,
281 Element select
282 ) {
283 Element choices = ProtocolUtils.createArtNode(
284 creator, "choices", null, null);
285
286 select.appendChild(choices);
287
288 Element[] items = createItems(creator, artifact, name, context);
289 if (items != null) {
290 for (Element item: items) {
291 choices.appendChild(item);
292 }
293 }
294 }
295
296
297 /**
298 * This method creates the root node that contains the list of selectable
299 * items.
300 *
301 * @param cr The ElementCreator.
302 *
303 * @return the root node of the item list.
304 */
305 protected Element createData(
306 ElementCreator cr,
307 Artifact artifact,
308 StateData data,
309 CallContext context)
310 {
311 Element select = ProtocolUtils.createArtNode(
312 cr, "select", null, null);
313 cr.addAttr(select, "name", data.getName(), true);
314
315 Element label = ProtocolUtils.createArtNode(
316 cr, "label", null, null);
317
318 select.appendChild(label);
319
320 label.setTextContent(Resources.getMsg(
321 context.getMeta(),
322 getID(),
323 getID()));
324
325 return select;
326 }
327
328
329 /**
330 * This method creates a list of items. These items represent the amount of
331 * input data that is possible for this state.
332 *
333 * @param cr The ElementCreator.
334 * @param name The name of the amount of data.
335 *
336 * @return a list of items.
337 */
338 protected Element[] createItems(
339 ElementCreator cr,
340 Artifact artifact,
341 String name,
342 CallContext context
343 ) {
344 return null;
345 }
346
347
348 /**
349 * This method is used to create an <i>item</i> Element that contains two
350 * further elements <i>label</i> and <i>value</i>. The label and value
351 * elements both have text nodes.
352 *
353 * @param cr The ElementCreator used to build new Elements.
354 * @param obj This implementation awaits a String array with [0] = label and
355 * [1] = value.
356 *
357 * @return an Element.
358 */
359 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
360 Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
361 Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
362 Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
363
364 String[] arr = (String[]) obj;
365
366 label.setTextContent(arr[0]);
367 value.setTextContent(arr[1]);
368
369 item.appendChild(label);
370 item.appendChild(value);
371
372 return item;
373 }
374
375
376 /**
377 * This method transform a given value into a StateData object.
378 *
379 * @param flys The FLYSArtifact.
380 * @param name The name of the data object.
381 * @param val The value of the data object.
382 *
383 * @return a StateData object with <i>name</i> and <i>val</i>ue.
384 */
385 public StateData transform(
386 FLYSArtifact flys,
387 CallContext cc,
388 StateData stateData,
389 String name,
390 String val
391 ) {
392 logger.debug("Transform data ('" + name + "','" + val + "')");
393
394 stateData.setValue(val);
395
396 return stateData;
397 }
398
399
400 /**
401 * This method validates the inserted data and returns true, if everything
402 * was correct, otherwise an exception is thrown.
403 *
404 * @param artifact A reference to the owner artifact.
405 *
406 * @return true, if everything was fine.
407 */
408 public boolean validate(Artifact artifact)
409 throws IllegalArgumentException
410 {
411 return true;
412 }
413
414
415 /**
416 * Returns which UIProvider shall be used to aid user input.
417 */
418 protected String getUIProvider() {
419 return null;
420 }
421
422
423 public Object computeAdvance(
424 FLYSArtifact artifact,
425 String hash,
426 CallContext context,
427 List<Facet> facets,
428 Object old
429 ) {
430 return null;
431 }
432
433
434 public Object computeFeed(
435 FLYSArtifact artifact,
436 String hash,
437 CallContext context,
438 List<Facet> facets,
439 Object old
440 ) {
441 return null;
442 }
443
444
445 public Object computeInit(
446 FLYSArtifact artifact,
447 String hash,
448 Object context,
449 CallMeta meta,
450 List<Facet> facets)
451 {
452 return null;
453 }
454 }
455 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org