comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java @ 3786:4adc35aa655c

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

http://dive4elements.wald.intevation.org