Created
May 11, 2013 20:18
-
-
Save thaniaclair/5561286 to your computer and use it in GitHub Desktop.
Conversor de entidade.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.Serializable; | |
| import javax.faces.application.FacesMessage; | |
| import javax.faces.bean.ManagedBean; | |
| import javax.faces.bean.RequestScoped; | |
| import javax.faces.component.UIComponent; | |
| import javax.faces.context.FacesContext; | |
| import javax.faces.convert.Converter; | |
| import javax.faces.convert.ConverterException; | |
| import javax.persistence.EntityManager; | |
| import javax.persistence.PersistenceContext; | |
| import org.apache.log4j.Logger; | |
| /** | |
| * <P> | |
| * Conversor genérico de CLASS + ID para {@link BaseEntity}. | |
| * </P> | |
| * | |
| * @author thania | |
| * @since | |
| */ | |
| @ManagedBean(name = "entityConverterMB") | |
| @RequestScoped | |
| public class EntityConverter implements Converter, Serializable { | |
| private static final long serialVersionUID = -303138937729541362L; | |
| private transient Logger _logger = Logger.getLogger(EntityConverter.class); | |
| private static final String SEPARATOR = "_"; | |
| @PersistenceContext | |
| private EntityManager entityManager; | |
| /** | |
| * @inheritDoc | |
| */ | |
| @Override | |
| public Object getAsObject(FacesContext fContext, UIComponent uiComponent, | |
| String values) { | |
| // Valida o valor recebido conforme formato obrigatório: <CLASS>_<ID>. | |
| // Caso venha em outro formato, é porque o usuário escolheu um item de | |
| // valor inválido. | |
| if (!values.contains(SEPARATOR)) { | |
| FacesMessage message = new FacesMessage(JSFUtil.getMessage("validacao_campoObrigatorio")); | |
| throw new ConverterException(message); | |
| } | |
| // Faz a conversão conforme o formato: <CLASS>_<ID>. | |
| String[] value = values.split(SEPARATOR); | |
| Long entityID = Long.parseLong(value[1]); | |
| String entityClassname = value[0]; | |
| Class<?> entityClass = null; | |
| try { | |
| entityClass = Class.forName(entityClassname); | |
| } catch (ClassNotFoundException e) { | |
| _logger.error("Problemas para converter valor [" + values + "] para entidade."); | |
| } | |
| // Recupera a entidade na base de dados através da classe e identificador. | |
| return entityManager.find(entityClass, entityID); | |
| } | |
| /** | |
| * @inheritDoc | |
| */ | |
| @Override | |
| public String getAsString(FacesContext fContext, UIComponent uiComponent, | |
| Object obj) { | |
| if (!(obj instanceof BaseEntity)) { | |
| return obj.toString(); | |
| } else { | |
| BaseEntity baseEntity = (BaseEntity) obj; | |
| return obj.getClass().getName() + SEPARATOR + baseEntity.getId(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment