use of io.automatiko.engine.workflow.AbstractProcess in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method unmarshallReadOnlyProcessInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
public ProcessInstance unmarshallReadOnlyProcessInstance(byte[] data, Process process) {
WorkflowProcessInstance wpi = unmarshallWorkflowProcessInstance(data, process);
Model model = ((AbstractProcess) process).createModel();
model.fromMap(wpi.getVariables());
return ((AbstractProcess) process).createReadOnlyInstance(wpi, model);
}
use of io.automatiko.engine.workflow.AbstractProcess in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method importWorkflowProcessInstance.
public WorkflowProcessInstance importWorkflowProcessInstance(String header, String data, List<Map<String, String>> timers, Process<?> process) {
Map<String, io.automatiko.engine.api.definition.process.Process> processes = new HashMap<String, io.automatiko.engine.api.definition.process.Process>();
io.automatiko.engine.api.definition.process.Process p = ((AbstractProcess<?>) process).process();
// this can include version number in the id
processes.put(process.id(), p);
// this is raw process id as defined in bpmn or so
processes.put(p.getId(), p);
try (ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0])) {
Map<String, Object> localEnv = new HashMap<String, Object>(env);
localEnv.put("_import_", true);
localEnv.put("_services_", ((AbstractProcess<?>) process).services());
AutomatikoMessages.ProcessInstance.Builder builder = AutomatikoMessages.ProcessInstance.newBuilder();
JsonFormat.parser().merge(data, builder);
AutomatikoMessages.Header.Builder headerBuilder = AutomatikoMessages.Header.newBuilder();
JsonFormat.parser().merge(header, headerBuilder);
MarshallerReaderContext context = new MarshallerReaderContext(bais, null, processes, localEnv) {
@Override
protected void readStreamHeader() throws IOException, StreamCorruptedException {
}
};
context.parameterObject = builder.build();
PersisterHelper.loadStrategiesIndex(context, headerBuilder.build());
io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(builder.getProcessType());
WorkflowProcessInstance pi = (WorkflowProcessInstance) marshaller.readProcessInstance(context);
context.close();
return pi;
} catch (Exception e) {
throw new RuntimeException("Error while unmarshalling process instance", e);
}
}
use of io.automatiko.engine.workflow.AbstractProcess 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;
}
use of io.automatiko.engine.workflow.AbstractProcess in project automatiko-engine by automatiko-io.
the class ProcessInstanceManagementResource method getInstance.
@APIResponses(value = { @APIResponse(responseCode = "404", description = "In case of instance with given id was not found", content = @Content(mediaType = "application/json")), @APIResponse(responseCode = "200", description = "Process instance details", content = @Content(mediaType = "application/json")) })
@Operation(summary = "Returns process instance details for given instance id")
@SuppressWarnings("unchecked")
@GET
@Path("/{processId}/instances/{instanceId}")
@Produces(MediaType.APPLICATION_JSON)
public ProcessInstanceDetailsDTO getInstance(@Context UriInfo uriInfo, @Parameter(description = "Unique identifier of the process", required = true) @PathParam("processId") String processId, @Parameter(description = "Unique identifier of the instance", required = true) @PathParam("instanceId") String instanceId, @Parameter(description = "Status of the process instance", required = false, schema = @Schema(enumeration = { "active", "completed", "aborted", "error" })) @QueryParam("status") @DefaultValue("active") final String status, @Parameter(description = "User identifier as alternative autroization info", required = false, hidden = true) @QueryParam("user") final String user, @Parameter(description = "Groups as alternative autroization info", required = false, hidden = true) @QueryParam("group") final List<String> groups) {
try {
identitySupplier.buildIdentityProvider(user, groups);
return UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
Process<?> process = processData.get(processId);
Optional<ProcessInstance<?>> instance = (Optional<ProcessInstance<?>>) process.instances().findById(instanceId, mapStatus(status), ProcessInstanceReadMode.READ_ONLY);
if (instance.isEmpty()) {
throw new ProcessInstanceNotFoundException(instanceId);
}
ProcessInstance<?> pi = instance.get();
ProcessInstanceDetailsDTO details = new ProcessInstanceDetailsDTO();
String id = pi.id();
if (pi.parentProcessInstanceId() != null) {
id = pi.parentProcessInstanceId() + ":" + id;
}
details.setId(id);
details.setProcessId(processId);
details.setBusinessKey(pi.businessKey() == null ? "" : pi.businessKey());
details.setDescription(pi.description());
details.setState(pi.status());
details.setFailed(pi.errors().isPresent());
if (pi.errors().isPresent()) {
details.setErrors(pi.errors().get().errors().stream().map(e -> new ErrorInfoDTO(e.failedNodeId(), e.errorId(), e.errorMessage(), e.errorDetails())).collect(Collectors.toList()));
}
details.setImage(uriInfo.getBaseUri().toString() + "management/processes/" + processId + "/instances/" + instanceId + "/image?status=" + reverseMapStatus(pi.status()));
details.setTags(pi.tags().values());
details.setVariables(pi.variables());
details.setSubprocesses(pi.subprocesses().stream().map(spi -> new ProcessInstanceDTO(spi.id(), spi.businessKey(), spi.description(), spi.tags().values(), spi.errors().isPresent(), spi.process().id(), spi.status())).collect(Collectors.toList()));
VariableScope variableScope = (VariableScope) ((ContextContainer) ((AbstractProcess<?>) process).process()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
details.setVersionedVariables(variableScope.getVariables().stream().filter(v -> v.hasTag(Variable.VERSIONED_TAG)).map(v -> v.getName()).collect(Collectors.toList()));
return details;
});
} finally {
IdentityProvider.set(null);
}
}
use of io.automatiko.engine.workflow.AbstractProcess in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method unmarshallWorkflowProcessInstance.
public WorkflowProcessInstance unmarshallWorkflowProcessInstance(byte[] data, Process<?> process) {
Map<String, io.automatiko.engine.api.definition.process.Process> processes = new HashMap<String, io.automatiko.engine.api.definition.process.Process>();
io.automatiko.engine.api.definition.process.Process p = ((AbstractProcess<?>) process).process();
// this can include version number in the id
processes.put(process.id(), p);
// this is raw process id as defined in bpmn or so
processes.put(p.getId(), p);
try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
MarshallerReaderContext context = new MarshallerReaderContext(bais, null, processes, this.env);
ObjectInputStream stream = context.stream;
String processInstanceType = stream.readUTF();
io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processInstanceType);
WorkflowProcessInstance pi = (WorkflowProcessInstance) marshaller.readProcessInstance(context);
context.close();
return pi;
} catch (Exception e) {
throw new RuntimeException("Error while unmarshalling process instance", e);
}
}
Aggregations