[AEM6.5] Issue with PageManager API to move content from one path to another within same hirearchy.
The problem I am facing here that If I move the node from one path to another by creating the same hirearchy and if we move the parent of parent node then I am facing issues like some pages are not moving means the pages whoes child or child of childs are already moved,
I am moving and iterating the node using PageManager API
Example if we pass payload to our workflow : [/content/project/websites/en-us/c/cfolder/cfolder-relateddocs/elevated/super-parent-1/parent-b/child-b/child-of-child-b/grand-child-b]

So we can see our code perfectly moved the grand-child-b to /content/archived/websites/en-us/c/cfolder/cfolder-relateddocs/elevated/super-parent-1/parent-b/child-b/child-of-child-b/grand-child-b:

But Now if we want to move parent-b [/content/archived/websites/en-us/c/cfolder/cfolder-relateddocs/elevated/super-parent-1/parent-b]
the expectation will be it should move all the node from source and retain the already moved node :

But it is moving its child of childs of first child page and returns out of loop:
Archived like only chid of child-b moved and it came out of loop:

and we can see in source : in the child-b jcr:content and childs are are moved and rest didn't moved

Can Anyone please guide here the optimized approach to achive this issue:
here is the sample code I have written for this
@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
String payload = workItem.getWorkflowData().getPayload().toString();
ResourceResolver resourceResolver = resourceResolverUtil.getResourceResolver();
Session session = resourceResolver.adaptTo(Session.class);
Page rootPage = Objects.requireNonNull(resourceResolver.getResource(payload)).adaptTo(Page.class);
Resource resource = resourceResolver.getResource(payload);
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
if (rootPage != null) {
List<String> list = new ArrayList<>();
list.add(payload);
Iterator<Page> childPages;
childPages = rootPage.listChildren(new PublishedPageFilter(), true);
if (childPages != null) {
while (childPages.hasNext()) {
Page page1 = childPages.next();
list.add(page1.getPath());
}
}
String[] paths = list.toArray(new String[0]);
try {
assert resource != null;
Page page = resource.adaptTo(Page.class);
String archivePath = StringUtils.substringAfter(payload, ROOT_PATH);
archivePath = StringUtils.substringBeforeLast(archivePath, DELIMITER_SLASH);
archivePath = StringUtils.join("/content/archived", archivePath);
Node archived = JcrUtils.getOrCreateByPath(archivePath, "cq:Page", session);
String targetPath = archived.getPath() + "/" + rootPage.getName();
if (session.nodeExists(targetPath)) {
callingChildOc(pageManager, rootPage, session);
} else {
if (pageManager != null) {
pageManager.move(page, targetPath, null, false, true, null);
}
}
if (!session.nodeExists(targetPath + "/jcr:content") && session.nodeExists(rootPage.getPath() + "/jcr:content")) {
Node jcr = session.getNode(rootPage.getPath() + "/jcr:content");
String targetJcr = jcr.getPath().replace("/content/project", "/content/archived");
session.move(jcr.getPath(), targetJcr);
pageManager.delete(rootPage, true);
//session.save();
}
session.save();
} catch (RepositoryException | WCMException e) {
loggerService.postLog(LogLevel.ERROR, "RepositoryException exception occurred at :: {}" + Arrays.toString(paths));
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (resourceResolver.isLive()) {
resourceResolver.close();
}
}
} else {
String workflowId = workItem.getWorkflow().getId();
Workflow workflow = workflowSession.getWorkflow(workflowId);
workflowSession.terminateWorkflow(workflow);
loggerService.postLog(LogLevel.ERROR, "Workflow Terminated as path does not start with /content/wem-content :: {}" + workflowId + " or payload is having more than one level of child pages ");
}
}
private static void callingChildOc(PageManager pageManager, Page rootPath, Session session) throws WCMException, RepositoryException, InterruptedException {
Iterator<Page> childIterators = rootPath.listChildren();
LOG.debug("1 :: " + rootPath.getName());
if (childIterators != null) {
while (childIterators.hasNext()) {
Page innerPage = childIterators.next();
callingChildOc(pageManager, innerPage, session);
}
}
String rootPathPayload = rootPath.getPath();
LOG.debug("2 :: {}" + rootPathPayload);
String childOfChildPathTarget = rootPathPayload.replace("/content/wem-content", "/content/wem-archived");
if (!session.nodeExists(childOfChildPathTarget)) {
LOG.debug("3 archive ::{}" + childOfChildPathTarget);
pageManager.move(rootPath, childOfChildPathTarget, null, false, true, null);
} else {
if (!session.nodeExists(rootPathPayload.replace("/content/wem-content", "/content/wem-archived") + "/jcr:content")
&& session.nodeExists(rootPathPayload + "/jcr:content")) {
Node jcr = session.getNode(rootPath.getPath() + "/jcr:content");
String childOfChildPathTargets = jcr.getPath().replace("/content/wem-content", "/content/wem-archived");
LOG.debug("4 final jcr move :: {}" + childOfChildPathTargets);
session.move(jcr.getPath(), childOfChildPathTargets);
pageManager.delete(rootPath, true);
//session.save();
}
}
}
}
Thanks