1
votes

I have integrated Camunda Engine with Spring in our application. I want to find properties assigned to each active task for the running process instance. I am able to get the task instances with following code

List<Task> tasks = this.taskService.createTaskQuery().processInstanceId("12").list() 

but if i cast task object into TaskEntity and then use getTaskDefinition() , I get null. Other way to get task details is through ProcessDefinitionEntity.getTaskDefinitions() but it also returns null.

How should I get the task detail?

2
What is your goal?jklee
@jklee During modelling of workflow (in .bpmn file), I added some properties in each task and I want to access those properties and also documentation attribute for currently active task.Tejas

2 Answers

2
votes

For read properties and documentation attributes use the BPMN Model API.

This example use a elementId for read both.

String processDefinitionId = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey(DEFINITON_KEY).singleResult().getId();

BpmnModelInstance bpmnModelInstance = repositoryService.getBpmnModelInstance(processDefinitionId);
ServiceTask serviceTask = (ServiceTask) bpmnModelInstance.getModelElementById(ELEMENT_ID);

// Documentation, is a collection, but the modeler supports only one attribute
Collection<Documentation> documentations = serviceTask.getDocumentations();

// Properties
Collection<Property> properties = serviceTask.getProperties();
2
votes

Above answer gave me a hint but didn't solve the problem completely so here is my code which is serving the purpose. My usertask in .bpmn file looks like:

 <bpmn:userTask id="Task_063x95d" name="Tech Task">
  <bpmn:documentation>SUCCESS,FAIL</bpmn:documentation>
  <bpmn:extensionElements>
    <camunda:inputOutput>
      <camunda:inputParameter name="language">Java</camunda:inputParameter>
      <camunda:outputParameter name="Platform">Linux</camunda:outputParameter>
    </camunda:inputOutput>
    <camunda:properties>
      <camunda:property name="user" value="Test_User" />
    </camunda:properties>
  </bpmn:extensionElements>
  <bpmn:incoming>SequenceFlow_1xjoyjq</bpmn:incoming>
  <bpmn:outgoing>SequenceFlow_028pkxo</bpmn:outgoing>
</bpmn:userTask>

I have analysed the .bpmn file and then just rendered its elements with help of below code

 // Active tasks for currently running instanceId(input to below code)
 List<Task> tasks = this.taskService.createTaskQuery().processInstanceId(instanceId).list();

 String documentation= null;

for (Task task : tasks)
{
//This gives [documentation][1] field.
documentation = task.getDescription();

UserTaskImpl modelElementById = (UserTaskImpl) bpmnModelInstance.getModelElementById(tasks.get(0)
    .getTaskDefinitionKey());
ExtensionElements childElementsByType2 = modelElementById.getExtensionElements();
Collection<ModelElementInstance> elements = childElementsByType2.getElements();
for (ModelElementInstance elem : elements)
{
    //To access all properties.
    if (elem instanceof CamundaPropertiesImpl)
    {
        CamundaPropertiesImpl camundaPropertiesImpl = (CamundaPropertiesImpl) elem;
        Collection<CamundaProperty> camundaProperties = camundaPropertiesImpl.getCamundaProperties();
        for (CamundaProperty test : camundaProperties)
        {
            System.out.println("camunda property name :" + test.getCamundaName() + " $ " + test.getCamundaValue());
        }

    }
    else if (elem instanceof CamundaInputOutputImpl)
    {
        // To access input/output param
        CamundaInputOutputImpl camundaInputOutputImpl = (CamundaInputOutputImpl) elem;
        for (CamundaInputParameter test : camundaInputOutputImpl.getCamundaInputParameters())
        {
            log.info("camunda input params name :" + test.getCamundaName() + " $ " + test.getTextContent());
        }
        for (CamundaOutputParameter test : camundaInputOutputImpl.getCamundaOutputParameters())
        {
            log.info("camunda output params name :" + test.getCamundaName() + " $ " + test.getTextContent());
        }
    }
 }
}