/**
   * Lazy initialization of the status map. For each ambulance that is currently being displayed
   * (the current CRUD page, represented by the attribute entities), checks if there's an active
   * deactivation and stores the appropriate status (active or inactive, properly internationalized)
   * to the status map.
   */
  private void initCurrentStatuses() {
    logger.log(
        Level.INFO, "Initializing maintenance status map for {0} entities.", entities.size());
    currentStatuses = new TreeMap<Long, String>();
    for (Ambulance ambulance : entities) {
      // Retrieves the current deactivation (if any) for this ambulance.
      AmbulanceDeactivation deactivation = null;
      try {
        deactivation = getCrudService().retrieveActiveDeactivation(ambulance);
      } catch (AmbulanceHasMultipleCurrentDeactivationsException e) {
        logger.log(
            Level.SEVERE,
            "There has been an error while retrieving the active deactivation of ambulance # "
                + ambulance.getNumber()
                + " (id "
                + ambulance.getId()
                + ")",
            e);
        addGlobalI18nMessage(
            "msgs",
            FacesMessage.SEVERITY_ERROR,
            "manageAmbulances.error.deactivationError",
            ambulance.getNumber());
      }

      // Displays the status depending if the ambulance is active or not.
      currentStatuses.put(
          ambulance.getId(),
          getI18nMessage(
              "msgs",
              (deactivation == null)
                  ? "manageAmbulances.text.status.active"
                  : "manageAmbulances.text.status.inactive"));
    }
  }
  /**
   * Lazy initialization of the assignment map. For each ambulance that is currently being displayed
   * (the current CRUD page, represented by the attribute entities), checks if there's an active
   * assignment and stores the appropriate information (driver name, or "unassigned", properly
   * internationalized) to the assignment map.
   */
  private void initCurrentAssignments() {
    logger.log(Level.INFO, "Initializing current drivers map for {0} entities.", entities.size());
    currentAssignments = new TreeMap<Long, String>();
    for (Ambulance ambulance : entities) {
      // Retrieves the current assignment (if any) for this ambulance.
      DriverAssignment assignment = null;
      try {
        assignment = getCrudService().retrieveActiveAssignment(ambulance);
      } catch (AmbulanceHasMultipleCurrentDriverAssignmentsException e) {
        logger.log(
            Level.SEVERE,
            "There has been an error while retrieving the active assignment of ambulance # "
                + ambulance.getNumber()
                + " (id "
                + ambulance.getId()
                + ")",
            e);
        addGlobalI18nMessage(
            "msgs",
            FacesMessage.SEVERITY_ERROR,
            "manageAmbulances.error.assignmentError",
            ambulance.getNumber());
      }

      // Displays the status depending if the ambulance is active or not.
      currentAssignments.put(
          ambulance.getId(),
          getI18nMessage(
              "msgs",
              (assignment == null)
                  ? "manageAmbulances.text.currentDriver.none"
                  : assignment.getDriver().getName()));
    }
  }
  /** @see br.com.engenhodesoftware.util.ejb3.controller.CrudAction#listTrash() */
  @Override
  protected String listTrash() {
    // List the usernames of the deleted ambulances.
    StringBuilder usernames = new StringBuilder();
    for (Ambulance entity : trashCan)
      usernames
          .append("# ")
          .append(entity.getNumber())
          .append(" (")
          .append(entity.getLicensePlate())
          .append("), ");

    // Removes the final comma and returns the string.
    int length = usernames.length();
    if (length > 0) usernames.delete(length - 2, length);

    logger.log(Level.INFO, "Listing the ambulances in the trash can: {0}.", usernames.toString());
    return usernames.toString();
  }