First, I'm using Activiti 5.15. (If this has been fixed in V5.15.1, let me know; my understanding is that version is exclusively for the MySQL 5.6 bug.)
When calling the "Start a Process Instance" REST resource, I noticed that the "variables" array was always empty, regardless of whether a process instance had associated variables or not and regardless of whether it was in process or completed. After looking into the code, it appeared that this snippet was causing the issue:
if (processInstance.getProcessVariables() != null) {
Map<String, Object> variableMap = processInstance.getProcessVariables();
for (String name : variableMap.keySet()) {
result.addVariable(createRestVariable(securedResource, name, variableMap.get(name),
RestVariableScope.LOCAL, processInstance.getId(), VARIABLE_PROCESS, false));
}
}
Of specific importance is the "processInstance.getProcessVariables()" call; it was always returning a Map with size 0.
I added the following code instead of the code above, and now the resource always returns a complete variable list:
RuntimeService runtimeService = ActivitiUtil.getRuntimeService();
HistoryService historyService = ActivitiUtil.getHistoryService();
if(processInstance.isEnded()) {
//Process complete. Get variable values from the history service.
result.setCompleted(true);
Map<String, Object> variableMap = new HashMap<String, Object>();
List<HistoricDetail> historicDetailList = historyService.createHistoricDetailQuery().executionId(processInstance.getId()).list();
for(HistoricDetail historicDetail : historicDetailList) {
Map<String, Integer> versionMap = new HashMap<String, Integer>();
if(historicDetail instanceof HistoricVariableUpdate) {
HistoricVariableUpdate historicVariableUpdate = (HistoricVariableUpdate) historicDetail;
if(versionMap.get(historicVariableUpdate.getVariableName()) == null) {
versionMap.put(historicVariableUpdate.getVariableName(), historicVariableUpdate.getRevision());
variableMap.put(historicVariableUpdate.getVariableName(), historicVariableUpdate.getValue());
}
else {
Integer currentRevision = historicVariableUpdate.getRevision();
Integer previousRevision = versionMap.get(historicVariableUpdate.getVariableName());
if(currentRevision > previousRevision) {
versionMap.put(historicVariableUpdate.getVariableName(), currentRevision);
variableMap.put(historicVariableUpdate.getVariableName(), historicVariableUpdate.getValue());
}
}
}
}
for (String name : variableMap.keySet()) {
result.addVariable(createRestVariable(securedResource, name, variableMap.get(name),
RestVariableScope.LOCAL, processInstance.getId(), VARIABLE_PROCESS, false));
}
}
else {
//Process not complete. Get runtime variables.
result.setCompleted(false);
Map<String, Object> variableMap = runtimeService.getVariables(processInstance.getId());
for (String name : variableMap.keySet()) {
result.addVariable(createRestVariable(securedResource, name, variableMap.get(name),
RestVariableScope.LOCAL, processInstance.getId(), VARIABLE_PROCESS, false));
}
}
(Note that I also added a boolean member variable to "ProcessInstanceResponse" to indicate whether a process instance had been completed or not.)
Perhaps I have something in the application misconfigured, and that is what is causing processInstance.getProcessVariables() call to return an empty set of variables? If so, please let me know. If not, maybe the code above will prove helpful to someone out there.