/*
 * 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.ServiceGroupe;
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 Asma
 */
@FacesConverter("groupeConverter")
@Component
public class GroupeConverter implements Converter{
    @Autowired
    protected transient TransactionService service;

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        try {
            System.out.println("service "+ service);
            return service.getGroupsById(value);
        } catch (NumberFormatException e) {
            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Entreprise n'existe pas.");
        }

        return null;
    }

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