Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java @ 657:af3f56758f59
merged gnv-artifacts/0.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:53 +0200 |
parents | 078ba6799bd2 |
children | dde7f51dbe1e |
comparison
equal
deleted
inserted
replaced
590:5f5f273c8566 | 657:af3f56758f59 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.artifacts; | |
5 | |
6 import de.intevation.artifactdatabase.Config; | |
7 import de.intevation.artifactdatabase.ProxyArtifact; | |
8 import de.intevation.artifactdatabase.XMLUtils; | |
9 | |
10 import de.intevation.artifacts.Artifact; | |
11 import de.intevation.artifacts.ArtifactDatabase; | |
12 import de.intevation.artifacts.ArtifactFactory; | |
13 import de.intevation.artifacts.ArtifactNamespaceContext; | |
14 import de.intevation.artifacts.CallContext; | |
15 import de.intevation.artifacts.CallMeta; | |
16 | |
17 import de.intevation.gnv.artifacts.context.GNVArtifactContext; | |
18 | |
19 import de.intevation.gnv.artifacts.fis.product.Product; | |
20 | |
21 import de.intevation.gnv.artifacts.ressource.RessourceFactory; | |
22 | |
23 import de.intevation.gnv.state.DefaultInputData; | |
24 import de.intevation.gnv.state.ExportMode; | |
25 import de.intevation.gnv.state.InputData; | |
26 import de.intevation.gnv.state.InputValue; | |
27 import de.intevation.gnv.state.OutputMode; | |
28 import de.intevation.gnv.state.OutputState; | |
29 import de.intevation.gnv.state.State; | |
30 import de.intevation.gnv.state.StateFactory; | |
31 | |
32 import de.intevation.gnv.state.exception.StateException; | |
33 | |
34 import de.intevation.gnv.transition.Transition; | |
35 import de.intevation.gnv.transition.TransitionFactory; | |
36 | |
37 import de.intevation.gnv.utils.ArtifactXMLUtilities; | |
38 | |
39 import java.io.IOException; | |
40 import java.io.OutputStream; | |
41 | |
42 import java.util.ArrayList; | |
43 import java.util.Collection; | |
44 import java.util.HashMap; | |
45 import java.util.Iterator; | |
46 import java.util.List; | |
47 import java.util.Map; | |
48 | |
49 import javax.xml.xpath.XPathConstants; | |
50 | |
51 import org.apache.log4j.Logger; | |
52 | |
53 import org.w3c.dom.Document; | |
54 import org.w3c.dom.Element; | |
55 import org.w3c.dom.Node; | |
56 import org.w3c.dom.NodeList; | |
57 | |
58 /** | |
59 * @author Tim Englich (tim.englich@intevation.de) | |
60 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) | |
61 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) | |
62 */ | |
63 public abstract class GNVArtifactBase extends GNVDefaultArtifact | |
64 implements PreSettingArtifact { | |
65 | |
66 /** | |
67 * the logger, used to log exceptions and additonaly information | |
68 */ | |
69 private static Logger log = Logger.getLogger(GNVArtifactBase.class); | |
70 /** | |
71 * The UID of this Class | |
72 */ | |
73 private static final long serialVersionUID = -8907096744400741458L; | |
74 | |
75 /** | |
76 * The Identifier for the Replacement of the Artifactname | |
77 */ | |
78 public static final String XPATH_IDENTIFIER_REPLACE = "IDENTIFIER"; | |
79 | |
80 /** | |
81 * The XPATH to the XML-Fragment that should be used for the Configuration | |
82 */ | |
83 public static final String XPATH_ARTIFACT_CONFIGURATION = "/artifact-database/artifacts/artifact[@name='" | |
84 + XPATH_IDENTIFIER_REPLACE | |
85 + "']"; | |
86 | |
87 public static final String XPATH_STATIC_NODE = "/art:result/art:ui/art:static"; | |
88 | |
89 public static final String XPATH_INPUT_DATA = "/art:action/art:data/art:input"; | |
90 | |
91 public static final String XPATH_INCLUDE_UI = "/art:action/art:include-ui"; | |
92 | |
93 public static final String XPATH_TARGET_NAME = "/art:action/art:target/@name"; | |
94 | |
95 public static final String XPATH_OUTPUT_NAME = "/art:action/art:out/@name"; | |
96 | |
97 public static final String XPATH_OUTPUT_PARAMS = "/art:action/art:out/art:params/art:input"; | |
98 | |
99 public static final String INITIAL_STATE = "product"; | |
100 | |
101 /** | |
102 * The current State | |
103 */ | |
104 protected State current = null; | |
105 | |
106 /** | |
107 * The States that can be used | |
108 */ | |
109 protected Map<String, State> states = null; | |
110 | |
111 /** | |
112 * The Transitions which can switch between the different States. | |
113 */ | |
114 protected Collection<Transition> transitions = null; | |
115 | |
116 /** | |
117 * The current product | |
118 */ | |
119 protected Product product; | |
120 | |
121 /** | |
122 * The Name of the Artifact | |
123 */ | |
124 protected String name = null; | |
125 | |
126 /** | |
127 * The Presettings of InputData which can be used to | |
128 * travel through the States in different Ways or | |
129 * manipulate the InputData | |
130 */ | |
131 private Map<String, InputData> preSettings = null; | |
132 | |
133 /** | |
134 * Constructor | |
135 */ | |
136 public GNVArtifactBase() { | |
137 super(); | |
138 } | |
139 | |
140 | |
141 @Override | |
142 public Document advance(Document target, CallContext context) { | |
143 log.debug("GNVArtifactBase.advance()"); | |
144 | |
145 Document result = XMLUtils.newDocument(); | |
146 String targetState = XMLUtils.xpathString( | |
147 target, XPATH_TARGET_NAME, ArtifactNamespaceContext.INSTANCE | |
148 ); | |
149 | |
150 // no current state... | |
151 if (current == null) { | |
152 log.warn("No current state. Advance not possible."); | |
153 | |
154 result = createReport( | |
155 result, | |
156 "exceptionreport", | |
157 "exception", | |
158 "No State activated." | |
159 ); | |
160 | |
161 return result; | |
162 } | |
163 | |
164 State next = null; | |
165 | |
166 try { | |
167 | |
168 // step forward | |
169 if (isStateCurrentlyReachable(targetState)) { | |
170 | |
171 next = states.get(targetState); | |
172 | |
173 // 2. Transfer Results | |
174 next.putInputData(current.getInputData(), identifier); | |
175 next.setParent(current); | |
176 next.setPreSettings(this.preSettings); | |
177 | |
178 if (current != null) { | |
179 current.endOfLife(context.globalContext()); | |
180 } | |
181 | |
182 // 3. Switch to next State | |
183 current = next; | |
184 | |
185 // 4. Initialize next Step | |
186 current.initialize(identifier, context); | |
187 | |
188 result = createReport( | |
189 result, "result", "success", "Advance success" | |
190 ); | |
191 } | |
192 | |
193 // step backward | |
194 else if((next = getPreviousState(current, targetState)) != null) { | |
195 | |
196 if (current != null) { | |
197 current.endOfLife(context.globalContext()); | |
198 } | |
199 | |
200 current = next; | |
201 | |
202 // 2. Transfer Results | |
203 current.reset(identifier); | |
204 | |
205 result = createReport( | |
206 result, "result", "success", "Advance success" | |
207 ); | |
208 } | |
209 | |
210 // goto initial step | |
211 else if(targetState.equals(INITIAL_STATE)) { | |
212 | |
213 String fis = product.getArtifactFactory(); | |
214 ArtifactDatabase db = context.getDatabase(); | |
215 GNVProductArtifactFactory fac = (GNVProductArtifactFactory) | |
216 db.getInternalArtifactFactory(fis); | |
217 | |
218 Artifact select = fac.createArtifact(identifier, context, null); | |
219 context.putContextValue(ProxyArtifact.REPLACE_PROXY, select); | |
220 | |
221 result = createReport( | |
222 result, "result", "success", "Advance success" | |
223 ); | |
224 } | |
225 | |
226 // advance not possible | |
227 else { | |
228 log.warn("advance not possible for target: " + targetState); | |
229 result = createReport( | |
230 result, | |
231 "exceptionreport", | |
232 "exception", | |
233 "Statetransition not supported" | |
234 ); | |
235 } | |
236 } | |
237 catch (StateException se) { | |
238 log.error(se, se); | |
239 result = createReport( | |
240 result, | |
241 "exceptionreport", | |
242 "exception", | |
243 se.getLocalizedMessage() | |
244 ); | |
245 } | |
246 | |
247 return result; | |
248 } | |
249 | |
250 | |
251 protected Document createReport( | |
252 Document document, | |
253 String nodeName, | |
254 String state, | |
255 String msg | |
256 ) { | |
257 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
258 document, | |
259 ArtifactNamespaceContext.NAMESPACE_URI, | |
260 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
261 ); | |
262 | |
263 Element reportNode = creator.create(nodeName); | |
264 Element stateNode = creator.create(state); | |
265 | |
266 stateNode.setTextContent(msg); | |
267 reportNode.appendChild(stateNode); | |
268 document.appendChild(reportNode); | |
269 | |
270 return document; | |
271 | |
272 } | |
273 | |
274 | |
275 protected State getPreviousState(State current, String name) { | |
276 if (current == null) { | |
277 return null; | |
278 } | |
279 | |
280 if (current.getID().equals(name)) { | |
281 return current; | |
282 } | |
283 else { | |
284 return getPreviousState(current.getParent(), name); | |
285 } | |
286 } | |
287 | |
288 | |
289 private boolean isStateCurrentlyReachable(String stateid){ | |
290 Iterator<Transition> it = this.transitions.iterator(); | |
291 String from = this.current.getID(); | |
292 while (it.hasNext()){ | |
293 Transition transition = it.next(); | |
294 if (transition.getFrom().equals(from)){ | |
295 if (transition.getTo().equals(stateid) && transition.isValid(this.current)){ | |
296 return true; | |
297 } | |
298 } | |
299 } | |
300 return false; | |
301 } | |
302 | |
303 public Document initialize (CallContext context) { | |
304 Document result = XMLUtils.newDocument(); | |
305 try { | |
306 this.current.initialize(super.identifier, context); | |
307 result = new ArtifactXMLUtilities() | |
308 .createSuccessReport("Initialize success", | |
309 XMLUtils.newDocument()); | |
310 } catch (StateException e) { | |
311 log.error(e,e); | |
312 result = new ArtifactXMLUtilities().createExceptionReport(e | |
313 .getLocalizedMessage(), XMLUtils.newDocument()); | |
314 } | |
315 return result; | |
316 } | |
317 | |
318 protected String readStateName(Document document) { | |
319 String returnValue = XMLUtils.xpathString( | |
320 document, XPATH_TARGET_NAME, ArtifactNamespaceContext.INSTANCE); | |
321 return returnValue; | |
322 } | |
323 | |
324 protected Node getConfigurationFragment(Document document) { | |
325 log.debug("GNVArtifactBase.getConfigurationFragment"); | |
326 String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll( | |
327 XPATH_IDENTIFIER_REPLACE, this.name); | |
328 | |
329 Element configurationNode = (Element)Config.getNodeXPath(document, xpathQuery); | |
330 | |
331 String link = configurationNode.getAttribute("xlink:href"); | |
332 if (link != null ){ | |
333 String absolutFileName = Config.replaceConfigDir(link); | |
334 configurationNode = (Element)new ArtifactXMLUtilities().readConfiguration(absolutFileName); | |
335 } | |
336 | |
337 return configurationNode; | |
338 } | |
339 | |
340 /** | |
341 * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, | |
342 * de.intevation.artifacts.CallContext) | |
343 */ | |
344 @Override | |
345 public Document feed(Document target, CallContext context) { | |
346 log.debug("GNVArtifactBase.feed"); | |
347 Document result = XMLUtils.newDocument(); | |
348 try { | |
349 if (this.current != null) { | |
350 Collection<InputData> inputData = this.parseInputData( | |
351 target, | |
352 XPATH_INPUT_DATA); | |
353 | |
354 if (!inputData.isEmpty()){ | |
355 current.feed(inputData, super.identifier); | |
356 result = new ArtifactXMLUtilities().createSuccessReport( | |
357 "Feed success", XMLUtils.newDocument()); | |
358 }else{ | |
359 String msg = "No Inputdata given. Please select at least one Entry."; | |
360 log.warn(msg); | |
361 result = new ArtifactXMLUtilities().createExceptionReport(msg, | |
362 XMLUtils.newDocument()); | |
363 } | |
364 } else { | |
365 String msg = "No State instantiated"; | |
366 log.warn(msg); | |
367 result = new ArtifactXMLUtilities().createExceptionReport(msg, | |
368 XMLUtils.newDocument()); | |
369 } | |
370 } catch (StateException e) { | |
371 log.error(e, e); | |
372 result = new ArtifactXMLUtilities().createExceptionReport(e | |
373 .getLocalizedMessage(), XMLUtils.newDocument()); | |
374 } | |
375 return result; | |
376 } | |
377 | |
378 | |
379 /** | |
380 * @see de.intevation.artifactdatabase.DefaultArtifact#describe(org.w3c.dom.Document, de.intevation.artifacts.CallContext) | |
381 */ | |
382 @Override | |
383 public Document describe(Document data, CallContext context) { | |
384 log.debug("GNVArtifactBase.describe"); | |
385 | |
386 Document document = createDescibeOutput( | |
387 context, | |
388 identifier, | |
389 getIncludeUIFromDocument(data) | |
390 ); | |
391 | |
392 return document; | |
393 } | |
394 | |
395 /** | |
396 * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, de.intevation.artifacts.ArtifactFactory, java.lang.Object, org.w3c.dom.Document) | |
397 */ | |
398 @Override | |
399 public void setup(String identifier, ArtifactFactory factory, | |
400 Object context,Document data) { | |
401 log.debug("GNVArtifactBase.setup"); | |
402 super.setup(identifier, factory, context, data); | |
403 | |
404 Object localContext = context; | |
405 if (context instanceof CallContext) { | |
406 localContext = ((CallContext) context).globalContext(); | |
407 | |
408 } | |
409 | |
410 if (localContext instanceof GNVArtifactContext) { | |
411 GNVArtifactContext gnvContext = (GNVArtifactContext) localContext; | |
412 Document doc = gnvContext.getConfig(); | |
413 Node artifactNode = this.getConfigurationFragment(doc); | |
414 | |
415 NodeList stateList = Config.getNodeSetXPath(artifactNode, | |
416 "states/state"); | |
417 this.states = new HashMap<String, State>(stateList | |
418 .getLength()); | |
419 for (int i = 0; i < stateList.getLength(); i++) { | |
420 State tmpState = StateFactory.getInstance() | |
421 .createState(stateList.item(i)); | |
422 if (tmpState != null) { | |
423 log.debug("Initiate new state: " + tmpState.getID()); | |
424 this.states.put(tmpState.getID(), tmpState); | |
425 if (this.current == null) { | |
426 this.current = tmpState; | |
427 } | |
428 } | |
429 } | |
430 | |
431 NodeList transitionList = Config.getNodeSetXPath(artifactNode, | |
432 "states/transition"); | |
433 this.transitions = new ArrayList<Transition>(transitionList.getLength()); | |
434 for (int i = 0; i < transitionList.getLength(); i++) { | |
435 Transition tmpTransition = TransitionFactory.getInstance() | |
436 .createTransition(transitionList.item(i)); | |
437 if (tmpTransition != null) { | |
438 this.transitions.add(tmpTransition); | |
439 } | |
440 } | |
441 | |
442 } | |
443 } | |
444 | |
445 | |
446 protected Document createDescibeOutput( | |
447 CallContext context, | |
448 String uuid, | |
449 boolean incudeUI | |
450 ) { | |
451 log.debug("GNVArtifactBase.createDescibeOutput"); | |
452 Document document = XMLUtils.newDocument(); | |
453 | |
454 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
455 document, | |
456 ArtifactNamespaceContext.NAMESPACE_URI, | |
457 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
458 ); | |
459 Element rootNode = this.createRootNode(creator, document); | |
460 this.createHeader(creator, rootNode, document, "describe"); | |
461 this.createOutputs(creator, rootNode, document); | |
462 this.createCurrentState(creator, rootNode, document); | |
463 this.createReachableStates(creator, rootNode, document); | |
464 this.createModel(creator, rootNode, document); | |
465 if (incudeUI){ | |
466 this.createUserInterface(creator, rootNode, document, context, uuid); | |
467 } | |
468 | |
469 return document; | |
470 } | |
471 | |
472 protected boolean getIncludeUIFromDocument(Document document){ | |
473 String value = XMLUtils.xpathString( | |
474 document, XPATH_INCLUDE_UI, ArtifactNamespaceContext.INSTANCE); | |
475 | |
476 boolean includeUI = false; | |
477 if (value != null){ | |
478 includeUI = Boolean.parseBoolean(value); | |
479 } | |
480 return includeUI; | |
481 } | |
482 | |
483 protected Element createRootNode( | |
484 XMLUtils.ElementCreator creator, | |
485 Document document | |
486 ) { | |
487 Element rootNode = creator.create("result"); | |
488 document.appendChild(rootNode); | |
489 return rootNode; | |
490 } | |
491 | |
492 protected void createHeader( | |
493 XMLUtils.ElementCreator creator, | |
494 Element parent, | |
495 Document document, | |
496 String documentType | |
497 ) { | |
498 Element typeNode = creator.create("type"); | |
499 creator.addAttr(typeNode, "name", documentType); | |
500 parent.appendChild(typeNode); | |
501 | |
502 Element uuidNode = creator.create("uuid"); | |
503 creator.addAttr(uuidNode, "value", super.identifier); | |
504 parent.appendChild(uuidNode); | |
505 | |
506 Element hashNode = creator.create("hash"); | |
507 creator.addAttr(hashNode, "value", this.hash()); | |
508 parent.appendChild(hashNode); | |
509 } | |
510 | |
511 protected Element createSelectBox( | |
512 XMLUtils.ElementCreator artCreator, | |
513 XMLUtils.ElementCreator creator, | |
514 Document document, | |
515 CallContext context | |
516 ) { | |
517 RessourceFactory resource = RessourceFactory.getInstance(); | |
518 CallMeta callMeta = (CallMeta) context.getMeta(); | |
519 String productName = product.getName(); | |
520 | |
521 Element selectNode = creator.create("select1"); | |
522 creator.addAttr(selectNode, "ref", "product"); | |
523 artCreator.addAttr(selectNode, "state", INITIAL_STATE, true); | |
524 | |
525 | |
526 Element labelNode = creator.create("label"); | |
527 labelNode.setTextContent( | |
528 resource.getRessource(callMeta.getLanguages(), "product", "product") | |
529 ); | |
530 | |
531 Element choicesNode = creator.create("choices"); | |
532 | |
533 Element itemNode = creator.create("item"); | |
534 creator.addAttr(itemNode, "selected", "true"); | |
535 | |
536 Element choiceLabel = creator.create("label"); | |
537 choiceLabel.setTextContent(resource.getRessource( | |
538 callMeta.getLanguages(), | |
539 productName, | |
540 productName | |
541 )); | |
542 | |
543 Element choiceValue = creator.create("value"); | |
544 choiceValue.setTextContent(productName); | |
545 | |
546 itemNode.appendChild(choiceLabel); | |
547 itemNode.appendChild(choiceValue); | |
548 choicesNode.appendChild(itemNode); | |
549 | |
550 selectNode.appendChild(labelNode); | |
551 selectNode.appendChild(choicesNode); | |
552 | |
553 return selectNode; | |
554 } | |
555 | |
556 | |
557 protected void createReachableStates( | |
558 XMLUtils.ElementCreator creator, | |
559 Element parent, | |
560 Document document | |
561 ) { | |
562 Element stateNode = creator.create("reachable-states"); | |
563 if (this.current != null) { | |
564 | |
565 // add future states | |
566 Iterator<Transition> transitions = this.transitions.iterator(); | |
567 while (transitions.hasNext()) { | |
568 Transition tmpTransition = transitions.next(); | |
569 if (tmpTransition.getFrom().equals(current.getID()) && | |
570 tmpTransition.isValid(this.current)){ | |
571 Element currentNode = creator.create("state"); | |
572 creator.addAttr(currentNode, "name", tmpTransition.getTo()); | |
573 creator.addAttr( | |
574 currentNode, | |
575 "description", | |
576 this.states.get(tmpTransition.getTo()).getDescription()); | |
577 stateNode.appendChild(currentNode); | |
578 } | |
579 } | |
580 | |
581 | |
582 // add old states | |
583 appendOldReachableStates(creator, stateNode, current); | |
584 } | |
585 parent.appendChild(stateNode); | |
586 } | |
587 | |
588 | |
589 protected void appendOldReachableStates( | |
590 XMLUtils.ElementCreator creator, | |
591 Element parent, | |
592 State state | |
593 ) { | |
594 if (state == null) | |
595 return; | |
596 | |
597 while (state != null) { | |
598 Element currentNode = creator.create("state"); | |
599 creator.addAttr(currentNode, "name", state.getID()); | |
600 creator.addAttr(currentNode, "description", state.getDescription()); | |
601 parent.appendChild(currentNode); | |
602 | |
603 state = state.getParent(); | |
604 } | |
605 } | |
606 | |
607 | |
608 protected void createCurrentState( | |
609 XMLUtils.ElementCreator creator, | |
610 Element parent, | |
611 Document document | |
612 ) { | |
613 Element stateNode = creator.create("state"); | |
614 creator.addAttr(stateNode, "name", this.current.getID()); | |
615 creator.addAttr(stateNode, "description", this.current.getDescription()); | |
616 parent.appendChild(stateNode); | |
617 } | |
618 | |
619 protected void createModel( | |
620 XMLUtils.ElementCreator creator, | |
621 Element parent, | |
622 Document document | |
623 ) { | |
624 Element modelNode = creator.create("model"); | |
625 if (this.current != null) { | |
626 Collection<InputValue> inputValues = this.current | |
627 .getRequiredInputValues(); | |
628 if (inputValues != null) { | |
629 Iterator<InputValue> it = inputValues.iterator(); | |
630 while (it.hasNext()) { | |
631 InputValue inputValue = it.next(); | |
632 Element inputNode = creator.create("input"); | |
633 creator.addAttr(inputNode, "name", inputValue.getName()); | |
634 creator.addAttr(inputNode, "type", inputValue.getType()); | |
635 modelNode.appendChild(inputNode); | |
636 } | |
637 } | |
638 } | |
639 parent.appendChild(modelNode); | |
640 } | |
641 | |
642 protected void createUserInterface( | |
643 XMLUtils.ElementCreator creator, | |
644 Element parent, | |
645 Document document, | |
646 CallContext context, | |
647 String uuid | |
648 ) { | |
649 XMLUtils.ElementCreator xCreator = new XMLUtils.ElementCreator( | |
650 document, | |
651 XMLUtils.XFORM_URL, | |
652 XMLUtils.XFORM_PREFIX | |
653 ); | |
654 | |
655 Element uiNode = creator.create("ui"); | |
656 Element staticNode = creator.create("static"); | |
657 Element dynamic = creator.create("dynamic"); | |
658 | |
659 uiNode.appendChild(staticNode); | |
660 uiNode.appendChild(dynamic); | |
661 | |
662 parent.appendChild(uiNode); | |
663 | |
664 // append fis to dynamic part | |
665 appendFis(document, staticNode, context, product.getArtifactFactory()); | |
666 | |
667 if (this.current != null) { | |
668 Element staticUI = createSelectBox( | |
669 creator, xCreator, document, context | |
670 ); | |
671 staticNode.appendChild(staticUI); | |
672 | |
673 this.current.describe( | |
674 document, uiNode, context, uuid | |
675 ); | |
676 } | |
677 } | |
678 | |
679 | |
680 protected void createOutputs( | |
681 XMLUtils.ElementCreator creator, | |
682 Element parent, | |
683 Document document | |
684 ) { | |
685 log.debug("GNVArtifactBase.createOutputs"); | |
686 Element outputsNode = creator.create("outputs"); | |
687 if (this.current instanceof OutputState) { | |
688 Collection<OutputMode> outputModes = ((OutputState) this.current) | |
689 .getOutputModes(); | |
690 if (outputModes != null) { | |
691 Iterator<OutputMode> it = outputModes.iterator(); | |
692 while (it.hasNext()) { | |
693 OutputMode outputMode = it.next(); | |
694 log.debug("Write Outputnode for " + outputMode.toString()); | |
695 Element outputModeNode = creator.create("output"); | |
696 creator.addAttr( | |
697 outputModeNode, "name", outputMode.getName()); | |
698 creator.addAttr( | |
699 outputModeNode, "description", outputMode.getDescription()); | |
700 creator.addAttr( | |
701 outputModeNode, "mime-type", outputMode.getMimeType()); | |
702 outputsNode.appendChild(outputModeNode); | |
703 | |
704 Collection<InputValue> inputParameters = outputMode | |
705 .getInputParameters(); | |
706 if (inputParameters != null) { | |
707 Element inputParametersNode = creator.create("parameter"); | |
708 outputModeNode.appendChild(inputParametersNode); | |
709 Iterator<InputValue> it2 = inputParameters.iterator(); | |
710 while (it2.hasNext()) { | |
711 InputValue inputValue = it2.next(); | |
712 Element inputParameterNode = | |
713 creator.create("parameter"); | |
714 creator.addAttr( | |
715 inputParameterNode, "name", inputValue.getName()); | |
716 creator.addAttr( | |
717 inputParameterNode, "type", inputValue.getType()); | |
718 creator.addAttr( | |
719 inputParameterNode, "value", inputValue.getDefaultValue()); | |
720 inputParametersNode.appendChild(inputParameterNode); | |
721 } | |
722 } | |
723 | |
724 // append export modes | |
725 List<ExportMode> exportModes = outputMode.getExportModes(); | |
726 if (exportModes != null) { | |
727 Element exports = creator.create("exports"); | |
728 outputModeNode.appendChild(exports); | |
729 | |
730 for (ExportMode exp: exportModes) { | |
731 Element export = creator.create("export"); | |
732 creator.addAttr( | |
733 export, "name", exp.getName()); | |
734 creator.addAttr( | |
735 export, "description", exp.getDescription()); | |
736 creator.addAttr( | |
737 export, "mime-type", exp.getMimeType()); | |
738 | |
739 exports.appendChild(export); | |
740 } | |
741 } | |
742 } | |
743 } else { | |
744 log.warn("No Outputmodes given."); | |
745 } | |
746 } | |
747 parent.appendChild(outputsNode); | |
748 } | |
749 | |
750 protected Collection<InputData> parseInputData(Document document, | |
751 String xPath) { | |
752 HashMap<String, InputData> returnValue = null; | |
753 | |
754 NodeList inputElemets = (NodeList) XMLUtils.xpath(document, xPath, | |
755 XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE); | |
756 if (inputElemets != null) { | |
757 returnValue = new HashMap<String, InputData>(); | |
758 | |
759 for (int i = 0; i < inputElemets.getLength(); i++) { | |
760 Element inputDataNode = (Element)inputElemets.item(i); | |
761 String name = inputDataNode.getAttribute("name"); | |
762 String value = inputDataNode.getAttribute("value"); | |
763 | |
764 if (returnValue.containsKey(name)) { | |
765 InputData inputData = returnValue.get(name); | |
766 inputData.concartValue(value); | |
767 log.debug(inputData.toString()); | |
768 returnValue.put(name, inputData); | |
769 } else { | |
770 InputData inputData = new DefaultInputData(name, value); | |
771 | |
772 returnValue.put(name, inputData); | |
773 } | |
774 } | |
775 } | |
776 return returnValue.values(); | |
777 } | |
778 | |
779 /** | |
780 * @see de.intevation.artifactdatabase.DefaultArtifact#out(org.w3c.dom.Document, | |
781 * java.io.OutputStream, de.intevation.artifacts.CallContext) | |
782 */ | |
783 @Override | |
784 public void out(Document format, OutputStream outputStream, | |
785 CallContext context) throws IOException { | |
786 log.debug("TGNVArtifactBase.out"); | |
787 try { | |
788 | |
789 if (current != null && current instanceof OutputState) { | |
790 ((OutputState) current) | |
791 .out(format, this.parseInputData( | |
792 format, XPATH_OUTPUT_PARAMS), | |
793 outputStream, super.identifier, context); | |
794 } | |
795 } catch (StateException e) { | |
796 log.error(e, e); | |
797 throw new IOException(e.getMessage()); | |
798 } | |
799 } | |
800 | |
801 protected static String readOutputType(Document document) { | |
802 String value = XMLUtils.xpathString( | |
803 document, XPATH_OUTPUT_NAME, ArtifactNamespaceContext.INSTANCE); | |
804 return value; | |
805 } | |
806 | |
807 | |
808 public void setProduct(Product product) { | |
809 this.product = product; | |
810 } | |
811 | |
812 public void endOfLife(Object globalContext) { | |
813 super.endOfLife(globalContext); | |
814 | |
815 if (current != null) { | |
816 current.endOfLife(globalContext); | |
817 } | |
818 } | |
819 | |
820 /** | |
821 * @see de.intevation.gnv.artifacts.PreSettingArtifact#getPreSettings() | |
822 */ | |
823 public Map<String, InputData> getPreSettings() { | |
824 return this.preSettings; | |
825 } | |
826 | |
827 /** | |
828 * @see de.intevation.gnv.artifacts.PreSettingArtifact#setPreSettings(java.util.Map) | |
829 */ | |
830 public void setPreSettings(Map<String, InputData> preSettings) { | |
831 this.preSettings = preSettings; | |
832 if (this.current != null){ | |
833 this.current.setPreSettings(preSettings); | |
834 } | |
835 } | |
836 } | |
837 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |