user2142786
user2142786

Reputation: 1482

How to create a directory on the basis of path in cq5?

I have a String which is the path of the page for example /content/xperia/public/events/eventeditor. I am gererating the XML of this page and saving it to DAM, but I want to save it in the similar tree structure under /content.

I tried the following code

String page = "/content/xperia/public/events/eventeditor";
page = page.replace("/content", "/content/dam");
if (adminSession.nodeExists(page+ "/"+ "jcr:content")) {
    Node node = adminSession.getNode(page+ "/"+ "jcr:content");
    node.setProperty("jcr:data", sb.toString());                
} else {
    Node feedNode = JcrUtil.createPath(page,"nt:file", adminSession);           
    Node dataNode = JcrUtil.createPath(feedNode.getPath() + "/"+ "jcr:content", "nt:resource", adminSession);       
    dataNode.setProperty("jcr:data",sb.toString());
}

But it gives the following error

No matching child node definition found for {http://www.jcp.org/jcr/1.0}content

Because there is no such path in the repository. Is there a way through which I can create a directory on the fly. Because to save this file, I need to create the entire tree xperia/public/events under /content/dam and then save eventeditor.xml in that directory .

Please suggest.

Upvotes: 2

Views: 5253

Answers (1)

rakhi4110
rakhi4110

Reputation: 9281

There are a few issues with your code. The JcrUtil.createPath(String absolutePath, String nodeType, Session session) creates all the non-existent intermediate path with the given NodeType.

This means that all the nodes xperia, public and events are created with type nt:file instead of sling:OrderedFolder.

You can use the createPath(String absolutePath, boolean createUniqueLeaf, String intermediateNodeType, String nodeType, Session session, boolean autoSave) method instead, to specify the type of intermediary nodes that are to be created.

String page = "/content/xperia/public/events/eventeditor";
page = page.replace("/content", "/content/dam");
page += ".xml";

if (adminSession.nodeExists(page+ "/"+ "jcr:content")) {
    Node node = adminSession.getNode(page+ "/"+ "jcr:content");
    node.setProperty("jcr:data", sb.toString());                
} else {
    Node feedNode = JcrUtil.createPath(page, true, "sling:OrderedFolder", "nt:file", adminSession, false);           
    Node dataNode = feedNode.addNode("jcr:content", "nt:resource");       
    dataNode.setProperty("jcr:data",sb.toString());
}

adminSession.save();

Upvotes: 6

Related Questions