/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.taxiservicios.convert;

import com.arrebol.taxiservicios.model.view.report.SavingDriver;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@FacesConverter("savingDriverConverter")
public class SavingDriverConverter implements Converter<SavingDriver> {

    @Override
    public SavingDriver getAsObject(FacesContext fc, UIComponent uic, String id) {
        try {
            return new SavingDriver(id);
        } catch (Exception e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error en búsqueda", "No se pudo obtener los datos del chofer búscado."));
        }
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, SavingDriver driver) {
        if (driver != null) {
            return driver.getId();
        } else {
            return null;
        }
    }
}
