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;
}
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;
}
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());
}
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()));
}
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());
}
Aggregations