Project

General

Profile

Download (4.99 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

    
10
package eu.etaxonomy.cdm.io.common.mapping.out;
11

    
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.sql.Types;
15

    
16
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
17
import org.joda.time.DateTime;
18

    
19
import eu.etaxonomy.cdm.io.common.DbExportBase;
20
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
21
import eu.etaxonomy.cdm.model.common.CdmBase;
22

    
23
/**
24
 * @author a.mueller
25
 * @since 12.05.2009
26
 */
27
public class MethodMapper
28
            extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>>
29
            implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer> {
30

    
31
    private static final Logger logger = LogManager.getLogger(MethodMapper.class);
32

    
33
	private Method method;
34
	private Class<?>[] parameterTypes;
35

    
36
	public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, DbExportBase exportBase){
37
		String methodName = getMethodName(dbAttributeString);
38
		return NewInstance(dbAttributeString, exportBase, methodName);
39
	}
40

    
41
	public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, DbExportBase exportBase, Class<?>... parameterTypes){
42
		String methodName = getMethodName(dbAttributeString);
43
		return new MethodMapper(dbAttributeString, exportBase.getClass(), methodName, parameterTypes);
44
	}
45

    
46
	public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, DbExportBase exportBase, String methodName){
47
		Class<?> parameterTypes = exportBase.getStandardMethodParameter();
48
		MethodMapper result = new MethodMapper(dbAttributeString, exportBase.getClass(), methodName, parameterTypes);
49
		return result;
50
	}
51

    
52
	public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, Class<?> clazz, Class<?>... parameterTypes){
53
	    String methodName = getMethodName(dbAttributeString);
54
        MethodMapper result = new MethodMapper(dbAttributeString, clazz, methodName, parameterTypes);
55
        return result;
56
    }
57

    
58
	public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, Class<?> clazz, String methodName, Class<?>... parameterTypes){
59
		MethodMapper result = new MethodMapper(dbAttributeString, clazz, methodName, parameterTypes);
60
		return result;
61
	}
62

    
63
	public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, Class<?> clazz, String methodName, Class<?> parameterType1, Class<?> parameterType2){
64
		MethodMapper result = new MethodMapper(dbAttributeString, clazz, methodName, parameterType1, parameterType2);
65
		return result;
66
	}
67

    
68
	protected MethodMapper(String dbAttributeString, Class<?> clazz, String methodName, Class<?>... parameterTypes) {
69
		super(null, dbAttributeString, null);
70
		try {
71
			this.parameterTypes = parameterTypes;
72
			method = clazz.getDeclaredMethod(methodName, parameterTypes);
73
			method.setAccessible(true);
74
		} catch (SecurityException e) {
75
			logger.error("SecurityException", e);
76
		} catch (NoSuchMethodException e) {
77
			logger.error("NoSuchMethodException", e);
78
		}
79
	}
80

    
81
    private static String getMethodName(String dbAttributeString) {
82
        String methodName = "get" + dbAttributeString;
83
        return methodName;
84
    }
85

    
86
	@Override
87
	public Class<?> getTypeClass() {
88
		return method.getReturnType();
89
	}
90

    
91
	@Override
92
	protected Object getValue(CdmBase cdmBase) {
93
		try{
94
			if (this.parameterTypes.length > 1 && DbExportStateBase.class.isAssignableFrom(parameterTypes[1])){
95
				return method.invoke(null, cdmBase, getState());
96
			}else{
97
				return method.invoke(null, cdmBase);
98
			}
99
		} catch (IllegalAccessException e) {
100
			logger.error("IllegalAccessException: " + e.getMessage() + " when invoking MethodMapper " +  this.toString());
101
			throw new RuntimeException(e);
102
		} catch (InvocationTargetException e) {
103
			logger.error("InvocationTargetException: " + e.getMessage() + " when invoking MethodMapper " +  this.toString());
104
			throw new RuntimeException(e);
105
		} catch (Exception e) {
106
			logger.error("Any Exception: " + e.getMessage() + " when invoking MethodMapper " +  this.toString());
107
			throw new RuntimeException(e);
108
		}
109
	}
110

    
111
	@Override
112
	protected int getSqlType() {
113
		Class<?> returnType = method.getReturnType();
114
		if (returnType == Integer.class){
115
			return Types.INTEGER;
116
		}else if (returnType == String.class){
117
			return Types.VARCHAR;
118
		}else if (returnType == Boolean.class){
119
			return Types.BOOLEAN;
120
		}else if (returnType == DateTime.class){
121
			return Types.DATE;
122
		}else{
123
			logger.warn("Return type not supported yet: " + returnType.getSimpleName());
124
			throw new IllegalArgumentException("Return type not supported yet: " + returnType.getSimpleName());
125
		}
126
	}
127
}
(43-43/44)