|
|
@@ -1,164 +0,0 @@
|
|
|
-package commons.utility.dao;
|
|
|
-
|
|
|
-import java.lang.reflect.Field;
|
|
|
-import java.lang.reflect.ParameterizedType;
|
|
|
-import java.lang.reflect.Type;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import org.hibernate.query.TupleTransformer;
|
|
|
-
|
|
|
-import commons.exception.ZSystemException;
|
|
|
-
|
|
|
-/**
|
|
|
- * Result transformer che permette di trasformare un resultSet in una
|
|
|
- * classe specifica definita dall'utente popolata via setter.
|
|
|
- * Per popolare la classe vengono utilizzati gli alias. Viene supportata la dotNotation.
|
|
|
- */
|
|
|
-public class AliasToBeanResultTransformerEnanced implements TupleTransformer {
|
|
|
-
|
|
|
- private static final String THIS = "this";
|
|
|
-
|
|
|
- private static final long serialVersionUID = 1L;
|
|
|
-
|
|
|
- private final Class resultClass;
|
|
|
-
|
|
|
- public AliasToBeanResultTransformerEnanced(Class resultClass) {
|
|
|
- if(resultClass==null) throw new IllegalArgumentException("resultClass cannot be null");
|
|
|
- this.resultClass = resultClass;
|
|
|
- }
|
|
|
-
|
|
|
- public Object transformTuple(Object[] tuple, String[] aliases) {
|
|
|
- Map<String,Tuplizer> firstLevelObject = new HashMap<String,Tuplizer>();
|
|
|
-
|
|
|
- for (int i = 0; i < aliases.length; i++)
|
|
|
- {
|
|
|
- if(tuple[i] == null || aliases[i] == null) continue;
|
|
|
- String alias = aliases[i];
|
|
|
- String key = THIS;
|
|
|
- if(alias != null && alias.indexOf(".") != -1) {
|
|
|
- key = alias.substring(0,alias.indexOf("."));
|
|
|
- alias = alias.substring(alias.indexOf(".") + 1);
|
|
|
- }
|
|
|
-
|
|
|
- Tuplizer myList = null;
|
|
|
- if(firstLevelObject.containsKey(key)) {
|
|
|
- myList = firstLevelObject.get(key);
|
|
|
- } else {
|
|
|
- myList = new Tuplizer();
|
|
|
- firstLevelObject.put(key,myList);
|
|
|
- }
|
|
|
- myList.addTupla(tuple[i]);
|
|
|
- myList.addAlias(alias);
|
|
|
- }
|
|
|
-
|
|
|
- Tuplizer tuplizer = firstLevelObject.remove(THIS);
|
|
|
- Object mainObj = buildBean(this.resultClass, tuplizer.getTuple(), tuplizer.getAliases());
|
|
|
-
|
|
|
- Iterator<String> keys = firstLevelObject.keySet().iterator();
|
|
|
- while (keys.hasNext())
|
|
|
- {
|
|
|
- String property = keys.next();
|
|
|
- Field declaredField = null;
|
|
|
- try
|
|
|
- {
|
|
|
- try {
|
|
|
- declaredField = resultClass.getDeclaredField(property);
|
|
|
- } catch (NoSuchFieldException e) {
|
|
|
- declaredField = resultClass.getSuperclass().getDeclaredField(property);
|
|
|
- }
|
|
|
-
|
|
|
- declaredField.setAccessible(true);
|
|
|
-
|
|
|
- AliasToBeanResultTransformerEnanced beanTransPlus;
|
|
|
- if (declaredField.getGenericType() instanceof ParameterizedType)
|
|
|
- {
|
|
|
- Type[] tipi = ((ParameterizedType) declaredField.getGenericType()).getActualTypeArguments();
|
|
|
- beanTransPlus = new AliasToBeanResultTransformerEnanced((Class<?>) tipi[0]);
|
|
|
- Tuplizer t = firstLevelObject.get(property);
|
|
|
- Object propertyObj = beanTransPlus.transformTuple(t.getTuple(),t.getAliases());
|
|
|
- declaredField.set(mainObj,propertyObj);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- beanTransPlus = new AliasToBeanResultTransformerEnanced(declaredField.getType());
|
|
|
- Tuplizer t = firstLevelObject.get(property);
|
|
|
- Object propertyObj = beanTransPlus.transformTuple(t.getTuple(),t.getAliases());
|
|
|
- declaredField.set(mainObj,propertyObj);
|
|
|
- }
|
|
|
- }
|
|
|
- catch (SecurityException e)
|
|
|
- {
|
|
|
- throw new ZSystemException(e);
|
|
|
- }
|
|
|
- catch (NoSuchFieldException e)
|
|
|
- {
|
|
|
- throw new ZSystemException(e);
|
|
|
- }
|
|
|
- catch (IllegalArgumentException e)
|
|
|
- {
|
|
|
- throw new ZSystemException(e);
|
|
|
- }
|
|
|
- catch (IllegalAccessException e)
|
|
|
- {
|
|
|
- throw new ZSystemException(e);
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- declaredField.setAccessible(false);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return mainObj;
|
|
|
- }
|
|
|
-
|
|
|
- private Object buildBean(Class clazz, Object[] tuple, String[] aliases) {
|
|
|
- try {
|
|
|
- Object bean = clazz.getDeclaredConstructor().newInstance();
|
|
|
- for (int i = 0; i < aliases.length; i++) {
|
|
|
- if (tuple[i] == null || aliases[i] == null) continue;
|
|
|
- try {
|
|
|
- Field field = clazz.getDeclaredField(aliases[i]);
|
|
|
- field.setAccessible(true);
|
|
|
- field.set(bean, tuple[i]);
|
|
|
- } catch (NoSuchFieldException e) {
|
|
|
- // ignore
|
|
|
- }
|
|
|
- }
|
|
|
- return bean;
|
|
|
- } catch (Exception e) {
|
|
|
- throw new ZSystemException(e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- class Tuplizer {
|
|
|
-
|
|
|
- List<Object> tuple = null;
|
|
|
- List<String> aliases = null;
|
|
|
-
|
|
|
- public Tuplizer() {
|
|
|
- tuple = new ArrayList<Object>();
|
|
|
- aliases = new ArrayList<String>();
|
|
|
- }
|
|
|
-
|
|
|
- public void addTupla(Object obj) {
|
|
|
- tuple.add(obj);
|
|
|
- }
|
|
|
-
|
|
|
- public void addAlias(String alias) {
|
|
|
- aliases.add(alias);
|
|
|
- }
|
|
|
-
|
|
|
- public Object[] getTuple() {
|
|
|
- return tuple.toArray();
|
|
|
- }
|
|
|
-
|
|
|
- public String[] getAliases() {
|
|
|
- return aliases.toArray(new String[aliases.size()]);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|