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.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
|
* @created 12.05.2009
|
26
|
* @version 1.0
|
27
|
*/
|
28
|
public class MethodMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer> {
|
29
|
private static final Logger logger = Logger.getLogger(MethodMapper.class);
|
30
|
|
31
|
private Method method;
|
32
|
private Class<?>[] parameterTypes;
|
33
|
|
34
|
public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, DbExportBase exportBase){
|
35
|
String methodName = "get" + dbAttributeString;
|
36
|
return NewInstance(dbAttributeString, exportBase, methodName);
|
37
|
}
|
38
|
|
39
|
public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, DbExportBase exportBase, Class<?>... parameterTypes){
|
40
|
String methodName = "get" + dbAttributeString;
|
41
|
return new MethodMapper(dbAttributeString, exportBase.getClass(), methodName, parameterTypes);
|
42
|
}
|
43
|
|
44
|
public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, DbExportBase exportBase, String methodName){
|
45
|
Class<?> parameterTypes = exportBase.getStandardMethodParameter();
|
46
|
MethodMapper result = new MethodMapper(dbAttributeString, exportBase.getClass(), methodName, parameterTypes);
|
47
|
return result;
|
48
|
}
|
49
|
|
50
|
public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, Class<?> clazz, String methodName, Class<?>... parameterTypes){
|
51
|
MethodMapper result = new MethodMapper(dbAttributeString, clazz, methodName, parameterTypes);
|
52
|
return result;
|
53
|
}
|
54
|
|
55
|
public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, Class<?> clazz, String methodName, Class<?> parameterType1, Class<?> parameterType2){
|
56
|
MethodMapper result = new MethodMapper(dbAttributeString, clazz, methodName, parameterType1,parameterType2);
|
57
|
return result;
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* @param parameterTypes
|
62
|
* @param dbIdAttributString
|
63
|
*/
|
64
|
protected MethodMapper(String dbAttributeString, Class<?> clazz, String methodName, Class<?>... parameterTypes) {
|
65
|
super(null, dbAttributeString, null);
|
66
|
try {
|
67
|
this.parameterTypes = parameterTypes;
|
68
|
method = clazz.getDeclaredMethod(methodName, parameterTypes);
|
69
|
method.setAccessible(true);
|
70
|
} catch (SecurityException e) {
|
71
|
logger.error("SecurityException", e);
|
72
|
} catch (NoSuchMethodException e) {
|
73
|
logger.error("NoSuchMethodException", e);
|
74
|
}
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
public Class<?> getTypeClass() {
|
79
|
return method.getReturnType();
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
protected Object getValue(CdmBase cdmBase) {
|
84
|
try{
|
85
|
if (this.parameterTypes.length > 1 && DbExportStateBase.class.isAssignableFrom(parameterTypes[1])){
|
86
|
return method.invoke(null, cdmBase, getState());
|
87
|
}else{
|
88
|
return method.invoke(null, cdmBase);
|
89
|
}
|
90
|
|
91
|
} catch (IllegalAccessException e) {
|
92
|
logger.error("IllegalAccessException: " + e.getMessage() + " when invoking MethodMapper " + this.toString());
|
93
|
return false;
|
94
|
} catch (InvocationTargetException e) {
|
95
|
logger.error("InvocationTargetException: " + e.getMessage() + " when invoking MethodMapper " + this.toString());
|
96
|
return false;
|
97
|
} catch (Exception e) {
|
98
|
logger.error("Any Exception: " + e.getMessage() + " when invoking MethodMapper " + this.toString());
|
99
|
throw new RuntimeException(e);
|
100
|
}
|
101
|
}
|
102
|
|
103
|
@Override
|
104
|
protected int getSqlType() {
|
105
|
Class<?> returnType = method.getReturnType();
|
106
|
if (returnType == Integer.class){
|
107
|
return Types.INTEGER;
|
108
|
}else if (returnType == String.class){
|
109
|
return Types.VARCHAR;
|
110
|
}else if (returnType == Boolean.class){
|
111
|
return Types.BOOLEAN;
|
112
|
}else if (returnType == DateTime.class){
|
113
|
return Types.DATE;
|
114
|
}else{
|
115
|
logger.warn("Return type not supported yet: " + returnType.getSimpleName());
|
116
|
throw new IllegalArgumentException("Return type not supported yet: " + returnType.getSimpleName());
|
117
|
}
|
118
|
}
|
119
|
|
120
|
}
|