use of org.motechproject.tasks.service.util.KeyEvaluator in project motech by motech.
the class KeyEvaluatorTest method shouldProperlyApplyManipulations.
@Test
public void shouldProperlyApplyManipulations() throws Exception {
String string = "ala-has-a-cat";
DateTime now = DateUtil.now();
String toString = now.toString();
String toStringWithPattern = now.toString("yyyy-MM-dd");
KeyEvaluator keyEvaluator = new KeyEvaluator(null);
String pastDate = "2015-05-15";
// Figure out the correct time zone for the given date and locale
String timeZone = new DateTime(pastDate).toString("Z");
assertEquals("lower_case", keyEvaluator.manipulate("tolower", "LOWER_CASE"));
assertEquals("UPPER_CASE", keyEvaluator.manipulate("toupper", "upper_case"));
assertEquals("Capitalize", keyEvaluator.manipulate("capitalize", "capitalize"));
assertEquals("My+sample+message", keyEvaluator.manipulate("urlencode", "My sample message"));
assertEquals("37%2365%4078%2490", keyEvaluator.manipulate("URLEncode", "37#65@78$90"));
assertEquals("67890", keyEvaluator.manipulate("substring(5)", "1234567890"));
assertEquals("67", keyEvaluator.manipulate("substring(5,7)", "1234567890"));
assertEquals(string, keyEvaluator.manipulate("join(-)", "ala has a cat"));
assertEquals("ala", keyEvaluator.manipulate("split(-,0)", string));
assertEquals("cat", keyEvaluator.manipulate("split(-,3)", string));
assertEquals(pastDate + "T11:32:00.000" + timeZone, keyEvaluator.manipulate("parseDate(yyyy/dd/MM hh:mm)", "2015/15/05 11:32"));
assertEquals(toStringWithPattern, keyEvaluator.manipulate("datetime(yyyy-MM-dd)", toString));
assertEquals(now.dayOfMonth().withMinimumValue().withTime(0, 0, 0, 0).toString(), keyEvaluator.manipulate("beginningOfMonth", toString));
assertEquals(now.dayOfMonth().withMaximumValue().withTime(23, 59, 59, 999).toString(), keyEvaluator.manipulate("endOfMonth", toString));
assertEquals(now.plusMonths(1).toString(), keyEvaluator.manipulate("plusMonths(1)", toString));
assertEquals("Q" + ((now.monthOfYear().get() / 3) + 1) + "-" + now.year().getAsString(), keyEvaluator.manipulate("quarter", toString));
assertEquals(now.plusDays(1).toString(), keyEvaluator.manipulate("plusDays(1)", toString));
assertEquals(now.minusDays(1).toString(), keyEvaluator.manipulate("minusDays(1)", toString));
assertEquals(now.plusHours(2).toString(), keyEvaluator.manipulate("plusHours(2)", toString));
assertEquals(now.minusHours(2).toString(), keyEvaluator.manipulate("minusHours(2)", toString));
assertEquals(now.plusMinutes(20).toString(), keyEvaluator.manipulate("plusMinutes(20)", toString));
assertEquals(now.minusMinutes(20).toString(), keyEvaluator.manipulate("minusMinutes(20)", toString));
assertEquals("", keyEvaluator.manipulate("getvalue(mapEntry)", "{}"));
assertEquals("value", keyEvaluator.manipulate("getvalue(mapEntry)", "{mapEntry=value, mapEntry2=value2}"));
}
use of org.motechproject.tasks.service.util.KeyEvaluator 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.service.util.KeyEvaluator in project motech by motech.
the class TaskActionExecutor method execute.
/**
* 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
*/
@Transactional
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());
}
}
Aggregations