use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.
the class MongodbProcessInstances 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;
}
use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.
the class FileSystemProcessInstances 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;
}
use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.
the class ExportProcessInstanceTest method testBasicUserTaskProcessArchive.
@Test
public void testBasicUserTaskProcessArchive() throws Exception {
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<>();
parameters.put("name", "John");
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);
// archive active process instance
ArchivedProcessInstance archived = processInstance.archive(new ArchiveBuilder() {
@Override
public ArchivedVariable variable(String name, Object value) {
return new ArchivedVariable(name, value) {
@Override
public byte[] data() {
return getValue().toString().getBytes();
}
};
}
@Override
public ArchivedProcessInstance instance(String id, String processId, ExportedProcessInstance<?> exported) {
return new ArchivedProcessInstance(id, processId, exported);
}
});
assertThat(archived).isNotNull();
assertThat(archived.getExport()).isNotNull();
assertThat(archived.getVariables()).isNotNull();
assertThat(archived.getSubInstances()).isNotNull();
assertThat(archived.getVariables()).hasSize(1);
assertThat(archived.getSubInstances()).hasSize(0);
workItems = processInstance.workItems(securityPolicy);
assertEquals(1, workItems.size());
assertEquals("SecondTask", workItems.get(0).getName());
processInstance.completeWorkItem(workItems.get(0).getId(), null, securityPolicy);
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
// and now let's export completed instance
archived = processInstance.archive(new ArchiveBuilder() {
@Override
public ArchivedVariable variable(String name, Object value) {
return new ArchivedVariable(name, value) {
@Override
public byte[] data() {
return getValue().toString().getBytes();
}
};
}
@Override
public ArchivedProcessInstance instance(String id, String processId, ExportedProcessInstance<?> exported) {
return new ArchivedProcessInstance(id, processId, exported);
}
});
assertThat(archived).isNotNull();
assertThat(archived.getExport()).isNotNull();
assertThat(archived.getVariables()).isNotNull();
assertThat(archived.getSubInstances()).isNotNull();
assertThat(archived.getVariables()).hasSize(1);
assertThat(archived.getSubInstances()).hasSize(0);
}
use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method exportProcessInstance.
public ExportedProcessInstance<?> exportProcessInstance(ProcessInstance<?> processInstance) {
io.automatiko.engine.api.runtime.process.ProcessInstance pi = ((AbstractProcessInstance<?>) processInstance).internalGetProcessInstance();
if (pi == null) {
return null;
}
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Map<String, Object> localEnv = new HashMap<String, Object>(env);
localEnv.put("_export_", true);
ProcessMarshallerWriteContext context = new ProcessMarshallerWriteContext(baos, ((io.automatiko.engine.workflow.base.instance.ProcessInstance) pi).getProcessRuntime(), null, localEnv);
context.setProcessInstanceId(pi.getId());
context.setState(pi.getState());
String processType = pi.getProcess().getType();
context.stream.writeUTF(processType);
io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processType);
Object result = marshaller.writeProcessInstance(context, pi);
AutomatikoMessages.Header.Builder _header = AutomatikoMessages.Header.newBuilder();
_header.setVersion(AutomatikoMessages.Version.newBuilder().setVersionMajor(1).setVersionMinor(0).setVersionRevision(0).build());
PersisterHelper.writeStrategiesIndex(context, _header);
String header = JsonFormat.printer().print(_header);
context.close();
// collect all information about timers
Collection<io.automatiko.engine.workflow.process.instance.NodeInstance> nodes = ((WorkflowProcessInstanceImpl) pi).getNodeInstances(true);
StringBuilder timers = new StringBuilder("[");
for (io.automatiko.engine.workflow.process.instance.NodeInstance ni : nodes) {
String timerId = null;
if (ni instanceof TimerNodeInstance) {
timerId = ((TimerNodeInstance) ni).getTimerId();
} else if (ni instanceof StateBasedNodeInstance) {
if (((StateBasedNodeInstance) ni).getTimerInstances() != null) {
((StateBasedNodeInstance) ni).getTimerInstances().forEach(timer -> {
ZonedDateTime scheduledTime = context.processRuntime.getJobsService().getScheduledTime(timer);
timers.append("{\"timerId\":\"").append(timer).append("\",\"scheduledAt\":\"").append(scheduledTime.toString()).append("\",\"nodeInstanceId\":\"").append(ni.getId()).append("\"}");
});
}
}
if (timerId != null) {
ZonedDateTime scheduledTime = context.processRuntime.getJobsService().getScheduledTime(timerId);
timers.append("{\"timerId\":\"").append(timerId).append("\",\"scheduledAt\":\"").append(scheduledTime.toString()).append("\",\"nodeInstanceId\":\"").append(ni.getId()).append("\"}");
}
if (((NodeInstanceImpl) ni).getRetryJobId() != null && !((NodeInstanceImpl) ni).getRetryJobId().isEmpty()) {
ZonedDateTime scheduledTime = context.processRuntime.getJobsService().getScheduledTime(((NodeInstanceImpl) ni).getRetryJobId());
timers.append("{\"timerId\":\"").append(((NodeInstanceImpl) ni).getRetryJobId()).append("\",\"scheduledAt\":\"").append(scheduledTime.toString()).append("\",\"nodeInstanceId\":\"").append(ni.getId()).append("\"}");
}
}
timers.append("]");
return StringExportedProcessInstance.of(header, JsonFormat.printer().print((MessageOrBuilder) result), timers.toString(), null);
} catch (Exception e) {
throw new RuntimeException("Error while marshalling process instance", e);
}
}
use of io.automatiko.engine.api.workflow.ExportedProcessInstance in project automatiko-engine by automatiko-io.
the class MapProcessInstances 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;
}
Aggregations