jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jukka Zitting" <jukka.zitt...@gmail.com>
Subject Re: Moving nodes from one parent to another?
Date Fri, 29 Dec 2006 09:18:06 GMT
Hi,

On 12/29/06, Sridhar Raman <sridhar.raman@gmail.com> wrote:
> I have the following two nodes, with the children:
> /AB:Entities/AB:entity
> /AB:Entities/AB:entity[2]
> /AB:Entities/AB:entity[3]
> ...
> ...
> /AB:Entities[2]/AB:entity
> /AB:Entities[2]/AB:entity[2]
> /AB:Entities[2]/AB:entity[3]
> ...
> ...
>
> I want to put all the AB:entity nodes under a single parent - AB:Entities.
> So, how do I do the moving?
>
> I tried a session.move("/AB:Entities[2]", "/AB:Entities"). But it does
> nothing. Neither does it throw an exception. When I save the session and
> display the contents, it is as it was earlier. No change!

The move you requested asked to move the /AB:Entities[2] node into a
new root-level node with the name "AB:Entities". Since there already
was another node with the same name, the node ended up as a same-name
sibling, i.e. /AB:Entities[2]. Thus, nothing seemed to change.

The move method can not be used to merge two nodes into one, you need
to explicitly move each child node of /AB:Entities[2] to /AB:Entities.
Use something like this:

    Node root = session.getRootNode();
    Node node1 = root.getNode("AB:Entities");
    Node node2 = root.getNode("AB:Entities[2]");
    NodeIterator iterator = node2.getNodes();
    while (iterator.hasNext()) {
        Node child = iterator.nextNode();
        session.move(child.getPath(), node1.getPath() + "/" + child.getName());
    }
    node2.remove();
    session.save();

This will move all the AB:Entity nodes under /AB:Entities[2] to the
end of the child list of /AB:Entities and remove the remaining empty
/AB:Entities[2] node.

BR,

Jukka Zitting

Mime
View raw message