Search in sources :

Example 76 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TaskTriggerHandler method handle.

@Override
@Transactional
public void handle(MotechEvent event) {
    LOGGER.info("Handling the motech event with subject: {}", event.getSubject());
    // Look for custom event parser
    Map<String, Object> eventParams = event.getParameters();
    eventParams.putAll(event.getMetadata());
    TasksEventParser parser = null;
    if (eventParams != null) {
        parser = taskService.findCustomParser((String) eventParams.get(TasksEventParser.CUSTOM_PARSER_EVENT_KEY));
    }
    // Use custom event parser, if it exists, to modify event
    String triggerSubject = parser == null ? event.getSubject() : parser.parseEventSubject(event.getSubject(), eventParams);
    Map<String, Object> parameters = parser == null ? eventParams : parser.parseEventParameters(event.getSubject(), eventParams);
    List<Task> tasks = taskService.findActiveTasksForTriggerSubject(triggerSubject);
    // Handle all tasks one by one
    for (Task task : tasks) {
        checkAndHandleTask(task, parameters);
    }
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TasksEventParser(org.motechproject.commons.api.TasksEventParser) Transactional(org.springframework.transaction.annotation.Transactional)

Example 77 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TasksEventCallbackService method failureCallback.

@Override
public boolean failureCallback(MotechEvent event, Throwable throwable) {
    LOGGER.debug("Received failure callback for event subject {}", event.getSubject());
    Map<String, Object> metadata = event.getMetadata();
    Long activityId = (Long) metadata.get(EventDataKeys.TASK_ACTIVITY_ID);
    Task task = taskService.getTask((Long) metadata.get(EventDataKeys.TASK_ID));
    postExecutionHandler.handleError(event.getParameters(), metadata, task, new TaskHandlerException(TaskFailureCause.ACTION, "task.error.eventHandlerFailed", throwable), activityId);
    return false;
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException)

Example 78 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TaskControllerTest method shouldImportTask.

@Test
public void shouldImportTask() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    StringWriter writer = new StringWriter();
    IOUtils.copy(this.getClass().getResourceAsStream("/new-task-version.json"), writer);
    ObjectNode node = (ObjectNode) mapper.readTree(writer.toString());
    node.remove(asList("validationErrors", "type", "_rev"));
    String json = node.toString();
    when(taskService.importTask(json.toString())).thenReturn(new Task(null, trigger, null));
    when(file.getInputStream()).thenReturn(new ByteArrayInputStream(json.getBytes()));
    controller.importTask(file);
    verify(taskService).importTask(json);
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) StringWriter(java.io.StringWriter) ObjectNode(org.codehaus.jackson.node.ObjectNode) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 79 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TaskControllerTest method shouldSaveTask.

@Test
public void shouldSaveTask() {
    String subject = "trigger1";
    TaskActionInformation action = new TaskActionInformation("send", "action1", "action", "0.15", "send", new HashMap<String, String>());
    TaskTriggerInformation trigger = new TaskTriggerInformation("trigger", "trigger1", "trigger", "0.16", subject, subject);
    Task expected = new Task("name", trigger, asList(action));
    when(eventListenerRegistryService.getListeners(subject)).thenReturn(new HashSet<EventListener>());
    controller.save(expected);
    verify(taskWebService).save(expected);
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) Task(org.motechproject.tasks.domain.mds.task.Task) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) EventListener(org.motechproject.event.listener.EventListener) Test(org.junit.Test)

Example 80 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TaskBuilderTest method shouldReturnBuiltTaskObject.

@Test
public void shouldReturnBuiltTaskObject() throws Exception {
    TaskBuilder builder = new TaskBuilder();
    Task task = builder.withName(TASK_NAME).withDescription(TASK_DESCRIPTION).isEnabled(isEnabled).withTrigger(new TaskTriggerInformation()).addAction(new TaskActionInformation()).withTaskConfig(new TaskConfig()).addFilterSet(new FilterSet()).addDataSource(new DataSource()).build();
    assertNotNull(task);
    assertEquals(TASK_NAME, task.getName());
    assertEquals(TASK_DESCRIPTION, task.getDescription());
    assertEquals(isEnabled, task.isEnabled());
    assertNotNull(task.getTrigger());
    assertNotNull(task.getActions());
    assertFalse(task.getActions().isEmpty());
    assertNotNull(task.getTaskConfig());
    assertFalse(task.getTaskConfig().getDataSources().isEmpty());
    assertFalse(task.getTaskConfig().getFilters().isEmpty());
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) Task(org.motechproject.tasks.domain.mds.task.Task) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Test(org.junit.Test)

Aggregations

Task (org.motechproject.tasks.domain.mds.task.Task)86 Test (org.junit.Test)65 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)35 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)26 HashMap (java.util.HashMap)21 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)19 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)19 ArrayList (java.util.ArrayList)18 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)17 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)16 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)15 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)14 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)13 Channel (org.motechproject.tasks.domain.mds.channel.Channel)12 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)11 KeyInformation (org.motechproject.tasks.domain.KeyInformation)10 TreeSet (java.util.TreeSet)9 MotechEvent (org.motechproject.event.MotechEvent)9 ObjectTest (org.motechproject.tasks.domain.ObjectTest)9 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)9