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
11
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
20 final String stepctx = StringUtils.trimToNull(node.valueOf("stepctx"));
21 final WorkflowStepState foundStep = lookupContext(stepctx, workflowState);
22
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
35
36
37
38
39
40
41 public static WorkflowStepState lookupContext(final String stepctx, final WorkflowState initial) {
42 final String[] parts = stepctx.split("/");
43
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 }