use of io.automatiko.engine.api.runtime.process.WorkItemHandler in project automatiko-engine by automatiko-io.
the class EventStateWorkflowsTest method testStartWithEventStateWorkflow.
@Test
public void testStartWithEventStateWorkflow() throws Exception {
ProcessConfig processConfig = ServerlessProcess.processConfig();
((DefaultWorkItemHandlerConfig) processConfig.workItemHandlers()).register("Service Task", new WorkItemHandler() {
@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
System.out.println(workItem.getParameters());
ObjectMapper mapper = new ObjectMapper();
ObjectNode data = mapper.createObjectNode();
data.put("greeting", "Hello " + ((TextNode) workItem.getParameter("name")).asText());
manager.completeWorkItem(workItem.getId(), Collections.singletonMap(JsonVariableScope.WORKFLOWDATA_KEY, data));
}
@Override
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
}
});
ServerlessProcess process = ServerlessProcess.from(processConfig, new ClassPathResource("event-state/event-state-greeting.json")).get(0);
assertThat(process).isNotNull();
JsonNode data = new ObjectMapper().readTree("{\n" + " \"data\": {\"greet\" : { \"name\" : \"john\"}}\n" + "}");
ServerlessProcessInstance pi = (ServerlessProcessInstance) process.createInstance();
pi.start("Message-GreetingEvent", null, data);
assertThat(pi.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
assertThat(pi.variables().toMap()).hasSize(2).containsKey("greeting").extracting("greeting").isEqualTo(new TextNode("Hello john"));
}
use of io.automatiko.engine.api.runtime.process.WorkItemHandler in project automatiko-engine by automatiko-io.
the class SwitchStateWorkflowsTest method testSwitchWithEventConditions.
@Test
public void testSwitchWithEventConditions() throws Exception {
ProcessConfig processConfig = ServerlessProcess.processConfig();
((DefaultWorkItemHandlerConfig) processConfig.workItemHandlers()).register("Service Task", new WorkItemHandler() {
@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
ObjectMapper mapper = new ObjectMapper();
ObjectNode data = mapper.createObjectNode();
if ("emailStart".equals(workItem.getParameter("Operation"))) {
data.put("status", "accepted");
manager.completeWorkItem(workItem.getId(), Collections.singletonMap(JsonVariableScope.WORKFLOWDATA_KEY, data));
} else if ("emailRejection".equals(workItem.getParameter("Operation"))) {
data.put("status", "rejected");
manager.completeWorkItem(workItem.getId(), Collections.singletonMap(JsonVariableScope.WORKFLOWDATA_KEY, data));
}
}
@Override
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
}
});
ServerlessProcess process = ServerlessProcess.from(processConfig, new ClassPathResource("switch-state/event-condition.json")).get(0);
assertThat(process).isNotNull();
JsonNode data = new ObjectMapper().readTree("{\"applicant\" : {\"name\":\"John\", \"age\":20}}");
ServerlessProcessInstance pi = (ServerlessProcessInstance) process.createInstance(ServerlessModel.from(data));
pi.start();
assertThat(pi.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
pi.send(Sig.of("Message-visaApprovedEvent", data));
assertThat(pi.variables().toMap()).hasSize(2).containsKey("status").extracting("status").isEqualTo(new TextNode("accepted"));
assertThat(pi.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
data = new ObjectMapper().readTree("{\"applicant\" : {\"name\":\"Mary\", \"age\":10}}");
pi = (ServerlessProcessInstance) process.createInstance(ServerlessModel.from(data));
pi.start();
assertThat(pi.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
pi.send(Sig.of("Message-visaRejectedEvent", data));
assertThat(pi.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
assertThat(pi.variables().toMap()).hasSize(2).containsKey("status").extracting("status").isEqualTo(new TextNode("rejected"));
}
use of io.automatiko.engine.api.runtime.process.WorkItemHandler in project automatiko-engine by automatiko-io.
the class LightWorkItemManager method internalAbortWorkItem.
public void internalAbortWorkItem(String id) {
WorkItemImpl workItem = (WorkItemImpl) workItems.get(id);
// work item may have been aborted
if (workItem != null) {
workItem.setCompleteDate(new Date());
WorkItemHandler handler = this.workItemHandlers.get(workItem.getName());
if (handler != null) {
ProcessInstance processInstance = workItem.getProcessInstance();
Transition<?> transition = new TransitionToAbort(Collections.emptyList());
eventSupport.fireBeforeWorkItemTransition(processInstance, workItem, transition, null);
handler.abortWorkItem(workItem, this);
workItem.setPhaseId(ID);
workItem.setPhaseStatus(STATUS);
eventSupport.fireAfterWorkItemTransition(processInstance, workItem, transition, null);
} else {
workItems.remove(workItem.getId());
throw new WorkItemHandlerNotFoundException(workItem.getName());
}
workItems.remove(workItem.getId());
}
}
use of io.automatiko.engine.api.runtime.process.WorkItemHandler in project automatiko-engine by automatiko-io.
the class LightWorkItemManager method transitionWorkItem.
@SuppressWarnings("unchecked")
@Override
public void transitionWorkItem(String id, Transition<?> transition) {
WorkItem workItem = workItems.get(id);
// work item may have been aborted
if (workItem != null) {
WorkItemHandler handler = this.workItemHandlers.get(workItem.getName());
if (handler != null) {
ProcessInstance processInstance = workItem.getProcessInstance();
eventSupport.fireBeforeWorkItemTransition(processInstance, workItem, transition, null);
try {
handler.transitionToPhase(workItem, this, transition);
} catch (UnsupportedOperationException e) {
((WorkItemImpl) workItem).setResults((Map<String, Object>) transition.data());
workItem.setPhaseId(Complete.ID);
workItem.setPhaseStatus(Complete.STATUS);
completePhase.apply(workItem, transition);
internalCompleteWorkItem(workItem);
}
eventSupport.fireAfterWorkItemTransition(processInstance, workItem, transition, null);
} else {
throw new WorkItemHandlerNotFoundException(workItem.getName());
}
} else {
throw new WorkItemNotFoundException("Work Item (" + id + ") does not exist", id);
}
}
use of io.automatiko.engine.api.runtime.process.WorkItemHandler in project automatiko-engine by automatiko-io.
the class DefaultWorkItemManager method internalExecuteWorkItem.
public void internalExecuteWorkItem(WorkItem workItem) {
((WorkItemImpl) workItem).setId(UUID.randomUUID().toString());
internalAddWorkItem(workItem);
WorkItemHandler handler = this.workItemHandlers.get(workItem.getName());
if (handler != null) {
handler.executeWorkItem(workItem, this);
} else
throw new WorkItemHandlerNotFoundException(workItem.getName());
}
Aggregations