Search in sources :

Example 1 with TaskError

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

the class TaskValidator method validateProvider.

/**
 * Validates if the given provider contains the given data source and trigger event.
 *
 * @param provider  the provider to be checked, not null
 * @param dataSource  the data source to be validated, not null
 * @param trigger  the trigger to be validated, not null
 * @param availableProviders  the map of the IDs and the providers, not null
 * @return  the set of encountered errors
 */
@Transactional
public Set<TaskError> validateProvider(TaskDataProvider provider, DataSource dataSource, TriggerEvent trigger, Map<Long, TaskDataProvider> availableProviders) {
    Set<TaskError> errors = new HashSet<>();
    Map<String, String> fields = new HashMap<>();
    Map<String, ParameterType> fieldsTypes = new HashMap<>();
    if (!provider.containsProviderObject(dataSource.getType())) {
        errors.add(new TaskError("task.validation.error.providerObjectNotExist", dataSource.getType(), provider.getName()));
    } else {
        for (Lookup lookup : dataSource.getLookup()) {
            if (!provider.containsProviderObjectLookup(dataSource.getType(), dataSource.getName())) {
                errors.add(new TaskError("task.validation.error.providerObjectLookupNotExist", lookup.getField(), dataSource.getType(), provider.getName()));
            }
            fields.put(lookup.getField(), lookup.getValue());
            fieldsTypes.put(lookup.getField(), ParameterType.UNKNOWN);
        }
        errors.addAll(validateFieldsParameter(fields, fieldsTypes, trigger, availableProviders));
    }
    return errors;
}
Also used : ParameterType(org.motechproject.tasks.domain.enums.ParameterType) HashMap(java.util.HashMap) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with TaskError

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

the class TaskValidator method validateTrigger.

/**
 * Validates the trigger of the given task by checking if it is specified in the given channel.
 *
 * @param task  the task for which the trigger should be validated, not null
 * @return  the set of encountered errors
 */
@Transactional
public Set<TaskError> validateTrigger(Task task) {
    Set<TaskError> errors = new HashSet<>();
    TaskTriggerInformation triggerInformation = task.getTrigger();
    boolean exists = triggerEventService.triggerExists(triggerInformation);
    if (!exists) {
        errors.add(new TaskError("task.validation.error.triggerNotExist", triggerInformation.getDisplayName()));
    }
    return errors;
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with TaskError

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

the class TaskValidator method validateAction.

/**
 * Checks if the channel contains the given actions.
 *
 * @param actionInformation  the information about action, not null
 * @param channel  the channel to be checked, not null
 * @return  the set of encountered errors
 */
@Transactional
public Set<TaskError> validateAction(TaskActionInformation actionInformation, Channel channel) {
    Set<TaskError> errors = new HashSet<>();
    boolean exists = channel.containsAction(actionInformation);
    if (!exists) {
        errors.add(new TaskError("task.validation.error.actionNotExist", actionInformation.getDisplayName(), channel.getDisplayName()));
    }
    return errors;
}
Also used : TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with TaskError

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

the class TaskValidator method validateManipulations.

private Set<TaskError> validateManipulations(String manipulation, KeyInformation key, ParameterType parameterType, ParameterType fieldType) {
    Set<TaskError> errors = new HashSet<>();
    TaskError error;
    String at = key.getKey() + "?" + manipulation;
    if (parameterType == ParameterType.UNICODE || parameterType == ParameterType.TEXTAREA || parameterType == ParameterType.UNKNOWN) {
        error = validateStringManipulation(manipulation, at);
        if (error != null) {
            errors.add(error);
        }
    } else if (parameterType == ParameterType.DATE) {
        error = validateDateManipulation(manipulation, fieldType, at);
        if (error != null) {
            errors.add(error);
        }
    } else {
        errors.add(new TaskError("task.validation.error.wrongAnotherManipulation", manipulation, at));
    }
    return errors;
}
Also used : TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet)

Example 5 with TaskError

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

the class TaskValidator method validateDateManipulation.

private TaskError validateDateManipulation(String manipulation, ParameterType fieldType, String foundAt) {
    TaskError error = null;
    ManipulationType type = ManipulationType.fromString(manipulation.replaceAll("\\((.*?)\\)", ""));
    if (type.getTarget() != ManipulationTarget.DATE) {
        if (type.getTarget() == ManipulationTarget.ALL) {
            error = new TaskError("task.validation.error.wrongAnotherManipulation", manipulation, foundAt);
        } else {
            error = new TaskError("task.validation.error.wrongDateManipulation", manipulation, foundAt);
        }
    } else if (fieldType.equals(ParameterType.DATE) && !type.allowResultType(ManipulationTarget.DATE)) {
        error = new TaskError("task.validation.error.wrongDateManipulationTarget", manipulation, foundAt);
    }
    return error;
}
Also used : TaskError(org.motechproject.tasks.domain.mds.task.TaskError) ManipulationType(org.motechproject.tasks.domain.ManipulationType)

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