AEM6.5 || Workflow execute only if payload is having only one level or no level of Child pages. | Community
Skip to main content
Level 2
April 5, 2023
Solved

AEM6.5 || Workflow execute only if payload is having only one level or no level of Child pages.

  • April 5, 2023
  • 1 reply
  • 609 views

Execute workflow process step only if the payload is having only one level of child or no child.

otherwise display error message to author that "Node is having grand child pages"

 

Example : 

 

 

Scene 1 [true]: if we pass payload : /content/we-retail/ca/en/experience/arctic-surfing-in-lofoten to workflow it will check the payload is having only one level of child, then execute the logic.

 

Scene 2 [true]: if we pass payload : /content/we-retail/ca/en/experience/arctic-surfing-in-lofoten/men to workflow it will check the payload is having only one level or no level of child, then execute the logic

 

Scene 3 [false]: if we pass payload : /content/we-retail/ca/en/experience to workflow it will check the payload is having only one level or no level of child, then execute the logic otherwise display error message to author that "Node is having grand child pages"

 

@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 = resourceResolver.getResource(payload).adaptTo(Page.class);
  //TODO check page is having only one level or no child and update the values.
  if (rootPage != null && payload.startsWith(ROOT_PATH) && "Check if node is having one level or no child") {
    // code logic here..
  }
}

 

Thanks 

 @arunpatidar  @lukasz-m  @kautuk_sahni 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

Hi @marcos_aem,

You can use getDepth() method from Page object and simply compare depth value of root and child pages. Here is a sample method that is doing it.

@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 = resourceResolver.getResource(payload).adaptTo(Page.class); //TODO check page is having only one level or no child and update the values. if (rootPage != null && payload.startsWith(ROOT_PATH) && accept(rootPage)) { // code logic here.. } } private boolean accept(Page rootPage) { int rootPageDepth = rootPage.getDepth(); // getting entire strucutre of childeren, grandchildren etc Iterator<Page> childPages = rootPage.listChildren(null, true); while (childPages.hasNext()) { Page page = childPages.next(); if (page.getDepth() - rootPageDepth > 1) { return false; } } return true; }

1 reply

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
April 5, 2023

Hi @marcos_aem,

You can use getDepth() method from Page object and simply compare depth value of root and child pages. Here is a sample method that is doing it.

@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 = resourceResolver.getResource(payload).adaptTo(Page.class); //TODO check page is having only one level or no child and update the values. if (rootPage != null && payload.startsWith(ROOT_PATH) && accept(rootPage)) { // code logic here.. } } private boolean accept(Page rootPage) { int rootPageDepth = rootPage.getDepth(); // getting entire strucutre of childeren, grandchildren etc Iterator<Page> childPages = rootPage.listChildren(null, true); while (childPages.hasNext()) { Page page = childPages.next(); if (page.getDepth() - rootPageDepth > 1) { return false; } } return true; }