comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java @ 3193:2f922be407ea

Moved common code of the *Artifacts into FLYSArtifact. flys-artifacts/trunk@4809 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 27 Jun 2012 13:45:55 +0000
parents 7dc4681a2bed
children 1b9f791937c3
comparison
equal deleted inserted replaced
3192:cd309f8597f6 3193:2f922be407ea
31 import de.intevation.flys.artifacts.context.FLYSContext; 31 import de.intevation.flys.artifacts.context.FLYSContext;
32 32
33 import de.intevation.flys.artifacts.states.DefaultState; 33 import de.intevation.flys.artifacts.states.DefaultState;
34 import de.intevation.flys.artifacts.states.DefaultState.ComputeType; 34 import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
35 35
36 import de.intevation.artifactdatabase.ProtocolUtils;
37
36 import de.intevation.flys.utils.FLYSUtils; 38 import de.intevation.flys.utils.FLYSUtils;
39
40 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
37 41
38 import java.util.ArrayList; 42 import java.util.ArrayList;
39 import java.util.Collection; 43 import java.util.Collection;
40 import java.util.HashMap; 44 import java.util.HashMap;
41 import java.util.HashSet; 45 import java.util.HashSet;
42 import java.util.List; 46 import java.util.List;
47 import java.util.LinkedList;
43 import java.util.Map; 48 import java.util.Map;
44 import java.util.Set; 49 import java.util.Set;
45 import java.util.TreeMap; 50 import java.util.TreeMap;
46 51
47 import javax.xml.xpath.XPathConstants; 52 import javax.xml.xpath.XPathConstants;
51 import org.apache.log4j.Logger; 56 import org.apache.log4j.Logger;
52 57
53 import org.w3c.dom.Document; 58 import org.w3c.dom.Document;
54 import org.w3c.dom.Element; 59 import org.w3c.dom.Element;
55 import org.w3c.dom.NodeList; 60 import org.w3c.dom.NodeList;
61 import org.w3c.dom.Node;
62
63 import de.intevation.artifacts.Message;
64 import de.intevation.flys.artifacts.model.CalculationMessage;
56 65
57 /** 66 /**
58 * The default FLYS artifact with convenience added. 67 * The default FLYS artifact with convenience added.
59 * (Subclass to get fully functional artifacts). 68 * (Subclass to get fully functional artifacts).
60 * 69 *
114 */ 123 */
115 public FLYSArtifact() { 124 public FLYSArtifact() {
116 data = new TreeMap<String, StateData>(); 125 data = new TreeMap<String, StateData>();
117 previousStateIds = new ArrayList<String>(); 126 previousStateIds = new ArrayList<String>();
118 facets = new HashMap<String, List<Facet>>(); 127 facets = new HashMap<String, List<Facet>>();
128 }
129
130 /**
131 * This method appends the static data - that has already been inserted by
132 * the user - to the static node of the DESCRIBE document.
133 *
134 * @param doc The document.
135 * @param ui The root node.
136 * @param context The CallContext.
137 * @param uuid The identifier of the artifact.
138 */
139 protected void appendStaticUI(
140 Document doc,
141 Node ui,
142 CallContext context,
143 String uuid)
144 {
145 List<String> stateIds = getPreviousStateIds();
146
147 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
148 StateEngine engine = (StateEngine) flysContext.get(
149 FLYSContext.STATE_ENGINE_KEY);
150
151 for (String stateId: stateIds) {
152 logger.debug("Append static data for state: " + stateId);
153 DefaultState state = (DefaultState) engine.getState(stateId);
154
155 ui.appendChild(state.describeStatic(this, doc, ui, context, uuid));
156 }
119 } 157 }
120 158
121 159
122 /** 160 /**
123 * Returns the name of the concrete artifact. 161 * Returns the name of the concrete artifact.
372 410
373 result.setTextContent(iae.getMessage()); 411 result.setTextContent(iae.getMessage());
374 } 412 }
375 413
376 return doc; 414 return doc;
415 }
416
417 /**
418 * This method returns a description of this artifact.
419 *
420 * @param data Some data.
421 * @param context The CallContext.
422 *
423 * @return the description of this artifact.
424 */
425 public Document describe(Document data, CallContext context) {
426 logger.debug("Describe: the current state is: " + getCurrentStateId());
427
428 if (logger.isDebugEnabled()) {
429 dumpArtifact();
430 }
431
432 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
433
434 StateEngine stateEngine = (StateEngine) flysContext.get(
435 FLYSContext.STATE_ENGINE_KEY);
436
437 TransitionEngine transitionEngine = (TransitionEngine) flysContext.get(
438 FLYSContext.TRANSITION_ENGINE_KEY);
439
440 List<State> reachable = transitionEngine.getReachableStates(
441 this, getCurrentState(context), stateEngine);
442
443 Document description = XMLUtils.newDocument();
444 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
445 description,
446 ArtifactNamespaceContext.NAMESPACE_URI,
447 ArtifactNamespaceContext.NAMESPACE_PREFIX);
448
449 Element root = ProtocolUtils.createRootNode(creator);
450 description.appendChild(root);
451
452 State current = getCurrentState(context);
453
454 ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());
455 ProtocolUtils.appendState(creator, root, current);
456 ProtocolUtils.appendReachableStates(creator, root, reachable);
457
458 appendBackgroundActivity(creator, root, context);
459
460 Element ui = ProtocolUtils.createArtNode(
461 creator, "ui", null, null);
462
463 Element staticUI = ProtocolUtils.createArtNode(
464 creator, "static", null, null);
465
466 Element outs = ProtocolUtils.createArtNode(
467 creator, "outputmodes", null, null);
468 appendOutputModes(description, outs, context, identifier());
469
470 appendStaticUI(description, staticUI, context, identifier());
471
472 Element name = ProtocolUtils.createArtNode(
473 creator, "name",
474 new String[] { "value" },
475 new String[] { getName() });
476
477 Element dynamic = current.describe(
478 this,
479 description,
480 root,
481 context,
482 identifier());
483
484 if (dynamic != null) {
485 ui.appendChild(dynamic);
486 }
487
488 ui.appendChild(staticUI);
489
490 root.appendChild(name);
491 root.appendChild(ui);
492 root.appendChild(outs);
493
494 return description;
495 }
496
497 /** Override me! */
498
499 protected void appendBackgroundActivity(
500 ElementCreator cr,
501 Element root,
502 CallContext context
503 ) {
504 LinkedList<Message> messages = context.getBackgroundMessages();
505
506 if (messages == null) {
507 return;
508 }
509
510 Element inBackground = cr.create("background-processing");
511 root.appendChild(inBackground);
512
513 cr.addAttr(
514 inBackground,
515 "value",
516 String.valueOf(context.isInBackground()),
517 true);
518
519 CalculationMessage message = (CalculationMessage) messages.getLast();
520 cr.addAttr(
521 inBackground,
522 "steps",
523 String.valueOf(message.getSteps()),
524 true);
525
526 cr.addAttr(
527 inBackground,
528 "currentStep",
529 String.valueOf(message.getCurrentStep()),
530 true);
531
532 inBackground.setTextContent(message.getMessage());
533 }
534
535 /**
536 * Append output mode nodes to a document.
537 */
538 protected void appendOutputModes(
539 Document doc,
540 Element outs,
541 CallContext context,
542 String uuid)
543 {
544 List<Output> generated = getOutputs(context);
545 logger.debug("This Artifact has " + generated.size() + " Outputs.");
546
547 ProtocolUtils.appendOutputModes(doc, outs, generated);
377 } 548 }
378 549
379 550
380 /** 551 /**
381 * This method handles request for changing the current state of an 552 * This method handles request for changing the current state of an
1296 * defaults to "1". 1467 * defaults to "1".
1297 */ 1468 */
1298 public int getInitialFacetActivity( 1469 public int getInitialFacetActivity(
1299 String outputName, 1470 String outputName,
1300 String facetName, 1471 String facetName,
1301 int index) 1472 int index
1473 )
1302 { 1474 {
1303 return 1; 1475 return 1;
1304 } 1476 }
1305 } 1477 }
1306 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 1478 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org