package com.arrebol.taxiservicios.convert;

import com.arrebol.taxiservicios.beans.stocktaking.invoice.InvoiceAddBean;
import com.arrebol.taxiservicios.model.catalog.Product;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Carlos Janitzio Zavala Lopez
 */
@FacesConverter("productConverter")
public class ProductConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
        ValueExpression vex = facesContext.getApplication()
                .getExpressionFactory()
                .createValueExpression(facesContext.getELContext(), "#{invoiceAddBean}", InvoiceAddBean.class);

        InvoiceAddBean bean = (InvoiceAddBean)vex.getValue(facesContext.getELContext());
        
        return bean.findProduct(submittedValue);
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        Product person = (Product) value;
        return person.getId();
    }
}
