April 10, 2020
Estimated Post Reading Time ~

Workflows in AEM 02 - Process Step

Today, we are going to discuss the most widely used Workflow step - the process step. This is generally used when we want our application to execute a certain logic.

It executes an ECMA script or an OSGi service to perform automatic processing. A process can be implemented using the following steps -

  1. Create an OSGi service implementing the interface com.adobe.granite.workflow.exec.WorkflowProcess.
  2. Set the property process.label. This is the String value by which our workflow needs to be listed.
  3. Implement the execute(WorkItem, WorkflowSession, MetaDataMap) method with the implementation code.


The execute() method has three parameters -
  1. WorkItem - It is the unit that is passed through a Workflow instance of a WorkflowModel. It contains the WorkflowData. The instances act on and a reference to the WorkflowNode that describes the underlying workflow step.
  2. WorkflowSession - This class provides all functionality (depending on the users' rights) for managing WorkflowModels, Workflow instances, and their execution.
  3. MetaDataMap - A value map for generic access to metadata values.
Following is the sample implementation of a process step - 
package org.redquark.demo.core.workflows;

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
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;

/**
 * @author Anirudh Sharma
 */
@Component(service = WorkflowProcess.class, property = {
 "process.label=" + "Process Step Example"
})
public class ProcessStepExample implements WorkflowProcess {

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

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

  // Getting payload from Workflow - workItem -> workflowData -> payload
  String payloadType = workItem.getWorkflowData().getPayloadType();

  // Check type of payload; there are two - JCR_PATH and JCR_UUID
  if (StringUtils.equals(payloadType, "JCR_PATH")) {
   log.info("Payload type: {}", payloadType);
   // Get the JCR path from the payload
   String path = workItem.getWorkflowData().getPayload().toString();
   log.info("Payload path: {}", path);
  }

  // Get workflow process arguments
  String[] processArguments = metaDataMap.get("PROCESS_ARGS", "Default").split(",");
  log.info("Process args: {}", Arrays.toString(processArguments));
 }


}

Here, we are implementing the WorkflowProcess interface and our business logic is written in the execute() method. Here we are getting the type of payload from the WorkflowData object and then also getting the process arguments that a user might pass while using this process step.

Please make note of the process.label property that defines the identifier by which this process step will be listed among many process steps (this is explained in the below section).

Configuring the workflow

Workflow models
  • Now click on "Create Model" and enter the workflow details and "Done".
Create a workflow model
  • Select workflow model created and click on "Edit"
Edit workflow model
  • Click on drag components here and select the process step from the list. Before that, delete step 1 which is created automatically.
Select the process step
  • Edit the process step using the following configuration. Note that we selected the process which is similar to the value we have given in the process.label property.
Configure the process step
  • After saving the dialog, click on "Sync" in the top right corner to save the workflow. Thus our workflow model is ready. Now, we can run it for any operation we want. The simplest example is to run this workflow model for a page.
  • When you run it, you will find the following messages in the logs -
2019-09-24 22:40:35.268 INFO [org.redquark.demo.core.workflows.ProcessStepExample] Payload type: JCR_PATH
2019-09-24 22:40:35.268 INFO [org.redquark.demo.core.workflows.ProcessStepExample] Payload path: /content/we-retail/language-masters/en/men
2019-09-24 22:40:35.269 INFO [org.redquark.demo.core.workflows.ProcessStepExample] Process args: [a=1, b=2, c=3]


By aem4beginner

No comments:

Post a Comment

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