/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.triz.trizservice.bean.converter;

import com.triz.trizservice.modeles.Etablissement;
import com.triz.trizservice.service.TransactionService;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author FORCE TECH
 */
@FacesConverter("etablissementConverter")
@Component
public class EtablissementConverter implements Converter{
    @Autowired
    protected transient TransactionService service;

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        try {                
                return service.getEtablissementById(value);
            } catch (NumberFormatException e) 
            {
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Etablissement n'existe pas.");
            }
       
        return null;
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if (object != null) {
            return String.valueOf(((Etablissement) object).getIdEtabStock());
        } else {
            return null;
        }
    }
    
}
