Search in sources :

Example 6 with FilterSet

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

the class TaskBuilderTest method shouldReturnEmptyTaskObject.

@Test
public void shouldReturnEmptyTaskObject() 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()).clear().build();
    assertNotNull(task);
    assertTrue(task.getName().isEmpty());
    assertTrue(task.getDescription().isEmpty());
    assertEquals(false, task.isEnabled());
    assertNull(task.getTrigger());
    assertNotNull(task.getActions());
    assertTrue(task.getActions().isEmpty());
    assertNotNull(task.getTaskConfig());
    assertTrue(task.getTaskConfig().getSteps().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)

Example 7 with FilterSet

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

the class TaskInitializer method evalConfigSteps.

/**
 * Executes all config steps (loading data from data sources, checking filters) defined for this task.
 *
 * @param dataProviders  the map of data providers, not null or empty
 * @return  true if all steps were executed, false otherwise
 * @throws TaskHandlerException if there were error while handling task
 */
@Transactional
public boolean evalConfigSteps(Map<String, DataProvider> dataProviders) throws TaskHandlerException {
    LOGGER.info("Executing all config steps for task: {}", taskContext.getTask().getName());
    Iterator<TaskConfigStep> iterator = taskContext.getTask().getTaskConfig().getSteps().iterator();
    boolean result = true;
    TaskFilterExecutor taskFilterExecutor = new TaskFilterExecutor();
    while (result && iterator.hasNext()) {
        TaskConfigStep step = iterator.next();
        if (step instanceof DataSource) {
            DataSource ds = (DataSource) step;
            taskContext.addDataSourceObject(ds.getObjectId().toString(), getDataSourceObject(ds, dataProviders), ds.isFailIfDataNotFound());
            LOGGER.info("Task data source: {} for task: {} added", ds.getName(), taskContext.getTask().getName());
        } else if (step instanceof FilterSet && !isActionFilter((FilterSet) step)) {
            try {
                FilterSet filterSet = (FilterSet) step;
                result = taskFilterExecutor.checkFilters(filterSet.getFilters(), filterSet.getOperator(), taskContext);
            } catch (RuntimeException e) {
                throw new TaskHandlerException(FILTER, "task.error.filterError", e);
            }
        }
    }
    return result;
}
Also used : TaskFilterExecutor(org.motechproject.tasks.service.util.TaskFilterExecutor) TaskConfigStep(org.motechproject.tasks.domain.mds.task.TaskConfigStep) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with FilterSet

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

the class TaskTriggerHandler method handleTask.

private void handleTask(Task task, Map<String, Object> parameters) {
    long activityId = activityService.addTaskStarted(task, parameters);
    Map<String, Object> metadata = prepareTaskMetadata(task.getId(), activityId);
    TaskContext taskContext = new TaskContext(task, parameters, metadata, activityService);
    TaskInitializer initializer = new TaskInitializer(taskContext);
    List<FilterSet> filterSetList = new ArrayList<>(task.getTaskConfig().getFilters());
    boolean actionFilterResult = true;
    int executedAndFilteredActions = 0;
    int stepIndex = 0;
    int actualFilterIndex = initializer.getActionFilters();
    try {
        LOGGER.info("Executing all actions from task: {}", task.getName());
        if (initializer.evalConfigSteps(dataProviders)) {
            while (executedAndFilteredActions < task.getActions().size()) {
                if (isActionFilter(filterSetList, actualFilterIndex, stepIndex)) {
                    actionFilterResult = initializer.checkActionFilter(actualFilterIndex, filterSetList);
                    actualFilterIndex++;
                    stepIndex++;
                }
                if (actionFilterResult) {
                    executor.execute(task, task.getActions().get(executedAndFilteredActions), executedAndFilteredActions, taskContext, activityId);
                    executedAndFilteredActions++;
                } else {
                    activityService.addFilteredExecution(activityId);
                    executedAndFilteredActions++;
                }
                stepIndex++;
                actionFilterResult = true;
            }
        } else {
            activityService.addTaskFiltered(activityId);
            LOGGER.warn("Actions from task: {} weren't executed, because config steps didn't pass the evaluation", task.getName());
        }
    } catch (TaskHandlerException e) {
        postExecutionHandler.handleError(parameters, metadata, task, e, activityId);
    } catch (RuntimeException e) {
        postExecutionHandler.handleError(parameters, metadata, task, new TaskHandlerException(TRIGGER, "task.error.unrecognizedError", e), activityId);
    }
}
Also used : TaskContext(org.motechproject.tasks.service.util.TaskContext) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) ArrayList(java.util.ArrayList) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException)

Example 9 with FilterSet

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

the class TaskConfigTest method shouldRemoveOnlyFilterSets.

@Test
public void shouldRemoveOnlyFilterSets() throws Exception {
    TaskConfig config = new TaskConfig().add(new FilterSet(), new FilterSet(), new FilterSet()).add(new DataSource(), new DataSource(), new DataSource());
    assertNotNull(config.getSteps());
    assertFalse(config.getSteps().isEmpty());
    config.removeFilterSets();
    assertNotNull(config.getSteps());
    assertFalse(config.getSteps().isEmpty());
    assertTrue(config.getFilters().isEmpty());
    assertFalse(config.getDataSources().isEmpty());
}
Also used : FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Test(org.junit.Test)

Example 10 with FilterSet

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

the class TaskConfigTest method shouldAddStepAndSetNewOrderNumber.

@Test
public void shouldAddStepAndSetNewOrderNumber() throws Exception {
    List<TaskConfigStep> steps = new ArrayList<>();
    steps.add(new FilterSet());
    steps.add(new DataSource());
    steps.add(new DataSource());
    steps.add(new FilterSet());
    Random random = new Random();
    for (TaskConfigStep step : steps) {
        step.setOrder(random.nextInt());
    }
    TaskConfig config = new TaskConfig();
    for (TaskConfigStep step : steps) {
        config.add(step);
    }
    List<Integer> orders = new ArrayList<>(steps.size());
    for (TaskConfigStep step : config.getSteps()) {
        orders.add(step.getOrder());
    }
    assertThat(orders, hasItems(0, 1, 2, 3));
}
Also used : TaskConfigStep(org.motechproject.tasks.domain.mds.task.TaskConfigStep) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) Random(java.util.Random) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Test(org.junit.Test)

Aggregations

FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)16 Test (org.junit.Test)11 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)11 ArrayList (java.util.ArrayList)9 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)9 Filter (org.motechproject.tasks.domain.mds.task.Filter)7 Matchers.anyString (org.mockito.Matchers.anyString)5 Task (org.motechproject.tasks.domain.mds.task.Task)5 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)4 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)4 MotechEvent (org.motechproject.event.MotechEvent)3 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)3 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)3 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)3 TaskConfigStep (org.motechproject.tasks.domain.mds.task.TaskConfigStep)3 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)3 HashMap (java.util.HashMap)2 Random (java.util.Random)2 DataProvider (org.motechproject.commons.api.DataProvider)2 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)2