Search in sources :

Example 6 with TaskHandlerException

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

the class TaskActionExecutor method callActionServiceMethod.

private boolean callActionServiceMethod(ActionEvent action, Integer actionIndex, Map<String, Object> parameters, TaskContext taskContext) throws TaskHandlerException {
    ServiceReference reference = bundleContext.getServiceReference(action.getServiceInterface());
    boolean serviceAvailable = reference != null;
    if (serviceAvailable) {
        Object service = bundleContext.getService(reference);
        String serviceMethod = action.getServiceMethod();
        MethodHandler methodHandler = new MethodHandler(action, parameters);
        try {
            Method method = service.getClass().getMethod(serviceMethod, methodHandler.getClasses());
            try {
                Object object = method.invoke(service, methodHandler.getObjects());
                if (object != null) {
                    addPostActionParametersToTaskContext(action, actionIndex, taskContext, object);
                }
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new TaskHandlerException(ACTION, "task.error.serviceMethodInvokeError", e, serviceMethod, action.getServiceInterface());
            }
        } catch (NoSuchMethodException e) {
            throw new TaskHandlerException(ACTION, "task.error.notFoundMethodForService", e, serviceMethod, action.getServiceInterface());
        }
    }
    return serviceAvailable;
}
Also used : TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceReference(org.osgi.framework.ServiceReference)

Example 7 with TaskHandlerException

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

the class TaskActivityServiceImpl method addFailedExecution.

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void addFailedExecution(Long activityId, Throwable e) {
    TaskActivity activity = taskActivitiesDataService.findById(activityId);
    if (activity == null) {
        return;
    }
    if (activity.getActivityType() != TaskActivityType.ERROR) {
        activity.setMessage(e.getMessage());
        activity.setActivityType(TaskActivityType.ERROR);
        if (e instanceof TaskHandlerException) {
            activity.setFields(((TaskHandlerException) e).getArgs());
        }
        activity.setStackTraceElement(ExceptionUtils.getStackTrace(e));
        taskActivitiesDataService.update(activity);
    }
}
Also used : TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) TaskActivity(org.motechproject.tasks.domain.mds.task.TaskActivity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with TaskHandlerException

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

the class TaskInitializer method getDataSourceObject.

private Object getDataSourceObject(DataSource dataSource, Map<String, DataProvider> providers) throws TaskHandlerException {
    if (providers == null || providers.isEmpty()) {
        throw new TaskHandlerException(DATA_SOURCE, "task.error.notFoundDataProvider", dataSource.getType());
    }
    DataProvider provider = providers.get(dataSource.getProviderName());
    if (provider == null) {
        throw new TaskHandlerException(DATA_SOURCE, "task.error.notFoundDataProvider", dataSource.getType());
    }
    KeyEvaluator keyEvaluator = new KeyEvaluator(taskContext);
    Map<String, String> lookupFields = new HashMap<>();
    for (Lookup lookup : dataSource.getLookup()) {
        lookupFields.put(lookup.getField(), keyEvaluator.evaluateTemplateString(lookup.getValue()));
    }
    return provider.lookup(dataSource.getType(), dataSource.getName(), lookupFields);
}
Also used : DataProvider(org.motechproject.commons.api.DataProvider) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) HashMap(java.util.HashMap) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) KeyEvaluator(org.motechproject.tasks.service.util.KeyEvaluator)

Example 9 with TaskHandlerException

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

the class TasksEventCallbackService method failureCallback.

@Override
public boolean failureCallback(MotechEvent event, Throwable throwable) {
    LOGGER.debug("Received failure callback for event subject {}", event.getSubject());
    Map<String, Object> metadata = event.getMetadata();
    Long activityId = (Long) metadata.get(EventDataKeys.TASK_ACTIVITY_ID);
    Task task = taskService.getTask((Long) metadata.get(EventDataKeys.TASK_ID));
    postExecutionHandler.handleError(event.getParameters(), metadata, task, new TaskHandlerException(TaskFailureCause.ACTION, "task.error.eventHandlerFailed", throwable), activityId);
    return false;
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException)

Example 10 with TaskHandlerException

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

the class KeyEvaluator method manipulateValue.

private String manipulateValue(List<String> manipulations, String value) throws TaskHandlerException {
    String manipulateValue = value;
    for (String manipulation : manipulations) {
        if (manipulation.contains("format")) {
            String formatElements = manipulation.substring(FORMAT_PATTERN_BEGIN_INDEX, manipulation.length() - 1);
            if (isNotBlank(formatElements)) {
                String[] items = formatElements.split(",");
                for (int i = 0; i < items.length; ++i) {
                    String item = items[i];
                    if (item.startsWith("{{") && item.endsWith("}}")) {
                        item = item.substring(2, item.length() - 2);
                        KeyInformation subKey = KeyInformation.parse(item);
                        Object subValue = getValue(subKey);
                        items[i] = subValue != null ? subValue.toString() : "";
                    }
                }
                manipulateValue = String.format(manipulateValue, items);
            }
        } else {
            try {
                manipulateValue = manipulate(manipulation, manipulateValue);
            } catch (MotechException e) {
                String msg = e.getMessage();
                if ("task.warning.manipulation".equalsIgnoreCase(msg)) {
                    taskContext.publishWarningActivity(msg, manipulation);
                } else {
                    throw new TaskHandlerException(TRIGGER, msg, e, manipulation);
                }
            }
        }
    }
    return manipulateValue;
}
Also used : MotechException(org.motechproject.commons.api.MotechException) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) KeyInformation(org.motechproject.tasks.domain.KeyInformation)

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