Skip to content

Instantly share code, notes, and snippets.

@andreluizf
Created June 20, 2014 12:28
Show Gist options
  • Select an option

  • Save andreluizf/ca84b67da7a60ad2b5c9 to your computer and use it in GitHub Desktop.

Select an option

Save andreluizf/ca84b67da7a60ad2b5c9 to your computer and use it in GitHub Desktop.

Revisions

  1. andreluizf created this gist Jun 20, 2014.
    47 changes: 47 additions & 0 deletions produtoConverter.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    import br.com.xkey.cax.model.Produto;
    import br.com.xkey.cax.repository.produto.ProdutoRepository;
    import javax.ejb.EJB;
    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;

    @FacesConverter(value = "produtoConverter")
    public class ConverterProduto implements Converter {

    @EJB(beanName = "ProdutoRepositoryImpl")
    ProdutoRepository pr;

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
    if (submittedValue.trim().equals("")) {
    return null;
    } else {
    try {
    Produto produto;
    produto = pr.findById(Long.parseLong(submittedValue));
    return produto;
    } catch (NumberFormatException exception) {
    throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }

    return null;
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
    if (value == null || value.equals("")) {
    return null;
    } else {
    return String.valueOf(((Produto) value).getId());
    // return "";

    }
    }

    }