2013-03-30 63 views
1

當使用Apache Sling和CRX/CQ5和JCR等...CQ5 CRX JCR - 節點迭代 - 如何重命名節點?

是否有可能遍歷JCR中的CQ5頁面節點並重命名頁面。

我目前有一個腳本,可以在特定路徑的所有子頁面內更改屬性。

我拼命尋找一種方法來使用NodeIterator和Node類在特定路徑上重命名每個頁面(不僅是標題和任意屬性,而是形成路徑的名稱)。

例子:

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

我想要做的東西相當於:

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"); 
} 

請告知爲地方在那裏我能找到的CQ5 /吊帶/阿帕奇/ CRX在這個功能棧這將是非常有幫助的。

我知道我可能能夠adaptTo()其他一些其他類的節點 - 但絕望地不知道如何繼續。

回答

4

要將JCR節點重命名爲不同的路徑,可以使用需要save()調用的Session.move(...)或立即行爲的Workspace.move(...)。

我不知道是否以及如何幹擾當前的NodeIterator - 如果遇到問題,您可以使用迭代來添加要重命名爲List的節點的路徑,然後迭代該列表在NodeIterator之外以重命名節點。

0
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(); 
}