Search in sources :

Example 1 with ProcessInstanceQuery

use of org.flowable.engine.runtime.ProcessInstanceQuery in project petals-se-flowable by petalslink.

the class Assert method assertProcessInstancePending.

/**
 * Assertion to check that a process instance is running. The process instance is not finished.
 *
 * @param processInstanceId
 *            The process instance identifier
 * @param processDefinitionKey
 *            The process definition key
 * @param runtimeService
 *            The Flowable's runtime service used to request the Flowable engine about process instances.
 */
public static void assertProcessInstancePending(final String processInstanceId, final String processDefinitionKey, final RuntimeService runtimeService) {
    final ProcessInstanceQuery processInstQuery = runtimeService.createProcessInstanceQuery();
    final ProcessInstance processInstance = processInstQuery.processInstanceId(processInstanceId).singleResult();
    assertNotNull(processInstance);
    assertEquals(processInstanceId, processInstance.getProcessInstanceId());
    assertEquals(processDefinitionKey, processInstance.getProcessDefinitionKey());
    assertFalse(processInstance.isEnded());
    assertFalse(processInstance.isSuspended());
}
Also used : ProcessInstanceQuery(org.flowable.engine.runtime.ProcessInstanceQuery) HistoricProcessInstanceQuery(org.flowable.engine.history.HistoricProcessInstanceQuery) ProcessInstance(org.flowable.engine.runtime.ProcessInstance) HistoricProcessInstance(org.flowable.engine.history.HistoricProcessInstance)

Example 2 with ProcessInstanceQuery

use of org.flowable.engine.runtime.ProcessInstanceQuery in project petals-se-flowable by petalslink.

the class GetProcessInstancesOperation method searchProcessInstances.

/**
 * Search process instances that are not ended (in state 'active' or 'suspended').
 */
private GetProcessInstancesResponse searchProcessInstances(final GetProcessInstances incomingObject, final ProcessInstanceState state) throws MessagingException {
    final ProcessInstanceQuery processInstanceQuery = this.runtimeService.createProcessInstanceQuery();
    if (state == ProcessInstanceState.ACTIVE) {
        processInstanceQuery.active();
    } else if (state == ProcessInstanceState.SUSPENDED) {
        processInstanceQuery.suspended();
    } else {
        // we search all process instances independently of their state: ACTIVE and SUSPENDED
        assert state == null;
    }
    final String processDefinitionId = incomingObject.getProcessDefinitionIdentifier();
    if (processDefinitionId != null && !processDefinitionId.isEmpty()) {
        processInstanceQuery.processDefinitionKey(processDefinitionId);
    }
    final String processInstanceId = incomingObject.getProcessInstanceIdentifier();
    if (processInstanceId != null && !processInstanceId.isEmpty()) {
        processInstanceQuery.processInstanceId(processInstanceId);
    }
    final Variables variables = incomingObject.getVariables();
    if (variables != null && !variables.getVariable().isEmpty()) {
        for (final Variable variable : variables.getVariable()) {
            processInstanceQuery.variableValueEquals(variable.getName(), Utils.parseVariableValue(variable));
        }
    }
    processInstanceQuery.includeProcessVariables();
    final GetProcessInstancesResponse response = new GetProcessInstancesResponse();
    final ProcessInstances responseProcessInstances = new ProcessInstances();
    response.setProcessInstances(responseProcessInstances);
    final List<ProcessInstance> processInstances = processInstanceQuery.list();
    for (final ProcessInstance processInstance : processInstances) {
        final org.ow2.petals.components.flowable.generic._1.ProcessInstance responseProcessInstance = new org.ow2.petals.components.flowable.generic._1.ProcessInstance();
        responseProcessInstances.getProcessInstance().add(responseProcessInstance);
        responseProcessInstance.setProcessInstanceIdentifier(processInstance.getProcessInstanceId());
        if (processInstance.isSuspended()) {
            responseProcessInstance.setState(ProcessInstanceState.SUSPENDED);
        } else if (processInstance.isEnded()) {
            responseProcessInstance.setState(ProcessInstanceState.FINISHED);
        } else {
            responseProcessInstance.setState(ProcessInstanceState.ACTIVE);
        }
        final ProcessDefinitionQuery processDefQuery = this.repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId());
        final ProcessDefinition processDefinition = processDefQuery.singleResult();
        responseProcessInstance.setProcessDefinitionIdentifier(processDefinition.getKey());
        responseProcessInstance.setVariables(Utils.buildVariables(processInstance.getProcessVariables(), this.log));
    }
    return response;
}
Also used : Variable(org.ow2.petals.components.flowable.generic._1.Variable) GetProcessInstancesResponse(org.ow2.petals.components.flowable.generic._1.GetProcessInstancesResponse) ProcessDefinitionQuery(org.flowable.engine.repository.ProcessDefinitionQuery) ProcessDefinition(org.flowable.engine.repository.ProcessDefinition) Variables(org.ow2.petals.components.flowable.generic._1.Variables) ProcessInstances(org.ow2.petals.components.flowable.generic._1.ProcessInstances) GetProcessInstances(org.ow2.petals.components.flowable.generic._1.GetProcessInstances) ProcessInstanceQuery(org.flowable.engine.runtime.ProcessInstanceQuery) HistoricProcessInstanceQuery(org.flowable.engine.history.HistoricProcessInstanceQuery) ProcessInstance(org.flowable.engine.runtime.ProcessInstance) HistoricProcessInstance(org.flowable.engine.history.HistoricProcessInstance)

Example 3 with ProcessInstanceQuery

use of org.flowable.engine.runtime.ProcessInstanceQuery in project petals-se-flowable by petalslink.

the class CallActivityStartedEventListener method createLogData.

@Override
protected AbstractFlowLogData createLogData(final FlowableEvent event) {
    if (event instanceof FlowableProcessStartedEventImpl) {
        final FlowableProcessStartedEventImpl eventImpl = (FlowableProcessStartedEventImpl) event;
        if (eventImpl.getNestedProcessDefinitionId() != null) {
            final String callActivityInstanceId = eventImpl.getProcessInstanceId();
            final String parentInstanceId = eventImpl.getNestedProcessInstanceId();
            final ExecutionEntity callActivityEntity = (ExecutionEntity) eventImpl.getEntity();
            this.log.fine(String.format("The call activity '%s' (instance id '%s') is started from instance '%s'.", callActivityEntity.getProcessDefinitionKey(), callActivityInstanceId, parentInstanceId));
            // To be able to retrieve variables of the parent process, the call activity must be executed
            // asynchronously. See note in this class documentation.
            final ProcessInstanceQuery processInstanceQuery = this.runtimeService.createProcessInstanceQuery().processInstanceId(parentInstanceId).includeProcessVariables();
            final ProcessInstance processInstance = processInstanceQuery.singleResult();
            final Map<String, Object> processVariables = processInstance.getProcessVariables();
            final String flowInstanceId = (String) processVariables.get(VAR_PETALS_FLOW_INSTANCE_ID);
            final String previousFlowStepId = (String) processVariables.get(VAR_PETALS_FLOW_STEP_ID);
            if (previousFlowStepId == null) {
                this.log.warning(String.format(MISSING_VARIABLE_PATTERN, VAR_PETALS_FLOW_STEP_ID, parentInstanceId));
            }
            final String flowStepId = this.simpleUUIDGenerator.getNewID();
            // final Map callACtivityVariables = eventImpl.getVariables();
            // callACtivityVariables.put(FlowableSEConstants.Flowable.VAR_PETALS_FLOW_INSTANCE_ID, flowInstanceId);
            // callACtivityVariables.put(FlowableSEConstants.Flowable.VAR_PETALS_FLOW_STEP_ID, flowStepId);
            this.runtimeService.setVariable(eventImpl.getExecutionId(), VAR_PETALS_FLOW_INSTANCE_ID, flowInstanceId);
            this.runtimeService.setVariable(eventImpl.getExecutionId(), VAR_PETALS_FLOW_STEP_ID, flowStepId);
            // Placeholders must be copied in the call activity scope
            this.runtimeService.setVariable(eventImpl.getExecutionId(), VAR_PETALS_PLACEHOLDERS, processVariables.get(VAR_PETALS_PLACEHOLDERS));
            final ExecutionEntity parentEntity = callActivityEntity.getParent();
            return new CallActivityFlowStepBeginLogData(flowInstanceId, flowStepId, previousFlowStepId, parentEntity.getProcessDefinitionKey(), parentInstanceId, callActivityEntity.getProcessDefinitionKey(), callActivityInstanceId);
        } else {
            // It's a nominal process instance start. It is processed by ProcessInstanceStartedEventListener
            return null;
        }
    } else {
        this.log.warning("Unexpected event implementation: " + event.getClass().getName());
        return null;
    }
}
Also used : ProcessInstanceQuery(org.flowable.engine.runtime.ProcessInstanceQuery) ExecutionEntity(org.flowable.engine.impl.persistence.entity.ExecutionEntity) CallActivityFlowStepBeginLogData(org.ow2.petals.flowable.monitoring.CallActivityFlowStepBeginLogData) FlowableProcessStartedEventImpl(org.flowable.engine.delegate.event.impl.FlowableProcessStartedEventImpl) ProcessInstance(org.flowable.engine.runtime.ProcessInstance)

Example 4 with ProcessInstanceQuery

use of org.flowable.engine.runtime.ProcessInstanceQuery in project petals-se-flowable by petalslink.

the class IntermediateCatchMessageEventEndedEventListener method createLogData.

@Override
protected AbstractFlowLogData createLogData(final FlowableEvent event) {
    if (event instanceof FlowableMessageEvent) {
        final FlowableMessageEvent messageEvent = (FlowableMessageEvent) event;
        final ProcessInstanceQuery processInstanceQuery = this.runtimeService.createProcessInstanceQuery().processInstanceId(messageEvent.getProcessInstanceId()).includeProcessVariables();
        final ProcessInstance processInstance = processInstanceQuery.singleResult();
        final Map<String, Object> processVariables = processInstance.getProcessVariables();
        final String flowInstanceId = (String) processVariables.get(VAR_PETALS_FLOW_INSTANCE_ID);
        final Map<String, Object> localVariable = this.runtimeService.getVariablesLocal(messageEvent.getExecutionId());
        final String flowStepId = (String) localVariable.get(VAR_PETALS_FLOW_STEP_ID);
        final String correlatedFlowInstanceId = (String) localVariable.get(VAR_PETALS_CORRELATED_FLOW_INSTANCE_ID);
        final String correlatedFlowStepId = (String) localVariable.get(VAR_PETALS_CORRELATED_FLOW_STEP_ID);
        if (flowInstanceId != null && flowStepId != null) {
            return new IntermediateCatchMessageEventFlowStepEndLogData(flowInstanceId, flowStepId, correlatedFlowInstanceId, correlatedFlowStepId);
        } else {
            this.log.warning(String.format("No MONIT end trace generated because a variable is missing: %s.", flowInstanceId == null ? VAR_PETALS_FLOW_INSTANCE_ID : VAR_PETALS_FLOW_STEP_ID));
            return null;
        }
    }
    return null;
}
Also used : ProcessInstanceQuery(org.flowable.engine.runtime.ProcessInstanceQuery) IntermediateCatchMessageEventFlowStepEndLogData(org.ow2.petals.flowable.monitoring.IntermediateCatchMessageEventFlowStepEndLogData) ProcessInstance(org.flowable.engine.runtime.ProcessInstance) FlowableMessageEvent(org.flowable.engine.delegate.event.FlowableMessageEvent)

Example 5 with ProcessInstanceQuery

use of org.flowable.engine.runtime.ProcessInstanceQuery in project petals-se-flowable by petalslink.

the class ServiceTaskStartedEventListener method processEvent.

@Override
protected void processEvent(final FlowableEvent event) {
    if (event instanceof FlowableActivityEvent) {
        final FlowableActivityEvent eventImpl = (FlowableActivityEvent) event;
        if ("serviceTask".equals(eventImpl.getActivityType())) {
            final ProcessInstanceQuery processInstanceQuery = this.runtimeService.createProcessInstanceQuery().processInstanceId(eventImpl.getProcessInstanceId()).includeProcessVariables();
            final ProcessInstance processInstance = processInstanceQuery.singleResult();
            final Map<String, Object> processVariables = processInstance.getProcessVariables();
            final String flowInstanceId = (String) processVariables.get(VAR_PETALS_FLOW_INSTANCE_ID);
            final String flowStepId = (String) processVariables.get(VAR_PETALS_FLOW_STEP_ID);
            final Optional<Boolean> extFlowTracingActivated;
            if (processVariables.containsKey(VAR_PETALS_EXT_FLOW_TRACING_ACTIVATION_STATE)) {
                extFlowTracingActivated = Optional.of(Boolean.valueOf((boolean) processVariables.get(VAR_PETALS_EXT_FLOW_TRACING_ACTIVATION_STATE)));
            } else {
                extFlowTracingActivated = Optional.empty();
            }
            PetalsConduit.flowAttributes.set(new FlowAttributes(flowInstanceId, flowStepId));
            PetalsConduit.extFlowTracingActivated.set(extFlowTracingActivated);
        }
    } else {
        this.log.warning("Unexpected event implementation: " + event.getClass().getName());
    }
}
Also used : ProcessInstanceQuery(org.flowable.engine.runtime.ProcessInstanceQuery) FlowAttributes(org.ow2.petals.commons.log.FlowAttributes) ProcessInstance(org.flowable.engine.runtime.ProcessInstance) FlowableActivityEvent(org.flowable.engine.delegate.event.FlowableActivityEvent)

Aggregations

ProcessInstanceQuery (org.flowable.engine.runtime.ProcessInstanceQuery)9 ProcessInstance (org.flowable.engine.runtime.ProcessInstance)8 HistoricProcessInstanceQuery (org.flowable.engine.history.HistoricProcessInstanceQuery)3 FlowableMessageEvent (org.flowable.engine.delegate.event.FlowableMessageEvent)2 HistoricProcessInstance (org.flowable.engine.history.HistoricProcessInstance)2 ProcessDefinition (org.flowable.engine.repository.ProcessDefinition)2 SimpleDateFormat (java.text.SimpleDateFormat)1 GregorianCalendar (java.util.GregorianCalendar)1 LogRecord (java.util.logging.LogRecord)1 QName (javax.xml.namespace.QName)1 Source (javax.xml.transform.Source)1 FlowableActivityEvent (org.flowable.engine.delegate.event.FlowableActivityEvent)1 FlowableProcessStartedEventImpl (org.flowable.engine.delegate.event.impl.FlowableProcessStartedEventImpl)1 ExecutionEntity (org.flowable.engine.impl.persistence.entity.ExecutionEntity)1 ProcessDefinitionQuery (org.flowable.engine.repository.ProcessDefinitionQuery)1 Test (org.junit.Test)1 PetalsException (org.ow2.petals.basisapi.exception.PetalsException)1 FlowAttributes (org.ow2.petals.commons.log.FlowAttributes)1 FlowLogData (org.ow2.petals.commons.log.FlowLogData)1 Message (org.ow2.petals.component.framework.junit.Message)1