public void executeAction(Action action, ExecutionContext executionContext) {
if (action instanceof CreateTimerAction) {
CreateTimerAction createTimerAction = (CreateTimerAction) action;
String timerName = createTimerAction.getTimerName();
TimerInstance timer = new TimerInstance();
long delay = BUSINESS_CALENDAR.add(new Date(0),
new Duration(createTimerAction.getDueDate())).getTime();
timer.setDelay(delay);
if (createTimerAction.getRepeat() != null) {
long period = BUSINESS_CALENDAR.add(new Date(0),
new Duration(createTimerAction.getRepeat())).getTime();
timer.setPeriod(period);
}
if (timerActions.isEmpty()) {
addTimerListener();
}
getProcessInstance().getWorkingMemory().getTimerManager()
.registerTimer(timer, getProcessInstance());
timerActions.put(timer.getId(), createTimerAction.getTimerAction());
List<TimerInstance> timerList = timers.get(timerName);
if (timerList == null) {
timerList = new ArrayList<TimerInstance>();
timers.put(timerName, timerList);
}
timerList.add(timer);
} else if (action instanceof CancelTimerAction) {
String timerName = ((CancelTimerAction) action).getTimerName();
List<TimerInstance> timerList = timers.get(timerName);
if (timerList != null) {
for (TimerInstance timer: timerList) {
timerActions.remove(timer.getId());
getProcessInstance().getWorkingMemory().getTimerManager()
.cancelTimer(timer.getId());
}
timers.remove(timerName);
if (timerActions.isEmpty()) {
removeTimerListener();
}