Search in sources :

Example 71 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TaskServiceImpl method validateTasksAfterTaskDataProviderUpdate.

@MotechListener(subjects = DATA_PROVIDER_UPDATE_SUBJECT)
@Transactional
public void validateTasksAfterTaskDataProviderUpdate(MotechEvent event) {
    String providerName = event.getParameters().get(DATA_PROVIDER_NAME).toString();
    TaskDataProvider provider = providerService.getProvider(providerName);
    LOGGER.debug("Handling a task data provider update: {}", providerName);
    for (Task task : getAllTasks()) {
        SortedSet<DataSource> dataSources = task.getTaskConfig().getDataSources(provider.getName());
        if (isNotEmpty(dataSources)) {
            Set<TaskError> errors = new HashSet<>();
            for (DataSource dataSource : dataSources) {
                errors.addAll(validateProvider(provider, dataSource, task, new HashMap<Long, TaskDataProvider>()));
            }
            errors.addAll(validateActions(task));
            handleValidationErrors(task, errors, TASK_DATA_PROVIDER_VALIDATION_ERRORS);
        }
    }
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) HashSet(java.util.HashSet) MotechListener(org.motechproject.event.listener.annotations.MotechListener) Transactional(org.springframework.transaction.annotation.Transactional)

Example 72 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TaskServiceImpl method exportTask.

@Override
@Transactional
public String exportTask(Long taskId) {
    Task task = getTask(taskId);
    if (null != task) {
        LOGGER.info("Exporting task: {} with ID: {}", task.getName(), task.getId());
        JsonNode node = new ObjectMapper().valueToTree(task.toDto());
        removeIgnoredFields(node);
        return node.toString();
    } else {
        throw new TaskNotFoundException(taskId);
    }
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TaskNotFoundException(org.motechproject.tasks.exception.TaskNotFoundException) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Transactional(org.springframework.transaction.annotation.Transactional)

Example 73 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TaskServiceImpl method checkTimeWindowInTasks.

private List<Task> checkTimeWindowInTasks(List<Task> tasks) {
    List<Task> checked = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(tasks)) {
        for (Task task : tasks) {
            if (checkTimeWindowInTask(task)) {
                if (!task.isEnabled()) {
                    task.setEnabled(true);
                    addOrUpdate(task);
                }
                checked.add(task);
            } else {
                if (task.isEnabled()) {
                    task.setEnabled(false);
                    addOrUpdate(task);
                }
            }
        }
    }
    return checked;
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) ArrayList(java.util.ArrayList)

Example 74 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TaskServiceImpl method findActiveTasksForTriggerSubject.

@Override
@Transactional
public List<Task> findActiveTasksForTriggerSubject(final String subject) {
    List<Task> list = null;
    if (isNotBlank(subject)) {
        List enabledTasks = tasksDataService.executeQuery(new QueryExecution<List<Task>>() {

            @Override
            public List<Task> execute(Query query, InstanceSecurityRestriction restriction) {
                String byTriggerSubject = "trigger.subject == param";
                String isTaskActive = "enabled == true";
                String isUsingTimeWindow = "useTimeWindow == true";
                String filter = String.format("((%s) || (%s)) && (%s)", isTaskActive, isUsingTimeWindow, byTriggerSubject);
                query.setFilter(filter);
                query.declareParameters("java.lang.String param");
                query.setOrdering("id asc");
                return (List) QueryExecutor.execute(query, subject, restriction);
            }
        });
        if (enabledTasks != null) {
            checkChannelAvailableInTasks(enabledTasks);
            list = checkTimeWindowInTasks(enabledTasks);
            CollectionUtils.filter(list, tasksWithRegisteredChannel());
        }
    }
    return list == null ? new ArrayList<>() : list;
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) Query(javax.jdo.Query) InstanceSecurityRestriction(org.motechproject.mds.util.InstanceSecurityRestriction) List(java.util.List) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 75 with Task

use of org.motechproject.tasks.domain.mds.task.Task in project motech by motech.

the class TaskTriggerHandler method handleRetry.

@Override
@Transactional
public void handleRetry(MotechEvent event) {
    LOGGER.info("Handling the motech event with subject: {} for task retry", event.getSubject());
    Map<String, Object> eventParams = event.getParameters();
    Map<String, Object> eventMetadata = event.getMetadata();
    Task task = taskService.getTask((Long) eventMetadata.get(TASK_ID));
    if (task != null && task.isEnabled()) {
        checkAndHandleTask(task, eventParams);
    }
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Task (org.motechproject.tasks.domain.mds.task.Task)86 Test (org.junit.Test)65 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)35 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)26 HashMap (java.util.HashMap)21 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)19 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)19 ArrayList (java.util.ArrayList)18 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)17 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)16 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)15 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)14 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)13 Channel (org.motechproject.tasks.domain.mds.channel.Channel)12 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)11 KeyInformation (org.motechproject.tasks.domain.KeyInformation)10 TreeSet (java.util.TreeSet)9 MotechEvent (org.motechproject.event.MotechEvent)9 ObjectTest (org.motechproject.tasks.domain.ObjectTest)9 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)9