Skip to content

Instantly share code, notes, and snippets.

@andreluizf
Created June 20, 2014 12:28
Show Gist options
  • Save andreluizf/ca84b67da7a60ad2b5c9 to your computer and use it in GitHub Desktop.
Save andreluizf/ca84b67da7a60ad2b5c9 to your computer and use it in GitHub Desktop.
converter
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 "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment