Search in sources :

Example 1 with CorrelationActionRequest

use of org.wso2.carbon.bpmn.rest.model.correlation.CorrelationActionRequest in project carbon-business-process by wso2.

the class CorrelationProcess method getQueryResponse.

public Response getQueryResponse(CorrelationActionRequest correlationActionRequest, UriInfo uriInfo) {
    RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
    ExecutionQuery query = runtimeService.createExecutionQuery();
    String value = correlationActionRequest.getProcessDefinitionId();
    if (value != null) {
        query.processDefinitionId(value);
    }
    value = correlationActionRequest.getProcessDefinitionKey();
    if (value != null) {
        query.processDefinitionKey(value);
    }
    value = correlationActionRequest.getMessageName();
    if (value != null) {
        query.messageEventSubscriptionName(value);
    }
    value = correlationActionRequest.getSignalName();
    if (value != null) {
        query.signalEventSubscriptionName(value);
    }
    List<QueryVariable> queryVariableList = correlationActionRequest.getCorrelationVariables();
    if (queryVariableList != null) {
        List<QueryVariable> updatedQueryVariableList = new ArrayList<>();
        for (QueryVariable queryVariable : queryVariableList) {
            if (queryVariable.getVariableOperation() == null) {
                queryVariable.setOperation("equals");
            }
            updatedQueryVariableList.add(queryVariable);
        }
        addVariables(query, updatedQueryVariableList, true);
    }
    value = correlationActionRequest.getTenantId();
    if (value != null) {
        query.executionTenantId(value);
    }
    QueryProperty qp = allowedSortProperties.get("processInstanceId");
    ((AbstractQuery) query).orderBy(qp);
    query.asc();
    List<Execution> executionList = query.listPage(0, 10);
    int size = executionList.size();
    if (size == 0) {
        throw new ActivitiIllegalArgumentException("No Executions found to correlate with given information");
    }
    if (size > 1) {
        throw new ActivitiIllegalArgumentException("More than one Executions found to correlate with given information");
    }
    Execution execution = executionList.get(0);
    String action = correlationActionRequest.getAction();
    if (CorrelationActionRequest.ACTION_SIGNAL.equals(action)) {
        if (correlationActionRequest.getVariables() != null) {
            runtimeService.signal(execution.getId(), getVariablesToSet(correlationActionRequest));
        } else {
            runtimeService.signal(execution.getId());
        }
    } else if (CorrelationActionRequest.ACTION_SIGNAL_EVENT_RECEIVED.equals(action)) {
        if (correlationActionRequest.getSignalName() == null) {
            throw new ActivitiIllegalArgumentException("Signal name is required");
        }
        if (correlationActionRequest.getVariables() != null) {
            runtimeService.signalEventReceived(correlationActionRequest.getSignalName(), execution.getId(), getVariablesToSet(correlationActionRequest));
        } else {
            runtimeService.signalEventReceived(correlationActionRequest.getSignalName(), execution.getId());
        }
    } else if (CorrelationActionRequest.ACTION_MESSAGE_EVENT_RECEIVED.equals(action)) {
        if (correlationActionRequest.getMessageName() == null) {
            throw new ActivitiIllegalArgumentException("Message name is required");
        }
        if (correlationActionRequest.getVariables() != null) {
            runtimeService.messageEventReceived(correlationActionRequest.getMessageName(), execution.getId(), getVariablesToSet(correlationActionRequest));
        } else {
            runtimeService.messageEventReceived(correlationActionRequest.getMessageName(), execution.getId());
        }
    } else {
        throw new ActivitiIllegalArgumentException("Invalid action: '" + correlationActionRequest.getAction() + "'.");
    }
    Response.ResponseBuilder responseBuilder = Response.ok();
    // Re-fetch the execution, could have changed due to action or even completed
    execution = runtimeService.createExecutionQuery().executionId(execution.getId()).singleResult();
    if (execution == null) {
        // Execution is finished, return empty body to inform user
        responseBuilder.status(Response.Status.NO_CONTENT);
        return responseBuilder.build();
    } else {
        return responseBuilder.entity(new RestResponseFactory().createExecutionResponse(execution, uriInfo.getBaseUri().toString())).build();
    }
}
Also used : RuntimeService(org.activiti.engine.RuntimeService) QueryVariable(org.wso2.carbon.bpmn.rest.engine.variable.QueryVariable) ArrayList(java.util.ArrayList) CorrelationQueryProperty(org.wso2.carbon.bpmn.rest.model.common.CorrelationQueryProperty) QueryProperty(org.activiti.engine.query.QueryProperty) ExecutionQuery(org.activiti.engine.runtime.ExecutionQuery) Response(javax.ws.rs.core.Response) Execution(org.activiti.engine.runtime.Execution) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) AbstractQuery(org.activiti.engine.impl.AbstractQuery)

Example 2 with CorrelationActionRequest

use of org.wso2.carbon.bpmn.rest.model.correlation.CorrelationActionRequest in project carbon-business-process by wso2.

the class BaseExecutionService method getVariablesToSet.

protected Map<String, Object> getVariablesToSet(CorrelationActionRequest correlationActionRequest) {
    Map<String, Object> variablesToSet = new HashMap<String, Object>();
    for (RestVariable var : correlationActionRequest.getVariables()) {
        if (var.getName() == null) {
            throw new ActivitiIllegalArgumentException("Variable name is required");
        }
        Object actualVariableValue = new RestResponseFactory().getVariableValue(var);
        variablesToSet.put(var.getName(), actualVariableValue);
    }
    return variablesToSet;
}
Also used : RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException)

Example 3 with CorrelationActionRequest

use of org.wso2.carbon.bpmn.rest.model.correlation.CorrelationActionRequest in project carbon-business-process by wso2.

the class CorrelationService method recieveMessage.

@POST
@Path("/")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response recieveMessage(CorrelationActionRequest correlationActionRequest) {
    if (correlationActionRequest.getProcessDefinitionId() == null && correlationActionRequest.getProcessDefinitionKey() == null && (correlationActionRequest.getMessageName() == null && correlationActionRequest.getSignalName() == null)) {
        throw new ActivitiIllegalArgumentException("Either processDefinitionId, processDefinitionKey, signal or " + "message is required.");
    }
    int paramsSet = ((correlationActionRequest.getProcessDefinitionId() != null) ? 1 : 0) + ((correlationActionRequest.getProcessDefinitionKey() != null) ? 1 : 0);
    if (paramsSet > 1) {
        throw new ActivitiIllegalArgumentException("Only one of processDefinitionId or processDefinitionKey should be set.");
    }
    paramsSet = ((correlationActionRequest.getMessageName() != null) ? 1 : 0) + ((correlationActionRequest.getSignalName() != null) ? 1 : 0);
    if (paramsSet > 1) {
        throw new ActivitiIllegalArgumentException("Only one of message name or signal should be " + "set.");
    }
    CorrelationProcess correlationProcess = new CorrelationProcess();
    return correlationProcess.getQueryResponse(correlationActionRequest, uriInfo);
}
Also used : ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) CorrelationProcess(org.wso2.carbon.bpmn.rest.common.CorrelationProcess) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 4 with CorrelationActionRequest

use of org.wso2.carbon.bpmn.rest.model.correlation.CorrelationActionRequest in project carbon-business-process by wso2.

the class CorrelationProcess method getVariablesToSet.

protected Map<String, Object> getVariablesToSet(CorrelationActionRequest correlationActionRequest) {
    Map<String, Object> variablesToSet = new HashMap<String, Object>();
    for (RestVariable var : correlationActionRequest.getVariables()) {
        if (var.getName() == null) {
            throw new ActivitiIllegalArgumentException("Variable name is required");
        }
        Object actualVariableValue = new RestResponseFactory().getVariableValue(var);
        variablesToSet.put(var.getName(), actualVariableValue);
    }
    return variablesToSet;
}
Also used : RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) HashMap(java.util.HashMap) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException)

Example 5 with CorrelationActionRequest

use of org.wso2.carbon.bpmn.rest.model.correlation.CorrelationActionRequest in project carbon-business-process by wso2.

the class ProcessInstanceService method performCorrelation.

private Response performCorrelation(ProcessInstanceCreateRequest processInstanceCreateRequest) {
    CorrelationActionRequest correlationActionRequest = new CorrelationActionRequest();
    String requestValue = processInstanceCreateRequest.getProcessDefinitionId();
    if (requestValue != null) {
        correlationActionRequest.setProcessDefinitionId(processInstanceCreateRequest.getProcessDefinitionId());
    }
    requestValue = processInstanceCreateRequest.getProcessDefinitionKey();
    if (requestValue != null) {
        correlationActionRequest.setProcessDefinitionKey(requestValue);
    }
    if (processInstanceCreateRequest.isCustomTenantSet()) {
        correlationActionRequest.setTenantId(processInstanceCreateRequest.getTenantId());
    }
    requestValue = processInstanceCreateRequest.getMessageName();
    if (requestValue != null) {
        correlationActionRequest.setMessageName(requestValue);
    }
    List<RestVariable> variables = processInstanceCreateRequest.getVariables();
    if (variables != null) {
        RestResponseFactory restResponseFactory = new RestResponseFactory();
        List<QueryVariable> correlationVariableList = new ArrayList<>();
        for (RestVariable variable : variables) {
            QueryVariable correlationVariable = new QueryVariable();
            correlationVariable.setName(variable.getName());
            correlationVariable.setOperation("equals");
            correlationVariable.setType(variable.getType());
            correlationVariable.setValue(restResponseFactory.getVariableValue(variable));
            correlationVariableList.add(correlationVariable);
        }
        correlationActionRequest.setCorrelationVariables(correlationVariableList);
    }
    variables = processInstanceCreateRequest.getAdditionalVariables();
    if (variables != null) {
        correlationActionRequest.setVariables(variables);
    }
    correlationActionRequest.setAction(CorrelationActionRequest.ACTION_MESSAGE_EVENT_RECEIVED);
    return new CorrelationProcess().getQueryResponse(correlationActionRequest, uriInfo);
}
Also used : RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) CorrelationProcess(org.wso2.carbon.bpmn.rest.common.CorrelationProcess) CorrelationActionRequest(org.wso2.carbon.bpmn.rest.model.correlation.CorrelationActionRequest) QueryVariable(org.wso2.carbon.bpmn.rest.engine.variable.QueryVariable) ArrayList(java.util.ArrayList)

Aggregations

ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)4 RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)3 ArrayList (java.util.ArrayList)2 CorrelationProcess (org.wso2.carbon.bpmn.rest.common.CorrelationProcess)2 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)2 QueryVariable (org.wso2.carbon.bpmn.rest.engine.variable.QueryVariable)2 HashMap (java.util.HashMap)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Response (javax.ws.rs.core.Response)1 RuntimeService (org.activiti.engine.RuntimeService)1 AbstractQuery (org.activiti.engine.impl.AbstractQuery)1 QueryProperty (org.activiti.engine.query.QueryProperty)1 Execution (org.activiti.engine.runtime.Execution)1 ExecutionQuery (org.activiti.engine.runtime.ExecutionQuery)1 CorrelationQueryProperty (org.wso2.carbon.bpmn.rest.model.common.CorrelationQueryProperty)1 CorrelationActionRequest (org.wso2.carbon.bpmn.rest.model.correlation.CorrelationActionRequest)1