Identity payload type in workflow step/s | Community
Skip to main content
Nitin_laad
New Participant
April 8, 2022
Solved

Identity payload type in workflow step/s

  • April 8, 2022
  • 2 replies
  • 1121 views

Hi All!

In workflow steps, is there any out of the box option to identify the payload type or MIME type (cq:page vs dam:asset)?

Goal is to make author execute workflow only in sites pages, and not on any of the dam assets.  

 

TIA,

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 DEBAL_DAS

Hi @nitin_laad ,

 

I was exploring page lock operation using Page api and wrote this sample logic -

/**
*
*/
package com.aem.demo.core.workflows;

import java.util.Objects;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMException;

/**
* @author debal This workflow step is responsible to lock AEM page.
*/

@Component(property = { Constants.SERVICE_DESCRIPTION + "=This workflow step is responsible to lock AEM page",
Constants.SERVICE_VENDOR + "=AEM Demo Debal", "process.label" + "=Page lock process" })
public class PageLockWorkflowStep implements WorkflowProcess {

private final Logger logger = LoggerFactory.getLogger(PageLockWorkflowStep.class);

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
throws WorkflowException {

String payloadPath = workItem.getWorkflowData().getPayload().toString();

ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);

Resource resource = resourceResolver.getResource(payloadPath);

if (Objects.nonNull(resource) && resource.isResourceType("cq:Page")) {

Page page = resource.adaptTo(Page.class);
try {
page.lock();
} catch (WCMException e) {

logger.error("Unable to lock the given Page", e.getMessage());
}

} else {
logger.info("**** Resource isn't a page**** {} ");
}
}

}

 

In this sample workflow step, I was checking resource type cq:Page with following logic - resource.isResourceType("cq:Page") explicitly.

2 replies

DEBAL_DAS
DEBAL_DASAccepted solution
New Participant
April 8, 2022

Hi @nitin_laad ,

 

I was exploring page lock operation using Page api and wrote this sample logic -

/**
*
*/
package com.aem.demo.core.workflows;

import java.util.Objects;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMException;

/**
* @author debal This workflow step is responsible to lock AEM page.
*/

@Component(property = { Constants.SERVICE_DESCRIPTION + "=This workflow step is responsible to lock AEM page",
Constants.SERVICE_VENDOR + "=AEM Demo Debal", "process.label" + "=Page lock process" })
public class PageLockWorkflowStep implements WorkflowProcess {

private final Logger logger = LoggerFactory.getLogger(PageLockWorkflowStep.class);

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
throws WorkflowException {

String payloadPath = workItem.getWorkflowData().getPayload().toString();

ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);

Resource resource = resourceResolver.getResource(payloadPath);

if (Objects.nonNull(resource) && resource.isResourceType("cq:Page")) {

Page page = resource.adaptTo(Page.class);
try {
page.lock();
} catch (WCMException e) {

logger.error("Unable to lock the given Page", e.getMessage());
}

} else {
logger.info("**** Resource isn't a page**** {} ");
}
}

}

 

In this sample workflow step, I was checking resource type cq:Page with following logic - resource.isResourceType("cq:Page") explicitly.

lukasz-m
New Participant
April 8, 2022

Hi @nitin_laad,

I am not aware about OOTB workflow step that will meet your requirement. However you can consider to use OOTB OR Split, with proper ecma script to recognize payload type and choose different workflow path. Here is an example of ecma script, that is checking node type base on path form payload:

function check() {
    if (workflowData.getPayloadType() == "JCR_PATH") {
        var path = workflowData.getPayload().toString();
        var jcrSession = workflowSession.getSession();
        if (jcrSession.nodeExists(path)) {
            var node = jcrSession.getNode(path);
            return node.getProperty("jcr:primaryType").getString().equals("cq:Page");
        }
    }
    return false;
}

Of course anything you can define as part of ecma script, you can implement via custom workflow step. Maybe this documentation will be useful if you would like to learn more about ecma script.

Nitin_laad
New Participant
April 8, 2022

Thank you @lukasz-m , @debal_dasI'm just avoiding writing any custom code (JAVA/ECMA script), so I decided to check here in the community if there is any OOB solution.