Skip to content

Instantly share code, notes, and snippets.

@toodooleedoo
Last active March 23, 2021 22:23
Show Gist options
  • Save toodooleedoo/87a196817fee1ca6ca11 to your computer and use it in GitHub Desktop.
Save toodooleedoo/87a196817fee1ca6ca11 to your computer and use it in GitHub Desktop.
AEM Just playing with adding properties via Node

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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment