use of io.automatiko.engine.api.workflow.WorkItem in project automatiko-engine by automatiko-io.
the class UserTaskManagementResource method get.
@APIResponses(value = { @APIResponse(responseCode = "404", description = "In case of instance with given id was not found", content = @Content(mediaType = "text/html")), @APIResponse(responseCode = "200", description = "List of available processes", content = @Content(mediaType = "text/html")) })
@Operation(summary = "Retrives user task form for given user task", operationId = "getUserTaskForm")
@SuppressWarnings("unchecked")
@GET
@Path("{processId}/{pInstanceId}/{taskId}")
@Produces(MediaType.TEXT_HTML)
public TemplateInstance get(@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("pInstanceId") String pInstanceId, @Parameter(description = "Unique identifier of the task", required = true) @PathParam("taskId") String taskId, @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) {
IdentityProvider identityProvider = identitySupplier.buildIdentityProvider(user, groups);
try {
Process<?> process = processData.get(processId);
if (process == null) {
return getTemplate("process-not-found", PROCESS_INSTANCE_NOT_FOUND).instance().data("instanceId", pInstanceId);
}
Optional<ProcessInstance<?>> instance = (Optional<ProcessInstance<?>>) process.instances().findById(pInstanceId, ProcessInstanceReadMode.READ_ONLY);
if (instance.isEmpty()) {
return getTemplate("process-instance-not-found", PROCESS_INSTANCE_NOT_FOUND).instance().data("instanceId", pInstanceId);
}
ProcessInstance<?> pi = instance.get();
WorkItem task = pi.workItem(taskId, SecurityPolicy.of(identityProvider));
Template template = getTemplate(process.id(), task);
if (template == null) {
template = engine.getTemplate(DEFAULT_TEMPLATE);
}
String link = task.getReferenceId() + "?redirect=true";
if (user != null && !user.isEmpty()) {
link += "&user=" + user;
}
if (groups != null && !groups.isEmpty()) {
for (String group : groups) {
link += "&group=" + group;
}
}
TemplateInstance templateInstance = template.data("task", task).data("url", new RawString(link));
templateInstance.data("inputs", process.taskInputs(task.getId(), task.getName(), task.getParameters()));
Map<String, Object> results = task.getResults();
task.getParameters().entrySet().stream().forEach(e -> results.putIfAbsent(e.getKey(), e.getValue()));
templateInstance.data("results", process.taskOutputs(task.getId(), task.getName(), results));
return templateInstance;
} catch (WorkItemNotFoundException e) {
return getTemplate("task-not-found", TASK_INSTANCE_NOT_FOUND).instance().data("taskId", taskId);
} finally {
IdentityProvider.set(null);
}
}
use of io.automatiko.engine.api.workflow.WorkItem in project automatiko-engine by automatiko-io.
the class UserTaskTest method testBasicUserTaskProcessVersioned.
@Test
public void testBasicUserTaskProcessVersioned() throws Exception {
Application app = generateCodeProcessesOnly("usertask/UserTasksProcessVersion.bpmn2", "usertask/UserTasksProcessVersion2.bpmn2");
assertThat(app).isNotNull();
final List<String> workItemTransitionEvents = new ArrayList<>();
app.config().process().processEventListeners().listeners().add(new DefaultProcessEventListener() {
@Override
public void beforeWorkItemTransition(ProcessWorkItemTransitionEvent event) {
workItemTransitionEvents.add("BEFORE:: " + event);
}
@Override
public void afterWorkItemTransition(ProcessWorkItemTransitionEvent event) {
workItemTransitionEvents.add("AFTER:: " + event);
}
});
// verify that both versions have corresponding process definitions
assertThat(app.processes().processById("UserTasksProcess_2")).isNotNull();
Process<? extends Model> p = app.processes().processById("UserTasksProcess_1");
assertThat(p).isNotNull();
// verify that both version have corresponding data model classes
Class<?> model1Class = Class.forName("org.kie.kogito.test.UserTasksProcess_1Model", true, testClassLoader());
assertNotNull(model1Class);
Class<?> model2Class = Class.forName("org.kie.kogito.test.UserTasksProcess_2Model", true, testClassLoader());
assertNotNull(model2Class);
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);
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);
assertThat(workItemTransitionEvents).hasSize(8);
}
use of io.automatiko.engine.api.workflow.WorkItem in project automatiko-engine by automatiko-io.
the class UserTaskTest method testBasicUserTaskProcessCancelAndRetriggerNode.
@Test
public void testBasicUserTaskProcessCancelAndRetriggerNode() 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<>();
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());
WorkItem wi = workItems.get(0);
assertEquals("FirstTask", wi.getName());
assertEquals(Active.ID, wi.getPhase());
assertEquals(Active.STATUS, wi.getPhaseStatus());
processInstance.transitionWorkItem(workItems.get(0).getId(), new HumanTaskTransition(Complete.ID, null, securityPolicy));
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
workItems = processInstance.workItems(securityPolicy);
assertEquals(1, workItems.size());
wi = workItems.get(0);
assertEquals("SecondTask", wi.getName());
assertEquals(Active.ID, wi.getPhase());
assertEquals(Active.STATUS, wi.getPhaseStatus());
String firstSecondTaskNodeInstanceId = wi.getNodeInstanceId();
processInstance.retriggerNodeInstance(wi.getNodeInstanceId());
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
workItems = processInstance.workItems(securityPolicy);
assertEquals(1, workItems.size());
wi = workItems.get(0);
assertEquals("SecondTask", wi.getName());
assertEquals(Active.ID, wi.getPhase());
assertEquals(Active.STATUS, wi.getPhaseStatus());
// since it was retriggered it must have different node instance id
assertNotEquals(firstSecondTaskNodeInstanceId, wi.getNodeInstanceId());
processInstance.transitionWorkItem(workItems.get(0).getId(), new HumanTaskTransition(Complete.ID, null, securityPolicy));
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
}
use of io.automatiko.engine.api.workflow.WorkItem in project automatiko-engine by automatiko-io.
the class UserTaskTest method testApprovalWithExcludedOwnerViaPhases.
@Test
public void testApprovalWithExcludedOwnerViaPhases() throws Exception {
Application app = generateCodeProcessesOnly("usertask/approval.bpmn2");
assertThat(app).isNotNull();
Process<? extends Model> p = app.processes().processById("approvals");
Model m = p.createModel();
Map<String, Object> parameters = new HashMap<>();
m.fromMap(parameters);
ProcessInstance<?> processInstance = p.createInstance(m);
processInstance.start();
assertEquals(io.automatiko.engine.api.runtime.process.ProcessInstance.STATE_ACTIVE, processInstance.status());
StaticIdentityProvider identity = new StaticIdentityProvider("admin", Collections.singletonList("managers"));
SecurityPolicy policy = SecurityPolicy.of(identity);
processInstance.workItems(policy);
List<WorkItem> workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());
HumanTaskTransition transition = new HumanTaskTransition(Complete.ID, null, identity);
processInstance.transitionWorkItem(workItems.get(0).getId(), transition);
// actual owner of the first task is excluded owner on the second task so won't
// find it
workItems = processInstance.workItems(policy);
assertEquals(0, workItems.size());
identity = new StaticIdentityProvider("john", Collections.singletonList("managers"));
policy = SecurityPolicy.of(identity);
workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());
transition = new HumanTaskTransition(Complete.ID, null, identity);
processInstance.transitionWorkItem(workItems.get(0).getId(), transition);
assertEquals(io.automatiko.engine.api.runtime.process.ProcessInstance.STATE_COMPLETED, processInstance.status());
}
use of io.automatiko.engine.api.workflow.WorkItem in project automatiko-engine by automatiko-io.
the class UserTaskTest method testBasicUserTaskProcessClaimAndCompleteWrongUser.
@Test
public void testBasicUserTaskProcessClaimAndCompleteWrongUser() 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<>();
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());
WorkItem wi = workItems.get(0);
assertEquals("FirstTask", wi.getName());
assertEquals(Active.ID, wi.getPhase());
assertEquals(Active.STATUS, wi.getPhaseStatus());
assertEquals(0, wi.getResults().size());
final String wiId = wi.getId();
IdentityProvider identity = new StaticIdentityProvider("kelly");
// if user that is not authorized to work on work item both listing and getting
// by id should apply it
List<WorkItem> securedWorkItems = processInstance.workItems(SecurityPolicy.of(identity));
assertEquals(0, securedWorkItems.size());
assertThrows(WorkItemNotFoundException.class, () -> processInstance.workItem(wiId, SecurityPolicy.of(identity)));
assertThrows(NotAuthorizedException.class, () -> processInstance.transitionWorkItem(wiId, new HumanTaskTransition(Claim.ID, null, identity)));
assertThrows(NotAuthorizedException.class, () -> processInstance.completeWorkItem(wiId, null, SecurityPolicy.of(identity)));
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
workItems = processInstance.workItems(securityPolicy);
assertEquals(1, workItems.size());
wi = workItems.get(0);
assertEquals("FirstTask", wi.getName());
assertEquals(Active.ID, wi.getPhase());
assertEquals(Active.STATUS, wi.getPhaseStatus());
assertEquals(0, wi.getResults().size());
IdentityProvider identityCorrect = new StaticIdentityProvider("john");
processInstance.transitionWorkItem(workItems.get(0).getId(), new HumanTaskTransition(Complete.ID, null, identityCorrect));
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
workItems = processInstance.workItems(securityPolicy);
assertEquals(1, workItems.size());
wi = workItems.get(0);
assertEquals("SecondTask", wi.getName());
assertEquals(Active.ID, wi.getPhase());
assertEquals(Active.STATUS, wi.getPhaseStatus());
assertEquals(0, wi.getResults().size());
processInstance.abort();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
}
Aggregations