@Override
  public void scheduledRun() {
    log.info("Starting scheduled monitoring task");

    // Find all the rules belonging to groups that will send alerts to user roles.

    Set<ValidationRule> rules = getAlertRules();

    Collection<OrganisationUnit> sources = organisationUnitService.getAllOrganisationUnits();

    Set<Period> periods = getAlertPeriodsFromRules(rules);

    Date lastScheduledRun =
        (Date) systemSettingManager.getSystemSetting(SystemSettingManager.KEY_LAST_MONITORING_RUN);

    // Any database changes after this moment will contribute to the next run.

    Date thisRun = new Date();

    log.info(
        "Scheduled monitoring run sources: "
            + sources.size()
            + ", periods: "
            + periods.size()
            + ", rules:"
            + rules.size()
            + ", last run: "
            + (lastScheduledRun == null ? "[none]" : lastScheduledRun));

    Collection<ValidationResult> results =
        Validator.validate(
            sources,
            periods,
            rules,
            null,
            lastScheduledRun,
            constantService,
            expressionService,
            periodService,
            dataValueService,
            dataElementCategoryService,
            userService,
            currentUserService);

    log.info("Validation run result count: " + results.size());

    if (!results.isEmpty()) {
      postAlerts(results, thisRun);
    }

    log.info("Posted alerts, monitoring task done");

    systemSettingManager.saveSystemSetting(SystemSettingManager.KEY_LAST_MONITORING_RUN, thisRun);
  }
  public String execute() {
    TaskId taskId =
        new TaskId(TaskCategory.SENDING_REMINDER_MESSAGE, currentUserService.getCurrentUser());
    notifier.clear(taskId);
    sendMessageScheduled.setTaskId(taskId);

    systemSettingManager.saveSystemSetting(KEY_TIME_FOR_SENDING_MESSAGE, timeSendingMessage);

    if (execute) {
      schedulingManager.executeTasks();
    } else {
      if (Scheduler.STATUS_RUNNING.equals(schedulingManager.getTaskStatus())) {
        schedulingManager.stopTasks();
      } else {
        Map<String, String> keyCronMap = new HashMap<String, String>();
        String time =
            (String)
                systemSettingManager.getSystemSetting(
                    KEY_TIME_FOR_SENDING_MESSAGE, DEFAULT_TIME_FOR_SENDING_MESSAGE);

        // Schedule for sending messages
        String[] infor = time.split(":");
        String hour = infor[0].trim();
        String minute = infor[1].trim();

        if (hour.trim().equals("00")) {
          hour = "0";
        }
        if (minute.trim().equals("00")) {
          minute = "0";
        }
        String cron = "0 " + Integer.parseInt(minute) + " " + Integer.parseInt(hour) + " ? * *";

        keyCronMap.put(KEY_SEND_MESSAGE_SCHEDULED_TASKS, cron);
        keyCronMap.put(KEY_SCHEDULE_MESSAGE_TASKS, "0 0 0 * * ?");

        schedulingManager.scheduleTasks(keyCronMap);
      }
    }

    status = schedulingManager.getTaskStatus();
    running = Scheduler.STATUS_RUNNING.equals(status);

    return SUCCESS;
  }