comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java @ 3812:f788d2d901d6

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

http://dive4elements.wald.intevation.org