Doubt Converter and CDI

1

I am testing a few hours trying to understand why this code here is not working and I am getting a NullPointerExcepetion . Can anyone notice something wrong?

    import javax.enterprise.context.RequestScoped;
    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.inject.Inject;
    import javax.inject.Named;

    import modelo.Condomino;
    import repositorio.Condominos;

    @Named
    @RequestScoped
    public class CondominoConverter implements Converter{

    @Inject
    private Condominos condominos;

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {

    if(!value.equals(null)){
        Condomino condomino = condominos.getCondominoPorCPF(value);

        if (condomino == null)
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Não existe","Não existe"));

        return  condomino;
    }
    return null;

}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {

    if (null == value || !(value instanceof Condomino))
        return null;
    Condomino condomino = (Condomino) value;

    return condomino.getCPF()==null?null:condomino.getCPF().toString();

}

}

    
asked by anonymous 07.11.2017 / 21:22

1 answer

1

I was able to solve my problem in the following way knowing that I'm using jsf version 2.2.8-02, I thought the solution in converting using @Named would work but it did not work, so I followed a tip I saw in a class of Algaworks leaving my convert as follows:

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;
import modelo.Condomino;
import repositorio.Condominos;
import util.CDIServiceLocator;

@FacesConverter(forClass=Condomino.class)
public class CondominoConverter implements Converter{       

    private Condominos condominos;

    public CondominoConverter() {
        condominos =  CDIServiceLocator.getBean(Condominos.class);
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if(!value.equals(null)){
            Condomino condomino = condominos.getCondominoPorCPF(value);

            if (condomino == null)
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Não existe","Não existe"));

            return  condomino;
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (null == value || !(value instanceof Condomino))
            return null;
        Condomino condomino = (Condomino) value;

        return condomino.getCPF()==null?null:condomino.getCPF().toString();
    }


}

The class CDIServiceLocator is instantiating the object directly in its constructor, since by dependency injection it was calling twice and losing the reference, always returning null.

import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class CDIServiceLocator {

    private static BeanManager getBeanManager() {
        try {
            InitialContext initialContext = new InitialContext();
            return (BeanManager) initialContext.lookup("java:comp/env/BeanManager");
        } catch (NamingException e) {
            throw new RuntimeException("Não pôde encontrar BeanManager no JNDI.");
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        BeanManager bm = getBeanManager();
        Set<Bean<?>> beans = (Set<Bean<?>>) bm.getBeans(clazz);

        if (beans == null || beans.isEmpty()) {
            return null;
        }

        Bean<T> bean = (Bean<T>) beans.iterator().next();

        CreationalContext<T> ctx = bm.createCreationalContext(bean);
        T o = (T) bm.getReference(bean, clazz, ctx);

        return o;
    }

}
    
08.11.2017 / 14:53