1
2
3
4
5
6
7
8
9
10
11 package eu.etaxonomy.cdm.io.common.mapping;
12
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.sql.ResultSet;
16 import java.sql.SQLException;
17
18 import org.apache.log4j.Logger;
19
20 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22 import eu.etaxonomy.cdm.model.common.VersionableEntity;
23
24
25
26
27
28
29
30 public class DbImportMethodMapper<CDMBASE extends VersionableEntity, STATE extends DbImportStateBase<?,?>> extends DbImportMultiAttributeMapperBase<CDMBASE, STATE> {
31 private static final Logger logger = Logger.getLogger(DbImportMethodMapper.class);
32
33 /
34
35 private Method method;
36 private Class<?>[] parameterTypes;
37 private Object objectToInvoke;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public static <T extends DbImportStateBase> DbImportMethodMapper NewInstance(Class<?> clazz, String methodName, Class parameterTypes){
53 DbImportMethodMapper result = new DbImportMethodMapper(clazz, null, methodName, parameterTypes);
54 return result;
55 }
56
57 public static <T extends DbImportStateBase> DbImportMethodMapper NewInstance(Object objectToInvoke, String methodName, Class<?> parameterType1, Class<?> parameterType2){
58 DbImportMethodMapper result = new DbImportMethodMapper(objectToInvoke.getClass(), objectToInvoke, methodName, parameterType1,parameterType2);
59 return result;
60 }
61
62 public static <T extends DbImportStateBase> DbImportMethodMapper NewInstance(Object objectToInvoke, String methodName, Class<?>... parameterTypes){
63 DbImportMethodMapper result = new DbImportMethodMapper(objectToInvoke.getClass(), objectToInvoke, methodName, parameterTypes);
64 return result;
65 }
66
67 /
68
69
70
71
72
73
74 protected DbImportMethodMapper(Class<?> clazz, Object objectToInoke, String methodName, Class<?>... parameterTypes) {
75 super();
76 this.objectToInvoke = objectToInoke;
77 try {
78 this.parameterTypes = parameterTypes;
79 method = clazz.getDeclaredMethod(methodName, parameterTypes);
80 method.setAccessible(true);
81 } catch (SecurityException e) {
82 logger.error("SecurityException", e);
83 } catch (NoSuchMethodException e) {
84 logger.error("NoSuchMethodException", e);
85 }
86 }
87 /
88
89
90
91
92 public void initialize(STATE state, Class<? extends CdmBase> destinationClass) {
93 super.initialize(state, destinationClass);
94
95 }
96
97
98
99
100
101 public CDMBASE invoke(ResultSet rs, CDMBASE cdmBase) throws SQLException {
102 try{
103
104 CDMBASE result = (CDMBASE)method.invoke(objectToInvoke, rs, getState());
105
106
107
108
109
110 return result;
111 } catch (IllegalAccessException e) {
112 logger.error("IllegalAccessException: " + e.getLocalizedMessage());
113 return null;
114 } catch (InvocationTargetException e) {
115 logger.error("InvocationTargetException: " + e.getLocalizedMessage());
116 return null;
117 }
118 }
119
120
121
122
123
124
125 protected IInputTransformer getTransformer(){
126 return getState().getConfig().getTransformer();
127 }
128
129 }