/**
  * Formats and sets name on the period of each result.
  *
  * @param results the collection of validation results.
  * @param format the i18n format.
  */
 private void formatPeriods(Collection<ValidationResult> results, I18nFormat format) {
   if (format != null) {
     for (ValidationResult result : results) {
       if (result != null && result.getPeriod() != null) {
         result.getPeriod().setName(format.formatPeriod(result.getPeriod()));
       }
     }
   }
 }
  /**
   * Generate and send an alert message containing a list of validation results to a set of users.
   *
   * @param results results to put in this message
   * @param users users to receive these results
   * @param scheduledRunStart date/time when the scheduled run started
   */
  private void sendAlertmessage(
      SortedSet<ValidationResult> results, Set<User> users, Date scheduledRunStart) {
    StringBuilder builder = new StringBuilder();

    SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    Map<String, Integer> importanceCountMap = countResultsByImportanceType(results);

    String subject =
        "Alerts as of "
            + dateTimeFormatter.format(scheduledRunStart)
            + ": High "
            + (importanceCountMap.get("high") == null ? 0 : importanceCountMap.get("high"))
            + ", Medium "
            + (importanceCountMap.get("medium") == null ? 0 : importanceCountMap.get("medium"))
            + ", Low "
            + (importanceCountMap.get("low") == null ? 0 : importanceCountMap.get("low"));

    // TODO use velocity template for message

    for (ValidationResult result : results) {
      ValidationRule rule = result.getValidationRule();

      builder
          .append(result.getOrgUnit().getName())
          .append(" ")
          .append(result.getPeriod().getName())
          .append(
              result.getAttributeOptionCombo().isDefault()
                  ? ""
                  : " " + result.getAttributeOptionCombo().getName())
          .append(LN)
          .append(rule.getName())
          .append(" (")
          .append(rule.getImportance())
          .append(") ")
          .append(LN)
          .append(rule.getLeftSide().getDescription())
          .append(": ")
          .append(result.getLeftsideValue())
          .append(LN)
          .append(rule.getRightSide().getDescription())
          .append(": ")
          .append(result.getRightsideValue())
          .append(LN)
          .append(LN);
    }

    log.info("Alerting users: " + users.size() + ", subject: " + subject);

    messageService.sendMessage(subject, builder.toString(), null, users);
  }