@RequestMapping(value="/runtime/process-instances", method = RequestMethod.POST, produces="application/json")
public ProcessInstanceResponse createProcessInstance(@RequestBody ProcessInstanceCreateRequest request,
HttpServletRequest httpRequest, HttpServletResponse response) {
if (request.getProcessDefinitionId() == null && request.getProcessDefinitionKey() == null && request.getMessage() == null) {
throw new ActivitiIllegalArgumentException("Either processDefinitionId, processDefinitionKey or message is required.");
}
int paramsSet = ((request.getProcessDefinitionId() != null) ? 1 : 0)
+ ((request.getProcessDefinitionKey() != null) ? 1 : 0)
+ ((request.getMessage() != null) ? 1 : 0);
if (paramsSet > 1) {
throw new ActivitiIllegalArgumentException("Only one of processDefinitionId, processDefinitionKey or message should be set.");
}
if (request.isCustomTenantSet()) {
// Tenant-id can only be used with either key or message
if(request.getProcessDefinitionId() != null) {
throw new ActivitiIllegalArgumentException("TenantId can only be used with either processDefinitionKey or message.");
}
}
Map<String, Object> startVariables = null;
if (request.getVariables() != null) {
startVariables = new HashMap<String, Object>();
for (RestVariable variable : request.getVariables()) {
if (variable.getName() == null) {
throw new ActivitiIllegalArgumentException("Variable name is required.");
}
startVariables.put(variable.getName(), restResponseFactory.getVariableValue(variable));
}
}
// Actually start the instance based on key or id
try {
ProcessInstance instance = null;
if (request.getProcessDefinitionId() != null) {
instance = runtimeService.startProcessInstanceById(
request.getProcessDefinitionId(), request.getBusinessKey(), startVariables);
} else if (request.getProcessDefinitionKey() != null) {
if (request.isCustomTenantSet()) {
instance = runtimeService.startProcessInstanceByKeyAndTenantId(
request.getProcessDefinitionKey(), request.getBusinessKey(), startVariables, request.getTenantId());
} else {
instance = runtimeService.startProcessInstanceByKey(
request.getProcessDefinitionKey(), request.getBusinessKey(), startVariables);
}
} else {
if (request.isCustomTenantSet()) {
instance = runtimeService.startProcessInstanceByMessageAndTenantId(
request.getMessage(), request.getBusinessKey(), startVariables, request.getTenantId());
} else {
instance = runtimeService.startProcessInstanceByMessage(
request.getMessage(), request.getBusinessKey(), startVariables);
}
}
response.setStatus(HttpStatus.CREATED.value());
String serverRootUrl = httpRequest.getRequestURL().toString().replace("/runtime/process-instances", "");
//Added by Ryan Johnston
if (request.getReturnVariables()) {
Map<String, Object> runtimeVariableMap = null;
List<HistoricVariableInstance> historicVariableList = null;
if (instance.isEnded()) {
historicVariableList = historyService.createHistoricVariableInstanceQuery()
.processInstanceId(instance.getId())
.list();
} else {
runtimeVariableMap = runtimeService.getVariables(instance.getId());
}
return restResponseFactory.createProcessInstanceResponse(instance, true,
runtimeVariableMap, historicVariableList, serverRootUrl);
} else {
return restResponseFactory.createProcessInstanceResponse(instance, serverRootUrl);
}
//End Added by Ryan Johnston
//Removed by Ryan Johnston (obsolete given the above).
//return factory.createProcessInstanceResponse(this, instance);
} catch(ActivitiObjectNotFoundException aonfe) {
throw new ActivitiIllegalArgumentException(aonfe.getMessage(), aonfe);
}
}