/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package br.usp.nds.remote.persistencia; import java.beans.PropertyDescriptor; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.persistence.EntityManager; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.MultivaluedMap; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlTransient; import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.ConvertUtilsBean; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.PropertyUtilsBean; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Example; import br.usp.nds.remote.conversao.BigDecimalConverter; import br.usp.nds.remote.conversao.BooleanConverter; import br.usp.nds.remote.conversao.ConverterConfig; import br.usp.nds.remote.conversao.IntegerConverter; import br.usp.nds.remote.conversao.LongConverter; /** * * @author sussumu * @author tiago */ public abstract class Entidade implements Serializable { protected EntityManager em; protected Object resposta; protected HttpServletRequest request; protected String mediaType; public Entidade injetar(EntityManager em) { this.em = em; return this; } public static Entidade fromMultivaluedMap(String entidade, MultivaluedMap params, EntityManager em) { Entidade result = null; Class clazz = null; try { clazz = Class.forName("br.usp.nds.remote.persistencia." + entidade); } catch (Exception e) { e.printStackTrace(); } if (clazz != null) { try { result = (Entidade) clazz.newInstance(); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } } if (result != null) { ConverterConfig.getInstance(); BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtilsBean() { @Override public Object convert(String value, Class clazz) { if (clazz.isEnum()) { return value != null && !value.isEmpty() ? Enum .valueOf(clazz, value) : null; } else if (clazz.equals(Integer.class)) { return new IntegerConverter().convert(clazz, value); } else if (clazz.equals(Long.class)) { return new LongConverter().convert(clazz, value); } else if (clazz.equals(BigDecimal.class)) { return new BigDecimalConverter().convert(clazz, value); } else if (clazz.equals(Boolean.class)) { return new BooleanConverter().convert(clazz, value); } else { return super.convert(value, clazz); } } }, new PropertyUtilsBean()); for (String key : params.keySet()) { if (key.endsWith("!classe")) { try { beanUtils.setProperty(result, key .replaceAll("[_]", ".").replace("!classe", ""), Class.forName( "clickeat.domain." + params.getFirst(key)) .newInstance()); } catch (Exception e) { e.printStackTrace(); } } } for (String key : params.keySet()) { if (!key.endsWith("!classe") && !key.equals("whitelist") && !key.equals("blacklist")) { try { String fieldName = key.replaceAll("[_]", "."); instantiateNestedProperties(result, fieldName); beanUtils.setProperty(result, fieldName, params .getFirst(key)); } catch (Exception e) { e.printStackTrace(); } } } } return result; } private static void instantiateNestedProperties(Object obj, String fieldName) { try { String[] fieldNames = fieldName.split("\\."); if (fieldNames.length > 1) { StringBuffer nestedProperty = new StringBuffer(); for (int i = 0; i < fieldNames.length - 1; i++) { String fn = fieldNames[i]; if (i != 0) { nestedProperty.append("."); } nestedProperty.append(fn); Object value = PropertyUtils.getProperty(obj, nestedProperty.toString()); if (value == null) { PropertyDescriptor propertyDescriptor = PropertyUtils .getPropertyDescriptor(obj, nestedProperty .toString()); Class propertyType = propertyDescriptor .getPropertyType(); Object newInstance; if (propertyType.equals(List.class)) { newInstance = new ArrayList(); } else if (propertyType.equals(Set.class)) { newInstance = new HashSet(); } else if (propertyType.equals(Map.class)) { newInstance = new HashMap(); } else { newInstance = propertyType.newInstance(); } PropertyUtils.setProperty(obj, nestedProperty .toString(), newInstance); } } } } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } } @XmlTransient public String getMediaType() { return mediaType; } public void setMediaType(String mediaType) { this.mediaType = mediaType; } @XmlTransient public Object getResposta() { return resposta; } public void setResposta(Object resposta) { this.resposta = resposta; } @XmlTransient public HttpServletRequest getRequest() { return request; } public Entidade setRequest(HttpServletRequest request) { this.request = request; return this; } }