Search in sources :

Example 1 with ProcessInstanceResponse

use of org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse in project carbon-business-process by wso2.

the class BaseProcessInstanceService method activateProcessInstance.

protected ProcessInstanceResponse activateProcessInstance(ProcessInstance processInstance, UriInfo uriInfo) {
    if (!processInstance.isSuspended()) {
        throw new BPMNConflictException("Process instance with id '" + processInstance.getId() + "' is already active.");
    }
    RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
    runtimeService.activateProcessInstanceById(processInstance.getId());
    ProcessInstanceResponse response = new RestResponseFactory().createProcessInstanceResponse(processInstance, uriInfo.getBaseUri().toString());
    // No need to re-fetch the instance, just alter the suspended state of the result-object
    response.setSuspended(false);
    return response;
}
Also used : BPMNConflictException(org.wso2.carbon.bpmn.rest.common.exception.BPMNConflictException) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) RuntimeService(org.activiti.engine.RuntimeService) ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse)

Example 2 with ProcessInstanceResponse

use of org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse in project carbon-business-process by wso2.

the class FormDataService method submitForm.

@POST
@Path("/")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response submitForm(SubmitFormRequest submitRequest) {
    if (submitRequest == null) {
        throw new ActivitiException("A request body was expected when executing the form submit.");
    }
    if (submitRequest.getTaskId() == null && submitRequest.getProcessDefinitionId() == null) {
        throw new ActivitiIllegalArgumentException("The taskId or processDefinitionId property has to be provided");
    }
    Map<String, String> propertyMap = new HashMap<String, String>();
    if (submitRequest.getProperties() != null) {
        for (RestFormProperty formProperty : submitRequest.getProperties()) {
            propertyMap.put(formProperty.getId(), formProperty.getValue());
        }
    }
    FormService formService = BPMNOSGIService.getFormService();
    Response.ResponseBuilder response = Response.ok();
    if (submitRequest.getTaskId() != null) {
        formService.submitTaskFormData(submitRequest.getTaskId(), propertyMap);
        response.status(Response.Status.NO_CONTENT);
        return response.build();
    } else {
        ProcessInstance processInstance = null;
        if (submitRequest.getBusinessKey() != null) {
            processInstance = formService.submitStartFormData(submitRequest.getProcessDefinitionId(), submitRequest.getBusinessKey(), propertyMap);
        } else {
            processInstance = formService.submitStartFormData(submitRequest.getProcessDefinitionId(), propertyMap);
        }
        ProcessInstanceResponse processInstanceResponse = new RestResponseFactory().createProcessInstanceResponse(processInstance, uriInfo.getBaseUri().toString());
        return response.entity(processInstanceResponse).build();
    }
}
Also used : ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse) Response(javax.ws.rs.core.Response) ActivitiException(org.activiti.engine.ActivitiException) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) HashMap(java.util.HashMap) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) FormService(org.activiti.engine.FormService) ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse) RestFormProperty(org.wso2.carbon.bpmn.rest.model.form.RestFormProperty) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 3 with ProcessInstanceResponse

use of org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse in project carbon-business-process by wso2.

the class BaseProcessInstanceService method suspendProcessInstance.

protected ProcessInstanceResponse suspendProcessInstance(ProcessInstance processInstance, RestResponseFactory restResponseFactory, UriInfo uriInfo) {
    if (processInstance.isSuspended()) {
        throw new BPMNConflictException("Process instance with id '" + processInstance.getId() + "' is already suspended.");
    }
    RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
    runtimeService.suspendProcessInstanceById(processInstance.getId());
    ProcessInstanceResponse response = restResponseFactory.createProcessInstanceResponse(processInstance, uriInfo.getBaseUri().toString());
    // No need to re-fetch the instance, just alter the suspended state of the result-object
    response.setSuspended(true);
    return response;
}
Also used : BPMNConflictException(org.wso2.carbon.bpmn.rest.common.exception.BPMNConflictException) RuntimeService(org.activiti.engine.RuntimeService) ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse)

Example 4 with ProcessInstanceResponse

use of org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse in project carbon-business-process by wso2.

the class ProcessInstanceService method startInstance.

@POST
@Path("/")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response startInstance(ProcessInstanceCreateRequest processInstanceCreateRequest) {
    if (log.isDebugEnabled()) {
        log.debug("ProcessInstanceCreateRequest:" + processInstanceCreateRequest.getProcessDefinitionId());
        log.debug(" processInstanceCreateRequest.getVariables().size():" + processInstanceCreateRequest.getVariables().size());
    }
    if (processInstanceCreateRequest.getProcessDefinitionId() == null && processInstanceCreateRequest.getProcessDefinitionKey() == null && processInstanceCreateRequest.getMessage() == null) {
        throw new ActivitiIllegalArgumentException("Either processDefinitionId, processDefinitionKey or message is required.");
    }
    int paramsSet = ((processInstanceCreateRequest.getProcessDefinitionId() != null) ? 1 : 0) + ((processInstanceCreateRequest.getProcessDefinitionKey() != null) ? 1 : 0) + ((processInstanceCreateRequest.getMessage() != null) ? 1 : 0);
    if (paramsSet > 1) {
        throw new ActivitiIllegalArgumentException("Only one of processDefinitionId, processDefinitionKey or message should be set.");
    }
    if (processInstanceCreateRequest.isCustomTenantSet()) {
        // Tenant-id can only be used with either key or message
        if (processInstanceCreateRequest.getProcessDefinitionId() != null) {
            throw new ActivitiIllegalArgumentException("TenantId can only be used with either processDefinitionKey or message.");
        }
    } else {
        // if no tenantId, it must be from definitionId
        if (processInstanceCreateRequest.getProcessDefinitionId() == null) {
            throw new ActivitiIllegalArgumentException("TenantId should be specified to be used with either " + "processDefinitionKey or message.");
        }
    }
    // Have to add the validation part here
    if (!isValidUserToStartProcess(processInstanceCreateRequest)) {
        throw new RestApiBasicAuthenticationException("User doesn't have the necessary permission to start the process");
    }
    if (processInstanceCreateRequest.getSkipInstanceCreation() || processInstanceCreateRequest.getSkipInstanceCreationIfExist()) {
        ProcessInstanceQueryRequest processInstanceQueryRequest = processInstanceCreateRequest.cloneInstanceCreationRequest();
        Map<String, String> allRequestParams = allRequestParams(uriInfo);
        DataResponse dataResponse = getQueryResponse(processInstanceQueryRequest, allRequestParams, uriInfo);
        if (log.isDebugEnabled()) {
            log.debug("ProcessInstanceCreation check:" + dataResponse.getSize());
        }
        int dataResponseSize = dataResponse.getSize();
        if (dataResponseSize > 0) {
            if (processInstanceCreateRequest.getCorrelate()) {
                if (dataResponseSize != 1) {
                    String responseMessage = "Correlation matching failed as there are more than one matching instance with " + "given variables state";
                    throw new NotFoundException(Response.ok().entity(responseMessage).status(Response.Status.NOT_FOUND).build());
                }
                if (processInstanceCreateRequest.getMessageName() == null) {
                    String responseMessage = "Correlation matching failed as messageName property is not specified";
                    throw new ActivitiIllegalArgumentException(responseMessage);
                }
                return performCorrelation(processInstanceCreateRequest);
            } else {
                dataResponse.setMessage("Instance information corresponding to the request");
                return Response.ok().entity(dataResponse).build();
            }
        }
    }
    RestResponseFactory restResponseFactory = new RestResponseFactory();
    Map<String, Object> startVariables = null;
    if (processInstanceCreateRequest.getVariables() != null) {
        startVariables = new HashMap<>();
        for (RestVariable variable : processInstanceCreateRequest.getVariables()) {
            if (variable.getName() == null) {
                throw new ActivitiIllegalArgumentException("Variable name is required.");
            }
            startVariables.put(variable.getName(), restResponseFactory.getVariableValue(variable));
        }
    }
    // updated the additional variables
    if (processInstanceCreateRequest.getAdditionalVariables() != null) {
        if (startVariables == null) {
            startVariables = new HashMap<>();
        }
        for (RestVariable variable : processInstanceCreateRequest.getAdditionalVariables()) {
            if (variable.getName() == null) {
                throw new ActivitiIllegalArgumentException("Additional Variable name is required.");
            }
            startVariables.put(variable.getName(), restResponseFactory.getVariableValue(variable));
        }
    }
    RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
    IdentityService identityService = BPMNOSGIService.getIdentityService();
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    String userName = carbonContext.getUsername();
    ProcessInstanceResponse processInstanceResponse;
    // Actually start the instance based on key or id
    try {
        ProcessInstance instance;
        identityService.setAuthenticatedUserId(userName);
        if (processInstanceCreateRequest.getProcessDefinitionId() != null) {
            instance = runtimeService.startProcessInstanceById(processInstanceCreateRequest.getProcessDefinitionId(), processInstanceCreateRequest.getBusinessKey(), startVariables);
        } else if (processInstanceCreateRequest.getProcessDefinitionKey() != null) {
            if (processInstanceCreateRequest.isCustomTenantSet()) {
                instance = runtimeService.startProcessInstanceByKeyAndTenantId(processInstanceCreateRequest.getProcessDefinitionKey(), processInstanceCreateRequest.getBusinessKey(), startVariables, processInstanceCreateRequest.getTenantId());
            } else {
                instance = runtimeService.startProcessInstanceByKey(processInstanceCreateRequest.getProcessDefinitionKey(), processInstanceCreateRequest.getBusinessKey(), startVariables);
            }
        } else {
            if (processInstanceCreateRequest.isCustomTenantSet()) {
                instance = runtimeService.startProcessInstanceByMessageAndTenantId(processInstanceCreateRequest.getMessage(), processInstanceCreateRequest.getBusinessKey(), startVariables, processInstanceCreateRequest.getTenantId());
            } else {
                instance = runtimeService.startProcessInstanceByMessage(processInstanceCreateRequest.getMessage(), processInstanceCreateRequest.getBusinessKey(), startVariables);
            }
        }
        HistoryService historyService = BPMNOSGIService.getHistoryService();
        if (processInstanceCreateRequest.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());
            }
            processInstanceResponse = restResponseFactory.createProcessInstanceResponse(instance, true, runtimeVariableMap, historicVariableList, uriInfo.getBaseUri().toString());
        } else {
            processInstanceResponse = restResponseFactory.createProcessInstanceResponse(instance, uriInfo.getBaseUri().toString());
        }
    } catch (ActivitiObjectNotFoundException aonfe) {
        throw new ActivitiIllegalArgumentException(aonfe.getMessage(), aonfe);
    } finally {
        identityService.setAuthenticatedUserId(null);
    }
    return Response.ok().status(Response.Status.CREATED).entity(processInstanceResponse).build();
}
Also used : RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) RuntimeService(org.activiti.engine.RuntimeService) NotFoundException(javax.ws.rs.NotFoundException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) HistoryService(org.activiti.engine.HistoryService) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ProcessInstanceQueryRequest(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceQueryRequest) RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) IdentityService(org.activiti.engine.IdentityService) RestApiBasicAuthenticationException(org.wso2.carbon.bpmn.rest.common.exception.RestApiBasicAuthenticationException) DataResponse(org.wso2.carbon.bpmn.rest.model.common.DataResponse) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 5 with ProcessInstanceResponse

use of org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse in project carbon-business-process by wso2.

the class ProcessInstanceService method getProcessInstance.

@GET
@Path("/{processInstanceId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getProcessInstance(@PathParam("processInstanceId") String processInstanceId) {
    ProcessInstance processInstance = getProcessInstanceFromRequest(processInstanceId);
    RestResponseFactory restResponseFactory = new RestResponseFactory();
    ProcessInstanceResponse processInstanceResponse = restResponseFactory.createProcessInstanceResponse(processInstance, uriInfo.getBaseUri().toString());
    return Response.ok().entity(processInstanceResponse).build();
}
Also used : RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ProcessInstanceResponse (org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse)5 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)4 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 RuntimeService (org.activiti.engine.RuntimeService)3 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)3 POST (javax.ws.rs.POST)2 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)2 BPMNConflictException (org.wso2.carbon.bpmn.rest.common.exception.BPMNConflictException)2 HashMap (java.util.HashMap)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 NotFoundException (javax.ws.rs.NotFoundException)1 Response (javax.ws.rs.core.Response)1 ActivitiException (org.activiti.engine.ActivitiException)1 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)1 FormService (org.activiti.engine.FormService)1 HistoryService (org.activiti.engine.HistoryService)1 IdentityService (org.activiti.engine.IdentityService)1 HistoricVariableInstance (org.activiti.engine.history.HistoricVariableInstance)1