Search in sources :

Example 6 with DataSource

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

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

the class TaskServiceImpl method validateDataSources.

private Set<TaskError> validateDataSources(Task task) {
    LOGGER.debug("Validating task data sources in task: {} with ID: {}", task.getName(), task.getId());
    Set<TaskError> errors = new HashSet<>();
    Map<Long, TaskDataProvider> availableDataProviders = new HashMap<>();
    for (DataSource dataSource : task.getTaskConfig().getDataSources()) {
        TaskDataProvider provider = providerService.getProvider(dataSource.getProviderName());
        errors.addAll(validateProvider(provider, dataSource, task, availableDataProviders));
        if (provider != null) {
            availableDataProviders.put(provider.getId(), provider);
        }
    }
    logResultOfValidation("task data sources", task.getName(), errors);
    return errors;
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) HashMap(java.util.HashMap) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet) DataSource(org.motechproject.tasks.domain.mds.task.DataSource)

Example 8 with DataSource

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

the class ProviderNameTaskMigrator method migrateDataSources.

private void migrateDataSources(Task task) {
    // replace data source references in other data source lookups
    for (DataSource dataSource : task.getTaskConfig().getDataSources()) {
        for (Lookup lookup : dataSource.getLookup()) {
            String oldVal = lookup.getValue();
            String newVal = replaceTaskValue(oldVal, EXPR_PATTERN, task);
            lookup.setValue(newVal);
        }
    }
}
Also used : Lookup(org.motechproject.tasks.domain.mds.task.Lookup) DataSource(org.motechproject.tasks.domain.mds.task.DataSource)

Example 9 with DataSource

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

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

DataSource (org.motechproject.tasks.domain.mds.task.DataSource)25 Test (org.junit.Test)19 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)17 Task (org.motechproject.tasks.domain.mds.task.Task)15 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)13 FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)9 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)9 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)8 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)7 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)6 HashSet (java.util.HashSet)5 Matchers.anyString (org.mockito.Matchers.anyString)5 Channel (org.motechproject.tasks.domain.mds.channel.Channel)5 ActionParameterBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder)5 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)5 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)5 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)5