To start with a very typical answer, 'IT DEPENDS'.
Consider the following scenarios for your understanding:
Scenario 1: Read the title of the page which is containing the current resource.
Approach 1: Leverage the awesome Sling API's to work upon all the available context objects like currentPage, resource, pageManager, wcmmode and many more in your Java controller (Sling Model/ WCMUSe class).
// get the page that contains this resource.
// If the resource is a page the resource is returned. Otherwise it
// walks up the parent resources until a page is found.
Page page = pageManager.getContainingPage(resource);
// Check if the returned page object isn't null
if(page != null){
return page.getTitle();
}
Approach 2: Use the JCR API's:
// assign the current resource node to parent Node to check
// if the current resoure in itself is a page
Node parentNode = currentNode;
while (parentNode.getProperty("jcr:PrimaryTpe").getString() != "cq:Page" ){
parentNode = parentNode.getParent();
}
// The page Title
String pageTitle = null;
// find the jcr:content node of the page and return the
// jcr:title property of that node
if(parentNode.hasNode("jcr:content"){
Node jcrContentNode = parentNode.getNode("jcr:Content");
pageTitle = jcrContentNode.getProperty("jcr:title").getValue().getString();
}
return pageTitle;
In this scenario, obviously the Sling API's win by a huge margin on
the ease of access and usability. I have never experienced any
performance issue with the Sling APIs in comparison to the JCR APIs.
Scenario 2: Change the title of the first level page nodes (considering /content/mywebsite/en to be level ZERO) of your website to Upper Case letters.
Approach: In such a requirement where you need to do certain one-time changes to your JCR repository, you should use the JCR APIs by creating a Standalone Java Application to perform such tasks instead of creating an unnecessary component, its controller, an unnecessary page to put this component and then using the Sling API's in the controller to perform these tasks.
//Create a connection to the CQ repository running on local host
Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server");
//Create a Session
Session session = repository.login( new SimpleCredentials("username", "password".toCharArray()),"crx.default");
//Create a node that represents the root node
Node root = session.getRootNode();
// Get the level ZERO node
Node homepageNode = root.getNode("/content/mywebsite/en");
NodeIterator iter = homePageNode.getNodes;
while(iter.hasNext){
// if next node is of primarty type cq:Page
// get its jcr:content node and
// set its jcr:title property to uppercase letters.
}
Rule of Thumb:
If you want access to your AEM repository from within the AEM
application use Sling APIs over JCR APIs they are:
- higher APIs than JCR (have a lot of predefined methods to do a lot of work)
- provide access to all the Global Context objects inside the controller
- very easy to use
but in case if you need to accces your repository for large scale opertaions,
(generally one time changes) choose to work with a standalone Java
application using the JCR APIs.