private void migrateActions(Task task) { // replace data source references in actions for (TaskActionInformation action : task.getActions()) { for (Map.Entry<String, String> entry : action.getValues().entrySet()) { String oldVal = entry.getValue(); String newVal = replaceTaskValue(oldVal, EXPR_PATTERN, task); entry.setValue(newVal); } } }
private Map<String, Object> createParameters( TaskActionInformation info, ActionEvent action, KeyEvaluator keyEvaluator) throws TaskHandlerException { SortedSet<ActionParameter> actionParameters = action.getActionParameters(); Map<String, Object> parameters = new HashMap<>(actionParameters.size()); for (ActionParameter actionParameter : actionParameters) { String key = actionParameter.getKey(); if (info.getValues().containsKey(key)) { String template = info.getValues().get(key); if (template == null) { throw new TaskHandlerException( TRIGGER, "task.error.templateNull", key, action.getDisplayName()); } switch (actionParameter.getType()) { case LIST: parameters.put(key, convertToList((List<String>) LIST.parse(template), keyEvaluator)); break; case MAP: parameters.put(key, convertToMap(template, keyEvaluator)); break; default: try { String userInput = keyEvaluator.evaluateTemplateString(template); Object obj = actionParameter.getType().parse(userInput); parameters.put(key, obj); } catch (MotechException ex) { throw new TaskHandlerException(TRIGGER, ex.getMessage(), ex, key); } } } else { if (actionParameter.isRequired()) { throw new TaskHandlerException( TRIGGER, "task.error.taskActionNotContainsField", action.getDisplayName(), key); } else if (actionParameter.getType() == MAP) { parameters.put(key, new HashMap<>()); } else if (actionParameter.getType() == LIST) { parameters.put(key, new ArrayList<>()); } else { parameters.put(key, null); } } } return parameters; }
/** * Executes the action for the given task. * * @param task the task for which its action should be executed, not null * @param actionInformation the information about the action, not null * @param actionIndex the order of the task action * @param taskContext the context of the current task execution, not null * @param activityId the ID of the activity associated with this execution * @throws TaskHandlerException when the task couldn't be executed */ public void execute( Task task, TaskActionInformation actionInformation, Integer actionIndex, TaskContext taskContext, long activityId) throws TaskHandlerException { LOGGER.info( "Executing task action: {} from task: {}", actionInformation.getName(), task.getName()); KeyEvaluator keyEvaluator = new KeyEvaluator(taskContext); ActionEvent action = getActionEvent(actionInformation); Map<String, Object> parameters = createParameters(actionInformation, action, keyEvaluator); addTriggerParameters(task, action, parameters, taskContext.getTriggerParameters()); LOGGER.debug( "Parameters created: {} for task action: {}", parameters.toString(), action.getName()); if (action.hasService() && bundleContext != null) { if (callActionServiceMethod(action, actionIndex, parameters, taskContext)) { LOGGER.info( "Action: {} from task: {} was executed through an OSGi service call", actionInformation.getName(), task.getName()); postExecutionHandler.handleActionExecuted( taskContext.getTriggerParameters(), taskContext.getMetadata(), activityId); return; } LOGGER.info("There is no service: {}", action.getServiceInterface()); activityService.addWarning( task, "task.warning.serviceUnavailable", action.getServiceInterface()); } if (!action.hasSubject()) { throw new TaskHandlerException(ACTION, "task.error.cantExecuteAction"); } else { eventRelay.sendEventMessage( new MotechEvent( action.getSubject(), parameters, TasksEventCallbackService.TASKS_EVENT_CALLBACK_NAME, taskContext.getMetadata())); LOGGER.info("Event: {} was sent", action.getSubject()); } }