Search in sources :

Example 11 with ActionParameter

use of org.motechproject.tasks.domain.mds.channel.ActionParameter in project motech by motech.

the class TaskActionExecutor method createParameters.

private Map<String, Object> createParameters(TaskActionInformation info, ActionEvent action, KeyEvaluator keyEvaluator) throws TaskHandlerException {
    SortedSet<ActionParameter> actionParameters = action.getActionParameters();
    Map<String, Object> parameters = new HashMap<>(actionParameters.size());
    for (ActionParameter actionParameter : actionParameters) {
        String key = actionParameter.getKey();
        if (info.getValues().containsKey(key)) {
            String template = info.getValues().get(key);
            if (template == null) {
                throw new TaskHandlerException(TRIGGER, "task.error.templateNull", key, action.getDisplayName());
            }
            switch(actionParameter.getType()) {
                case LIST:
                    parameters.put(key, convertToList((List<String>) LIST.parse(template), keyEvaluator));
                    break;
                case MAP:
                    parameters.put(key, convertToMap(template, keyEvaluator));
                    break;
                default:
                    try {
                        String userInput = keyEvaluator.evaluateTemplateString(template);
                        Object obj = actionParameter.getType().parse(userInput);
                        parameters.put(key, obj);
                    } catch (MotechException ex) {
                        throw new TaskHandlerException(TRIGGER, ex.getMessage(), ex, key);
                    }
            }
        } else {
            if (actionParameter.isRequired()) {
                throw new TaskHandlerException(TRIGGER, "task.error.taskActionNotContainsField", action.getDisplayName(), key);
            } else if (actionParameter.getType() == MAP) {
                parameters.put(key, new HashMap<>());
            } else if (actionParameter.getType() == LIST) {
                parameters.put(key, new ArrayList<>());
            } else {
                parameters.put(key, null);
            }
        }
    }
    return parameters;
}
Also used : MotechException(org.motechproject.commons.api.MotechException) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter)

Example 12 with ActionParameter

use of org.motechproject.tasks.domain.mds.channel.ActionParameter in project motech by motech.

the class TaskTriggerHandlerTest method shouldNotTriggerErrorWhenActionDoesNotFindDataSourceWithFailIfDataNotFoundNotSelected.

@Test
public void shouldNotTriggerErrorWhenActionDoesNotFindDataSourceWithFailIfDataNotFoundNotSelected() throws Exception {
    Map<String, DataProvider> providers = new HashMap<>();
    DataProvider provider = mock(DataProvider.class);
    Map<String, String> lookup = new HashMap<>();
    lookup.put("patientId", "123");
    when(provider.lookup("Patient", null, lookup)).thenReturn(null);
    providers.put(TASK_DATA_PROVIDER_NAME, provider);
    handler.setDataProviders(providers);
    TriggerEvent trigger = new TriggerEvent();
    trigger.setSubject("trigger");
    List<EventParameter> triggerEventParameters = new ArrayList<>();
    triggerEventParameters.add(new EventParameter("patientId", "123"));
    trigger.setEventParameters(triggerEventParameters);
    ActionEvent action = new ActionEventBuilder().build();
    action.setSubject("action");
    SortedSet<ActionParameter> actionEventParameters = new TreeSet<>();
    actionEventParameters.add(new ActionParameterBuilder().setDisplayName("Patient ID").setKey("patientId").setType(UNICODE).setOrder(0).build());
    action.setActionParameters(actionEventParameters);
    Task task = new Task();
    task.setName("task");
    task.setTrigger(new TaskTriggerInformation("Trigger", "channel", "module", "0.1", "trigger", "listener"));
    Map<String, String> actionValues = new HashMap<>();
    actionValues.put("patientId", "{{ad.12345.Patient#1.patientId}}");
    task.addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "action", actionValues));
    task.setId(7l);
    task.setHasRegisteredChannel(true);
    TaskConfig taskConfig = new TaskConfig();
    task.setTaskConfig(taskConfig);
    taskConfig.add(new DataSource(TASK_DATA_PROVIDER_NAME, 3L, 1L, "Patient", "provider", "specifiedName", asList(new Lookup("patientId", "trigger.patientId")), false));
    List<Task> tasks = asList(task);
    when(taskService.findActiveTasksForTriggerSubject("trigger")).thenReturn(tasks);
    when(taskService.getActionEventFor(task.getActions().get(0))).thenReturn(action);
    setTaskActivities();
    task.setFailuresInRow(taskActivities.size());
    Map<String, Object> param = new HashMap<>(4);
    param.put("patientId", "123");
    handler.handle(new MotechEvent("trigger", param));
    verify(postExecutionHandler, never()).handleError(anyMap(), anyMap(), eq(task), any(TaskHandlerException.class), eq(TASK_ACTIVITY_ID));
    verify(taskActivityService).addWarning(eq(task), eq("task.warning.notFoundObjectForType"), eq("Patient"));
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) Matchers.anyString(org.mockito.Matchers.anyString) DataProvider(org.motechproject.commons.api.DataProvider) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) TreeSet(java.util.TreeSet) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) MotechEvent(org.motechproject.event.MotechEvent) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) Test(org.junit.Test)

Aggregations

ActionParameter (org.motechproject.tasks.domain.mds.channel.ActionParameter)12 TreeSet (java.util.TreeSet)8 ActionParameterBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder)6 HashMap (java.util.HashMap)4 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)4 ArrayList (java.util.ArrayList)3 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)3 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)3 Annotation (java.lang.annotation.Annotation)2 List (java.util.List)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 DataProvider (org.motechproject.commons.api.DataProvider)2 MotechEvent (org.motechproject.event.MotechEvent)2 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)2 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)2 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)2 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)2 Task (org.motechproject.tasks.domain.mds.task.Task)2 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)2