class CaseManagementSetChildCommand extends org.kie.workbench.common.stunner.core.client.canvas.command.SetChildrenCommand {

    @Override
    @SuppressWarnings("unchecked")
    protected Command<GraphCommandExecutionContext, RuleViolation> newGraphCommand(final AbstractCanvasHandler context) {
        // Get the original graph index
        OptionalInt originalIndex = originalParent
                .map(p -> OptionalInt.of(getChildGraphIndex(p, getCandidate())))
                .orElseGet(OptionalInt::empty);

        OptionalInt index = last.map(s -> {
            // Get the index from the last node
            int i = getChildGraphIndex(parent, s);
            if (originalIndex.isPresent() && originalIndex.getAsInt() < i) {
                // If move the node forward
                return OptionalInt.of(i);
            } else {
                return OptionalInt.of(i + 1);
            }
        }).orElseGet(() -> {
            if (canvasIndex.isPresent() && canvasIndex.getAsInt() == 0 ) {
                if (isStage(parent, getCandidate())) {
                    // If add a Stage to the start, find the StartNoneEvent
                    List<Node<View<?>, Edge>> childNodes = ((Node<View<?>, Edge>) parent).getOutEdges().stream()
                            .map(e -> (Node<View<?>, Edge>) e.getTargetNode()).collect(Collectors.toList());
                    for (int i = 0, n = childNodes.size(); i < n; i++) {
                        if (childNodes.get(i).getContent().getDefinition() instanceof StartNoneEvent) {
                            return OptionalInt.of(++i);
                        }
                    }
                    return OptionalInt.of(0);
                } else {
                    return canvasIndex;
                }
            } else {
                return OptionalInt.empty();
            }
        });

        return new CaseManagementSetChildNodeGraphCommand(parent,
                                                          getCandidate(),
                                                          index,
                                                          originalParent,
                                                          originalIndex);
    }

    public  CaseManagementSetChildCommand(final Node<View<?>, Edge> parent,
                                         final Node<View<?>, Edge> child);
    public  CaseManagementSetChildCommand(final Node<View<?>, Edge> parent,
                                         final Node<View<?>, Edge> child,
                                         final Optional<Node<View<?>, Edge>> last,
                                         final OptionalInt canvasIndex,
                                         final Optional<Node<View<?>, Edge>> originalParent,
                                         final OptionalInt originaCanvaslIndex);

}