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;
}
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);
}
}
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);
}
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;
}
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;
}
Aggregations