Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

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

Avatar

Level 2

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 : 

Marcos_aem_0-1680680634566.png

 

 

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 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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;
}

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

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;
}