3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.remote
.dto
.assembler
.converter
;
12 import java
.beans
.PropertyDescriptor
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.lang
.reflect
.Method
;
15 import java
.util
.Arrays
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
19 import org
.dozer
.ConfigurableCustomConverter
;
20 import org
.dozer
.Mapper
;
21 import org
.dozer
.MappingException
;
22 import org
.hibernate
.Hibernate
;
23 import org
.springframework
.beans
.BeanUtils
;
24 import org
.springframework
.beans
.BeansException
;
25 import org
.springframework
.context
.ApplicationContext
;
26 import org
.springframework
.context
.ApplicationContextAware
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 public class HibernateProxyNullSafeDeepConverter
implements ConfigurableCustomConverter
, ApplicationContextAware
{
32 private Mapper mapper
;
34 private ApplicationContext applicationContext
;
36 String parameter
= null;
38 private List
<String
> parameterList
;
41 * @see org.dozer.ConfigurableCustomConverter#setParameter(java.lang.String)
44 public void setParameter(String parameter
) {
45 this.parameter
= parameter
;
46 if(parameter
.indexOf('.') > -1){
47 this.parameterList
= Arrays
.asList(parameter
.split("."));
49 this.parameterList
= Arrays
.asList(new String
[]{parameter
});
54 protected Mapper
getMapper() {
56 this.setMapper((Mapper
)this.applicationContext
.getBean("dozerMapper", Mapper
.class));
61 public void setMapper(Mapper mapper
) {
65 public Object
convert(Object destination
, Object source
, Class
<?
> destClass
, Class
<?
> sourceClass
) {
66 if (source
== null || !Hibernate
.isInitialized(source
)) {
71 Object value
= invokeProperty(source
, sourceClass
, parameterList
.iterator());
72 if(value
== null || !Hibernate
.isInitialized(value
)) {
75 if(value
instanceof CdmBase
) {
76 return getMapper().map(value
, destClass
);
81 } catch (Exception e
) {
82 throw new MappingException("Converter HibernateProxyNullSafeDeepConverter used incorrectly. Arguments passed in were:"+ destination
+ " and " + source
+ " sourceClass " + sourceClass
+ " destClass " + destClass
, e
);
88 private Object
invokeProperty(Object source
, Class
<?
> sourceClass
, Iterator
<String
> parameterIterator
) throws IllegalAccessException
,
89 InvocationTargetException
{
91 String param
= parameterIterator
.next();
92 PropertyDescriptor propertyDescriptor
= BeanUtils
.getPropertyDescriptor(sourceClass
, param
);
93 Method method
= propertyDescriptor
.getReadMethod();
94 method
.setAccessible(true);
95 assert method
!= null;
96 value
= method
.invoke(source
);
97 if(parameterIterator
.hasNext()){
98 return invokeProperty(source
, sourceClass
, parameterIterator
);
104 public void setApplicationContext(ApplicationContext applicationContext
)
105 throws BeansException
{
106 this.applicationContext
= applicationContext
;