cancel
Showing results for 
Search instead for 
Did you mean: 

Converting process instance from JSON to BpmnModel

medovarszki
Champ in-the-making
Champ in-the-making
Hi all,

I got a JSON representation of a process instance from the Activiti REST (see the attached 'process.json')
I want to create a diagram representation from this JSON with highlighted process history (tasks and flow elements), so I'd like to parse this JSON to a BpmnModel object (processDefJson contains the attached definition):

ObjectMapper mapper = new ObjectMapper();
BpmnJsonConverter converter = new BpmnJsonConverter();
JsonNode procDefJson = mapper.readTree(processDefJson.toString());
BpmnModel procDefModel = converter.convertToBpmnModel(procDefJson);

//getting the process instance history
JsonObject jsonRequest = new JsonObject();
jsonRequest.addProperty("processInstanceId", processInstanceId);
Representation requestBodyObject = new StringRepresentation(jsonRequest.toString(), MediaType.APPLICATION_JSON);
Representation resource = WorkflowUtils.getClientResource("query/historic-activity-instances").post(requestBodyObject);
JsonObject processHistoryJson = WorkflowUtils.getAsJsonElement(resource).getAsJsonObject();
JsonArray processHistory = processHistoryJson.get("data").getAsJsonArray();

List<String> highLightedActivities = Lists.newArrayList();
List<String> highLightedFlows = Lists.newArrayList();
            
for(JsonElement element : processHistory) {
    highLightedActivities.add(element.getAsJsonObject().get("id").getAsString());
}
            
ProcessDiagramGenerator processDiagramGenerator = new DefaultProcessDiagramGenerator();
InputStream generatedDiagram = processDiagramGenerator.generateDiagram(procDefModel, "png", highLightedActivities, highLightedFlows);


The snippet above results an NPE at the diagram generation because of my procDefModel contains no processes, but the JSON does. If I'm right, something is wrong at the BpmnJsonConverter. The convertToBpmnModel(JsonNode modelNode) method returns at line 434 (shapesArrayNode is null), but I'm confused why.

I'm working with Activiti 5.18.0, Jackson-databind 2.2.3 for mapping and Restlet 2.1.1
4 REPLIES 4

jbarrez
Star Contributor
Star Contributor
Which REST endpoint did you use to get this json? The BpmnJsonConverter is used to convert the json coming from the Modeler to XML/BpmnModel. But the format seems not to be correct.

medovarszki
Champ in-the-making
Champ in-the-making
I get the definition ID from the process instance as the following way:
<java>
//processInstanceId given as a function parameter

Representation processInstance = WorkflowUtils.getClientResource("runtime/process-instances/" + processInstanceId).get(MediaType.APPLICATION_JSON);
JsonElement processInstanceJson = WorkflowUtils.getAsJsonElement(processInstance);
  
Representation processDefinition = WorkflowUtils.getClientResource("repository/process-definitions/" + processInstanceJson.getAsJsonObject().get("processDefinitionId").getAsString()).get(MediaType.APPLICATION_JSON);
JsonElement processDefinitionJson = WorkflowUtils.getAsJsonElement(processDefinition);

Representation procDefRepresentation = WorkflowUtils.getClientResource("repository/process-definitions/" + processDefinitionJson.getAsJsonObject().get("id").getAsString() + "/model").get(MediaType.APPLICATION_JSON);
JsonElement processDefJson = WorkflowUtils.getAsJsonElement(procDefRepresentation);
</java>

jbarrez
Star Contributor
Star Contributor
Ok, so the /repository/process-definitions/{processDefinitionId}/model gives back the BpmnModel representation, which does not contain any shapes or so, this is simply a JSON representation of the BPMNModel (ie the xml of the process definition).

The BpmJsonConverter does NOT work on that JSON, but on the JSON produced by for example the Modeler.

medovarszki
Champ in-the-making
Champ in-the-making
Ok I understand. I found a possible solution: I can use the /repository/process-definitions/{processDefinitionId}/resourcedata endpoint which returns the model with shapes and so in XML format. Then I parse it with BpmnXMLConverter to BpmnModel.