Project

General

Profile

Download (3.39 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.dto.assembler.converter;
10

    
11
import java.beans.PropertyDescriptor;
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.util.Arrays;
15
import java.util.Iterator;
16
import java.util.List;
17

    
18
import org.dozer.ConfigurableCustomConverter;
19
import org.dozer.Mapper;
20
import org.dozer.MappingException;
21
import org.hibernate.Hibernate;
22
import org.springframework.beans.BeanUtils;
23
import org.springframework.beans.BeansException;
24
import org.springframework.context.ApplicationContext;
25
import org.springframework.context.ApplicationContextAware;
26

    
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28

    
29
public class HibernateProxyNullSafeDeepConverter implements ConfigurableCustomConverter, ApplicationContextAware {
30
	
31
	private Mapper mapper;
32
	
33
	private ApplicationContext applicationContext;
34
	
35
	String parameter = null;
36

    
37
	private List<String> parameterList;
38
	
39
	/* (non-Javadoc)
40
	 * @see org.dozer.ConfigurableCustomConverter#setParameter(java.lang.String)
41
	 */
42
	@Override
43
	public void setParameter(String parameter) {
44
		this.parameter = parameter;
45
		if(parameter.indexOf('.') > -1){
46
			this.parameterList = Arrays.asList(parameter.split("."));
47
		} else {
48
			this.parameterList = Arrays.asList(new String[]{parameter});
49
		}
50
	}
51

    
52

    
53
	protected Mapper getMapper() {
54
		if(mapper == null) {
55
			this.setMapper((Mapper)this.applicationContext.getBean("dozerMapper", Mapper.class)); 
56
		}
57
		return mapper;
58
	}
59
	
60
	public void setMapper(Mapper mapper) {
61
		this.mapper = mapper;
62
	}
63

    
64
	public Object convert(Object destination, Object source, Class<?> destClass, Class<?> sourceClass) {
65
		if (source == null || !Hibernate.isInitialized(source)) {
66
		    return null;
67
	    } else {
68
	
69
	    	try {
70
	    		Object value = invokeProperty(source, sourceClass, parameterList.iterator());
71
	    		if(value == null || !Hibernate.isInitialized(value)) {
72
	    			return null;
73
	    		} else {
74
	    			if(value instanceof CdmBase) {
75
	    				return getMapper().map(value, destClass);
76
	    			} else {
77
	    				return value;
78
	    			}
79
	    		}
80
			} catch (Exception e) {
81
				throw new MappingException("Converter HibernateProxyNullSafeDeepConverter used incorrectly. Arguments passed in were:"+ destination + " and " + source + " sourceClass " + sourceClass + " destClass " + destClass, e);
82
			} 
83
	    	
84
		} 
85
	}
86

    
87
	private Object invokeProperty(Object source, Class<?> sourceClass, Iterator<String> parameterIterator) throws IllegalAccessException,
88
			InvocationTargetException {
89
		Object value;
90
		String param = parameterIterator.next();
91
		PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(sourceClass, param);
92
		Method method = propertyDescriptor.getReadMethod();
93
		method.setAccessible(true);	    		
94
		assert method != null;
95
		value = method.invoke(source);
96
		if(parameterIterator.hasNext()){
97
			return invokeProperty(source, sourceClass, parameterIterator);
98
		} else {			
99
			return value;
100
		}
101
	}
102

    
103
	public void setApplicationContext(ApplicationContext applicationContext)
104
			throws BeansException {
105
		this.applicationContext = applicationContext;
106
	}
107

    
108
	
109
}
(4-4/11)