Search in sources :

Example 1 with TaskHandlerException

use of org.motechproject.tasks.exception.TaskHandlerException in project motech by motech.

the class TaskActivityServiceImplTest method shouldAddTaskWarningActivityWithGivenException.

@Test
public void shouldAddTaskWarningActivityWithGivenException() {
    TaskHandlerException exception = new TaskHandlerException(TRIGGER, "trigger.exception", new TaskHandlerException(TRIGGER, "task.exception"));
    String messageKey = "warning.manipulation";
    ArgumentCaptor<TaskActivity> captor = ArgumentCaptor.forClass(TaskActivity.class);
    activityService.addWarningWithException(task, messageKey, ERROR_FIELD.get(0), exception);
    verify(taskActivitiesDataService).create(captor.capture());
    assertActivity(messageKey, ERROR_FIELD, TASK_ID, TaskActivityType.WARNING, getStackTrace(exception.getCause()), null, captor.getValue());
}
Also used : TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) TaskActivity(org.motechproject.tasks.domain.mds.task.TaskActivity) Test(org.junit.Test)

Example 2 with TaskHandlerException

use of org.motechproject.tasks.exception.TaskHandlerException in project motech by motech.

the class TaskContext method getDataSourceObjectValue.

/**
 * Returns the value of data source object based on it's field, id and type.
 *
 * @param objectId  the id of the object, not null
 * @param field  the name of the field, not null
 * @param objectType  the type of the object
 * @return  the value of data source object
 * @throws TaskHandlerException
 */
public Object getDataSourceObjectValue(String objectId, String field, String objectType) throws TaskHandlerException {
    LOGGER.info("Retrieving task data source object: {} with ID: {}", objectType, objectId);
    DataSourceObject dataSourceObject = getDataSourceObject(objectId);
    if (dataSourceObject == null) {
        throw new TaskHandlerException(TaskFailureCause.DATA_SOURCE, "task.error.objectOfTypeNotFound", objectType);
    }
    if (dataSourceObject.getObjectValue() == null) {
        if (dataSourceObject.isFailIfNotFound()) {
            throw new TaskHandlerException(TaskFailureCause.DATA_SOURCE, "task.error.objectOfTypeNotFound", objectType);
        }
        if (!dataSourceObject.isNullWarningPublished()) {
            LOGGER.warn("Task data source object of type: {} not found", objectType);
            publishWarningActivity("task.warning.notFoundObjectForType", objectType);
            dataSourceObject.setNullWarningPublished(true);
        }
        return null;
    }
    try {
        return getFieldValue(dataSourceObject.getObjectValue(), field);
    } catch (RuntimeException e) {
        if (dataSourceObject.isFailIfNotFound()) {
            throw new TaskHandlerException(TaskFailureCause.DATA_SOURCE, "task.error.objectDoesNotContainField", e, field);
        }
        LOGGER.warn("Task data source object: {} does not contain field: {}", objectType, field);
        publishWarningActivity("task.warning.objectNotContainsField", field);
    }
    return null;
}
Also used : TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException)

Example 3 with TaskHandlerException

use of org.motechproject.tasks.exception.TaskHandlerException in project motech by motech.

the class TaskInitializer method checkActionFilter.

@Transactional
public boolean checkActionFilter(int actualFilterIndex, List<FilterSet> filterSetList) throws TaskHandlerException {
    boolean result;
    TaskFilterExecutor taskFilterExecutor = new TaskFilterExecutor();
    try {
        result = taskFilterExecutor.checkFilters(filterSetList.get(actualFilterIndex).getFilters(), filterSetList.get(actualFilterIndex).getOperator(), taskContext);
    } catch (RuntimeException e) {
        throw new TaskHandlerException(FILTER, "task.error.filterError", e);
    }
    return result;
}
Also used : TaskFilterExecutor(org.motechproject.tasks.service.util.TaskFilterExecutor) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with TaskHandlerException

use of org.motechproject.tasks.exception.TaskHandlerException in project motech by motech.

the class TaskInitializer method evalConfigSteps.

/**
 * Executes all config steps (loading data from data sources, checking filters) defined for this task.
 *
 * @param dataProviders  the map of data providers, not null or empty
 * @return  true if all steps were executed, false otherwise
 * @throws TaskHandlerException if there were error while handling task
 */
@Transactional
public boolean evalConfigSteps(Map<String, DataProvider> dataProviders) throws TaskHandlerException {
    LOGGER.info("Executing all config steps for task: {}", taskContext.getTask().getName());
    Iterator<TaskConfigStep> iterator = taskContext.getTask().getTaskConfig().getSteps().iterator();
    boolean result = true;
    TaskFilterExecutor taskFilterExecutor = new TaskFilterExecutor();
    while (result && iterator.hasNext()) {
        TaskConfigStep step = iterator.next();
        if (step instanceof DataSource) {
            DataSource ds = (DataSource) step;
            taskContext.addDataSourceObject(ds.getObjectId().toString(), getDataSourceObject(ds, dataProviders), ds.isFailIfDataNotFound());
            LOGGER.info("Task data source: {} for task: {} added", ds.getName(), taskContext.getTask().getName());
        } else if (step instanceof FilterSet && !isActionFilter((FilterSet) step)) {
            try {
                FilterSet filterSet = (FilterSet) step;
                result = taskFilterExecutor.checkFilters(filterSet.getFilters(), filterSet.getOperator(), taskContext);
            } catch (RuntimeException e) {
                throw new TaskHandlerException(FILTER, "task.error.filterError", e);
            }
        }
    }
    return result;
}
Also used : TaskFilterExecutor(org.motechproject.tasks.service.util.TaskFilterExecutor) TaskConfigStep(org.motechproject.tasks.domain.mds.task.TaskConfigStep) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with TaskHandlerException

use of org.motechproject.tasks.exception.TaskHandlerException in project motech by motech.

the class TaskTriggerHandler method handleTask.

private void handleTask(Task task, Map<String, Object> parameters) {
    long activityId = activityService.addTaskStarted(task, parameters);
    Map<String, Object> metadata = prepareTaskMetadata(task.getId(), activityId);
    TaskContext taskContext = new TaskContext(task, parameters, metadata, activityService);
    TaskInitializer initializer = new TaskInitializer(taskContext);
    List<FilterSet> filterSetList = new ArrayList<>(task.getTaskConfig().getFilters());
    boolean actionFilterResult = true;
    int executedAndFilteredActions = 0;
    int stepIndex = 0;
    int actualFilterIndex = initializer.getActionFilters();
    try {
        LOGGER.info("Executing all actions from task: {}", task.getName());
        if (initializer.evalConfigSteps(dataProviders)) {
            while (executedAndFilteredActions < task.getActions().size()) {
                if (isActionFilter(filterSetList, actualFilterIndex, stepIndex)) {
                    actionFilterResult = initializer.checkActionFilter(actualFilterIndex, filterSetList);
                    actualFilterIndex++;
                    stepIndex++;
                }
                if (actionFilterResult) {
                    executor.execute(task, task.getActions().get(executedAndFilteredActions), executedAndFilteredActions, taskContext, activityId);
                    executedAndFilteredActions++;
                } else {
                    activityService.addFilteredExecution(activityId);
                    executedAndFilteredActions++;
                }
                stepIndex++;
                actionFilterResult = true;
            }
        } else {
            activityService.addTaskFiltered(activityId);
            LOGGER.warn("Actions from task: {} weren't executed, because config steps didn't pass the evaluation", task.getName());
        }
    } catch (TaskHandlerException e) {
        postExecutionHandler.handleError(parameters, metadata, task, e, activityId);
    } catch (RuntimeException e) {
        postExecutionHandler.handleError(parameters, metadata, task, new TaskHandlerException(TRIGGER, "task.error.unrecognizedError", e), activityId);
    }
}
Also used : TaskContext(org.motechproject.tasks.service.util.TaskContext) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) ArrayList(java.util.ArrayList) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException)

Aggregations

TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)15 Transactional (org.springframework.transaction.annotation.Transactional)4 Test (org.junit.Test)3 TaskActivity (org.motechproject.tasks.domain.mds.task.TaskActivity)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 MotechException (org.motechproject.commons.api.MotechException)2 KeyInformation (org.motechproject.tasks.domain.KeyInformation)2 FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)2 KeyEvaluator (org.motechproject.tasks.service.util.KeyEvaluator)2 TaskFilterExecutor (org.motechproject.tasks.service.util.TaskFilterExecutor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 DataProvider (org.motechproject.commons.api.DataProvider)1 MotechEvent (org.motechproject.event.MotechEvent)1 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)1 ActionParameter (org.motechproject.tasks.domain.mds.channel.ActionParameter)1 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)1 Filter (org.motechproject.tasks.domain.mds.task.Filter)1