Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/fis/SelectProductArtifact.java @ 475:c0504976e606
Renamed FISSelectArtifact to SelectProductArtifact which fits better, because the intent of this artifact is to choose a product - not a fis.
gnv-artifacts/trunk@542 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 14 Jan 2010 11:07:27 +0000 |
parents | |
children | 0e0c64c821dc |
comparison
equal
deleted
inserted
replaced
474:ab29e4ff2fda | 475:c0504976e606 |
---|---|
1 package de.intevation.gnv.artifacts.fis; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.OutputStream; | |
5 import java.util.ArrayList; | |
6 import java.util.Collection; | |
7 import java.util.HashMap; | |
8 import java.util.Iterator; | |
9 import javax.xml.xpath.XPathConstants; | |
10 | |
11 import org.apache.log4j.Logger; | |
12 import org.w3c.dom.Document; | |
13 import org.w3c.dom.Element; | |
14 import org.w3c.dom.Node; | |
15 import org.w3c.dom.NodeList; | |
16 | |
17 import de.intevation.artifactdatabase.DefaultArtifact; | |
18 import de.intevation.artifactdatabase.ProxyArtifact; | |
19 import de.intevation.artifactdatabase.XMLUtils; | |
20 import de.intevation.artifacts.Artifact; | |
21 import de.intevation.artifacts.ArtifactFactory; | |
22 import de.intevation.artifacts.ArtifactNamespaceContext; | |
23 import de.intevation.artifacts.CallContext; | |
24 import de.intevation.artifacts.CallMeta; | |
25 import de.intevation.gnv.artifacts.GNVArtifactBase; | |
26 import de.intevation.gnv.artifacts.context.GNVArtifactContext; | |
27 import de.intevation.gnv.artifacts.fis.product.DefaultProduct; | |
28 import de.intevation.gnv.artifacts.fis.product.Product; | |
29 import de.intevation.gnv.artifacts.ressource.RessourceFactory; | |
30 import de.intevation.gnv.state.DefaultInputData; | |
31 import de.intevation.gnv.state.InputData; | |
32 import de.intevation.gnv.utils.ArtifactFactoryUtilities; | |
33 | |
34 | |
35 /** | |
36 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> | |
37 */ | |
38 public class SelectProductArtifact extends DefaultArtifact { | |
39 | |
40 public static final String XPATH_IDENTIFIER_REPLACE = "IDENTIFIER"; | |
41 | |
42 public static final String XPATH_ARTIFACT_CONFIGURATION = | |
43 "/artifact-database/artifacts/artifact[@name='" | |
44 + XPATH_IDENTIFIER_REPLACE + "']"; | |
45 | |
46 public static final String XPATH_UUID = "art:action/art:uuid/@value"; | |
47 | |
48 public static final String XPATH_HASH = "art:action/art:hash/@value"; | |
49 | |
50 public static final String XPATH_INPUT_DATA_VALUE = | |
51 "art:action/art:data/art:input[@name='product']/@value"; | |
52 | |
53 /** | |
54 * this xpath is related to the config.xml document which doesn't have any | |
55 * prefixes yet | |
56 */ | |
57 public static final String XPATH_PRODUCTS = "products/product"; | |
58 | |
59 /** | |
60 * this xpath is related to the config.xml document which doesn't have any | |
61 * prefixes yet | |
62 */ | |
63 public static final String XPATH_PRODUCT_PARAMETER = "parameters/parameter"; | |
64 | |
65 public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; | |
66 public static final String XFORM_PREFIX = "xform"; | |
67 | |
68 private static Logger log = Logger.getLogger(SelectProductArtifact.class); | |
69 | |
70 private HashMap products; | |
71 private Product current; | |
72 private Artifact artifact; | |
73 private String name; | |
74 | |
75 public SelectProductArtifact() { | |
76 super(); | |
77 } | |
78 | |
79 | |
80 @Override | |
81 public void setup( | |
82 String identifier, | |
83 ArtifactFactory factory, | |
84 Object context | |
85 ) { | |
86 log.debug("setup()"); | |
87 super.setup(identifier, factory, context); | |
88 this.name = factory.getName(); | |
89 | |
90 if (context instanceof GNVArtifactContext) { | |
91 GNVArtifactContext gnvContext = (GNVArtifactContext) context; | |
92 Document doc = gnvContext.getConfig(); | |
93 Node artifactNode = getConfigurationFragment(doc); | |
94 | |
95 NodeList products = (NodeList) XMLUtils.xpath( | |
96 artifactNode, XPATH_PRODUCTS, XPathConstants.NODESET); | |
97 | |
98 if (products != null) { | |
99 this.products = new HashMap(products.getLength()); | |
100 | |
101 for (int i = 0; i < products.getLength(); i++) { | |
102 Element productNode = (Element)products.item(i); | |
103 String productName = productNode.getAttribute("name"); | |
104 NodeList parameterNodes = (NodeList) XMLUtils.xpath( | |
105 productNode, | |
106 XPATH_PRODUCT_PARAMETER, | |
107 XPathConstants.NODESET | |
108 ); | |
109 | |
110 Collection<InputData> parameter = null; | |
111 if (parameterNodes != null) { | |
112 parameter = new ArrayList(parameterNodes.getLength()); | |
113 | |
114 for (int j = 0; j < parameterNodes.getLength(); j++) { | |
115 Element parameterNode = (Element)parameterNodes.item(j); | |
116 String name = parameterNode.getAttribute("name"); | |
117 String value = parameterNode.getAttribute("value"); | |
118 parameter.add(new DefaultInputData(name, value)); | |
119 } | |
120 } | |
121 Node artifactFactoryNode = (Node) XMLUtils.xpath( | |
122 productNode, "artifact-factory", XPathConstants.NODE | |
123 ); | |
124 | |
125 ArtifactFactory artifactFactory = | |
126 new ArtifactFactoryUtilities().createArtitfactFactor( | |
127 doc, artifactFactoryNode | |
128 ); | |
129 | |
130 this.products.put(productName, new DefaultProduct( | |
131 productName, parameter, artifactFactory) | |
132 ); | |
133 } | |
134 } | |
135 } | |
136 } | |
137 | |
138 | |
139 @Override | |
140 public Document feed(Document target, CallContext context) { | |
141 log.debug("SelectProductArtifact.feed()"); | |
142 | |
143 if (artifact == null) { | |
144 Document document = XMLUtils.newDocument(); | |
145 String productName = XMLUtils.xpathString( | |
146 target, | |
147 XPATH_INPUT_DATA_VALUE, | |
148 ArtifactNamespaceContext.INSTANCE | |
149 ); | |
150 | |
151 current = (Product) products.get(productName); | |
152 | |
153 String reportNode = null; | |
154 String resultNode = null; | |
155 String msg = null; | |
156 | |
157 if (current != null) { | |
158 reportNode = "result"; | |
159 resultNode = "success"; | |
160 msg = "Feed was successfully. New Artifact created."; | |
161 } | |
162 else { | |
163 reportNode = "exceptionreport"; | |
164 resultNode = "exception"; | |
165 msg = "Product does not exist."; | |
166 } | |
167 | |
168 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
169 document, | |
170 ArtifactNamespaceContext.NAMESPACE_URI, | |
171 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
172 ); | |
173 | |
174 Element report = creator.create(reportNode); | |
175 Element success = creator.create(resultNode); | |
176 success.setTextContent(msg); | |
177 report.appendChild(success); | |
178 document.appendChild(report); | |
179 | |
180 return document; | |
181 } | |
182 else { | |
183 return artifact.feed(target, context); | |
184 } | |
185 } | |
186 | |
187 | |
188 @Override | |
189 public Document advance(Document target, CallContext context) { | |
190 log.debug("SelectProductArtifact.advance()"); | |
191 | |
192 if (artifact != null) { | |
193 Document result = artifact.advance(target, context); | |
194 context.putContextValue(ProxyArtifact.REPLACE_PROXY, artifact); | |
195 return result; | |
196 } | |
197 | |
198 Document result = XMLUtils.newDocument(); | |
199 if (current == null) { | |
200 // artifact needs to be feeded first | |
201 String msg = "Artifact is not configured properly. Call 'feed' fist."; | |
202 log.error(msg); | |
203 | |
204 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
205 result, | |
206 ArtifactNamespaceContext.NAMESPACE_URI, | |
207 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
208 ); | |
209 | |
210 Element report = creator.create("exceptionreport"); | |
211 Element exception = creator.create("exception"); | |
212 exception.setTextContent(msg); | |
213 report.appendChild(exception); | |
214 result.appendChild(report); | |
215 | |
216 return result; | |
217 } | |
218 | |
219 String uuid = XMLUtils.xpathString( | |
220 target, XPATH_UUID, ArtifactNamespaceContext.INSTANCE | |
221 ); | |
222 | |
223 String hash = XMLUtils.xpathString( | |
224 target, XPATH_HASH, ArtifactNamespaceContext.INSTANCE | |
225 ); | |
226 | |
227 artifact = current.getArtifactFactory().createArtifact( | |
228 uuid, context | |
229 ); | |
230 | |
231 Document feedDocument = feedDocument(uuid, hash); | |
232 artifact.feed(feedDocument(uuid, hash), context); | |
233 | |
234 result = ((GNVArtifactBase) artifact).initialize(context); | |
235 if (artifact instanceof GNVArtifactBase) { | |
236 ((GNVArtifactBase) artifact).setProduct(current); | |
237 } | |
238 context.putContextValue(ProxyArtifact.REPLACE_PROXY, artifact); | |
239 return result; | |
240 } | |
241 | |
242 | |
243 @Override | |
244 public Document describe(Document data, CallContext context) { | |
245 log.debug("SelectProductArtifact.describe()"); | |
246 | |
247 // create root node | |
248 Document document = XMLUtils.newDocument(); | |
249 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
250 document, | |
251 ArtifactNamespaceContext.NAMESPACE_URI, | |
252 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
253 ); | |
254 | |
255 Element rootNode = creator.create("result"); | |
256 Element typeNode = creator.create("type"); | |
257 creator.addAttr(typeNode, "name", "describe"); | |
258 rootNode.appendChild(typeNode); | |
259 | |
260 Element uuidNode = creator.create("uuid"); | |
261 creator.addAttr(uuidNode, "value", super.identifier); | |
262 rootNode.appendChild(uuidNode); | |
263 | |
264 Element hashNode = creator.create("hash"); | |
265 creator.addAttr(hashNode, "value", hash()); | |
266 rootNode.appendChild(hashNode); | |
267 | |
268 // create output node | |
269 Element out = creator.create("outputs"); | |
270 rootNode.appendChild(out); | |
271 | |
272 // create current state | |
273 Element state = creator.create("state"); | |
274 creator.addAttr(state, "name", "choose-product"); | |
275 creator.addAttr(state, "description", "Auswahl des Produktes."); | |
276 rootNode.appendChild(state); | |
277 | |
278 // create reachable states | |
279 Element rStates = creator.create("reachable-states"); | |
280 appendProducts(document, rStates, context); | |
281 rootNode.appendChild(rStates); | |
282 | |
283 // create model | |
284 Element model = creator.create("model"); | |
285 Element input = creator.create("input"); | |
286 creator.addAttr(input, "name", "product"); | |
287 creator.addAttr(input, "type", "String"); | |
288 model.appendChild(input); | |
289 rootNode.appendChild(model); | |
290 | |
291 // create ui | |
292 Element ui = creator.create("ui"); | |
293 Element dynamic = creator.create("dynamic"); | |
294 appendSelectProducts(document, dynamic, context.getMeta()); | |
295 ui.appendChild(dynamic); | |
296 rootNode.appendChild(ui); | |
297 | |
298 document.appendChild(rootNode); | |
299 return document; | |
300 } | |
301 | |
302 | |
303 @Override | |
304 public void out(Document document, OutputStream out, CallContext context) | |
305 throws IOException | |
306 { | |
307 log.debug("SelectProductArtifact.out()"); | |
308 if (artifact != null) { | |
309 artifact.out(document, out, context); | |
310 } | |
311 } | |
312 | |
313 | |
314 protected void appendProducts( | |
315 Document document, | |
316 Node parent, | |
317 Object context | |
318 ) { | |
319 Iterator iter = products.values().iterator(); | |
320 | |
321 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
322 document, | |
323 ArtifactNamespaceContext.NAMESPACE_URI, | |
324 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
325 ); | |
326 | |
327 while(iter.hasNext()) { | |
328 Product prod = (Product) iter.next(); | |
329 String name = prod.getName(); | |
330 | |
331 Element current = creator.create("state"); | |
332 creator.addAttr(current, "name", name); | |
333 creator.addAttr(current, "description", name); | |
334 parent.appendChild(current); | |
335 } | |
336 } | |
337 | |
338 | |
339 protected void appendSelectProducts( | |
340 Document document, | |
341 Node node, | |
342 CallMeta callMeta | |
343 ) { | |
344 RessourceFactory ressource = RessourceFactory.getInstance(); | |
345 | |
346 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
347 document, | |
348 XFORM_URL, | |
349 XFORM_PREFIX | |
350 ); | |
351 | |
352 String selectboxName = "product"; | |
353 Element selectNode = creator.create("select1"); | |
354 creator.addAttr(selectNode, "ref", selectboxName); | |
355 | |
356 Element lableNode = creator.create("label"); | |
357 lableNode.setTextContent(ressource.getRessource(callMeta.getLanguages(), | |
358 selectboxName, | |
359 selectboxName | |
360 ) | |
361 ); | |
362 | |
363 Element choiceNode = creator.create("choices"); | |
364 selectNode.appendChild(lableNode); | |
365 selectNode.appendChild(choiceNode); | |
366 | |
367 Iterator it = products.values().iterator(); | |
368 while (it.hasNext()) { | |
369 Product p = (Product) it.next(); | |
370 Element itemNode = creator.create("item"); | |
371 Element choiceLableNode = creator.create("label"); | |
372 choiceLableNode.setTextContent(ressource.getRessource( | |
373 callMeta.getLanguages(), | |
374 p.getName(), | |
375 p.getName() | |
376 )); | |
377 itemNode.appendChild(choiceLableNode); | |
378 | |
379 Element choiceValueNode = creator.create("value"); | |
380 choiceValueNode.setTextContent(p.getName()); | |
381 itemNode.appendChild(choiceValueNode); | |
382 choiceNode.appendChild(itemNode); | |
383 } | |
384 | |
385 node.appendChild(selectNode); | |
386 } | |
387 | |
388 | |
389 protected Node getConfigurationFragment(Document document) { | |
390 String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll( | |
391 XPATH_IDENTIFIER_REPLACE, name | |
392 ); | |
393 | |
394 return (Node) XMLUtils.xpath(document, xpathQuery, XPathConstants.NODE); | |
395 } | |
396 | |
397 | |
398 protected Document feedDocument(String uuid, String hash) { | |
399 Document document = XMLUtils.newDocument(); | |
400 | |
401 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
402 document, | |
403 ArtifactNamespaceContext.NAMESPACE_URI, | |
404 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
405 ); | |
406 Element rootNode = creator.create("action"); | |
407 | |
408 Element typeNode = creator.create("type"); | |
409 creator.addAttr(typeNode, "name", "feed"); | |
410 rootNode.appendChild(typeNode); | |
411 | |
412 Element uuidNode = creator.create("uuid"); | |
413 creator.addAttr(uuidNode, "value", uuid); | |
414 rootNode.appendChild(uuidNode); | |
415 | |
416 Element hashNode = creator.create("hash"); | |
417 creator.addAttr(hashNode, "value", hash); | |
418 rootNode.appendChild(hashNode); | |
419 | |
420 Element dataNode = creator.create("data"); | |
421 rootNode.appendChild(dataNode); | |
422 | |
423 Collection<InputData> parameter = this.current.getParameter(); | |
424 if (parameter != null) { | |
425 Iterator<InputData> parameterIt = parameter.iterator(); | |
426 | |
427 while (parameterIt.hasNext()) { | |
428 InputData inputData = parameterIt.next(); | |
429 | |
430 Element inputNode = creator.create("input"); | |
431 creator.addAttr(inputNode, "name", inputData.getName()); | |
432 creator.addAttr(inputNode, "value", inputData.getValue()); | |
433 dataNode.appendChild(inputNode); | |
434 } | |
435 } | |
436 document.appendChild(rootNode); | |
437 return document; | |
438 } | |
439 } | |
440 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |