Search in sources :

Example 6 with KeyInformation

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()));
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) MotechEvent(org.motechproject.event.MotechEvent) KeyInformation(org.motechproject.tasks.domain.KeyInformation) Test(org.junit.Test)

Example 7 with KeyInformation

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()));
}
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 8 with KeyInformation

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;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) KeyInformation(org.motechproject.tasks.domain.KeyInformation)

Example 9 with KeyInformation

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;
}
Also used : MotechException(org.motechproject.commons.api.MotechException) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) KeyInformation(org.motechproject.tasks.domain.KeyInformation)

Example 10 with KeyInformation

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;
}
Also used : TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet) KeyInformation(org.motechproject.tasks.domain.KeyInformation) DataSource(org.motechproject.tasks.domain.mds.task.DataSource)

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