cancel
Showing results for 
Search instead for 
Did you mean: 

Getting WorkflowInstance of a given DelegateTask in Java

cafeguy
Champ in-the-making
Champ in-the-making
Hi Forum,
  I have an Activiti task that invokes a custom Java.  In the custom Java I need to send an email to the workflow initiator with an update.  I'm a little stuck on how to get the WorkflowInstance given the current task so I could get the initiator.  My code snippet is below.

public class MailTask extends ScriptTaskListener {


ServiceRegistry servReg = getServiceRegistry();
String taskId = "activiti$" + task.getId();
WorkflowTask wfTask = servReg.getWorkflowService().getTaskById(taskId);

// how do I get the workflow wfTask belongs to from here? 
WorkflowService has method getActiveWorkflows(), but it'll give me all the active workflow instances.
// there is a method getWorkflowById(workflowId), but I'm stuck of how to get the workflowId.
}

Thank you forum for your help.

Environment:
Alfresco community 4.2a
Activiti Engine 5.7
2 REPLIES 2

mstang
Champ in-the-making
Champ in-the-making
ScriptTaskListener implements TaskListener
–>public void notify(DelegateTask delegateTask)
—->DelegateTask extends VariableScope
public interface VariableScope –> Object getVariable(String variableName)
Therefore, delegateTask.getVariable("initiator") will return the Task Initiator.
I expect it is an ActivitiScriptNode, so you may have to do a getNodeRef call on the returned object.
HTH, let me know if I am being a little too terse ;-).

regards,
Mark

cafeguy
Champ in-the-making
Champ in-the-making
Thanks so much mstang.  That was plenty of information to get started in the right direction.  This is the code snippet for anyone else.

ActivitiScriptNode actScriptNode = (ActivitiScriptNode) task
      .getVariable("initiator");

   NodeRef nodeRef = actScriptNode.getNodeRef();
   Map<QName, Serializable> props = getServiceRegistry()
         .getNodeService().getProperties(nodeRef);
   Set<QName> keys = props.keySet();
   for (QName qname : keys) {
      if (qname.getLocalName().equalsIgnoreCase("email")) {
         String email = (String) props.get(qname);
      }
   }