Parsing AEM workflow arguments

Photo by Lisa Fotios from Pexels

AEM (Adobe Experience Manager) provides the capability for creating custom workflow process. This can be very helpful when there is a need to automate some actions on the workflow.

The default workflow process dialog allows you to pass arguments to the workflow. The arguments functionality allows you to easily reuse your custom processes in other workflow models.

Workflow process dialog

Multiple Arguments

The arguments are stored in the arguments property “PROCESS_ARGS” and you can get them by using the MetaDataMap object passed on your workflow process:

 @Override
 public void execute(WorkItem item, WorkflowSession session, MetaDataMap args){
     String arguments = args.get(PROCESS_ARGS, "string");
     ...
 } 

If you want to pass multiple arguments with values, you can define your own convention on how to pass them. The example below uses comma separated arguments where argumentName=argumentValue. So when setting multiple arguments we have argument1=value1,argument2=value2. For processing the arguments we can use the following:

 private Map<String, String> buildArguments(MetaDataMap args) {
     if (args.containsKey(PROCESS_ARGS)) {
         String argumentsString = args.get(PROCESS_ARGS, "string");
         String[] arguments = StringUtils.split(argumentsString, ",");
         return Arrays.stream(arguments)
             .map(String::trim)
             .map(s -> s.split("="))
             .filter(s -> s.length > 1)
             .collect(Collectors.toMap(s -> s[0], s -> s[1]));
     }
     return Collections.emptyMap();
 }

Make it reusable

If you want multiple workflow processes that need to have arguments, you should make your code reusable and shared between your workflow process. You can use Java abstraction and inheritance to do that with the code below. 

 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 java.util.Arrays;
 import java.util.Collections;
 import java.util.Map;
 import java.util.stream.Collectors;
 import org.apache.commons.lang3.StringUtils;
 public abstract class BaseArgumentsProcess implements WorkflowProcess {
     private static final String PROCESS_ARGS = "PROCESS_ARGS";
     private Map<String, String> buildArguments(MetaDataMap args) {
         if (args.containsKey(PROCESS_ARGS)) {
             String argumentsString = args.get(PROCESS_ARGS, "string");
             String[] arguments = StringUtils.split(argumentsString, ",");
             return Arrays.stream(arguments)
                 .map(String::trim)
                 .map(s -> s.split("="))
                 .filter(s -> s.length > 1)
                 .collect(Collectors.toMap(s -> s[0], s -> s[1]));
         }
         return Collections.emptyMap();
     }
     @Override
     public void execute(WorkItem item, WorkflowSession session, MetaDataMap args)
         throws WorkflowException {
         execute(item, session, buildArguments(args));
     }
     protected abstract void execute(WorkItem item, WorkflowSession session,
         Map<String, String> args);
 } 

Your code would only have to extend the execute(WorkItem item, WorkflowSession session,Map<String, String> args) method in order the get the parsed arguments.

As with any problem there are many ways of solving a problem. If you have any comments or questions, please leave a comment below.

One comment

Leave a Reply