May 1, 2020
Estimated Post Reading Time ~

Content Copier Servlet to perform JCR copy operation

This servlet performs the JCR workspace copy operation. To demonstrate a particular use case I have worked on, there was a bunch of pages in a folder of type (sling:OrderedFolder) in CQ5 inside /content and more sub-pages inside the subfolder of type (sling:OrderedFolder) in it, and the ask was to migrate the pages from folder structure (nested) in CQ5 to AEM page structure.

To achieve the same I have written a servlet that accepts the source(path of the parent source folder) and destination (path of the parent destination page) paths as arguments and performs the workspace copy operation.
The primary method used to perform the operation is
Workspace.copy(String srcAbsPath, String destAbsPath)

This method copies the subgraph rooted at, and including, the node at srcAbsPath to the new location at destAbsPath.

Servlet to achieve the functionality
package com.foo.community.core.servlets;

import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import javax.jcr.Repository;
import javax.servlet.ServletException;
import java.io.IOException;
import javax.jcr.NodeIterator;

import javax.jcr.Session; 
import javax.jcr.Node; 
import javax.jcr.Workspace;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 


@SuppressWarnings("serial")
@SlingServlet(paths = "/system/utility/servletinvoker")
public class ContentCopierServlet extends SlingSafeMethodsServlet {

  private static final Logger log = LoggerFactory.getLogger(this.getClass());

  @Reference
  private Repository repository;
  @Override
  protected void doGet(final SlingHttpServletRequest req,
   final SlingHttpServletResponse resp) throws ServletException, IOException {

     String message="Initial servlet message";
     String requestPath1 = req.getParameter("path1");
     String requestPath2 = req.getParameter("path2");
     try {
 Session session= req.getResourceResolver().adaptTo(Session.class);
 copyNodes(requestPath1,requestPath2,session);
 message= "Content copied from: "+ requestPath1+ " to: "+ requestPath2; 
     } 
     catch (Exception e) {
 message="Error performing operation: " + e.getMessage();
 log.error("copyServlet main error", e);
 e.printStackTrace();
     }
     resp.getWriter().println(message);
    }
 
   private void copyNodes(String requestPath1, String requestPath2, Session session)
                                                              throws Exception{
 
    Workspace workspace = session.getWorkspace();
    String resrc = requestPath1.substring(1,requestPath1.length()); 
    String redes = requestPath2.substring(1,requestPath2.length()); 
  
    Node rootnode = session.getRootNode();
    Node srcnodepath = rootnode.getNode(resrc);
    Node destnode = rootnode.getNode(redes);
  
    if (srcnodepath.hasNodes()) {
 NodeIterator worknodes = srcnodepath.getNodes();
 while (worknodes.hasNext()) {
    Node childnde = worknodes.nextNode();
    
    // formation of destination path and its relative path variables
    String[] parts = childnde.getPath().split("/");
    String destrelativepath = parts[parts.length-1];
    String destpath = destnode.getPath() + "/" + destrelativepath;
    
    if (destnode.hasNode(destrelativepath)) {
    
  String prtype= childnde.getProperty("jcr:primaryType").getString();
  log.info("primary type is: "+prtype);
  int flag=0;
  if(prtype.equals("sling:OrderedFolder")){
   copyNodes(childnde.getPath(),destpath,session);
   flag=1;
  }     
     
  if(!destpath.contains("jcr:content") && flag==0){
   destnode.getNode(destrelativepath).remove();
   session.save();
   log.info("src path is: "+childnde.getPath());
   log.info("dest path is: "+destpath);
   workspace.copy(childnde.getPath(), destpath);
  }
    }else{
  String srpath= childnde.getPath();
  workspace.copy(srpath, destpath);  
           }
 }
       }
     } 
}

Before beginning the operation, make sure to have the destination page structure(skeleton) ready for this to work.
All you have to do is to create the destination pages corresponding to folders of your source.

If your source contains the sub-folders, you have to create a nested page structure in your destination also.
The screenshot of CRX before and after the operation is shown below:




The Servlet is registered as a path, so you can make an ajax call from your utility component and pass the source and destination paths as an argument.
Also, mostly the utilities like these are performed on author instance so you can find the proper path for the servlet to be registered at.

So, this was my use case, but if you have similar such requirements, you know how to do JCR workspace copy operation.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.