1
votes

When using Apache Sling and CRX/CQ5 and JCR etc...

is it possible to iterate over the CQ5 page nodes in the JCR and rename the pages.

I currently have a script that can change properties within all child pages at a specific path.

I am desperately looking for a way to use the NodeIterator and Node classes to rename each page (not only the title and an arbitrary property but the name that forms the path) at the the specific path.

Example:

-content/xproject/shared/cars/a/abegro-assam
-content/xproject/shared/cars/m/motofuel-iss

I want to do something equivalent to:

while(cars.hasNext()) {
   Node node = cars.nextNode();

   //this is the functionality I want somehow...
   node.setName("some-other-name");

   //similar to how we would set JCR properties
   node.setProperty("someProperty", "someValue");
}

Please advise as to somewhere where I can find this functionality within the CQ5/Sling/Apache/CRX stacks as this will be very helpful.

I am aware that I may be able to adaptTo() some other class other that node - but am desperately unsure as to how to proceed.

2

2 Answers

4
votes

To rename JCR nodes you move them to a different path, you can use either Session.move(...) which requires a save() call, or Workspace.move(...) which acts immediately.

I'm not sure if and how those interfere with the current NodeIterator - if there's trouble with that you could use the iteration to just add the paths of the Nodes that you want to rename to a List, and then iterate over that List outside of the NodeIterator to rename the nodes.

0
votes
void rename(Node node, String newName) throws RepositoryException 
{
    node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + newName);
    // Don't forget - not necessarily here at this place:
    // node.getSession().save();
}