OrderLineGroup parent = orderElement.getParent();
parent.remove(orderElement);
orderElement.detachFromParent();
if (!parent.isOrder() && parent.getChildren().isEmpty()) {
OrderElement newElement = parent.toLeaf();
if (!order.isCodeAutogenerated()) {
newElement.setCode(UUID.randomUUID().toString());
}
parent.getParent().replace(parent, newElement);
parent.detachFromParent();