Hello guys, how are you?
I'm trying to programmatically delete nodes in a given path using a Java Bundle.
My bundle code is:
public boolean deleteNodes(String path){ try { //Create a connection to the CQ repository running on local host Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server"); //Create a Session javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray())); //Create a node that represents the root node Node root = session.getRootNode(); // Retrieve content Node node = root.getNode(path); for (NodeIterator ni = node.getNodes(); ni.hasNext(); ) { Node childNode = ni.nextNode(); childNode.remove(); } // Save the session changes and log out session.save(); session.logout(); return true; } catch(Exception e){ e.printStackTrace(); } return false; }
And on my AEM, I've created a Template/Component that has the following code:
<html> <head> <title>Node Delete</title> </head> <body> <form action="deletenodeimpl.jsp" method="POST"> <input type="text" id="path" name="path" /> <input type="submit" value="Delete Child Nodes" /> </form> </body> </html>
My deletenodeimpl.jsp is on the same folder (the component deletenode) of the previous jsp file and the code is:
<% String path = request.getParameter("path"); DeleteNodesImpl deleteNodes = new DeleteNodesImpl(); if(deleteNodes.deleteNodes(path){ alert("success"); }else{ alert("error"); } %>
I do import my bundle (that is successfully builded), but upon clicking the submit, I get an "Error while processing /content/portal/deletenodeimpl.jsp" page, and my error.log file logs:
13.02.2017 16:09:47.827 *ERROR* [0:0:0:0:0:0:0:1 [1487009387825] POST /content/portal/deletenodeimpl.jsp HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Exception during response processing. javax.jcr.nodetype.ConstraintViolationException: No matching property definition: path = /apps/portal/testfolder
- /apps/portal/testfolder is the path I'm inputing on my page, and it exists on my CRX.
So, can anyone tell me whats wrong or if there is a better way to supply child node deletion upon path input?
Thanks a lot.