/**
  * Called from the deactivation page's form, sends the data on the end of the current deactivation
  * or the beginning of a new one and changes the status of the ambulance.
  *
  * <p>This method executes a scenario in the service class.
  */
 public void changeStatus() {
   // If the ambulance is active, create a deactivation. Otherwise, end the current deactivation.
   if (active) {
     logger.log(
         Level.INFO,
         "Deactivating ambulance # {0} (id {1}). Reason: {2}.",
         new Object[] {
           selectedEntity.getNumber(), selectedEntity.getId(), currentDeactivation.getReason()
         });
     currentDeactivation =
         getCrudService().deactivateAmbulance(selectedEntity, currentDeactivation.getReason());
     active = false;
     addGlobalI18nMessage(
         "msgs", FacesMessage.SEVERITY_INFO, "manageAmbulances.info.deactivationStarted");
   } else
     try {
       logger.log(
           Level.INFO,
           "Ending current deactivation for ambulance # {0} (id {1}).",
           new Object[] {selectedEntity.getNumber(), selectedEntity.getId()});
       getCrudService().endCurrentDeactivation(selectedEntity);
       currentDeactivation = null;
       active = true;
       addGlobalI18nMessage(
           "msgs", FacesMessage.SEVERITY_INFO, "manageAmbulances.info.deactivationEnded");
     } catch (AmbulanceHasMultipleCurrentDeactivationsException e) {
       logger.log(
           Level.SEVERE,
           "There has been an error while changing the status of ambulance # "
               + selectedEntity.getNumber()
               + " (id "
               + selectedEntity.getId()
               + ")",
           e);
       addGlobalI18nMessage(
           "msgs",
           FacesMessage.SEVERITY_ERROR,
           "manageAmbulances.error.deactivationError",
           selectedEntity.getNumber());
     } catch (AmbulanceHasNoCurrentDeactivationException e) {
       logger.log(
           Level.SEVERE,
           "There has been an error while changing the status of ambulance # "
               + selectedEntity.getNumber()
               + " (id "
               + selectedEntity.getId()
               + ")",
           e);
       addGlobalI18nMessage(
           "msgs",
           FacesMessage.SEVERITY_ERROR,
           "manageAmbulances.error.deactivationError",
           selectedEntity.getNumber());
     }
 }
  /**
   * Called from a link in the ambulance table, selects an ambulance (through its id) and view the
   * current (active) deactivation, if any. Redirects to the deactivations page, which allows the
   * administrator to end the current deactivation or start one, if the ambulance is not currently
   * deactivated.
   *
   * <p>This method executes a scenario in the service class.
   *
   * @param id The id of the selected ambulance.
   * @return The path to the deactivations page.
   */
  public String manageDeactivations(Long id) {
    // Retrieve the selected ambulance.
    selectedEntity = getCrudService().retrieve(id);
    logger.log(
        Level.INFO,
        "Selected ambulance # {0} (id {1}). Opening deactivations page.",
        new Object[] {selectedEntity.getNumber(), id});

    // Clear the deactivation history (possibly of another ambulance).
    allDeactivations = null;

    // Retireve the current deactivation (if any) for this ambulance.
    try {
      currentDeactivation = getCrudService().retrieveActiveDeactivation(selectedEntity);
    } catch (AmbulanceHasMultipleCurrentDeactivationsException e) {
      logger.log(
          Level.SEVERE,
          "There has been an error while retrieving the active deactivation of ambulance # "
              + selectedEntity.getNumber()
              + " (id "
              + selectedEntity.getId()
              + ")",
          e);
      addGlobalI18nMessage(
          "msgs",
          FacesMessage.SEVERITY_ERROR,
          "manageAmbulances.error.deactivationError",
          selectedEntity.getNumber());
      return null;
    }

    // If there is no current deactivation for the ambulance, create a new one for the new
    // deactivation form.
    active = currentDeactivation == null;
    if (active) {
      currentDeactivation = new AmbulanceDeactivation();
      currentDeactivation.setBeginDate(new Date(System.currentTimeMillis()));
    }

    return DEACTIVATIONS_PAGE;
  }