Thursday, September 21, 2017

AEM code to read content page details from CRX repository

Below is the AEM standalone application code to read page content details from a AEM CRX repository

import java.io.File;
import java.io.PrintStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import javax.jcr.AccessDeniedException;
import javax.jcr.InvalidItemStateException;
import javax.jcr.ItemExistsException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.ReferentialIntegrityException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.version.VersionException;

import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.log4j.Logger;

/**
 * Read Pages information from AEM with in a hierarchy path
 * @author Sailaxman R
 *
 */
public class readPageContentsInfo{

private static Logger LOG = Logger.getLogger(readPageContentsInfo.class);
/**
* AEM SERVER DETAILS
*/
private static final String WORKSPACE     ="crx.default";
private static final String LOGIN_USRNAME   ="admin";
private static final String LOGIN_PASWRD     ="admin";
private static final String SERVER_URL ="http://localhost:4502/crx/server";
/**
* CONTENT HIERARCHY - /content/en, /content/ca
*/
private static final String CONTENT_NODE_PATH    ="/content/ca";
/**
* NODE PROPERTIES
*/
private static final String IS_PAGE   ="cq:Page";
private static final String IS_PAGE_CONTENT   ="cq:PageContent";
private static final String JCR_PRIMARYTYPE   ="jcr:primaryType";
private static final String JCR_LANGUAGE   ="jcr:language";
private static final String CQ_TEMPLATE   ="cq:template";
/**
* JOB START & END TIME
*/
private static DateTimeFormatter dtformater = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
public static List<Node> pageWithChildsLst = null;
private static long PAGE_COUNT = 0;
/**
*
* @param args
* @throws AccessDeniedException
* @throws ItemExistsException
* @throws ReferentialIntegrityException
* @throws ConstraintViolationException
* @throws InvalidItemStateException
* @throws VersionException
* @throws LockException
* @throws NoSuchNodeTypeException
* @throws RepositoryException
*/
public static void main(String[] args) throws AccessDeniedException,ItemExistsException, ReferentialIntegrityException,
ConstraintViolationException, InvalidItemStateException,VersionException, LockException, NoSuchNodeTypeException,
RepositoryException {
Node parentNode;
Session session = null;
long jobStartTime = System.currentTimeMillis();
pageWithChildsLst = new ArrayList<Node>();
try {
System.out.println("######   CONNECTING AEM SERVER    ######");
Repository repository = JcrUtils.getRepository(SERVER_URL);
session = repository.login(new SimpleCredentials(LOGIN_USRNAME, LOGIN_PASWRD.toCharArray()), WORKSPACE);
//session = slingRequest.getResourceResolver().adaptTo(Session.class);

System.out.println("###### AEM CONNECTION SUCCESSFULL ######");
System.out.println("###### USER_ID connected  : " + session.getUserID());
System.out.println("###### SESSION.GETREPOSITORY():"+session.getRepository());

parentNode = session.getNode(CONTENT_NODE_PATH);
if (parentNode.hasNodes()){
System.out.println("###### TRAVERSING PAGE_PATH : "+CONTENT_NODE_PATH);
pageWithChildsLst.add(parentNode);
}

System.out.println("###### JOB STARTED ON :"+dtformater.format(LocalDateTime.now())); // Date & Time Format : 2016/01/12:08:16 43
/**
* Traverse base nodes under it for child pages
*/
getNodeHierarchyData(pageWithChildsLst,session);

pageWithChildsLst = null;
System.out.println("TOTAL PAGES COUNT IS :"+PAGE_COUNT);
PAGE_COUNT = 0;
} catch (Exception exe) {
LOG.error("Exception while connecting AEMLogin :" + exe.getMessage());
}
long jobEndTime = System.currentTimeMillis();
System.out.println("###### JOB ENDED AT :"+dtformater.format(LocalDateTime.now()));
System.out.println("###### TOTAL TIME TAKEN FOR EXECUTION : " + (jobEndTime - jobStartTime) / 1000 + " SECONDS");
System.out.println("########################## FINISH #############################");
}

public static void getNodeHierarchyData(List<Node> parentPageNdList,Session session){
String nodeType =null;
NodeIterator nodeItr =null;
List<Node> currfolderNdsList = new ArrayList<Node>();
try{
for(int j=0;j<parentPageNdList.size();j++){
Node currentNode =parentPageNdList.get(j);
nodeItr = currentNode.getNodes();
System.out.println(" PagePath  :"+currentNode.getPath());
PAGE_COUNT = PAGE_COUNT +  1;
while(nodeItr.hasNext()){
Node sbNode  =nodeItr.nextNode();
nodeType = sbNode.getProperty(JCR_PRIMARYTYPE).getString();

if(nodeType.equalsIgnoreCase(IS_PAGE) || nodeType.equalsIgnoreCase(IS_PAGE_CONTENT)){
 if(nodeType.equalsIgnoreCase(IS_PAGE)){
/**
 * consolidating list of parent pages which have childs
 */
currfolderNdsList.add(sbNode);
 }

if(nodeType.equalsIgnoreCase(IS_PAGE_CONTENT)){
Node pageNode = session.getNode(sbNode.getPath());
if(pageNode.hasProperty(JCR_LANGUAGE) && pageNode.hasProperty(CQ_TEMPLATE)){
  }
}
   }
} // while loop end
}
/**
* if current page has sub-child pages then traverse those pages also, else terminate job.
*/
if(currfolderNdsList.size() >0){
getNodeHierarchyData(currfolderNdsList,session);
}else{
return;
}
}catch (Exception exe) {
LOG.error("*** Exception while processing the node ***"+exe.getMessage());
}
}
}

No comments:

Post a Comment