Search in sources :

Example 56 with Variable

use of org.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.

the class ProcessTerminationKPIListener method notify.

@Override
public void notify(DelegateExecution delegateExecution) throws IOException {
    try {
        List<ProcessInstance> runtimeProcessInstances = delegateExecution.getEngineServices().getRuntimeService().createProcessInstanceQuery().processInstanceId(delegateExecution.getProcessInstanceId()).list();
        if (runtimeProcessInstances.size() == 1) {
            ProcessInstance runtimeProcessInstance = runtimeProcessInstances.get(0);
            BPMNAnalyticsHolder.getInstance().getBpmnDataPublisher().publishKPIvariableData(runtimeProcessInstance);
        }
    } catch (BPMNDataPublisherException e) {
        String errMsg = "Process Variable Data Publishing failed.";
        log.error(errMsg, e);
    // Caught exception is not thrown as we do not need to stop the process termination, due to an error in
    // process data publishing (for analytics)
    }
}
Also used : ProcessInstance(org.activiti.engine.runtime.ProcessInstance) BPMNDataPublisherException(org.wso2.carbon.bpmn.analytics.publisher.BPMNDataPublisherException)

Example 57 with Variable

use of org.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.

the class ProcessInstanceService method createBinaryExecutionVariable.

@POST
@Path("/{processInstanceId}/variables")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response createBinaryExecutionVariable(@PathParam("processInstanceId") String processInstanceId, @Context HttpServletRequest httpServletRequest, MultipartBody multipartBody) {
    Execution execution = getExecutionInstanceFromRequest(processInstanceId);
    Response response;
    try {
        response = createBinaryExecutionVariable(execution, false, RestResponseFactory.VARIABLE_PROCESS, multipartBody);
    } catch (IOException | ServletException e) {
        throw new BPMNRestException("Exception occured during creating binary execution variable", e);
    }
    return response;
}
Also used : DataResponse(org.wso2.carbon.bpmn.rest.model.common.DataResponse) Response(javax.ws.rs.core.Response) ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse) ServletException(javax.servlet.ServletException) Execution(org.activiti.engine.runtime.Execution) BPMNRestException(org.wso2.carbon.bpmn.rest.common.exception.BPMNRestException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 58 with Variable

use of org.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.

the class ProcessInstanceService method createExecutionVariable.

protected Response createExecutionVariable(Execution execution, boolean override, int variableType, HttpServletRequest httpServletRequest) throws IOException, ServletException {
    boolean debugEnabled = log.isDebugEnabled();
    if (debugEnabled) {
        log.debug("httpServletRequest.getContentType():" + httpServletRequest.getContentType());
    }
    Response.ResponseBuilder responseBuilder = Response.ok();
    if (debugEnabled) {
        log.debug("Processing non binary variable");
    }
    List<RestVariable> inputVariables = new ArrayList<>();
    List<RestVariable> resultVariables = new ArrayList<>();
    try {
        if (Utils.isApplicationJsonRequest(httpServletRequest)) {
            ObjectMapper objectMapper = new ObjectMapper();
            @SuppressWarnings("unchecked") List<Object> variableObjects = (List<Object>) objectMapper.readValue(httpServletRequest.getInputStream(), List.class);
            for (Object restObject : variableObjects) {
                RestVariable restVariable = objectMapper.convertValue(restObject, RestVariable.class);
                inputVariables.add(restVariable);
            }
        } else if (Utils.isApplicationXmlRequest(httpServletRequest)) {
            JAXBContext jaxbContext;
            try {
                XMLInputFactory xif = XMLInputFactory.newInstance();
                xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
                xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
                XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(httpServletRequest.getInputStream()));
                jaxbContext = JAXBContext.newInstance(RestVariableCollection.class);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                RestVariableCollection restVariableCollection = (RestVariableCollection) jaxbUnmarshaller.unmarshal(xsr);
                if (restVariableCollection == null) {
                    throw new ActivitiIllegalArgumentException("xml request body could not be transformed to a " + "RestVariable Collection instance.");
                }
                List<RestVariable> restVariableList = restVariableCollection.getRestVariables();
                if (restVariableList.size() == 0) {
                    throw new ActivitiIllegalArgumentException("xml request body could not identify any rest " + "variables to be updated");
                }
                for (RestVariable restVariable : restVariableList) {
                    inputVariables.add(restVariable);
                }
            } catch (JAXBException | IOException e) {
                throw new ActivitiIllegalArgumentException("xml request body could not be transformed to a " + "RestVariable instance.", e);
            }
        }
    } catch (Exception e) {
        throw new ActivitiIllegalArgumentException("Failed to serialize to a RestVariable instance", e);
    }
    if (inputVariables.size() == 0) {
        throw new ActivitiIllegalArgumentException("Request didn't contain a list of variables to create.");
    }
    RestVariable.RestVariableScope sharedScope = null;
    RestVariable.RestVariableScope varScope;
    Map<String, Object> variablesToSet = new HashMap<>();
    for (RestVariable var : inputVariables) {
        // Validate if scopes match
        varScope = var.getVariableScope();
        if (var.getName() == null) {
            throw new ActivitiIllegalArgumentException("Variable name is required");
        }
        if (varScope == null) {
            varScope = RestVariable.RestVariableScope.LOCAL;
        }
        if (sharedScope == null) {
            sharedScope = varScope;
        }
        if (varScope != sharedScope) {
            throw new ActivitiIllegalArgumentException("Only allowed to update multiple variables in the same scope.");
        }
        if (!override && hasVariableOnScope(execution, var.getName(), varScope)) {
            throw new BPMNConflictException("Variable '" + var.getName() + "' is already present on execution '" + execution.getId() + "'.");
        }
        RestResponseFactory restResponseFactory = new RestResponseFactory();
        Object actualVariableValue = restResponseFactory.getVariableValue(var);
        variablesToSet.put(var.getName(), actualVariableValue);
        resultVariables.add(restResponseFactory.createRestVariable(var.getName(), actualVariableValue, varScope, execution.getId(), variableType, false, uriInfo.getBaseUri().toString()));
    }
    if (!variablesToSet.isEmpty()) {
        RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
        if (sharedScope == RestVariable.RestVariableScope.LOCAL) {
            runtimeService.setVariablesLocal(execution.getId(), variablesToSet);
        } else {
            if (execution.getParentId() != null) {
                // Explicitly set on parent, setting non-local variables on execution itself will override local-variables if exists
                runtimeService.setVariables(execution.getParentId(), variablesToSet);
            } else {
                // Standalone task, no global variables possible
                throw new ActivitiIllegalArgumentException("Cannot set global variables on execution '" + execution.getId() + "', task is not part of process.");
            }
        }
    }
    RestVariableCollection restVariableCollection = new RestVariableCollection();
    restVariableCollection.setRestVariables(resultVariables);
    responseBuilder.entity(restVariableCollection);
    // }
    return responseBuilder.status(Response.Status.CREATED).build();
}
Also used : RestVariableCollection(org.wso2.carbon.bpmn.rest.model.runtime.RestVariableCollection) BPMNConflictException(org.wso2.carbon.bpmn.rest.common.exception.BPMNConflictException) XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) List(java.util.List) ArrayList(java.util.ArrayList) Unmarshaller(javax.xml.bind.Unmarshaller) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) RuntimeService(org.activiti.engine.RuntimeService) StreamSource(javax.xml.transform.stream.StreamSource) ActivitiException(org.activiti.engine.ActivitiException) ServletException(javax.servlet.ServletException) BPMNRestException(org.wso2.carbon.bpmn.rest.common.exception.BPMNRestException) BPMNContentNotSupportedException(org.wso2.carbon.bpmn.rest.common.exception.BPMNContentNotSupportedException) XMLStreamException(javax.xml.stream.XMLStreamException) BPMNConflictException(org.wso2.carbon.bpmn.rest.common.exception.BPMNConflictException) JAXBException(javax.xml.bind.JAXBException) NotFoundException(javax.ws.rs.NotFoundException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) IOException(java.io.IOException) RestApiBasicAuthenticationException(org.wso2.carbon.bpmn.rest.common.exception.RestApiBasicAuthenticationException) DataResponse(org.wso2.carbon.bpmn.rest.model.common.DataResponse) Response(javax.ws.rs.core.Response) ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 59 with Variable

use of org.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.

the class ProcessInstanceService method createOrUpdateExecutionVariable.

@PUT
@Path("/{processInstanceId}/variables")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createOrUpdateExecutionVariable(@PathParam("processInstanceId") String processInstanceId, @Context HttpServletRequest httpServletRequest) {
    Execution execution = getExecutionInstanceFromRequest(processInstanceId);
    Response result;
    try {
        result = createExecutionVariable(execution, true, RestResponseFactory.VARIABLE_PROCESS, httpServletRequest);
    } catch (IOException | ServletException e) {
        throw new BPMNRestException("Exception occured during creating execution variable", e);
    }
    return result;
}
Also used : DataResponse(org.wso2.carbon.bpmn.rest.model.common.DataResponse) Response(javax.ws.rs.core.Response) ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse) ServletException(javax.servlet.ServletException) Execution(org.activiti.engine.runtime.Execution) BPMNRestException(org.wso2.carbon.bpmn.rest.common.exception.BPMNRestException) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 60 with Variable

use of org.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.

the class ProcessInstanceService method setSimpleVariable.

protected RestVariable setSimpleVariable(RestVariable restVariable, Execution execution, boolean isNew) {
    if (restVariable.getName() == null) {
        throw new ActivitiIllegalArgumentException("Variable name is required");
    }
    // Figure out scope, revert to local is omitted
    RestVariable.RestVariableScope scope = restVariable.getVariableScope();
    if (scope == null) {
        scope = RestVariable.RestVariableScope.LOCAL;
    }
    Object actualVariableValue = new RestResponseFactory().getVariableValue(restVariable);
    setVariable(execution, restVariable.getName(), actualVariableValue, scope, isNew);
    return constructRestVariable(restVariable.getName(), actualVariableValue, scope, execution.getId(), false);
}
Also used : RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException)

Aggregations

RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)37 ArrayList (java.util.ArrayList)31 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)31 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)30 Test (org.testng.annotations.Test)28 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)26 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)26 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)26 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)23 BJSON (org.ballerinalang.model.values.BJSON)22 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)18 IOException (java.io.IOException)17 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)17 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)17 Variable (org.wso2.siddhi.query.api.expression.Variable)17 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)16 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)15 DataResponse (org.wso2.carbon.bpmn.rest.model.common.DataResponse)15 Response (javax.ws.rs.core.Response)14 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)13