1
votes

I am new to jBPM and struggling to find out the way to pull the available outgoing transition for the current user task. For example, if I have workflow as:

enter image description here

Now If I am assigned a Review Task, I have option to either revise it, reject it or approve it. What I want to do is I want to dynamically pull the available outgoing transitions(Revise, Reject, Approve) and show it to user taking action on the task dynamically from any jBPM service. Please guide me.

1

1 Answers

2
votes

You want to get the transitions for the next divergent gateway node.

First you have to get the active node (note there could be more than one depending on the complexity of the workflow)

public static List<Node> getProcessActiveNodeList(final StatefulKnowledgeSession inSession,
                                                                 final WorkflowProcessInstanceImpl inInstance) {
    final List<Node> nodes = new ArrayList<>();
    final WorkflowProcess process = (WorkflowProcess) inSession.getKnowledgeBase().getProcess(inInstance.getProcessId());
    for (Node node : process.getNodes()) {
        if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
            // a free-standing event, without an entry point;  this would be a start of an "optional" branch
            nodes.add(node);
        } else {
            // a node that has an inbound connection;  all nodes on the main branch are of this kind
            List<NodeInstance> nodeInstances = inInstance.getNodeInstances(node.getId());
            if (nodeInstances != null && !nodeInstances.isEmpty()) {
                for (NodeInstance nodeInstance : nodeInstances) {
                    Node nodeInstanceNode = process.getNode(nodeInstance.getNodeId());
                    nodes.add(0, nodeInstanceNode);
                }
            }
        }
    }
    return nodes;
}

Next you need to get the constraints for the next divergent gateway node:

public static Map<ConnectionRef, Constraint> getNextGatewayConstraints(final StatefulKnowledgeSession inSession,
                                                                       final WorkflowProcessInstanceImpl inInstance,
                                                                       final Node inTaskNode) {
    final Map<ConnectionRef, Constraint> constraints = new HashMap<>();
    final WorkflowProcess process = (WorkflowProcess) inSession.getKnowledgeBase().getProcess(inInstance.getProcessId());
    for (Node node : process.getNodes()) {
        if (!node.equals(inTaskNode)) {
            continue;
        }
        final List<Connection> nodeConnections = node.getOutgoingConnections(org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE);
        if (nodeConnections != null && !nodeConnections.isEmpty()) {
            for (Connection c : nodeConnections) {
                final Node nextNode = c.getTo();
                if (nextNode instanceof Split) {
                    constraints.putAll(((Split) nextNode).getConstraints());
                    return constraints;
                }
            }
        }
        break;
    }
    return constraints;
}

Each ConnectionRef points to a Node and the Constraint should contain the conditions (revise, reject, accept)