View Javadoc

1   package org.rundeck.api.parser;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.dom4j.Node;
5   import org.rundeck.api.domain.WorkflowState;
6   import org.rundeck.api.domain.WorkflowStepContextState;
7   import org.rundeck.api.domain.WorkflowStepState;
8   
9   /**
10   * Returns a WorkflowStepContextState by looking up the given Rundeck node's state in the workflow, using the step
11   * context path of the "stepctx" element of the selected DOM node.
12   */
13  public class IndexedWorkflowStepStateParser implements XmlNodeParser<WorkflowStepContextState> {
14      private final WorkflowState workflowState;
15      private String rundeckNodeName;
16  
17      @Override
18      public WorkflowStepContextState parseXmlNode(final Node node) {
19          //look for workflow step state based on node name and stepctx found on the node
20          final String stepctx = StringUtils.trimToNull(node.valueOf("stepctx"));
21          final WorkflowStepState foundStep = lookupContext(stepctx, workflowState);
22          //look up node state for this node
23          if (null != foundStep
24                  && null != foundStep.getNodeStates()
25                  && null != foundStep.getNodeStates().get(rundeckNodeName)) {
26              return foundStep.getNodeStates().get(rundeckNodeName);
27          }
28  
29  
30          return null;
31      }
32  
33      /**
34       * look up the workflow step state for the step context, from the root workflow
35       *
36       * @param stepctx
37       * @param initial
38       *
39       * @return
40       */
41      public static WorkflowStepState lookupContext(final String stepctx, final WorkflowState initial) {
42          final String[] parts = stepctx.split("/");
43          //descend workflow steps to find correct step
44          WorkflowState current = initial;
45          WorkflowStepState currentStep = null;
46          for (int i = 0; i < parts.length; i++) {
47              final String part = parts[i];
48              final WorkflowStepState workflowStepState = current.getSteps().get(Integer.parseInt(part) - 1);
49              currentStep = workflowStepState;
50              if (i < parts.length - 1) {
51                  current = currentStep.getSubWorkflow();
52              }
53          }
54          return currentStep;
55      }
56  
57      public IndexedWorkflowStepStateParser(final WorkflowState workflowState, final String rundeckNodeName) {
58          this.workflowState = workflowState;
59          this.rundeckNodeName = rundeckNodeName;
60      }
61  }