use of io.automatiko.engine.api.Application in project automatiko-engine by automatiko-io.
the class $Type$Resource method updateModel_$name$.
@APIResponses(value = { @APIResponse(responseCode = "500", description = "In case of processing errors", content = @Content(mediaType = "application/json")), @APIResponse(responseCode = "404", description = "In case of instance with given id was not found", content = @Content(mediaType = "application/json")), @APIResponse(responseCode = "200", description = "Successfully updated instance", content = @Content(mediaType = "application/json", schema = @Schema(implementation = $Type$Output.class))) })
@Operation(summary = "Updates data of $name$ instance with given id")
@POST()
@Path("$prefix$/$name$/{id_$name$}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateModel_$name$(@Context HttpHeaders httpHeaders, @PathParam("id") String id, @PathParam("id_$name$") String id_$name$, @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, @Parameter(description = "Indicates if instance metadata should be included", required = false) @QueryParam("metadata") @DefaultValue("false") final boolean metadata, $Type$ resource) {
String execMode = httpHeaders.getHeaderString("X-ATK-Mode");
if ("async".equalsIgnoreCase(execMode)) {
String callbackUrl = httpHeaders.getHeaderString("X-ATK-Callback");
Map<String, String> headers = httpHeaders.getRequestHeaders().entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> e.getValue().get(0)));
IdentityProvider identity = identitySupplier.buildIdentityProvider(user, groups);
IdentityProvider.set(null);
CompletableFuture.runAsync(() -> {
IdentityProvider.set(identity);
io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance<$Type$> pi = subprocess_$name$.instances().findById($parentprocessid$ + ":" + id_$name$).orElseThrow(() -> new ProcessInstanceNotFoundException(id));
tracing(pi);
pi.updateVariables(resource);
Object result = pi.abortCode() != null ? pi.abortData() : mapOutput(new $Type$Output(), pi.variables(), pi.businessKey(), metadata ? pi.metadata() : null);
io.automatiko.engine.workflow.http.HttpCallbacks.get().post(callbackUrl, result, httpAuth.produce(headers), pi.status());
return null;
});
});
ResponseBuilder builder = Response.accepted().entity(Collections.singletonMap("id", id));
return builder.build();
} else {
identitySupplier.buildIdentityProvider(user, groups);
return io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance<$Type$> pi = subprocess_$name$.instances().findById($parentprocessid$ + ":" + id_$name$).orElseThrow(() -> new ProcessInstanceNotFoundException(id));
tracing(pi);
pi.updateVariables(resource);
ResponseBuilder builder = Response.ok().entity(pi.abortCode() != null ? pi.abortData() : mapOutput(new $Type$Output(), pi.variables(), pi.businessKey(), metadata ? pi.metadata() : null));
return builder.build();
});
}
}
use of io.automatiko.engine.api.Application in project automatiko-engine by automatiko-io.
the class $Type$Resource method create_$name$.
@APIResponses(value = { @APIResponse(responseCode = "400", description = "In case request given does not meet expectations", content = @Content(mediaType = "application/json")), @APIResponse(responseCode = "500", description = "In case of processing errors", content = @Content(mediaType = "application/json")), @APIResponse(responseCode = "409", description = "In case an instance already exists with given business key", content = @Content(mediaType = "application/json")), @APIResponse(responseCode = "403", description = "In case an instance cannot be created due to access policy by the caller", content = @Content(mediaType = "application/json")), @APIResponse(responseCode = "200", description = "Successfully created instance", content = @Content(mediaType = "application/json", schema = @Schema(implementation = $Type$Output.class))), @APIResponse(responseCode = "202", description = "Successfully accepted request to create instance (applies only to async execution mode)", content = @Content(mediaType = "application/json", schema = @Schema(implementation = $Type$Output.class))) })
@Operation(summary = "Creates new instance of $name$")
@POST()
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response create_$name$(@Context HttpHeaders httpHeaders, @QueryParam("businessKey") @Parameter(description = "Alternative id to be assigned to the instance", required = false) String businessKey, @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, @Parameter(description = "Indicates if instance metadata should be included", required = false) @QueryParam("metadata") @DefaultValue("false") final boolean metadata, @Parameter(description = "The input model for $name$ instance") $Type$Input resource) {
if (resource == null) {
resource = new $Type$Input();
}
final $Type$Input value = resource;
String execMode = httpHeaders.getHeaderString("X-ATK-Mode");
if ("async".equalsIgnoreCase(execMode)) {
String callbackUrl = httpHeaders.getHeaderString("X-ATK-Callback");
String startFromNode = httpHeaders.getHeaderString("X-ATK-StartFromNode");
ProcessInstance<$Type$> pi = process.createInstance(businessKey, mapInput(value, new $Type$()));
((AbstractProcessInstance<$Type$>) pi).unlock(true);
$Type$Output output = mapOutput(new $Type$Output(), pi.variables(), businessKey, pi.metadata());
Map<String, String> headers = httpHeaders.getRequestHeaders().entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> e.getValue().get(0)));
IdentityProvider identity = identitySupplier.buildIdentityProvider(user, groups);
IdentityProvider.set(null);
CompletableFuture.runAsync(() -> {
IdentityProvider.set(identity);
io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
if (startFromNode != null) {
pi.startFrom(startFromNode);
} else {
pi.start();
}
tracing(pi);
Object result = pi.abortCode() != null ? pi.abortData() : getModel(pi, metadata);
io.automatiko.engine.workflow.http.HttpCallbacks.get().post(callbackUrl, result, httpAuth.produce(headers), pi.status());
return null;
});
});
ResponseBuilder builder = Response.accepted().entity(output);
return builder.build();
} else {
identitySupplier.buildIdentityProvider(user, groups);
return io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance<$Type$> pi = process.createInstance(businessKey, mapInput(value, new $Type$()));
String startFromNode = httpHeaders.getHeaderString("X-ATK-StartFromNode");
if (startFromNode != null) {
pi.startFrom(startFromNode);
} else {
pi.start();
}
tracing(pi);
ResponseBuilder builder = Response.ok().entity(getModel(pi, metadata));
return builder.build();
});
}
}
use of io.automatiko.engine.api.Application in project automatiko-engine by automatiko-io.
the class MilestoneTest method testConditionalMilestone.
@Test
void testConditionalMilestone() throws Exception {
Application app = generateCodeProcessesOnly("cases/milestones/ConditionalMilestone.bpmn");
assertThat(app).isNotNull();
Process<? extends Model> p = app.processes().processById("TestCase.ConditionalMilestone_1_0");
Model model = p.createModel();
Map<String, Object> params = new HashMap<>();
params.put("favouriteColour", "orange");
model.fromMap(params);
ProcessInstance<?> processInstance = p.createInstance(model);
assertState(processInstance, ProcessInstance.STATE_PENDING);
Collection<Milestone> expected = new ArrayList<>();
expected.add(Milestone.builder().withName("Milestone").withStatus(AVAILABLE).build());
assertMilestones(expected, processInstance.milestones());
processInstance.start();
assertState(processInstance, ProcessInstance.STATE_ACTIVE);
expected = expected.stream().map(m -> Milestone.builder().withId(m.getId()).withName(m.getName()).withStatus(AVAILABLE).build()).collect(Collectors.toList());
assertMilestones(expected, processInstance.milestones());
List<WorkItem> workItems = processInstance.workItems();
params.put("favouriteColour", "blue");
processInstance.completeWorkItem(workItems.get(0).getId(), params);
expected = expected.stream().map(m -> Milestone.builder().withId(m.getId()).withName(m.getName()).withStatus(COMPLETED).build()).collect(Collectors.toList());
assertMilestones(expected, processInstance.milestones());
}
use of io.automatiko.engine.api.Application in project automatiko-engine by automatiko-io.
the class MilestoneTest method testSimpleMilestone.
@Test
void testSimpleMilestone() throws Exception {
Application app = generateCodeProcessesOnly("cases/milestones/SimpleMilestone.bpmn");
assertThat(app).isNotNull();
Process<? extends Model> p = app.processes().processById("TestCase.SimpleMilestone_1_0");
ProcessInstance<?> processInstance = p.createInstance(p.createModel());
assertState(processInstance, ProcessInstance.STATE_PENDING);
Collection<Milestone> expected = new ArrayList<>();
expected.add(Milestone.builder().withName("AutoStartMilestone").withStatus(AVAILABLE).build());
expected.add(Milestone.builder().withName("SimpleMilestone").withStatus(AVAILABLE).build());
assertMilestones(expected, processInstance.milestones());
processInstance.start();
assertState(processInstance, ProcessInstance.STATE_COMPLETED);
expected = expected.stream().map(m -> Milestone.builder().withId(m.getId()).withName(m.getName()).withStatus(COMPLETED).build()).collect(Collectors.toList());
assertMilestones(expected, processInstance.milestones());
ExecutableProcessInstance legacyProcessInstance = (ExecutableProcessInstance) ((AbstractProcessInstance<?>) processInstance).processInstance();
assertThat(legacyProcessInstance.getNodeInstances()).isEmpty();
Optional<String> milestoneId = Stream.of(legacyProcessInstance.getNodeContainer().getNodes()).filter(node -> node.getName().equals("SimpleMilestone")).map(n -> (String) n.getMetaData().get(Metadata.UNIQUE_ID)).findFirst();
assertTrue(milestoneId.isPresent());
assertThat(legacyProcessInstance.getCompletedNodeIds()).contains(milestoneId.get());
}
use of io.automatiko.engine.api.Application in project automatiko-engine by automatiko-io.
the class PublishEventTest method testExclusiveGatewayStartToEnd.
@Test
public void testExclusiveGatewayStartToEnd() throws Exception {
Application app = generateCodeProcessesOnly("gateway/ExclusiveSplit.bpmn2");
assertThat(app).isNotNull();
TestEventPublisher publisher = new TestEventPublisher();
app.unitOfWorkManager().eventManager().setService("http://myhost");
app.unitOfWorkManager().eventManager().addPublisher(publisher);
UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
Process<? extends Model> p = app.processes().processById("ExclusiveSplit");
Map<String, Object> params = new HashMap<>();
params.put("x", "First");
params.put("y", "None");
Model m = p.createModel();
m.fromMap(params);
ProcessInstance<?> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = (Model) processInstance.variables();
assertThat(result.toMap()).hasSize(2).containsKeys("x", "y");
uow.end();
List<DataEvent<?>> events = publisher.extract();
assertThat(events).isNotNull().hasSize(1);
DataEvent<?> event = events.get(0);
assertThat(event).isInstanceOf(ProcessInstanceDataEvent.class);
ProcessInstanceEventBody body = assertProcessInstanceEvent(events.get(0), "ExclusiveSplit", "Basic process with gateway decision", 2);
assertThat(body.getNodeInstances()).hasSize(6).extractingResultOf("getNodeType").contains("StartNode", "ActionNode", "Split", "Join", "EndNode", "WorkItemNode");
assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").allMatch(v -> v != null);
}
Aggregations