Playing with adding properties via CQ's Node API.
Not meant to be used as a production solution but gives some handy concepts.
package org.main.test; | |
import javax.jcr.Node; | |
import javax.jcr.NodeIterator; | |
import javax.jcr.Session; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.day.cq.wcm.api.Page; | |
public class NodePlay { | |
private final static Logger LOG = LoggerFactory.getLogger(NodePlay.class.getName()); | |
public static void sample(Page currentPage, Session jcrSess) { | |
try { | |
Node jcrNode = currentPage.getContentResource().adaptTo(Node.class); | |
jcrNode.setProperty("javaProperty", "yes"); | |
LOG.error("set javaProperty properties value to yes of "+currentPage.getName()); | |
if(! jcrNode.hasNode("javaAddNode2")) { | |
jcrNode.addNode("javaAddNode2"); | |
jcrNode.addNode("javaAddNode2/javaAddNode2"); | |
jcrNode.addNode("javaAddNode2/javaAddNode2/javaaddednode"); | |
Node n3 = jcrNode.getNode("javaAddNode2/javaAddNode2"); | |
n3.setProperty("n3added", "yes"); | |
LOG.error("added javaAddNode2 and it's tree"); | |
} else { | |
LOG.error("javaAddNode2 already exists"); | |
} | |
NodeIterator jcrNodeItr = jcrNode.getNodes(); | |
while(jcrNodeItr.hasNext()) { | |
LOG.error("itr"); | |
Node nn = jcrNodeItr.nextNode(); | |
nn.setProperty("itrprop","yes"); | |
LOG.error("added itrprop to: "+nn.getName()); | |
if(nn.getName() != "javaAddNode2") { | |
LOG.error("Removing non javaAddNode2 node: "+nn.getName()); | |
nn.remove(); | |
} | |
} | |
LOG.error("Saving"); | |
jcrSess.save(); | |
} catch(Exception e) { | |
LOG.error("Caught Exception: "+e); | |
} | |
} | |
} |