Hi @johann_lu ,
To get the current acting node of a workflow in AEM 6.5 using Java code, you can use the Workflow API provided by AEM. Here is a step-by-step guide on how to achieve this:
Step-by-Step Guide
Add Dependencies: Ensure you have the necessary dependencies in your Maven project. Add the following dependencies to your pom.xml:
<dependency>
<groupId>com.adobe.granite</groupId>
<artifactId>com.adobe.granite.workflow.api</artifactId>
<version>2.0.28</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
<version>2.20.0</version>
</dependency>
Create a Java Class to Get the Current Workflow Node: Create a Java class that interacts with the Workflow API to get the current node of a workflow.
package com.example.aem.core.workflow;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.Route;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowInstance;
import com.adobe.granite.workflow.exec.WorkflowModel;
import com.adobe.granite.workflow.exec.WorkflowNode;
import com.adobe.granite.workflow.model.WorkflowNodeType;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Session;
@Component(service = WorkflowNodeService.class)
public class WorkflowNodeService {
private static final Logger LOG = LoggerFactory.getLogger(WorkflowNodeService.class);
@Reference
private com.adobe.granite.workflow.WorkflowSession workflowSession;
public WorkflowNode getCurrentWorkflowNode(String workflowInstanceId, ResourceResolver resourceResolver) {
try {
Session session = resourceResolver.adaptTo(Session.class);
WorkflowSession wfSession = workflowSession.adaptTo(WorkflowSession.class);
WorkflowInstance workflowInstance = wfSession.getWorkflow(workflowInstanceId);
for (WorkItem workItem : workflowInstance.getWorkItems()) {
WorkflowNode node = workItem.getNode();
if (node.getType().equals(WorkflowNodeType.PARTICIPANT)) {
return node;
}
}
} catch (Exception e) {
LOG.error("Error getting current workflow node: ", e);
}
return null;
}
}
Use the Service in Your Code: Inject the WorkflowNodeService into your servlet or another OSGi component to use it.
@Component(service = MyWorkflowServlet.class,
immediate = true,
property = {
"sling.servlet.paths=/bin/myworkflow",
"sling.servlet.methods=GET"
})
public class MyWorkflowServlet extends SlingAllMethodsServlet {
@Reference
private WorkflowNodeService workflowNodeService;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
String workflowInstanceId = request.getParameter("workflowInstanceId");
ResourceResolver resourceResolver = request.getResourceResolver();
WorkflowNode currentNode = workflowNodeService.getCurrentWorkflowNode(workflowInstanceId, resourceResolver);
if (currentNode != null) {
response.getWriter().write("Current Node: " + currentNode.getTitle());
} else {
response.getWriter().write("Workflow node not found.");
}
}
}
Explanation:
WorkflowNodeService Class:
- getCurrentWorkflowNode: This method takes the workflow instance ID and a ResourceResolver as parameters. It retrieves the current node of the workflow instance.
- workflowSession: This is a reference to the Workflow Session, which is used to interact with the workflow engine.
MyWorkflowServlet Class:
- doGet: This method handles GET requests. It retrieves the workflow instance ID from the request, uses the WorkflowNodeService to get the current node, and writes the node's title to the response.
Notes:
- Error Handling: Proper error handling is implemented to catch any exceptions that might occur during the workflow node retrieval.
- Service Registration: The WorkflowNodeService is registered as an OSGi service, making it available for injection into other components.
By following these steps, you can retrieve the current node of a workflow instance in AEM using Java code. This example demonstrates how to set up the necessary dependencies, create a service to interact with the Workflow API, and use this service in a servlet to respond with the current workflow node information.