Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java @ 430:7ab81ff32111 2.3
merged flys-artifacts/2.3
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:10 +0200 |
parents | c21fb8de54f8 |
children | 929137ee8154 |
comparison
equal
deleted
inserted
replaced
290:a6f56ed9238b | 430:7ab81ff32111 |
---|---|
1 package de.intevation.flys.artifacts.states; | |
2 | |
3 import java.util.Collection; | |
4 import java.util.Map; | |
5 | |
6 import org.apache.log4j.Logger; | |
7 | |
8 import org.w3c.dom.Document; | |
9 import org.w3c.dom.Element; | |
10 import org.w3c.dom.Node; | |
11 | |
12 import de.intevation.artifacts.Artifact; | |
13 import de.intevation.artifacts.ArtifactNamespaceContext; | |
14 import de.intevation.artifacts.CallContext; | |
15 import de.intevation.artifacts.CallMeta; | |
16 | |
17 import de.intevation.artifacts.common.utils.XMLUtils; | |
18 | |
19 import de.intevation.artifactdatabase.ProtocolUtils; | |
20 import de.intevation.artifactdatabase.data.StateData; | |
21 import de.intevation.artifactdatabase.state.AbstractState; | |
22 | |
23 import de.intevation.flys.artifacts.resources.Resources; | |
24 | |
25 | |
26 /** | |
27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
28 */ | |
29 public abstract class DefaultState extends AbstractState { | |
30 | |
31 /** The logger that is used in this class.*/ | |
32 private static Logger logger = Logger.getLogger(DefaultState.class); | |
33 | |
34 | |
35 public Element describeStatic( | |
36 Document document, | |
37 Node root, | |
38 CallContext context, | |
39 String uuid) | |
40 { | |
41 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
42 document, | |
43 ArtifactNamespaceContext.NAMESPACE_URI, | |
44 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
45 | |
46 CallMeta meta = context.getMeta(); | |
47 | |
48 String label = Resources.getMsg(meta, getID(), getID()); | |
49 Element ui = ProtocolUtils.createArtNode( | |
50 creator, "state", | |
51 new String[] { "name", "uiprovider", "label" }, | |
52 new String[] { getID(), getUIProvider(), label }); | |
53 | |
54 Map<String, StateData> theData = getData(); | |
55 if (theData == null) { | |
56 return ui; | |
57 } | |
58 | |
59 Collection<StateData> dataItems = theData.values(); | |
60 | |
61 for (StateData data: dataItems) { | |
62 String name = data.getName(); | |
63 String value = (String) data.getValue(); | |
64 | |
65 if (value == null) { | |
66 continue; | |
67 } | |
68 | |
69 logger.debug("Append element '" + name + "' (" + value + ")"); | |
70 | |
71 Element dataElement = creator.create("data"); | |
72 creator.addAttr(dataElement, "name", name, true); | |
73 creator.addAttr(dataElement, "type", data.getType(), true); | |
74 | |
75 Element itemElement = creator.create("item"); | |
76 creator.addAttr(itemElement, "value", value, true); | |
77 creator.addAttr( | |
78 itemElement, | |
79 "label", | |
80 Resources.getMsg(meta, value, value), | |
81 true); | |
82 | |
83 dataElement.appendChild(itemElement); | |
84 ui.appendChild(dataElement); | |
85 } | |
86 | |
87 return ui; | |
88 } | |
89 | |
90 | |
91 public Element describe( | |
92 Artifact artifact, | |
93 Document document, | |
94 Node root, | |
95 CallContext context, | |
96 String uuid) | |
97 { | |
98 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
99 document, | |
100 ArtifactNamespaceContext.NAMESPACE_URI, | |
101 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
102 | |
103 Element ui = null; | |
104 String uiprovider = getUIProvider(); | |
105 if (uiprovider != null) { | |
106 ui = ProtocolUtils.createArtNode( | |
107 creator, "dynamic", | |
108 new String[] { "uiprovider" }, | |
109 new String[] { uiprovider }); | |
110 } | |
111 else { | |
112 ui = ProtocolUtils.createArtNode(creator, "dynamic", null, null); | |
113 } | |
114 | |
115 Map<String, StateData> theData = getData(); | |
116 if (theData == null) { | |
117 return ui; | |
118 } | |
119 | |
120 Collection<StateData> dataItems = theData.values(); | |
121 | |
122 for (StateData data: dataItems) { | |
123 String name = data.getName(); | |
124 Element select = createData(creator, artifact, data, context); | |
125 | |
126 Element choices = ProtocolUtils.createArtNode( | |
127 creator, "choices", null, null); | |
128 | |
129 select.appendChild(choices); | |
130 ui.appendChild(select); | |
131 | |
132 Element[] items = createItems(creator, artifact, name, context); | |
133 for (Element item: items) { | |
134 choices.appendChild(item); | |
135 } | |
136 } | |
137 | |
138 return ui; | |
139 } | |
140 | |
141 | |
142 /** | |
143 * This method creates the root node that contains the list of selectable | |
144 * items. | |
145 * | |
146 * @param cr The ElementCreator. | |
147 * @param name The name of the amount of data. | |
148 * | |
149 * @return the root node of the item list. | |
150 */ | |
151 protected Element createData( | |
152 XMLUtils.ElementCreator cr, | |
153 Artifact artifact, | |
154 StateData data, | |
155 CallContext context) | |
156 { | |
157 Element select = ProtocolUtils.createArtNode( | |
158 cr, "select", null, null); | |
159 cr.addAttr(select, "name", data.getName(), true); | |
160 | |
161 Element label = ProtocolUtils.createArtNode( | |
162 cr, "label", null, null); | |
163 | |
164 select.appendChild(label); | |
165 | |
166 label.setTextContent(Resources.getMsg( | |
167 context.getMeta(), | |
168 getID(), | |
169 getID())); | |
170 | |
171 return select; | |
172 } | |
173 | |
174 | |
175 /** | |
176 * This method creates a list of items. These items represent the amount of | |
177 * input data that is possible for this state. | |
178 * | |
179 * @param cr The ElementCreator. | |
180 * @param name The name of the amount of data. | |
181 * | |
182 * @return a list of items. | |
183 */ | |
184 protected abstract Element[] createItems( | |
185 XMLUtils.ElementCreator cr, | |
186 Artifact artifact, | |
187 String name, | |
188 CallContext context); | |
189 | |
190 | |
191 /** | |
192 * This method validates the inserted data and returns true, if everything | |
193 * was correct, otherwise an exception is thrown. | |
194 * | |
195 * @param artifact A reference to the owner artifact. | |
196 * @param context The CallContext object. | |
197 * | |
198 * @return true, if everything was fine. | |
199 */ | |
200 public boolean validate(Artifact artifact, CallContext context) | |
201 throws IllegalArgumentException | |
202 { | |
203 return true; | |
204 } | |
205 | |
206 | |
207 protected String getUIProvider() { | |
208 return null; | |
209 } | |
210 } | |
211 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |