Obtaining Workflow Objects in ECMA Scripts

As described in Locating the Script, AEM (via Apache Sling) provides an ECMA script engine that executes server-side ECMA scripts. The org.apache.sling.scripting.core.ScriptHelper class is immediately available to your scripts as the sling variable.

The ScriptHelper class provides access to the SlingHttpServletRequest that you can use to eventually obtain the WorkflowSession object; for example:

var wfsession = sling.getRequest().getResource().getResourceResolver().adaptTo(Packages.com.adobe.granite.workflow.WorkflowSession);

Using the Workflow REST API

The Workflow console makes heavy use of the REST API; so this page describes the REST API for workflows.

NOTE
The curl command line tool enables you to use the Workflow REST API to access workflow objects and manage instance lifecycles. The examples throughout this page demonstrate the use of the REST API via the curl command line tool.

The following actions are supported with the REST API:

NOTE
By using Firebug, a Firefox extension for web development, it is possible to follow the HTTP traffic when the console is operated. For example, you can check the parameters and the values sent to the AEM server with a POST request.

In this page it is assumed that AEM runs on localhost at port 4502 and that the installation context is " /" (root). If it is not the case of your installation, the URIs, to which the HTTP requests apply, need to be adapted accordingly.

The rendering supported for GET requests is the JSON rendering. The URLs for GET should have the .json extension, for example:

http://localhost:4502/etc/workflow.json

Managing Workflow Instances

The following HTTP request methods apply to:

http://localhost:4502/etc/workflow/instances

HTTP request methodActions
GETLists the available workflow instances.
POSTCreates a new workflow instance. The parameters are:
- model: the ID (URI) of the respective workflow model
- payloadType: containing the type of the payload (for example, JCR_PATH or URL).
The payload is sent as parameter payload. A 201 (CREATED) response is sent back with a location header containing the URL of the new workflow instance resource.

Managing a Workflow Instance by its State

The following HTTP request methods apply to:

http://localhost:4502/etc/workflow/instances.{state}

HTTP request methodActions
GETLists the available workflow instances and their states ( RUNNING, SUSPENDED, ABORTED or COMPLETED)

Managing a Workflow Instance by its ID

The following HTTP request methods apply to:

http://localhost:4502/etc/workflow/instances/{id}

HTTP request methodActions
GETGets the instances data (definition and metadata) including the link to the respective workflow model.
POSTChanges the state of the instance. The new state is sent as the parameter state and must have one of the following values: RUNNING, SUSPENDED, or ABORTED.
If the new state is not reachable (for example, when suspending a terminated instance) a 409 (CONFLICT) response is sent back to the client.

Managing Workflow Models

The following HTTP request methods apply to:

http://localhost:4502/etc/workflow/models

HTTP request methodActions
GETLists the available workflow models.
POSTCreates a new workflow model. If the parameter title is sent, a new model is created with the specified title. Attaching a JSON model definition as parameter model creates a new workflow model according to the provided definition.
A 201 response (CREATED) is sent back with a location header containing the URL of the new workflow model resource.
The same happens when a model definition is attached as a file parameter called modelfile.
In both the cases of the model and modelfile parameters, an additional parameter called type is required to define the serialization format. New serialization formats can be integrated using the OSGI API. A standard JSON serializer is delivered with the workflow engine. Its type is JSON. See below for an example of the format.

Example: in the browser, a request to http://localhost:4502/etc/workflow/models.json generates a json response similar to the following:

[
    {"uri":"/var/workflow/models/activationmodel"}
    ,{"uri":"/var/workflow/models/dam/adddamsize"}
    ,{"uri":"/var/workflow/models/cloudconfigs/dtm-reactor/library-download"}
    ,{"uri":"/var/workflow/models/ac-newsletter-workflow-simple"}
    ,{"uri":"/var/workflow/models/dam/dam-create-language-copy"}
    ,{"uri":"/var/workflow/models/dam/dam-create-and-translate-language-copy"}
    ,{"uri":"/var/workflow/models/dam-indesign-proxy"}
    ,{"uri":"/var/workflow/models/dam-xmp-writeback"}
    ,{"uri":"/var/workflow/models/dam-parse-word-documents"}
    ,{"uri":"/var/workflow/models/dam/process_subasset"}
    ,{"uri":"/var/workflow/models/dam/dam_set_last_modified"}
    ,{"uri":"/var/workflow/models/dam/dam-autotag-assets"}
    ,{"uri":"/var/workflow/models/dam/update_asset"}
    ,{"uri":"/var/workflow/models/dam/update_asset_offloading"}
    ,{"uri":"/var/workflow/models/dam/dam-update-language-copy"}
    ,{"uri":"/var/workflow/models/dam/update_from_lightbox"}
    ,{"uri":"/var/workflow/models/cloudservices/DTM_bundle_download"}
    ,{"uri":"/var/workflow/models/dam/dam_download_asset"}
    ,{"uri":"/var/workflow/models/dam/dynamic-media-encode-video"}
    ,{"uri":"/var/workflow/models/dam/dynamic-media-video-thumbnail-replacement"}
    ,{"uri":"/var/workflow/models/dam/dynamic-media-video-user-uploaded-thumbnail"}
    ,{"uri":"/var/workflow/models/newsletter_bounce_check"}
    ,{"uri":"/var/workflow/models/projects/photo_shoot_submission"}
    ,{"uri":"/var/workflow/models/projects/product_photo_shoot"}
    ,{"uri":"/var/workflow/models/projects/approval_workflow"}
    ,{"uri":"/var/workflow/models/prototype-01"}
    ,{"uri":"/var/workflow/models/publish_example"}
    ,{"uri":"/var/workflow/models/publish_to_campaign"}
    ,{"uri":"/var/workflow/models/screens/publish_to_author_bin"}
    ,{"uri":"/var/workflow/models/s7dam/request_to_publish_to_youtube"}
    ,{"uri":"/var/workflow/models/projects/request_copy"}
    ,{"uri":"/var/workflow/models/projects/request_email"}
    ,{"uri":"/var/workflow/models/projects/request_landing_page"}
    ,{"uri":"/var/workflow/models/projects/request_launch"}
    ,{"uri":"/var/workflow/models/request_for_activation"}
    ,{"uri":"/var/workflow/models/request_for_deactivation"}
    ,{"uri":"/var/workflow/models/request_for_deletion"}
    ,{"uri":"/var/workflow/models/request_for_deletion_without_deactivation"}
    ,{"uri":"/var/workflow/models/request_to_complete_move_operation"}
    ,{"uri":"/var/workflow/models/reverse_replication"}
    ,{"uri":"/var/workflow/models/salesforce-com-export"}
    ,{"uri":"/var/workflow/models/scene7"}
    ,{"uri":"/var/workflow/models/scheduled_activation"}
    ,{"uri":"/var/workflow/models/scheduled_deactivation"}
    ,{"uri":"/var/workflow/models/screens/screens-update-asset"}
    ,{"uri":"/var/workflow/models/translation"}
    ,{"uri":"/var/workflow/models/s7dam/request_to_remove_from_youtube"}
    ,{"uri":"/var/workflow/models/wcm-translation/create_language_copy"}
    ,{"uri":"/var/workflow/models/wcm-translation/prepare_translation_project"}
    ,{"uri":"/var/workflow/models/wcm-translation/translate-i18n-dictionary"}
    ,{"uri":"/var/workflow/models/wcm-translation/sync_translation_job"}
    ,{"uri":"/var/workflow/models/wcm-translation/translate-language-copy"}
    ,{"uri":"/var/workflow/models/wcm-translation/update_language_copy"}
]

Managing a Specific Workflow Model

The following HTTP request methods apply to:

http://localhost:4502*{uri}*

Where *{uri}* is the path to the model node in the repository.

HTTP request methodActions
GETGets the HEAD version of the model (definition and metadata).
PUTUpdates the HEAD version of the model (creates a new version).
The complete model definition for the new version of the model must be added as a parameter called model. Additionally a type parameter is needed as when creating new models and needs to have the value JSON.
POSTSame behaviour as with PUT. Needed because AEM widgets do not support PUT operations.
DELETEDeletes the model. To solve firewall/proxy issues a POST that contains an X-HTTP-Method-Override header entry with value DELETE will also be accepted as DELETE request.

Example: in the browser, a request to http://localhost:4502/var/workflow/models/publish_example.json returns a json response that is similar to the following code:

{
  "id":"/var/workflow/models/publish_example",
  "title":"Publish Example",
  "version":"1.0",
  "description":"This example shows a simple review and publish process.",
  "metaData":
  {
    "multiResourceSupport":"true",
    "tags":"wcm,publish"
  },
  "nodes":
  [{
    "id":"node0",
    "type":"START",
    "title":"Start",
    "description":"The start node of the workflow.",
    "metaData":
    {
    }
  },
  {
    "id":"node1",
    "type":"PARTICIPANT",
    "title":"Validate Content",
    "description":"Validate the modified content.",
    "metaData":
    {
      "PARTICIPANT":"admin"
    }
  },
  {
    "id":"node2",
    "type":"PROCESS",
    "title":"Publish Content",
    "description":"Publish the modified content.",
    "metaData":
    {
      "PROCESS_AUTO_ADVANCE":"true",
      "PROCESS":"com.day.cq.wcm.workflow.process.ActivatePageProcess"
    }
  },
  {
    "id":"node3",
    "type":"END",
    "title":"End",
    "description":"The end node of the workflow.",
    "metaData":
    {
    }
  }],
  "transitions":
  [{
    "from":"node0",
    "to":"node1",
    "metaData":
    {
    }
  },
  {
    "from":"node1",
    "to":"node2",
    "metaData":
    {
    }
  },
  {
    "from":"node2",
    "to":"node3",
    "metaData":
    {
    }
  }
]}

Managing a Workflow Model by its Version

The following HTTP request methods apply to:

http://localhost:4502/etc/workflow/models/{id}.{version}

HTTP request methodActions
GETGets the data of the model in the given version (if it exists).

Managing (User) Inboxes

The following HTTP request methods apply to:

http://localhost:4502/bin/workflow/inbox

HTTP request methodActions
GETLists the work items that are in the inbox of the user, who is identified by the HTTP authentication headers.
POSTCompletes the work item whose URI is sent as the parameter item and advances the according workflow instance to the next nodes, that is defined by the parameter route or backroute if there is going back a step.
If the parameter delegatee is sent, the work item identified by the parameter item is delegated to the specified participant.

Managing a (User) Inbox by the WorkItem ID

The following HTTP request methods apply to:

http://localhost:4502/bin/workflow/inbox/{id}

HTTP request methodActions
GETGets the data (definition and metadata) of the inbox WorkItem identified by its ID.

Examples

How to get a List of all Running Workflows with their IDs

To get a list of all running workflows, do a GET to:

http://localhost:4502/etc/workflow/instances.RUNNING.json

How to get a List of all Running Workflows with their IDs - REST using curl

Example using curl:

curl -u admin:admin http://localhost:4502/etc/workflow/instances.RUNNING.json

The uri displayed in the results can be used as the instance id in other commands; for example:

[
    {"uri":"/etc/workflow/instances/server0/2017-03-08/request_for_activation_1"}
]
NOTE
This curl command can be used with any workflow status in place of RUNNING.

How to change the Workflow Title

To change the Workflow Title displayed in the Instances tab of the workflow console, send a POST command:

  • to: http://localhost:4502/etc/workflow/instances/{id}

  • with the following parameters:

    • action: its value has to be: UPDATE
    • workflowTitle: the workflow title

How to change the Workflow Title - REST using curl

Example using curl:

curl -u admin:admin -d "action=UPDATE&workflowTitle=myWorkflowTitle" http://localhost:4502/etc/workflow/instances/{id}

# for example
curl -u admin:admin -d "action=UPDATE&workflowTitle=myWorkflowTitle" http://localhost:4502/etc/workflow/instances/server0/2017-03-08/request_for_activation_1

How to List all Workflow Models

To get a list of all available workflow models, do a GET to:

http://localhost:4502/etc/workflow/models.json

How to List all Workflow Models - REST using curl

Example using curl:

curl -u admin:admin http://localhost:4502/etc/workflow/models.json
NOTE

Obtaining a WorkflowSession Object

The com.adobe.granite.workflow.WorkflowSession class is adaptable from a javax.jcr.Session object or a org.apache.sling.api.resource.ResourceResolver object.

Obtaining a WorkflowSession Object - Java

In a JSP script (or Java code for a servlet class), use the HTTP request object to obtain a SlingHttpServletRequest object, which provides access to a ResourceResolver object. Adapt the ResourceResolver object to WorkflowSession.

<%
%><%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false"
    import="com.adobe.granite.workflow.WorkflowSession,
  org.apache.sling.api.SlingHttpServletRequest"%><%

SlingHttpServletRequest slingReq = (SlingHttpServletRequest)request;
WorkflowSession wfSession = slingReq.getResourceResolver().adaptTo(WorkflowSession.class);
%>

Obtaining a WorkflowSession Object - ECMA Script

Use the sling variable to obtain the SlingHttpServletRequest object that you use to obtain a ResourceResolver object. Adapt the ResourceResolver object to the WorkflowSession object.

var wfsession = sling.getRequest().getResource().getResourceResolver().adaptTo(Packages.com.adobe.granite.workflow.WorkflowSession);

Creating, Reading or Deleting Workflow Models

The following examples show how to access workflow models:

  • The code for Java and ECMA script uses the WorkflowSession.createNewModel method.
  • The curl command accesses the model directly using its URL.

The examples used:

  1. Create a model (with the ID /var/workflow/models/mymodel/jcr:content/model).
  2. Delete the model.
NOTE
Deleting the model sets the deleted property of the model’s metaData child node to true.
Deletion does not remove the model node.

When creating a model:

  • The workflow model editor requires that models use a specific node structure below /var/workflow/models. The parent node of the model must be of the type cq:Page having a jcr:content node with the following property values:

    • sling:resourceType: cq/workflow/components/pages/model
    • cq:template: /libs/cq/workflow/templates/model

    When you create a model, you must first create this cq:Page node and use its jcr:content node as the parent of the model node.

  • The id argument that some methods require for identifying the model is the absolute path of the model node in the repository:

    /var/workflow/models/<*model_name>*/jcr:content/model

Creating, Reading or Deleting Workflow Models - Java

<%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false" import="com.adobe.granite.workflow.WorkflowSession,
                 com.adobe.granite.workflow.model.WorkflowModel,
             org.apache.sling.api.SlingHttpServletRequest"%><%

SlingHttpServletRequest slingReq = (SlingHttpServletRequest)request;
WorkflowSession wfSession = slingReq.getResourceResolver().adaptTo(WorkflowSession.class);
/* Create the parent page */
String modelRepo = new String("/var/workflow/models");
String modelTemplate = new String ("/libs/cq/workflow/templates/model");
String modelName = new String("mymodel");
Page modelParent = pageManager.create(modelRepo, modelName, modelTemplate, "My workflow model");

/* create the model */
String modelId = new String(modelParent.getPath()+"/jcr:content/model")
WorkflowModel model = wfSession.createNewModel("Made using Java",modelId);

/* delete the model */
wfSession.deleteModel(modelId);
%>

Creating, Reading or Deleting Workflow Models - ECMA Script

var resolver = sling.getRequest().getResource().getResourceResolver();
var wfSession = resolver.adaptTo(Packages.com.adobe.granite.workflow.WorkflowSession);
var pageManager = resolver.adaptTo(Packages.com.day.cq.wcm.api.PageManager);

//create the parent page node
var workflowPage = pageManager.create("/var/workflow/models", "mymodel", "/libs/cq/workflow/templates/model", "Created via ECMA Script");
var modelId = workflowPage.getPath()+ "/jcr:content/model";
//create the model
var model = wfSession.createNewModel("My Model", modelId);
//delete the model
var model = wfSession.deleteModel(modelId);

Deleting a Workflow Model - REST using curl

# deleting the model by its id
curl -u admin:admin -X DELETE http://localhost:4502/etc/workflow/models/{id}
NOTE
Due to the level of detail required, curl is not considered practical for creating and/or reading a model.

Filtering Out System Workflows when Checking Workflow Status

You can use the WorkflowStatus API to retrieve information about the workflow status of a node.

Various methods have the parameter:

excludeSystemWorkflows

This parameter can be set to true to indicate that system workflows should be excluded from the relevant results.

You can update the OSGi configuration Adobe Granite Workflow PayloadMapCache that specifies the workflow Models to be considered as system workflows. The default (runtime) workflow models are:

  • /var/workflow/models/scheduled_activation/jcr:content/model
  • /var/workflow/models/scheduled_deactivation/jcr:content/model

Auto-Advance Participant Step after a Timeout

If you need to auto-advance a Participant step that has not been completed within a predefined time you can:

  1. Implement an OSGI event listener to listen on task creation and modification.

  2. Specify a timeout (deadline), then create a scheduled sling job to fire at that time.

  3. Write a job handler that is notified when the timeout expires and triggers the job.

    This handler will take the required action on the task if the task is not yet completed

NOTE
The action to be taken must be clearly defined to be able to use this approach.

Interacting with Workflow Instances

The following provide basic examples of how to interact (progammatically) with workflow instances.

Interacting with Workflow Instances - Java

// starting a workflow
WorkflowModel model = wfSession.getModel(workflowId);
WorkflowData wfData = wfSession.newWorkflowData("JCR_PATH", repoPath);
wfSession.startWorkflow(model, wfData);

// querying and managing a workflow
Workflow[] workflows workflows = wfSession.getAllWorkflows();
Workflow workflow= wfSession.getWorkflow(id);
wfSession.suspendWorkflow(workflow);
wfSession.resumeWorkflow(workflow);
wfSession.terminateWorkflow(workflow);

Interacting with Workflow Instances - ECMA Script

// starting a workflow
var model = wfSession.getModel(workflowId);
var wfData = wfSession.newWorkflowData("JCR_PATH", repoPath);
wfSession.startWorkflow(model, wfData);

// querying and managing a workflow
var workflows = wfSession.getWorkflows("RUNNING");
var workflow= wfSession.getWorkflow(id);
wfSession.suspendWorkflow(workflow);
wfSession.resumeWorkflow(workflow);
wfSession.terminateWorkflow(workflow);

Interacting with Workflow Instances - REST using curl

  • Starting a workflow

    # starting a workflow
    curl -d "model={id}&payloadType={type}&payload={payload}" http://localhost:4502/etc/workflow/instances
    
    # for example:
    curl -u admin:admin -d "model=/var/workflow/models/request_for_activation&payloadType=JCR_PATH&payload=/content/we-retail/us/en/products" http://localhost:4502/etc/workflow/instances
    
  • Listing the instances

    # listing the instances
    curl -u admin:admin http://localhost:4502/etc/workflow/instances.json
    

    This will list all instances; for example:

    [
        {"uri":"/var/workflow/instances/server0/2018-02-26/prototype-01_1"}
        ,{"uri":"/var/workflow/instances/server0/2018-02-26/prototype-01_2"}
    ]
    
    NOTE
    See How to get a List of all Running Workflows with their IDs for listing instances with a specific status.
  • Suspending a workflow

    # suspending a workflow
    curl -d "state=SUSPENDED" http://localhost:4502/etc/workflow/instances/{id}
    
    # for example:
    curl -u admin:admin -d "state=SUSPENDED" http://localhost:4502/etc/workflow/instances/server0/2017-03-08/request_for_activation_1
    
  • Resuming a workflow

    # resuming a workflow
    curl -d "state=RUNNING" http://localhost:4502/etc/workflow/instances/{id}
    
    # for example:
    curl -u admin:admin -d "state=RUNNING" http://localhost:4502/etc/workflow/instances/server0/2017-03-08/request_for_activation_1
    
  • Terminating a workflow instance

    # terminating a workflow
    curl -d "state=ABORTED" http://localhost:4502/etc/workflow/instances/{id}
    
    # for example:
    curl -u admin:admin -d "state=ABORTED" http://localhost:4502/etc/workflow/instances/server0/2017-03-08/request_for_activation_1
    

Interacting with Work Items

The following provide basic examples of how to interact (progammatically) with work items.

Interacting with Work Items - Java

// querying work items
WorkItem[] workItems = wfSession.getActiveWorkItems();
WorkItem workItem = wfSession.getWorkItem(id);

// getting routes
List<Route> routes = wfSession.getRoutes(workItem);

// delegating
Iterator<Participant> delegatees = wfSession.getDelegatees(workItem);
wfSession.delegateWorkItem(workItem, delegatees.get(0));

// completing or advancing to the next step
wfSession.complete(workItem, routes.get(0));

Interacting with Work Items - ECMA Script

// querying work items
var workItems = wfSession.getActiveWorkItems();
var workItem = wfSession.getWorkItem(id);

// getting routes
var routes = wfSession.getRoutes(workItem);

// delegating
var delegatees = wfSession.getDelegatees(workItem);
wfSession.delegateWorkItem(workItem, delegatees.get(0));

// completing or advancing to the next step
wfSession.complete(workItem, routes.get(0));

Interacting with Work Items - REST using curl

  • Listing Work Items from the Inbox

    # listing the work items
    curl -u admin:admin http://localhost:4502/bin/workflow/inbox
    

    Details for work items currently in the Inbox will be listed; for example:

    [{
        "uri_xss": "/var/workflow/instances/server0/2018-02-26/prototype-01_2/workItems/node2_var_workflow_instances_server0_2018-02-26_prototype-01_2",
        "uri": "/var/workflow/instances/server0/2018-02-26/prototype-01_2/workItems/node2_var_workflow_instances_server0_2018-02-26_prototype-01_2",
        "currentAssignee_xss": "workflow-administrators",
        "currentAssignee": "workflow-administrators",
        "startTime": 1519656289274,
        "payloadType_xss": "JCR_PATH",
        "payloadType": "JCR_PATH",
        "payload_xss": "/content/we-retail/es/es",
        "payload": "/content/we-retail/es/es",
        "comment_xss": "Process resource is null",
        "comment": "Process resource is null",
        "type_xss": "WorkItem",
        "type": "WorkItem"
      },{
        "uri_xss": "configuration/configure_analyticstargeting",
        "uri": "configuration/configure_analyticstargeting",
        "currentAssignee_xss": "administrators",
        "currentAssignee": "administrators",
        "type_xss": "Task",
        "type": "Task"
      },{
        "uri_xss": "configuration/securitychecklist",
        "uri": "configuration/securitychecklist",
        "currentAssignee_xss": "administrators",
        "currentAssignee": "administrators",
        "type_xss": "Task",
        "type": "Task"
      },{
        "uri_xss": "configuration/enable_collectionofanonymoususagedata",
        "uri": "configuration/enable_collectionofanonymoususagedata",
        "currentAssignee_xss": "administrators",
        "currentAssignee": "administrators",
        "type_xss": "Task",
        "type": "Task"
      },{
        "uri_xss": "configuration/configuressl",
        "uri": "configuration/configuressl",
        "currentAssignee_xss": "administrators",
        "currentAssignee": "administrators",
        "type_xss": "Task",
        "type": "Task"
      }
    
  • Delegating Work Items

    # delegating
    curl -d "item={item}&delegatee={delegatee}" http://localhost:4502/bin/workflow/inbox
    
    # for example:
    curl -u admin:admin -d "item=/etc/workflow/instances/server0/2017-03-08/request_for_activation_1/workItems/node1_etc_workflow_instances_server0_2017-03-08_request_for_act_1&delegatee=administrators" http://localhost:4502/bin/workflow/inbox
    
    NOTE
    The delegatee must be a valid option for the workflow step.
  • Completing or advancing work items to the next step

    # retrieve the list of routes; the results will be similar to {"results":1,"routes":[{"rid":"233123169","label":"End","label_xss":"End"}]}
    http://localhost:4502/etc/workflow/instances/<path-to-the-workitem>.routes.json
    
    # completing or advancing to the next step; use the appropriate route ID (rid value) from the above list
    curl -d "item={item}&route={route}" http://localhost:4502/bin/workflow/inbox
    
    # for example:
    curl -u admin:admin -d "item=/etc/workflow/instances/server0/2017-03-08/request_for_activation_1/workItems/node1_etc_workflow_instances_server0_2017-03-08_request_for_activation_1&route=233123169" http://localhost:4502/bin/workflow/inbox
    

Listening for Workflow Events

Use the OSGi event framework to listen for events that the com.adobe.granite.workflow.event.WorkflowEvent class defines. This class also provides several useful methods for obtaining information about the subject of the event. For example, the getWorkItem method returns the WorkItem object for the workitem that is involved in the event.

The following example code defines a service that listens to workflow events and performs tasks according to the type of event.

package com.adobe.example.workflow.listeners;

import org.apache.sling.event.jobs.JobProcessor;
import org.apache.sling.event.jobs.JobUtil;

import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;

import com.adobe.granite.workflow.event.WorkflowEvent;
import com.adobe.granite.workflow.exec.WorkItem;

/**
 * The <code>WorkflowEventCatcher</code> class listens to workflow events.
 */
@Component(metatype=false, immediate=true)
@Service(value=org.osgi.service.event.EventHandler.class)
public class WorkflowEventCatcher implements EventHandler, JobProcessor {

 @Property(value=com.adobe.granite.workflow.event.WorkflowEvent.EVENT_TOPIC)
 static final String EVENT_TOPICS = "event.topics";

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

 public void handleEvent(Event event) {
  JobUtil.processJob(event, this);
 }

 public boolean process(Event event) {
  logger.info("Received event of topic: " + event.getTopic());
  String topic = event.getTopic();

  try {
   if (topic.equals(WorkflowEvent.EVENT_TOPIC)) {
    WorkflowEvent wfevent = (WorkflowEvent)event;
    String eventType = wfevent.getEventType();
    String instanceId = wfevent.getWorkflowInstanceId();

    if (instanceId != null) {
     //workflow instance events
     if (eventType.equals(WorkflowEvent.WORKFLOW_STARTED_EVENT) ||
       eventType.equals(WorkflowEvent.WORKFLOW_RESUMED_EVENT) ||
       eventType.equals(WorkflowEvent.WORKFLOW_SUSPENDED_EVENT)) {
      // your code comes here...
     } else if (
       eventType.equals(WorkflowEvent.WORKFLOW_ABORTED_EVENT) ||
       eventType.equals(WorkflowEvent.WORKFLOW_COMPLETED_EVENT)) {
      // your code comes here...
     }
     // workflow node event
     if (eventType.equals(WorkflowEvent.NODE_TRANSITION_EVENT)) {
      WorkItem currentItem = (WorkItem) event.getProperty(WorkflowEvent.WORK_ITEM);
      // your code comes here...
     }
    }
   }
  } catch(Exception e){
   logger.debug(e.getMessage());
   e.printStackTrace();
  }
  return true;
 }
}

Experience Manager


Elevate and Empower Teams with Agentic AI for Exceptional Experiences

Online | Strategy Keynote | General Audience

Elevate and empower your CX teams with AI that transforms creativity, personalization, and productivity. Discover how Adobe is...

Tue, Mar 18, 1:00 PM PDT (8:00 PM UTC)

Register

B2B Reimagined: Transforming Go-to-Market Strategies for Profitable Growth

Online | Strategy Keynote | General Audience

B2B brands are facing a digital revolution. Buyers expect hyper-relevant content and self-service, while internally AI is transforming...

Wed, Mar 19, 1:00 PM PDT (8:00 PM UTC)

Register

Connect with Experience League at Summit!

Get front-row access to top sessions, hands-on activities, and networking—wherever you are!

Learn more