KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > transform > AliasToBeanResultTransformer


1 /*
2  * Created on 27-Jan-2005
3  *
4  */

5 package org.hibernate.transform;
6
7 import java.util.List JavaDoc;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.property.ChainedPropertyAccessor;
11 import org.hibernate.property.PropertyAccessor;
12 import org.hibernate.property.PropertyAccessorFactory;
13 import org.hibernate.property.Setter;
14
15 /**
16  * Result transformer that allows to transform a result to
17  * a user specified class which will be populated via setter
18  * methods or fields matching the alias names.
19  *
20  * <pre>
21  * List resultWithAliasedBean = s.createCriteria(Enrolment.class)
22  * .createAlias("student", "st")
23  * .createAlias("course", "co")
24  * .setProjection( Projections.projectionList()
25  * .add( Projections.property("co.description"), "courseDescription" )
26  * )
27  * .setResultTransformer( new AliasToBeanResultTransformer(StudentDTO.class) )
28  * .list();
29  *
30  * StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0);
31  * </pre>
32  *
33  * @author max
34  *
35  */

36 public class AliasToBeanResultTransformer implements ResultTransformer {
37     
38     private final Class JavaDoc resultClass;
39     private Setter[] setters;
40     private PropertyAccessor propertyAccessor;
41     
42     public AliasToBeanResultTransformer(Class JavaDoc resultClass) {
43         if(resultClass==null) throw new IllegalArgumentException JavaDoc("resultClass cannot be null");
44         this.resultClass = resultClass;
45         propertyAccessor = new ChainedPropertyAccessor(new PropertyAccessor[] { PropertyAccessorFactory.getPropertyAccessor(resultClass,null), PropertyAccessorFactory.getPropertyAccessor("field")});
46     }
47
48     public Object JavaDoc transformTuple(Object JavaDoc[] tuple, String JavaDoc[] aliases) {
49         Object JavaDoc result;
50         
51         try {
52             if(setters==null) {
53                 setters = new Setter[aliases.length];
54                 for (int i = 0; i < aliases.length; i++) {
55                     String JavaDoc alias = aliases[i];
56                     if(alias != null) {
57                         setters[i] = propertyAccessor.getSetter(resultClass, alias);
58                     }
59                 }
60             }
61             result = resultClass.newInstance();
62             
63             for (int i = 0; i < aliases.length; i++) {
64                 if(setters[i]!=null) {
65                     setters[i].set(result, tuple[i], null);
66                 }
67             }
68         } catch (InstantiationException JavaDoc e) {
69             throw new HibernateException("Could not instantiate resultclass: " + resultClass.getName());
70         } catch (IllegalAccessException JavaDoc e) {
71             throw new HibernateException("Could not instantiate resultclass: " + resultClass.getName());
72         }
73         
74         return result;
75     }
76
77     public List JavaDoc transformList(List JavaDoc collection) {
78         return collection;
79     }
80
81 }
82
Popular Tags