Search in sources :

Example 26 with TaskError

use of org.motechproject.tasks.domain.mds.task.TaskError 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 27 with TaskError

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

the class ChannelValidator method validate.

/**
 * Validates the given channel by checking if all necessary data is set. Returns the set of {@code TaskError}s
 * containing information about missing fields.
 *
 * @param channel  the channel to be validated, not null
 * @return  the set of encountered errors
 */
public static Set<TaskError> validate(Channel channel) {
    Set<TaskError> errors = new HashSet<>();
    checkBlankValue(errors, CHANNEL, "displayName", channel.getDisplayName());
    checkBlankValue(errors, CHANNEL, "moduleName", channel.getModuleName());
    checkBlankValue(errors, CHANNEL, "moduleVersion", channel.getModuleVersion());
    checkVersion(errors, CHANNEL, "moduleVersion", channel.getModuleVersion());
    boolean containsTriggers = !isEmpty(channel.getTriggerTaskEvents());
    boolean containsActions = !isEmpty(channel.getActionTaskEvents());
    if (!containsTriggers && !containsActions) {
        errors.add(new TaskError("task.validation.error.channel"));
    } else {
        if (containsTriggers) {
            for (int i = 0; i < channel.getTriggerTaskEvents().size(); ++i) {
                errors.addAll(validateTrigger(i, channel.getTriggerTaskEvents().get(i)));
            }
        }
        if (containsActions) {
            for (int i = 0; i < channel.getActionTaskEvents().size(); ++i) {
                errors.addAll(validateAction(i, channel.getActionTaskEvents().get(i)));
            }
        }
    }
    return errors;
}
Also used : TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet)

Example 28 with TaskError

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

the class ChannelValidator method validateAction.

private static Set<TaskError> validateAction(int index, ActionEvent action) {
    Set<TaskError> errors = new HashSet<>();
    String field = "actionTaskEvents[" + index + "]";
    checkNullValue(errors, CHANNEL, field, action);
    if (isEmpty(errors)) {
        String objectName = CHANNEL + "." + field;
        checkBlankValue(errors, objectName, "displayName", action.getDisplayName());
        if (!action.hasSubject() && !action.hasService()) {
            errors.add(new TaskError("task.validation.error.channelAction"));
        }
        for (ActionParameter parameter : action.getActionParameters()) {
            errors.addAll(validateActionParameter(objectName, "actionParameters[" + parameter.getOrder() + "]", parameter));
        }
        for (ActionParameter parameter : action.getPostActionParameters()) {
            errors.addAll(validateActionParameter(objectName, "postActionParameters[" + parameter.getOrder() + "]", parameter));
        }
    }
    return errors;
}
Also used : TaskError(org.motechproject.tasks.domain.mds.task.TaskError) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter) HashSet(java.util.HashSet)

Example 29 with TaskError

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

the class TaskDataProviderValidator method validateProviderName.

private static void validateProviderName(Set<TaskError> errors, String providerName) {
    checkBlankValue(errors, TASK_DATA_PROVIDER, "name", providerName);
    // check only non-blank, we already validated against blank names one line above
    if (StringUtils.isNotBlank(providerName)) {
        Pattern pattern = Pattern.compile("^[A-Za-z0-9\\-_]+$");
        Matcher matcher = pattern.matcher(providerName);
        if (!matcher.matches()) {
            errors.add(new TaskError("task.validation.error.provider.name", providerName));
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) TaskError(org.motechproject.tasks.domain.mds.task.TaskError)

Example 30 with TaskError

use of org.motechproject.tasks.domain.mds.task.TaskError 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

TaskError (org.motechproject.tasks.domain.mds.task.TaskError)36 HashSet (java.util.HashSet)25 Test (org.junit.Test)10 Channel (org.motechproject.tasks.domain.mds.channel.Channel)8 Task (org.motechproject.tasks.domain.mds.task.Task)8 Transactional (org.springframework.transaction.annotation.Transactional)8 HashMap (java.util.HashMap)7 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)7 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)7 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)6 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)6 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)5 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)5 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)4 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)4 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)4 Map (java.util.Map)3 QueryExecution (org.motechproject.mds.query.QueryExecution)3 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)3 ValidationException (org.motechproject.tasks.exception.ValidationException)3