April 10, 2020
Estimated Post Reading Time ~

Workflows in AEM 03 - Dynamic Participant Step

A participant is a step that enables a user to assign ownership of a task to another user. The workflow will only go further if that user manually acknowledges the step.

A simple use case of this workflow is a review step, i.e. if you have made some changes in an AEM page and then you want someone to review it then you can add it to the participant step and that user will get a notification in his/her inbox. Obviously, the reviewer should have access to the page in question.

Dynamic Participant Step is similar to the Participant Step with the exception that the user is selected at run time. There might be a use case where you want an item to be assigned to a user who has the lowest number of items to review.

The business logic of a Dynamic Participant Step can be written in a class that implements the Participant Chooser interface.

Following is a code example that selects the participant from either "admin" or a user from the "administrators" group based on the condition if the workflow history is empty.


package org.redquark.demo.core.workflows;

import java.util.List;

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.HistoryItem;
import com.adobe.granite.workflow.exec.ParticipantStepChooser;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.Workflow;
import com.adobe.granite.workflow.metadata.MetaDataMap;

/**
 * @author Anirudh Sharma
 */
@Component(service = ParticipantStepChooser.class, property = {
 "chooser.label=" + "Dynamic Participant Step Example"
})
public class DynamicParticipantStepExample implements ParticipantStepChooser {

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

 @Override
 public String getParticipant(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
 throws WorkflowException {
  log.info("----------< [{}] >----------", this.getClass().getName());

  String participant = "admin";

  Workflow workflow = workItem.getWorkflow();

  // Getting the workflow history
  List < HistoryItem > workflowHistory = workflowSession.getHistory(workflow);

  if (!workflowHistory.isEmpty()) {
   // Setting the administrators group to the participant
   participant = "administrators";
  } else {
   participant = "admin";
  }

  log.info("----------< Participant: {} >----------", participant);

  return participant;
 }


}

Please note that the property "chooser.label" is important as the value of this property determines by which name our step will be listed.

Configure the workflow

Workflow models
  • Create a model with the name and title
Create workflow model
  • Select the workflow model and edit it
Edit workflow model
  • Delete predefined "Step 1" and add a new participant step by clicking on "+" sign
Add Dynamic Participant Step
  • Edit the component by clicking on the "Spanner" icon and configure the same as below
Configure the dialog
  • Click on the "Sync" button to activate the workflow model.
  • Run the workflow on a page and click on "Next"
Run a workflow on payload
  • Once you complete the workflow, you will see a notification in the "admin" user's inbox. Open it. As soon as you click on "Complete", you will see a dialog opened that will ask for a "Comment" box. This is the manual acknowledgment that a user has to provide in a participant step.

Conclusion

In this post, we looked into creating a custom workflow dynamic participant step and how to configure it. 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.