Search in sources :

Example 1 with ExportedProcessInstance

use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.

the class ProcessInstanceMarshaller method importProcessInstance.

@SuppressWarnings({ "rawtypes", "unchecked" })
public ProcessInstance importProcessInstance(ExportedProcessInstance<?> instance, Process process) {
    List<Map<String, String>> timers = instance.convertTimers();
    WorkflowProcessInstance wpi = importWorkflowProcessInstance((String) instance.getHeader(), (String) instance.getInstance(), timers, process);
    Model model = ((AbstractProcess) process).createModel();
    model.fromMap(wpi.getVariables());
    ProcessInstance processInstance = ((AbstractProcess) process).createInstance(wpi, model, 1);
    if (timers != null && !timers.isEmpty()) {
        String parentProcessInstanceId = wpi.getParentProcessInstanceId();
        if (parentProcessInstanceId != null && !parentProcessInstanceId.isEmpty()) {
            parentProcessInstanceId += ":";
        } else {
            parentProcessInstanceId = "";
        }
        JobsService jobService = ((WorkflowProcessInstanceImpl) wpi).getProcessRuntime().getJobsService();
        Collection<io.automatiko.engine.workflow.process.instance.NodeInstance> nodes = ((WorkflowProcessInstanceImpl) wpi).getNodeInstances(true);
        // keeps iterator for statebased node instance timers
        Map<String, Iterator<Timer>> nodeInstanceTimers = new LinkedHashMap<>();
        for (Map<String, String> timer : timers) {
            String nodeInstanceId = timer.get("nodeInstanceId");
            for (io.automatiko.engine.workflow.process.instance.NodeInstance ni : nodes) {
                if (ni.getId().equals(nodeInstanceId)) {
                    ExpirationTime expirationTime = null;
                    long timerNodeId = 0;
                    if (((NodeInstanceImpl) ni).getRetryJobId() != null && ((NodeInstanceImpl) ni).getRetryJobId().equals(timer.get("timerId"))) {
                        jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), ni.getNodeId(), "retry:" + ni.getId(), expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), ni.getProcessInstance().getRootProcessInstanceId(), ni.getProcessInstance().getProcessId(), ni.getProcessInstance().getProcess().getVersion(), ni.getProcessInstance().getRootProcessId()));
                        break;
                    } else if (ni instanceof TimerNodeInstance) {
                        TimerNodeInstance nodeInstance = (TimerNodeInstance) ni;
                        timerNodeId = nodeInstance.getTimerNode().getTimer().getId();
                        expirationTime = nodeInstance.createTimerInstance(nodeInstance.getTimerNode().getTimer());
                        expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
                    } else if (ni instanceof StateBasedNodeInstance) {
                        StateBasedNodeInstance nodeInstance = (StateBasedNodeInstance) ni;
                        if (nodeInstance.getTimerInstances().contains(timer.get("timerId"))) {
                            Iterator<Timer> it = nodeInstanceTimers.computeIfAbsent(nodeInstanceId, k -> nodeInstance.getEventBasedNode().getTimers().keySet().iterator());
                            expirationTime = nodeInstance.createTimerInstance(it.next());
                            expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
                        }
                    }
                    // lastly schedule timer on calculated expiration time
                    jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), timerNodeId, expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), wpi.getRootProcessInstanceId(), wpi.getProcessId(), wpi.getProcess().getVersion(), wpi.getRootProcessId()));
                    break;
                }
            }
        }
    }
    return processInstance;
}
Also used : WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ExpirationTime(io.automatiko.engine.api.jobs.ExpirationTime) LinkedHashMap(java.util.LinkedHashMap) JobsService(io.automatiko.engine.api.jobs.JobsService) Iterator(java.util.Iterator) NodeInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl) StateBasedNodeInstance(io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) Model(io.automatiko.engine.api.Model) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) StringExportedProcessInstance(io.automatiko.engine.workflow.StringExportedProcessInstance) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StateBasedNodeInstance(io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance) TimerNodeInstance(io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance) TimerNodeInstance(io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 2 with ExportedProcessInstance

use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.

the class CassandraProcessInstances method importInstance.

@Override
public ProcessInstance importInstance(ExportedProcessInstance instance, Process process) {
    ProcessInstance imported = marshaller.importProcessInstance(instance, process);
    if (exists(imported.id())) {
        throw new ProcessInstanceDuplicatedException(imported.id());
    }
    create(imported.id(), imported);
    return imported;
}
Also used : ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance)

Example 3 with ExportedProcessInstance

use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.

the class ExportProcessInstanceTest method testBasicUserTaskProcess.

@Test
public void testBasicUserTaskProcess() throws Exception {
    ProcessInstanceMarshaller marshaller = new ProcessInstanceMarshaller();
    Application app = generateCodeProcessesOnly("usertask/UserTasksProcess.bpmn2");
    assertThat(app).isNotNull();
    Process<? extends Model> p = app.processes().processById("UserTasksProcess");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    List<WorkItem> workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    assertEquals("FirstTask", workItems.get(0).getName());
    processInstance.completeWorkItem(workItems.get(0).getId(), null, securityPolicy);
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    // exprt process instance
    ExportedProcessInstance exported = marshaller.exportProcessInstance(processInstance);
    assertThat(exported).isNotNull();
    ProcessInstance<?> imported = marshaller.importProcessInstance(exported, p);
    workItems = imported.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    assertEquals("SecondTask", workItems.get(0).getName());
    imported.completeWorkItem(workItems.get(0).getId(), null, securityPolicy);
    assertThat(imported.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
}
Also used : ProcessInstanceMarshaller(io.automatiko.engine.workflow.marshalling.ProcessInstanceMarshaller) HashMap(java.util.HashMap) Model(io.automatiko.engine.api.Model) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance) Application(io.automatiko.engine.api.Application) WorkItem(io.automatiko.engine.api.workflow.WorkItem) AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Test(org.junit.jupiter.api.Test)

Example 4 with ExportedProcessInstance

use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.

the class DatabaseProcessInstances method importInstance.

@Override
public ProcessInstance importInstance(ExportedProcessInstance instance, Process process) {
    ProcessInstance imported = marshaller.importProcessInstance(instance, process);
    if (exists(imported.id())) {
        throw new ProcessInstanceDuplicatedException(imported.id());
    }
    create(imported.id(), imported);
    return imported;
}
Also used : ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance)

Example 5 with ExportedProcessInstance

use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.

the class DynamoDBProcessInstances method importInstance.

@Override
public ProcessInstance importInstance(ExportedProcessInstance instance, Process process) {
    ProcessInstance imported = marshaller.importProcessInstance(instance, process);
    if (exists(imported.id())) {
        throw new ProcessInstanceDuplicatedException(imported.id());
    }
    create(imported.id(), imported);
    return imported;
}
Also used : ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance)

Aggregations

ExportedProcessInstance (io.automatiko.engine.api.workflow.ExportedProcessInstance)11 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)9 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)7 ProcessInstanceDuplicatedException (io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException)6 Model (io.automatiko.engine.api.Model)5 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)4 HashMap (java.util.HashMap)4 StringExportedProcessInstance (io.automatiko.engine.workflow.StringExportedProcessInstance)3 Application (io.automatiko.engine.api.Application)2 ExpirationTime (io.automatiko.engine.api.jobs.ExpirationTime)2 JobsService (io.automatiko.engine.api.jobs.JobsService)2 WorkItem (io.automatiko.engine.api.workflow.WorkItem)2 AbstractCodegenTest (io.automatiko.engine.codegen.AbstractCodegenTest)2 AbstractProcess (io.automatiko.engine.workflow.AbstractProcess)2 Timer (io.automatiko.engine.workflow.base.core.timer.Timer)2 NodeInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl)2 WorkflowProcessInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)2 StateBasedNodeInstance (io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance)2 TimerNodeInstance (io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance)2 MessageOrBuilder (com.google.protobuf.MessageOrBuilder)1