Search in sources :

Example 36 with WorkflowProcessInstanceImpl

use of io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl in project automatiko-engine by automatiko-io.

the class LightProcessRuntime method createProcessInstance.

private io.automatiko.engine.workflow.base.instance.ProcessInstance createProcessInstance(Process process, CorrelationKey correlationKey, Map<String, Object> parameters) {
    io.automatiko.engine.workflow.base.instance.ProcessInstance pi = runtimeContext.createProcessInstance(process, correlationKey);
    pi.setProcessRuntime(this);
    runtimeContext.setupParameters(pi, parameters, variableInitializer);
    Map<String, Object> temp = new HashMap<>();
    if (parameters != null) {
        temp.putAll(parameters);
    }
    temp.putAll(pi.getVariables());
    String uuid;
    if (correlationKey != null) {
        uuid = UUID.nameUUIDFromBytes(correlationKey.toExternalForm().getBytes(StandardCharsets.UTF_8)).toString();
        ((WorkflowProcessInstanceImpl) pi).setCorrelationKey(correlationKey.toExternalForm());
    } else {
        VariableScope variableScope = (VariableScope) ((ContextContainer) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
        Optional<Object> businessKeyVar = variableScope.getVariables().stream().filter(var -> var.hasTag(Variable.BUSINESS_KEY_TAG)).map(v -> temp.get(v.getName())).filter(var -> var != null).findAny();
        if (businessKeyVar.isPresent()) {
            Object businessKey = businessKeyVar.get();
            uuid = UUID.nameUUIDFromBytes(businessKey.toString().getBytes(StandardCharsets.UTF_8)).toString();
            ((WorkflowProcessInstanceImpl) pi).setCorrelationKey(businessKey.toString());
        } else {
            uuid = UUID.randomUUID().toString();
        }
    }
    pi.setId(uuid);
    VariableScope variableScope = (VariableScope) ((ContextContainer) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) pi.getContextInstance(VariableScope.VARIABLE_SCOPE);
    // set input parameters
    if (parameters != null) {
        if (variableScope != null) {
            for (Map.Entry<String, Object> entry : parameters.entrySet()) {
                variableScope.validateVariable(process.getName(), entry.getKey(), entry.getValue());
                variableScopeInstance.setVariable(entry.getKey(), entry.getValue());
            }
        } else {
            throw new IllegalArgumentException("This process does not support parameters!");
        }
    }
    variableScopeInstance.enforceRequiredVariables();
    return pi;
}
Also used : SignalManager(io.automatiko.engine.api.workflow.signal.SignalManager) Trigger(io.automatiko.engine.workflow.process.core.node.Trigger) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) TimeUtils(io.automatiko.engine.workflow.base.core.timer.TimeUtils) ExactExpirationTime(io.automatiko.engine.api.jobs.ExactExpirationTime) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ExpirationTime(io.automatiko.engine.api.jobs.ExpirationTime) CorrelationKey(io.automatiko.engine.services.correlation.CorrelationKey) HashMap(java.util.HashMap) DefaultProcessInstanceManager(io.automatiko.engine.workflow.base.instance.impl.DefaultProcessInstanceManager) Node(io.automatiko.engine.api.definition.process.Node) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) DurationExpirationTime(io.automatiko.engine.api.jobs.DurationExpirationTime) ProcessJobDescription(io.automatiko.engine.api.jobs.ProcessJobDescription) JobsService(io.automatiko.engine.api.jobs.JobsService) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) ProcessEventSupport(io.automatiko.engine.workflow.base.core.event.ProcessEventSupport) Map(java.util.Map) EventListener(io.automatiko.engine.api.runtime.process.EventListener) Process(io.automatiko.engine.api.definition.process.Process) EventFilter(io.automatiko.engine.workflow.base.core.event.EventFilter) ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) UnitOfWorkManager(io.automatiko.engine.api.uow.UnitOfWorkManager) Collection(java.util.Collection) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) EventTransformer(io.automatiko.engine.workflow.base.core.event.EventTransformer) DateTimeUtils(io.automatiko.engine.workflow.base.core.timer.DateTimeUtils) UUID(java.util.UUID) InMemoryJobService(io.automatiko.engine.services.jobs.impl.InMemoryJobService) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) StandardCharsets(java.nio.charset.StandardCharsets) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) VariableInitializer(io.automatiko.engine.api.workflow.VariableInitializer) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) EventTypeFilter(io.automatiko.engine.workflow.base.core.event.EventTypeFilter) CronExpirationTime(io.automatiko.engine.workflow.base.core.timer.CronExpirationTime) Optional(java.util.Optional) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) WorkItemManager(io.automatiko.engine.api.runtime.process.WorkItemManager) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) EventTrigger(io.automatiko.engine.workflow.process.core.node.EventTrigger) Collections(java.util.Collections) HashMap(java.util.HashMap) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) Map(java.util.Map) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Aggregations

WorkflowProcessInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)36 ArrayList (java.util.ArrayList)10 EventSubProcessNodeInstance (io.automatiko.engine.workflow.process.instance.node.EventSubProcessNodeInstance)8 Node (io.automatiko.engine.api.definition.process.Node)7 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)7 HumanTaskNodeInstance (io.automatiko.engine.workflow.process.instance.node.HumanTaskNodeInstance)7 Process (io.automatiko.engine.api.definition.process.Process)6 NodeInstance (io.automatiko.engine.workflow.process.instance.NodeInstance)6 CompositeContextNodeInstance (io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Map (java.util.Map)6 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)5 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)5 NodeInstanceContainer (io.automatiko.engine.workflow.process.instance.NodeInstanceContainer)5 LambdaSubProcessNodeInstance (io.automatiko.engine.workflow.process.instance.node.LambdaSubProcessNodeInstance)5 WorkItemNodeInstance (io.automatiko.engine.workflow.process.instance.node.WorkItemNodeInstance)5 Date (java.util.Date)5 ExpirationTime (io.automatiko.engine.api.jobs.ExpirationTime)4 JobsService (io.automatiko.engine.api.jobs.JobsService)4