Skip to content

Instantly share code, notes, and snippets.

@toodooleedoo
Last active June 18, 2017 01:52
Show Gist options
  • Save toodooleedoo/f73141b316503382edc2 to your computer and use it in GitHub Desktop.
Save toodooleedoo/f73141b316503382edc2 to your computer and use it in GitHub Desktop.
#AEM #JAVA #JSP XPath Play
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;
import javax.jcr.query.QueryManager;
import javax.jcr.query.Query;
import javax.jcr.query.QueryResult;
public class QueryPlay {
private final static Logger LOG = LoggerFactory.getLogger(NodePlay.class.getName());
public static void sample(Session jcrSess) {
try {
String stmt="/jcr:root/content/main//*[jcr:contains(., 'findme')] order by @jcr:score descending";
Query query = jcrSess.getWorkspace().getQueryManager().createQuery(stmt, Query.XPATH);
QueryResult results = query.execute();
if (results.getNodes() != null && results.getNodes().hasNext()) {
LOG.error("Query "+stmt+"\n");
NodeIterator it = results.getNodes();
while(it.hasNext()) {
Node node = it.nextNode();
try { //tried if(node.getName() == "jcr:content") and it doesn't work
node.setProperty("QueryPlay", "yes");
LOG.error("setting property QueryPlay to yes");
//Would remove the node
//node.setProperty("QueryPlay","null");
} catch(Exception e) {
LOG.error("exception: "+e);
}
LOG.error(node.getName()+"|"+node.getPath());
}
jcrSess.save();
}
} catch(Exception e) {
LOG.error("Caught Exception: "+e);
}
}
}
<%@include file="/apps/main/global.jsp"%>
<%@ page import="org.main.test.NodePlay" %>
<%@ page import="org.main.test.QueryPlay" %>
<%
Session userSession = slingRequest.getResourceResolver().adaptTo(Session.class);
QueryPlay qp = new QueryPlay();
qp.sample(userSession);
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment