Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/fis/FISArtifact.java @ 376:d8f3ef441bf2
merged gnv-artifacts/0.3
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:47 +0200 |
parents | e964a3d8f7bc |
children |
comparison
equal
deleted
inserted
replaced
293:6b0ef2324d02 | 376:d8f3ef441bf2 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.artifacts.fis; | |
5 | |
6 import java.io.IOException; | |
7 import java.io.OutputStream; | |
8 import java.util.ArrayList; | |
9 import java.util.Collection; | |
10 import java.util.HashMap; | |
11 import java.util.Iterator; | |
12 import java.util.Map; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 import org.w3c.dom.Document; | |
16 import org.w3c.dom.Element; | |
17 import org.w3c.dom.Node; | |
18 import org.w3c.dom.NodeList; | |
19 | |
20 import de.intevation.artifactdatabase.Config; | |
21 import de.intevation.artifactdatabase.DefaultArtifact; | |
22 import de.intevation.artifactdatabase.XMLUtils; | |
23 import de.intevation.artifacts.Artifact; | |
24 import de.intevation.artifacts.ArtifactFactory; | |
25 import de.intevation.artifacts.CallContext; | |
26 import de.intevation.artifacts.CallMeta; | |
27 import de.intevation.gnv.artifacts.GNVArtifactBase; | |
28 import de.intevation.gnv.artifacts.context.GNVArtifactContext; | |
29 import de.intevation.gnv.artifacts.fis.product.DefaultProduct; | |
30 import de.intevation.gnv.artifacts.fis.product.Product; | |
31 import de.intevation.gnv.artifacts.ressource.RessourceFactory; | |
32 import de.intevation.gnv.state.DefaultInputData; | |
33 import de.intevation.gnv.state.InputData; | |
34 import de.intevation.gnv.utils.ArtifactFactoryUtilities; | |
35 import de.intevation.gnv.utils.ArtifactXMLUtilities; | |
36 | |
37 /** | |
38 * @author Tim Englich <tim.englich@intevation.de> | |
39 * | |
40 */ | |
41 public class FISArtifact extends DefaultArtifact { | |
42 | |
43 /** | |
44 * the logger, used to log exceptions and additonaly information | |
45 */ | |
46 private static Logger log = Logger.getLogger(GNVArtifactBase.class); | |
47 /** | |
48 * The UID of this Class | |
49 */ | |
50 private static final long serialVersionUID = 2874044542701727083L; | |
51 | |
52 /** | |
53 * The Identifier for the Replacement of the Artifactname | |
54 */ | |
55 public static final String XPATH_IDENTIFIER_REPLACE = "IDENTIFIER"; | |
56 | |
57 /** | |
58 * The XPATH to the XML-Fragment that should be used for the Configuration | |
59 */ | |
60 public static final String XPATH_ARTIFACT_CONFIGURATION = "/artifact-database/artifacts/artifact[@name='" | |
61 + XPATH_IDENTIFIER_REPLACE | |
62 + "']"; | |
63 | |
64 /** | |
65 * The Name of the Artifact | |
66 */ | |
67 private String name = null; | |
68 | |
69 private Map<String, Product> products = null; | |
70 | |
71 private Artifact productArtifact = null; | |
72 | |
73 private Product current = null; | |
74 | |
75 private ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); | |
76 | |
77 /** | |
78 * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document, | |
79 * de.intevation.artifacts.CallContext) | |
80 */ | |
81 @Override | |
82 public Document advance(Document target, CallContext context) { | |
83 | |
84 Document result = null; | |
85 if (this.productArtifact == null) { | |
86 if (this.current != null) { | |
87 | |
88 String uuid = Config.getStringXPath(target, | |
89 "action/uuid/@value"); | |
90 String hash = Config.getStringXPath(target, | |
91 "action/hash/@value"); | |
92 this.productArtifact = this.current.getArtifactFactory() | |
93 .createArtifact(uuid, context); | |
94 | |
95 Document feedDocument = xmlUtilities.reInitDocument(this | |
96 .createFeedProductArtifactDocument(uuid, hash)); | |
97 log.debug("Feed ==> " | |
98 + this.xmlUtilities | |
99 .writeDocument2String(feedDocument)); | |
100 this.productArtifact.feed(feedDocument, context); | |
101 result = ((GNVArtifactBase)this.productArtifact).initialize(context); | |
102 } else { | |
103 String msg = "Artifact is not configured properly. Call feed first."; | |
104 log.error(msg); | |
105 result = new ArtifactXMLUtilities().createExceptionReport(msg, | |
106 XMLUtils.newDocument()); | |
107 } | |
108 } else { | |
109 result = this.productArtifact.advance(target, context); | |
110 } | |
111 return result; | |
112 } | |
113 | |
114 private Document createDescribeRequestBody(String uuid, | |
115 String hash, | |
116 boolean includeUI){ | |
117 Document document = XMLUtils.newDocument(); | |
118 | |
119 Element rootNode = xmlUtilities.createArtifactElement(document,"action"); | |
120 document.appendChild(rootNode); | |
121 | |
122 Element typeNode = this.xmlUtilities.createArtifactElement(document, "type"); | |
123 typeNode.setAttribute("name", "describe"); | |
124 rootNode.appendChild(typeNode); | |
125 | |
126 Element uuidNode = this.xmlUtilities.createArtifactElement(document, "uuid"); | |
127 uuidNode.setAttribute("value",uuid); | |
128 rootNode.appendChild(uuidNode); | |
129 | |
130 Element hashNode = this.xmlUtilities.createArtifactElement(document, "hash"); | |
131 hashNode.setAttribute("value", hash); | |
132 rootNode.appendChild(hashNode); | |
133 | |
134 Element includeUINode = this.xmlUtilities.createArtifactElement(document, "include-ui"); | |
135 includeUINode.setTextContent(""+includeUI); | |
136 rootNode.appendChild(includeUINode); | |
137 | |
138 return document; | |
139 } | |
140 | |
141 private Document createAdvanceProductArtifactDocument(String uuid, | |
142 String hash, | |
143 String targetName) { | |
144 Document document = XMLUtils.newDocument(); | |
145 Element rootNode = xmlUtilities.createArtifactElement(document, | |
146 "action"); | |
147 | |
148 Element typeNode = xmlUtilities.createArtifactElement(document, "type"); | |
149 typeNode.setAttribute("name", "advanve"); | |
150 rootNode.appendChild(typeNode); | |
151 | |
152 Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid"); | |
153 uuidNode.setAttribute("value", uuid); | |
154 rootNode.appendChild(uuidNode); | |
155 | |
156 Element hashNode = xmlUtilities.createArtifactElement(document, "hash"); | |
157 hashNode.setAttribute("value", hash); | |
158 rootNode.appendChild(hashNode); | |
159 Element targetNode = xmlUtilities.createArtifactElement(document, | |
160 "target"); | |
161 targetNode.setAttribute("name", targetName); | |
162 rootNode.appendChild(targetNode); | |
163 | |
164 document.appendChild(rootNode); | |
165 return document; | |
166 } | |
167 | |
168 private Document createFeedProductArtifactDocument(String uuid, String hash) { | |
169 Document document = XMLUtils.newDocument(); | |
170 Element rootNode = xmlUtilities.createArtifactElement(document, | |
171 "action"); | |
172 | |
173 Element typeNode = xmlUtilities.createArtifactElement(document, "type"); | |
174 typeNode.setAttribute("name", "feed"); | |
175 rootNode.appendChild(typeNode); | |
176 | |
177 Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid"); | |
178 uuidNode.setAttribute("value", uuid); | |
179 rootNode.appendChild(uuidNode); | |
180 | |
181 Element hashNode = xmlUtilities.createArtifactElement(document, "hash"); | |
182 hashNode.setAttribute("value", hash); | |
183 rootNode.appendChild(hashNode); | |
184 | |
185 Element dataNode = xmlUtilities.createArtifactElement(document, "data"); | |
186 rootNode.appendChild(dataNode); | |
187 | |
188 Collection<InputData> parameter = this.current.getParameter(); | |
189 if (parameter != null) { | |
190 Iterator<InputData> parameterIt = parameter.iterator(); | |
191 while (parameterIt.hasNext()) { | |
192 InputData inputData = parameterIt.next(); | |
193 Element inputNode = xmlUtilities.createArtifactElement( | |
194 document, "input"); | |
195 inputNode.setAttribute("name", inputData.getName()); | |
196 inputNode.setAttribute("value", inputData.getValue()); | |
197 dataNode.appendChild(inputNode); | |
198 } | |
199 } | |
200 document.appendChild(rootNode); | |
201 return document; | |
202 | |
203 } | |
204 | |
205 /** | |
206 * @see de.intevation.artifactdatabase.DefaultArtifact#describe(org.w3c.dom.Document, de.intevation.artifacts.CallContext) | |
207 */ | |
208 @Override | |
209 public Document describe(Document data, CallContext context) { | |
210 if (this.productArtifact == null) { | |
211 return this.createDescibeOutput(context.getMeta()); | |
212 } else { | |
213 Document document = this.productArtifact.describe(data,context); | |
214 document = new ArtifactXMLUtilities().reInitDocument(document); | |
215 Node staticNode = Config | |
216 .getNodeXPath(document, "/result/ui/static"); | |
217 if (staticNode != null) { | |
218 Node staticUI = this.createSelectBox(document, context | |
219 .getMeta()); | |
220 staticNode.insertBefore(staticUI, staticNode.getFirstChild()); | |
221 } | |
222 return document; | |
223 } | |
224 } | |
225 | |
226 | |
227 /** | |
228 * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, | |
229 * java.lang.Object) | |
230 */ | |
231 @Override | |
232 public Document feed(Document target, CallContext context) { | |
233 log.debug("FISArtifact.feed"); | |
234 Document result = null; | |
235 if (this.productArtifact == null) { | |
236 String productName = Config.getStringXPath(target, | |
237 "action/data/input[@name='product']/@value"); | |
238 log.debug("Looking for ProductArtifact " + productName); | |
239 if (this.products.containsKey(productName)) { | |
240 this.current = this.products.get(productName); | |
241 result = new ArtifactXMLUtilities().createSuccessReport( | |
242 "Feed success New ProductArtifact created", XMLUtils | |
243 .newDocument()); | |
244 } else { | |
245 String msg = "Product does not exists for " + productName; | |
246 log.error(msg); | |
247 result = new ArtifactXMLUtilities().createExceptionReport(msg, | |
248 XMLUtils.newDocument()); | |
249 } | |
250 } else { | |
251 log.debug("Feed a Productartifact"); | |
252 result = this.productArtifact.feed(target, context); | |
253 } | |
254 return result; | |
255 } | |
256 | |
257 /** | |
258 * @see de.intevation.artifactdatabase.DefaultArtifact#out(org.w3c.dom.Document, | |
259 * java.lang.Object) | |
260 */ | |
261 @Override | |
262 public void out(Document format, OutputStream outputStream, | |
263 CallContext context) throws IOException { | |
264 if (this.productArtifact != null) { | |
265 this.productArtifact.out(format, outputStream, context); | |
266 } | |
267 } | |
268 | |
269 /** | |
270 * Constructor | |
271 */ | |
272 public FISArtifact() { | |
273 super(); | |
274 } | |
275 | |
276 /** | |
277 * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, | |
278 * de.intevation.artifacts.ArtifactFactory, java.lang.Object) | |
279 */ | |
280 @Override | |
281 public void setup(String identifier, ArtifactFactory factory, Object context) { | |
282 log.debug("FISArtifact.setup"); | |
283 this.name = factory.getName(); | |
284 super.setup(identifier, factory, context); | |
285 if (context instanceof GNVArtifactContext) { | |
286 GNVArtifactContext gnvContext = (GNVArtifactContext) context; | |
287 Document doc = gnvContext.getConfig(); | |
288 Node artifactNode = this.getConfigurationFragment(doc); | |
289 | |
290 NodeList products = Config.getNodeSetXPath(artifactNode, | |
291 "products/product"); | |
292 if (products != null) { | |
293 this.products = new HashMap<String, Product>(products | |
294 .getLength()); | |
295 | |
296 for (int i = 0; i < products.getLength(); i++) { | |
297 Element productNode = (Element)products.item(i); | |
298 String productName = productNode.getAttribute("name"); | |
299 NodeList parameterNodes = Config.getNodeSetXPath( | |
300 productNode, "parameters/parameter"); | |
301 Collection<InputData> parameter = null; | |
302 if (parameterNodes != null) { | |
303 parameter = new ArrayList<InputData>(parameterNodes | |
304 .getLength()); | |
305 for (int j = 0; j < parameterNodes.getLength(); j++) { | |
306 Element parameterNode = (Element)parameterNodes.item(j); | |
307 String name = parameterNode.getAttribute("name"); | |
308 String value = parameterNode.getAttribute("value"); | |
309 parameter.add(new DefaultInputData(name, value)); | |
310 } | |
311 } | |
312 Node artifactFactoryNode = Config.getNodeXPath(productNode, | |
313 "artifact-factory"); | |
314 ArtifactFactory artifactFactory = new ArtifactFactoryUtilities() | |
315 .createArtitfactFactor(doc, artifactFactoryNode); | |
316 this.products.put(productName, new DefaultProduct( | |
317 productName, parameter, artifactFactory)); | |
318 } | |
319 } | |
320 } | |
321 } | |
322 | |
323 protected Node getConfigurationFragment(Document document) { | |
324 log.debug("GNVArtifactBase.getConfigurationFragment"); | |
325 String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll( | |
326 XPATH_IDENTIFIER_REPLACE, this.name); | |
327 log.debug(xpathQuery); | |
328 return Config.getNodeXPath(document, xpathQuery); | |
329 } | |
330 | |
331 protected Document createDescibeOutput(CallMeta callMeta) { | |
332 log.debug("GNVArtifactBase.createDescibeOutput"); | |
333 Document document = XMLUtils.newDocument(); | |
334 Element rootNode = this.createRootNode(document); | |
335 this.createHeader(rootNode, document, "describe"); | |
336 this.createOutputs(rootNode, document); | |
337 this.createCurrentState(rootNode, document); | |
338 this.createReachableStates(rootNode, document); | |
339 this.createModel(rootNode, document); | |
340 this.createUserInterface(rootNode, document, callMeta); | |
341 | |
342 return document; | |
343 } | |
344 | |
345 protected Element createRootNode(Document document) { | |
346 Element rootNode = xmlUtilities.createArtifactElement(document, | |
347 "result"); | |
348 document.appendChild(rootNode); | |
349 return rootNode; | |
350 } | |
351 | |
352 protected void createHeader(Element parent, Document document, | |
353 String documentType) { | |
354 Element typeNode = xmlUtilities.createArtifactElement(document, "type"); | |
355 typeNode.setAttribute("name", documentType); | |
356 parent.appendChild(typeNode); | |
357 | |
358 Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid"); | |
359 uuidNode.setAttribute("value", super.identifier); | |
360 parent.appendChild(uuidNode); | |
361 | |
362 Element hashNode = xmlUtilities.createArtifactElement(document, "hash"); | |
363 hashNode.setAttribute("value", this.hash()); | |
364 parent.appendChild(hashNode); | |
365 | |
366 } | |
367 | |
368 protected void createReachableStates(Element parent, Document document) { | |
369 Element stateNode = xmlUtilities.createArtifactElement(document, | |
370 "reachable-states"); | |
371 if (this.products != null) { | |
372 Iterator<Product> products = this.products.values().iterator(); | |
373 while (products.hasNext()) { | |
374 Product product = products.next(); | |
375 Element currentNode = xmlUtilities.createArtifactElement( | |
376 document, "state"); | |
377 currentNode.setAttribute("name", product.getName()); | |
378 currentNode.setAttribute("description", product.getName()); | |
379 stateNode.appendChild(currentNode); | |
380 } | |
381 } | |
382 parent.appendChild(stateNode); | |
383 } | |
384 | |
385 protected void createCurrentState(Element parent, Document document) { | |
386 Element stateNode = xmlUtilities.createArtifactElement(document, | |
387 "state"); | |
388 stateNode.setAttribute("name", "choose-product"); | |
389 stateNode.setAttribute("description", | |
390 "Initialer Stand Auswahl des products"); | |
391 parent.appendChild(stateNode); | |
392 } | |
393 | |
394 protected void createModel(Element parent, Document document) { | |
395 Element modelNode = xmlUtilities.createArtifactElement(document, | |
396 "model"); | |
397 | |
398 Element inputNode = xmlUtilities.createArtifactElement(document, | |
399 "input"); | |
400 inputNode.setAttribute("name", "product"); | |
401 inputNode.setAttribute("type", "String"); | |
402 modelNode.appendChild(inputNode); | |
403 | |
404 parent.appendChild(modelNode); | |
405 } | |
406 | |
407 protected void createUserInterface(Element parent, Document document, | |
408 CallMeta callMeta) { | |
409 Element uiNode = xmlUtilities.createArtifactElement(document, "ui"); | |
410 ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); | |
411 Node dynamic = xmlUtilities.createArtifactElement(document, "dynamic"); | |
412 uiNode.appendChild(dynamic); | |
413 if (this.products != null && !this.products.isEmpty()) { | |
414 Element selectNode = createSelectBox(document, callMeta); | |
415 | |
416 dynamic.appendChild(selectNode); | |
417 } | |
418 | |
419 parent.appendChild(uiNode); | |
420 } | |
421 | |
422 /** | |
423 * @param document | |
424 * @param xmlUtilities | |
425 * @return | |
426 */ | |
427 private Element createSelectBox(Document document, CallMeta callMeta) { | |
428 | |
429 ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); | |
430 String selectboxName = "product"; | |
431 Iterator<Product> it = this.products.values().iterator(); | |
432 Element selectNode = xmlUtilities.createXFormElement(document, | |
433 "select1"); | |
434 selectNode.setAttribute("ref", selectboxName); | |
435 | |
436 Element lableNode = xmlUtilities.createXFormElement(document, "label"); | |
437 lableNode.setTextContent(RessourceFactory.getInstance().getRessource( | |
438 callMeta.getLanguages(), selectboxName, selectboxName)); | |
439 selectNode.appendChild(lableNode); | |
440 Element choiceNode = xmlUtilities.createXFormElement(document, | |
441 "choices"); | |
442 selectNode.appendChild(choiceNode); | |
443 while (it.hasNext()) { | |
444 Product p = it.next(); | |
445 Element itemNode = xmlUtilities | |
446 .createXFormElement(document, "item"); | |
447 | |
448 if (this.current != null | |
449 && this.current.getName().equals(p.getName())) { | |
450 itemNode.setAttribute("selected", "true"); | |
451 } | |
452 | |
453 Element choiceLableNode = xmlUtilities.createXFormElement(document, | |
454 "label"); | |
455 choiceLableNode.setTextContent(RessourceFactory.getInstance() | |
456 .getRessource(callMeta.getLanguages(), p.getName(), | |
457 p.getName())); | |
458 itemNode.appendChild(choiceLableNode); | |
459 | |
460 Element choicValueNode = xmlUtilities.createXFormElement(document, | |
461 "value"); | |
462 choicValueNode.setTextContent(p.getName()); | |
463 itemNode.appendChild(choicValueNode); | |
464 | |
465 choiceNode.appendChild(itemNode); | |
466 | |
467 } | |
468 return selectNode; | |
469 } | |
470 | |
471 protected void createOutputs(Element parent, Document document) { | |
472 log.debug("GNVArtifactBase.createOutputs"); | |
473 Element outputsNode = xmlUtilities.createArtifactElement(document, | |
474 "outputs"); | |
475 parent.appendChild(outputsNode); | |
476 } | |
477 | |
478 } |