Project

General

Profile

Download (2.47 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
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.
9
*/
10
package eu.etaxonomy.cdm.remote.dto.assembler.converter;
11

    
12
import java.beans.PropertyDescriptor;
13
import java.lang.reflect.Method;
14

    
15
import net.sf.dozer.util.mapping.MapperIF;
16
import net.sf.dozer.util.mapping.MappingException;
17
import net.sf.dozer.util.mapping.converters.ConfigurableCustomConverter;
18

    
19
import org.hibernate.Hibernate;
20
import org.springframework.beans.BeanUtils;
21
import org.springframework.beans.BeansException;
22
import org.springframework.context.ApplicationContext;
23
import org.springframework.context.ApplicationContextAware;
24

    
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26

    
27
public class HibernateProxyNullSafeDeepConverter implements ConfigurableCustomConverter, ApplicationContextAware {
28
	
29
	private MapperIF mapper;
30
	
31
	private ApplicationContext applicationContext;
32
	
33
	protected MapperIF getMapper() {
34
		if(mapper == null) {
35
			this.setMapper((MapperIF)this.applicationContext.getBean("dozerMapper", MapperIF.class)); 
36
		}
37
		return mapper;
38
	}
39
	
40
	public void setMapper(MapperIF mapper) {
41
		this.mapper = mapper;
42
	}
43

    
44
	public Object convert(Object destination, Object source, Class destClass, Class sourceClass, String arg) {
45
		if (source == null || !Hibernate.isInitialized(source)) {
46
		    return null;
47
	    } else {
48
	    	try {
49
	    		PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(sourceClass, arg);
50
	    		Method method = propertyDescriptor.getReadMethod();
51
	    		method.setAccessible(true);	    		
52
	    		assert method != null;
53
	    		Object value = method.invoke(source);
54
	    		if(value == null || !Hibernate.isInitialized(value)) {
55
	    		    return null;
56
	    		} else {
57
	    			if(value instanceof CdmBase) {
58
	    			return getMapper().map(value, destClass);
59
	    			} else {
60
	    				return value;
61
	    		}
62
	    		}
63
			} catch (Exception e) {
64
				throw new MappingException("Converter HibernateProxyNullSafeDeepConverter used incorrectly. Arguments passed in were:"+ destination + " and " + source + " sourceClass " + sourceClass + " destClass " + destClass, e);
65
			} 
66
		} 
67
	}
68

    
69
	public void setApplicationContext(ApplicationContext applicationContext)
70
			throws BeansException {
71
		this.applicationContext = applicationContext;
72
	}
73
}
(4-4/9)