Showing posts with label cq events on node. Show all posts
Showing posts with label cq events on node. Show all posts

Sunday, 21 December 2014

AEM/CQ Node change observer


Node Change Observer Code:
Some times we may have to observe changes in nodes.

Below code helps to observe any change in jcr nodes(Changes can be addition, deletion or modification). This helps us to observe and node for modification and process some tasks.

/** Class which observes an event at /jcr:system implementation
    *
    *
    */
  
    public class NodeObserver implements EventListener{
      
    private Logger log = LoggerFactory.getLogger(getClass());
      
        @Reference
        private SlingRepository repository;
      
        private Session session;
        private ObservationManager observationManager;
             
      
        protected void activate(ComponentContext context)  throws Exception {
            session = repository.loginAdministrative(null);
           // Listen for changes to our orders
            if (repository.getDescriptor(Repository.OPTION_OBSERVATION_SUPPORTED).equals("true")) {
                observationManager = session.getWorkspace().getObservationManager();
                //We are putting types of nodes to be observed under below string array: sample in next line
               // final String[] types = { "nt:unstructured","rep:system","rep:versionStorage","nt:frozenNode" };
               //I am just observing nt:unstructured, bcz that is my requirement
                final String[] types = { "nt:unstructured" };
                //I am observing changes under /jcr:system nodes
                final String path = "/jcr:system";
                observationManager.addEventListener(this, Event.NODE_ADDED, path, true, null, types, false);
                log.error("Observing node changes to {} nodes under {}", Arrays.asList(types), path);
            }
          
        }

        protected void deactivate(ComponentContext componentContext) throws RepositoryException {
  
            if (observationManager != null) {
                observationManager.removeEventListener(this);
            }
            if (session != null) {
                session.logout();
                session = null;
  
            }
        }
      

        public void onEvent(EventIterator itr) {
                      
            try {
                while (itr.hasNext()){
                  Node versionNode = null;
                
                  String eventPath = (itr.nextEvent()).getPath();
                  log.info("something has been added : {}", eventPath);
                
                  Node jcrContentNode = session.getNode(eventPath);
                
                  Node jcrParentNode =jcrContentNode.getParent();
                  //process nodes after checking conditions
                  if(eventPath.endsWith("jcr:content") && (null!= jcrParentNode) ){
                                  
                
                  String metaDataNodePath = eventPath + "/metadata";
                  Node metaDataNode = session.getNode(metaDataNodePath);
                  log.info("metaDataNode Created" + metaDataNode);
                
                  }
                //  break;

                }
               } catch(RepositoryException e){
               log.error("Error while processing events",e);
              }
          
        }
-------Similar Posts---------------
Mapping of requests in AEM
Interact With AEM
Run Modes
AEM
Apache Sling in AEM
-------------------------------------