Search in sources :

Example 11 with KeyInformation

use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.

the class TaskActionExecutor method getValue.

private Object getValue(String row, KeyEvaluator keyEvaluator) throws TaskHandlerException {
    List<KeyInformation> keys = KeyInformation.parseAll(row);
    Object result;
    if (keys.isEmpty()) {
        result = row;
    } else {
        KeyInformation rowKeyInfo = keys.get(0);
        result = keyEvaluator.getValue(rowKeyInfo);
    }
    return result;
}
Also used : KeyInformation(org.motechproject.tasks.domain.KeyInformation)

Example 12 with KeyInformation

use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.

the class TaskFilterExecutor method checkFilters.

/**
 * Checks whether task with the given context matches the given filters.
 *
 * @param filters  the filters, null returns true
 * @param logicalOperator  the logical operator
 * @param taskContext  the task context, not null
 * @return true if the task matches the filters
 * @throws TaskHandlerException if there were problems while handling task
 */
public boolean checkFilters(List<Filter> filters, LogicalOperator logicalOperator, TaskContext taskContext) throws TaskHandlerException {
    LOGGER.debug("Checking if task: {} matches the filters", taskContext.getTask().getName());
    Map<String, Object> parameters = taskContext.getTriggerParameters();
    if (isEmpty(filters) || parameters == null) {
        return true;
    }
    boolean filterCheck = false;
    for (Filter filter : filters) {
        KeyInformation key = parse(filter.getKey());
        Object value;
        try {
            KeyEvaluator keyEvaluator = new KeyEvaluator(taskContext);
            value = keyEvaluator.getManipulatedValue(key);
        } catch (TaskHandlerException e) {
            if (TaskFailureCause.DATA_SOURCE.equals(e.getFailureCause())) {
                // data source lookups disable the task
                throw e;
            }
            // trigger parameter lookups don't disable the task
            value = null;
            LOGGER.error("Unable to retrieve value for filter", e);
        } catch (RuntimeException e) {
            value = null;
            LOGGER.error("Unable to retrieve value for filter", e);
        }
        filterCheck = value != null && checkValue(filter, value);
        if (!filter.isNegationOperator()) {
            filterCheck = !filterCheck;
        }
        LOGGER.debug("Result of checking filter: {} for task: {} is: {}", filter.getDisplayName(), taskContext.getTask().getName(), filterCheck);
        if (isFilterConditionFulfilled(filterCheck, logicalOperator)) {
            LOGGER.debug("Filters condition is fulfilled, because logicalOperator is: {} and filters checking has already: {} value", logicalOperator, filterCheck);
            break;
        }
    }
    LOGGER.info("Result of checking filters for task: {} is: {}", taskContext.getTask().getName(), filterCheck);
    return filterCheck;
}
Also used : TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) Filter(org.motechproject.tasks.domain.mds.task.Filter) KeyInformation(org.motechproject.tasks.domain.KeyInformation)

Example 13 with KeyInformation

use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.

the class TaskContextTest method shouldThrowExceptionWhenDataSourceIsNull.

@Test
public void shouldThrowExceptionWhenDataSourceIsNull() throws Exception {
    Task task = new TaskBuilder().addAction(new TaskActionInformation()).build();
    TaskContext taskContext = new TaskContext(task, null, null, activityService);
    taskContext.addDataSourceObject("1", null, true);
    KeyInformation key = parse("ad.1.Integer#1.id");
    expectedException.expect(TaskHandlerException.class);
    expectedException.expect(new TaskHandlerExceptionMatcher(TaskFailureCause.DATA_SOURCE, "task.error.objectOfTypeNotFound", key.getObjectType()));
    taskContext.getDataSourceObjectValue(key.getObjectId().toString(), key.getKey(), key.getObjectType());
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) KeyInformation(org.motechproject.tasks.domain.KeyInformation) Test(org.junit.Test)

Example 14 with KeyInformation

use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.

the class TaskContextTest method testGetDataSourceValue.

@Test
public void testGetDataSourceValue() throws Exception {
    Task task = new TaskBuilder().addAction(new TaskActionInformation()).build();
    TaskContext taskContext = new TaskContext(task, null, null, activityService);
    taskContext.addDataSourceObject("1", new TestDataSourceObject(), true);
    KeyInformation key = parse("ad.1.Integer#1.id");
    assertEquals(OBJECT_ID.intValue(), taskContext.getDataSourceObjectValue(key.getObjectId().toString(), key.getKey(), key.getObjectType()));
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) KeyInformation(org.motechproject.tasks.domain.KeyInformation) Test(org.junit.Test)

Example 15 with KeyInformation

use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.

the class TaskContextTest method testGetTriggerKeyShouldThrowException.

@Test(expected = IllegalStateException.class)
public void testGetTriggerKeyShouldThrowException() throws Exception {
    MotechEvent event = mock(MotechEvent.class);
    when(event.getParameters()).thenReturn(new HashMap<>());
    KeyInformation key = parse(String.format("%s.%s", TRIGGER_PREFIX, EVENT_KEY));
    Task task = new TaskBuilder().addAction(new TaskActionInformation()).build();
    new TaskContext(task, event.getParameters(), null, activityService).getTriggerValue(key.getKey());
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) MotechEvent(org.motechproject.event.MotechEvent) KeyInformation(org.motechproject.tasks.domain.KeyInformation) Test(org.junit.Test)

Aggregations

KeyInformation (org.motechproject.tasks.domain.KeyInformation)15 Test (org.junit.Test)10 Task (org.motechproject.tasks.domain.mds.task.Task)10 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)10 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)10 HashMap (java.util.HashMap)2 MotechEvent (org.motechproject.event.MotechEvent)2 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)2 SimpleDateFormat (java.text.SimpleDateFormat)1 HashSet (java.util.HashSet)1 MotechException (org.motechproject.commons.api.MotechException)1 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)1 Filter (org.motechproject.tasks.domain.mds.task.Filter)1 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)1