Created
August 24, 2012 18:52
-
-
Save carlosspohr/3454255 to your computer and use it in GitHub Desktop.
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
| package br.inf.bcamp.generics.utils; | |
| import java.lang.reflect.Field; | |
| import java.sql.Timestamp; | |
| import java.util.ArrayList; | |
| import java.util.Calendar; | |
| import java.util.Collection; | |
| import java.util.Date; | |
| import java.util.HashMap; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Map.Entry; | |
| import net.vidageek.mirror.dsl.Mirror; | |
| import org.hibernate.transform.ResultTransformer; | |
| import br.inf.bcamp.generics.beans.interfaces.AbstractEntity; | |
| public class ResultGenerico implements ResultTransformer { | |
| private static final long serialVersionUID = 4796070670074230561L; | |
| private Class<? extends AbstractEntity> clazz; | |
| public ResultGenerico(Class<? extends AbstractEntity> clazz) { | |
| super(); | |
| this.clazz = clazz; | |
| } | |
| @SuppressWarnings("unchecked") | |
| @Override | |
| public List<? extends AbstractEntity> transformList(List list) { | |
| Collection<AbstractEntity> result = new ArrayList<AbstractEntity>(0); | |
| if(list == null || list.isEmpty()){ | |
| return (List<? extends AbstractEntity>) result; | |
| } | |
| Mirror m = new Mirror(); | |
| for (Object object : list) { | |
| Map<Object, Object> map = (Map<Object, Object>) object; | |
| AbstractEntity instancia = m.on(clazz).invoke().constructor().withoutArgs(); | |
| Iterator<Entry<Object, Object>> iterator = map.entrySet().iterator(); | |
| while (iterator.hasNext()) { | |
| Entry<Object, Object> entry = iterator.next(); | |
| Field target = m.on(clazz).reflect().field(entry.getKey().toString()); | |
| Object value = entry.getValue(); | |
| if(target.getType().equals(Calendar.class)){ | |
| if(value instanceof Timestamp){ | |
| Calendar cal = Calendar.getInstance(); | |
| cal.setTime(new Date(((Timestamp) value).getTime())); | |
| value = cal; | |
| } | |
| if(value instanceof java.sql.Date){ | |
| Calendar cal = Calendar.getInstance(); | |
| cal.setTime(new Date(((java.sql.Date) value).getTime())); | |
| value = cal; | |
| } | |
| } | |
| m.on(instancia).set().field(entry.getKey().toString()).withValue(value); | |
| } | |
| result.add(instancia); | |
| } | |
| return (List<? extends AbstractEntity>) result; | |
| } | |
| @Override | |
| public Object transformTuple(Object[] tuple, String[] aliases) { | |
| Map result = new HashMap(tuple.length); | |
| for (int i = 0; i < tuple.length; i++) { | |
| String alias = aliases[i]; | |
| if (alias != null) { | |
| result.put(alias, tuple[i]); | |
| } | |
| } | |
| return result; | |
| } | |
| private class DateConvertAdapter { | |
| private final Mirror mirror; | |
| public DateConvertAdapter(Mirror mirror) { | |
| super(); | |
| this.mirror = mirror; | |
| } | |
| public Object convert(Object dataValue, Field target){ | |
| if(target.getType().getSimpleName().equals(dataValue.getClass().getSimpleName())){ | |
| } | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment