0
votes

Is there some simple way how to replace existing node with another node in JCR 2.0?

Due to the ordering of nodes, currently I am doing these steps:

step 1: Find sibling node which is right after existing node i want to replace:

        if (preserveOrdering) {
        NodeIterator iter = parent.getNodes();
        boolean found = false;
        while (iter.hasNext()) {
            if (tempNode.equals(iter.nextNode())) {
                found = true;
                if (iter.hasNext()) {
                    tempNodeSibling = iter.nextNode();
                    break;
                }
            }
        }
        assert found;
    }

step 2: delete existing node:

tempNode.remove();

step 3: Create new node (I am doing clone, but probably node.addNode() method can be used, new node is appended to the end of the child node list):

workspace.clone(workspace.getName(), existingNodePath, tempNodePath, false);

step 4: Move new node before his old tempNode sibling (remebered in the first step)

parent.orderBefore(tempNodeName, tempNodeSibling.getName());

All these steps looks to me quite cumbersome. But I cannot find in JCR API better way. Mainly because there is only one method orderBefore() working with ordering.

Do you think there is some different/more simple approach for solving this problem?

1

1 Answers

0
votes

Unfortunately, with JCR 2.0 new nodes are always added at the end and using javax.jcr.Node.reorder(...) is the only way to change the position of a child node within the parent's list of children. It is inconvenient to say the least, but I suspect adding such methods would have added too much complexity to an already complicated API.