rnolen
rnolen

Reputation: 141

Nodes added to a page are not being saved in CQ

I have a service that's attempting to import blog pages into CQ 5.5.0. I am able to successfully create the page, but when I add nodes representing the content the nodes are not being saved. No errors are reported by CQ and I can see the nodes in the service immediately after creation. But when I look at the page in CRXDE Light the nodes are not part of the page content. The section of code that adds the nodes is here:

    Node blogNode = blogPage.adaptTo(Node.class);
    logOutput( INFO, "blogPage name = "+ blogPage.getName() );
    // Create the author date node
    Node authorDateNode = blogNode.addNode("jcr:content/authorDate", "nt:unstructured");
    authorDateNode.setProperty("author", blog.getCreator());
    authorDateNode.setProperty("date", sdf.format(blog.getPublishDate().getTime()));
    authorDateNode.setProperty("sling:resourceType", "history/components/blog/authordate");

    // Create the content node
    Node blogPostNode = blogNode.addNode("jcr:content/blogPostBodyParSys", "nt:unstructured");
    blogPostNode.setProperty("sling:resourceType", "history/components/parsys");

    Node blogContentNode = blogNode.addNode("jcr:content/blogPostBodyParSys/text", "nt:unstructured");
    blogContentNode.setProperty("sling:resourceType", "history/components/text");
    blogContentNode.setProperty("text", blog.getContent());
    blogContentNode.setProperty("textIsRich", "true");

    // TODO: Test code only
    NodeIterator itr = blogNode.getNode("jcr:content").getNodes();
    while(itr.hasNext()) {
        Node child = itr.nextNode();
        logOutput(INFO, "Child node: " + child.getName(), 1 );
        PropertyIterator propItr = child.getProperties();
        while( propItr.hasNext() ) {
            Property prop = propItr.nextProperty();
            logOutput(INFO, "Property " + prop.getName() + ", value " + prop.getValue().getString(),2);
        }
    }

The test code at the bottom displays the newly created nodes and it shows values as expected. The last thing that occurs is a call to 'session.save' before the service exits.

No errors are reported but I do not see the nodes when I look at the page. Does anyone have any idea about what might be wrong here?

Upvotes: 0

Views: 1797

Answers (2)

rnolen
rnolen

Reputation: 141

I appreciate the input and I finally figured out what was causing my problem: I had created two ResourceResolver instances so the session I was saving was apparently a different session from where the nodes were being created. And that session was not being saved.

Upvotes: 0

yash ahuja
yash ahuja

Reputation: 480

As pointed by @Sharath Maddapa you need to save the session. See the changes done in your code.

    Node blogNode = blogPage.adaptTo(Node.class);
        logOutput( INFO, "blogPage name = "+ blogPage.getName() );
        // Create the author date node
        Node authorDateNode = blogNode.addNode("jcr:content/authorDate", "nt:unstructured");
        authorDateNode.setProperty("author", blog.getCreator());
        authorDateNode.setProperty("date", sdf.format(blog.getPublishDate().getTime()));
        authorDateNode.setProperty("sling:resourceType", "history/components/blog/authordate");

        // Create the content node
        Node blogPostNode = blogNode.addNode("jcr:content/blogPostBodyParSys", "nt:unstructured");
        blogPostNode.setProperty("sling:resourceType", "history/components/parsys");

        Node blogContentNode = blogNode.addNode("jcr:content/blogPostBodyParSys/text", "nt:unstructured");
        blogContentNode.setProperty("sling:resourceType", "history/components/text");
        blogContentNode.setProperty("text", blog.getContent());
        blogContentNode.setProperty("textIsRich", "true");
//YOU must save the session here.
    try {
    blogNode.getSession().save();
    } catch(Exception e) {// TODO Ideally log specific exceptions
    logOutput( ERROR, "Error saving jcr session ");
    }
        // TODO: Test code only
        NodeIterator itr = blogNode.getNode("jcr:content").getNodes();
        while(itr.hasNext()) {
            Node child = itr.nextNode();
            logOutput(INFO, "Child node: " + child.getName(), 1 );
            PropertyIterator propItr = child.getProperties();
            while( propItr.hasNext() ) {
                Property prop = propItr.nextProperty();
                logOutput(INFO, "Property " + prop.getName() + ", value " + prop.getValue().getString(),2);
            }
        }

Upvotes: 1

Related Questions