Search in sources :

Example 1 with TaskConfigStep

use of org.motechproject.tasks.domain.mds.task.TaskConfigStep 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 2 with TaskConfigStep

use of org.motechproject.tasks.domain.mds.task.TaskConfigStep 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)

Example 3 with TaskConfigStep

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

the class TaskConfigTest method shouldAddAllStepsAndSetNewOrderNumber.

@Test
public void shouldAddAllStepsAndSetNewOrderNumber() 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().addAll(null).addAll(new TreeSet<TaskConfigStep>()).addAll(new TreeSet<>(steps));
    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) TreeSet(java.util.TreeSet) 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

DataSource (org.motechproject.tasks.domain.mds.task.DataSource)3 FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)3 TaskConfigStep (org.motechproject.tasks.domain.mds.task.TaskConfigStep)3 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 Test (org.junit.Test)2 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)2 TreeSet (java.util.TreeSet)1 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)1 TaskFilterExecutor (org.motechproject.tasks.service.util.TaskFilterExecutor)1 Transactional (org.springframework.transaction.annotation.Transactional)1