April 10, 2020
Estimated Post Reading Time ~

Workflows in AEM 04 - MetaDataMap

Sometimes we are faced with a situation where we need to pass data from one workflow step to another. AEM's workflow API provides an easy way to achieve this using MetaDataMap.

Please note that only primitive data types like Integer, String, etc. can be passed from one step to another. If you have a requirement to pass non-primitive data then use the byte[] array.

Caution: If the data to be passed is too large, refrain passing InputStream. Instead, a better approach is to save the data in a JCR node in the step and retrieve it from the JCR node in a later step.

A MetaDataMap is the data structure that acts as a value map and allows users to set and get data among the steps.

Code example

Let's see a code example to demonstrate this concept. Here we will be using two process steps. In the first, we will be passing data and in the second, we will be retrieving it.

FirstProcessStep.java

package org.redquark.demo.core.workflows;

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=" + "First Process Step"
})
public class FirstProcessStep implements WorkflowProcess {

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

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

  log.info("Executing first workflow process step");

  // Getting the metadata map
  MetaDataMap map = workItem.getWorkflow().getWorkflowData().getMetaDataMap();

  // Putting some values in the map
  map.put("Data", "Data from the first step");
 }

}

Here, you can see that we are getting the instance of MetaDataMap from the WorkItem and then we are saving a key-value pair in it. The key we are using here is "Data" and using this only, we will retrieve the values in the later steps.

SecondProcessStep.java

package org.redquark.demo.core.workflows;

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=" + "Second Process Step"
})
public class SecondProcessStep implements WorkflowProcess {

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

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

  log.info("Executing second workflow process step");

  // Getting the metadata map
  MetaDataMap map = workItem.getWorkflow().getWorkflowData().getMetaDataMap();

  // Getting the data stored
  String data = (String) map.get("Data");

  log.info("Data from the first step: {}", data);
 }


}

In the second process step, we are getting the value from the same key "Data" and then we can use as per out requirement.

Conclusion

So in this post, we looked into how to work with MetaDataMap API and saw how can we pass data from one workflow step to another.

You can find the code to this in my GitHub repository.



By aem4beginner

No comments:

Post a Comment

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