Showing posts with label Recursively fetching the nodes in cq. Show all posts
Showing posts with label Recursively fetching the nodes in cq. Show all posts

Sunday, 21 December 2014

CQ/AEM node operations- add,delete,Recursively fetching the nodes

Delete a node

//Below code delete 'textRenderer' node if present under /content/projectname

private static final String TEXT_RENDERER_NODE_URL = "/content/projectname";
rendererNode = currentSession.getNode(TEXT_RENDERER_NODE_URL);
                                          Node delNode = currentSession.getNode(TEXT_RENDERER_NODE_URL + "/textRenderer");
Ensure to save the session after any node modification.

--------------Similar Posts:----------

Add a node
//Below code adds 'textRenderer' node of type 'cq:Page' under /content/projectname

private static final String TEXT_RENDERER_NODE_URL = "/content/projectname";

Node fileNode = rendererNode.addNode("textRenderer", "cq:Page");
Recursively fetching the nodes under CQ node

Below code recursively iterates over a root node and populate the node attributes.

// Node- the root node to be iterated
// Session - current session
 public static void visitRecursively(Node node, Session currentSession) {
                    // get all child nodes
                          NodeIterator list = node.getNodes();
                          while(list.hasNext())   {
                  // get child node
                          Node childNode = list.nextNode();
                          // Verify child node for cqPage type
                          if((childNode.hasProperty("jcr:primaryType")) && (childNode.getProperty("jcr:primaryType").getValue().getString()).equals("cq:Page") ){
                            Node jcrNode = childNode.getNode("jcr:content");
                            // Iterate some of the page properties
                            String articleTitle="";String jcrDesc="";String jcrTitle="";String keywords="";
                                articleTitle = jcrNode.getProperty("articleTitle").getString();
                                jcrDesc = jcrNode.getProperty("jcr:description").getString();
                                jcrTitle = jcrNode.getProperty("jcr:title").getString();
                                keywords = jcrNode.getProperty("keywords").getString();
                            String pagePropertiesString = "articleTitle--->"+articleTitle + "jcr:description--->"+jcrDesc+"jcr:title--->"+jcrTitle + "keywords--->"+keywords ;
                "Page Properties :---> Node "+ childNode.getName()+ "Properties : " + pagePropertiesString );
                          catch (RepositoryException rpe){
                    "Exception in recursive listing:");