use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.
the class TaskContextTest method testGetNullTriggerKey.
@Test
public void testGetNullTriggerKey() throws Exception {
Map<String, Object> parameters = new HashMap<>();
MotechEvent event = mock(MotechEvent.class);
KeyInformation key = parse(String.format("%s.%s", TRIGGER_PREFIX, EVENT_KEY));
Map<String, String> child = new HashMap<>();
child.put("key", null);
parameters.put("event", child);
Task task = new TaskBuilder().addAction(new TaskActionInformation()).build();
assertEquals(null, new TaskContext(task, parameters, null, activityService).getTriggerValue(key.getKey()));
// should not throw any exceptions
assertNull(new TaskContext(task, parameters, null, activityService).getTriggerValue(key.getKey()));
}
use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.
the class TaskContextTest method shouldNotThrowExceptionWhenDataSourceFieldValueEvaluationThrowsException_IfFailNotFoundIsFalse.
@Test
public void shouldNotThrowExceptionWhenDataSourceFieldValueEvaluationThrowsException_IfFailNotFoundIsFalse() throws Exception {
Task task = new TaskBuilder().addAction(new TaskActionInformation()).build();
TaskContext taskContext = new TaskContext(task, null, null, activityService);
taskContext.addDataSourceObject("1", new TestDataSourceObject(), false);
KeyInformation key = parse("ad.1.Integer#1.providerId");
assertNull(taskContext.getDataSourceObjectValue(key.getObjectId().toString(), key.getKey(), key.getObjectType()));
}
use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.
the class KeyEvaluator method evaluateTemplateString.
/**
* Evaluates the given template by replacing the keys with their manipulated values.
*
* @param template the template to be evaluated
* @return the evaluated template
* @throws TaskHandlerException if there was problem while manipulating the value
*/
public String evaluateTemplateString(String template) throws TaskHandlerException {
String conversionTemplate = template;
List<KeyInformation> keysList = parseAll(template);
for (KeyInformation key : keysList) {
Object value = getValue(key);
if (value == null && keysList.size() <= 1) {
conversionTemplate = null;
} else {
if (value instanceof java.util.Date) {
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
value = sdf.format(value);
}
String stringValue = value != null ? value.toString() : "";
stringValue = manipulateValue(key.getManipulations(), stringValue);
conversionTemplate = conversionTemplate.replace(String.format("{{%s}}", key.getOriginalKey()), stringValue);
}
}
return conversionTemplate;
}
use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.
the class KeyEvaluator method manipulateValue.
private String manipulateValue(List<String> manipulations, String value) throws TaskHandlerException {
String manipulateValue = value;
for (String manipulation : manipulations) {
if (manipulation.contains("format")) {
String formatElements = manipulation.substring(FORMAT_PATTERN_BEGIN_INDEX, manipulation.length() - 1);
if (isNotBlank(formatElements)) {
String[] items = formatElements.split(",");
for (int i = 0; i < items.length; ++i) {
String item = items[i];
if (item.startsWith("{{") && item.endsWith("}}")) {
item = item.substring(2, item.length() - 2);
KeyInformation subKey = KeyInformation.parse(item);
Object subValue = getValue(subKey);
items[i] = subValue != null ? subValue.toString() : "";
}
}
manipulateValue = String.format(manipulateValue, items);
}
} else {
try {
manipulateValue = manipulate(manipulation, manipulateValue);
} catch (MotechException e) {
String msg = e.getMessage();
if ("task.warning.manipulation".equalsIgnoreCase(msg)) {
taskContext.publishWarningActivity(msg, manipulation);
} else {
throw new TaskHandlerException(TRIGGER, msg, e, manipulation);
}
}
}
}
return manipulateValue;
}
use of org.motechproject.tasks.domain.KeyInformation in project motech by motech.
the class TaskValidator method validateFilter.
private Set<TaskError> validateFilter(Integer setOrder, int index, Filter filter, TaskConfig config) {
Set<TaskError> errors = new HashSet<>();
String field = String.format("taskConfig.filterSet[%d].filters[%d]", setOrder, index);
KeyInformation key = parse(filter.getKey());
DataSource dataSource = config.getDataSource(key.getDataProviderName(), key.getObjectId(), key.getObjectType());
checkNullValue(errors, TASK, field, filter);
if (isEmpty(errors)) {
String objectName = "task." + field;
if (key.fromAdditionalData() && dataSource == null) {
errors.add(new TaskError("task.validation.error.DataSourceNotExist", key.getObjectType()));
}
checkBlankValue(errors, objectName, "key", filter.getKey());
checkBlankValue(errors, objectName, "displayName", filter.getDisplayName());
checkBlankValue(errors, objectName, "operator", filter.getOperator());
checkNullValue(errors, objectName, "type", filter.getType());
if (OperatorType.needExpression(filter.getOperator())) {
checkBlankValue(errors, objectName, "expression", filter.getExpression());
}
}
return errors;
}
Aggregations