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;
|
11
|
|
12
|
import java.lang.reflect.InvocationTargetException;
|
13
|
import java.lang.reflect.Method;
|
14
|
import java.sql.ResultSet;
|
15
|
import java.sql.SQLException;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
|
19
|
import eu.etaxonomy.cdm.database.update.DatabaseTypeNotSupportedException;
|
20
|
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
|
21
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
22
|
import eu.etaxonomy.cdm.io.common.Source;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
|
25
|
/**
|
26
|
* @author a.mueller
|
27
|
* @since 12.05.2009
|
28
|
*/
|
29
|
public abstract class DbSingleAttributeImportMapperBase<STATE extends DbImportStateBase<?,?>, CDM_BASE extends CdmBase> extends CdmSingleAttributeMapperBase implements IDbImportMapper<STATE, CDM_BASE> {
|
30
|
private static final Logger logger = Logger.getLogger(DbSingleAttributeImportMapperBase.class);
|
31
|
|
32
|
protected DbImportMapperBase<STATE> importMapperHelper = new DbImportMapperBase<>();
|
33
|
// private Integer precision = null;
|
34
|
protected boolean obligatory = true;
|
35
|
protected boolean ignore = false;
|
36
|
|
37
|
protected Method destinationMethod = null;
|
38
|
protected Class<?> targetClass;
|
39
|
|
40
|
/**
|
41
|
* @param dbAttributString
|
42
|
* @param cdmAttributeString
|
43
|
*/
|
44
|
protected DbSingleAttributeImportMapperBase(String dbAttributString, String cdmAttributeString) {
|
45
|
super(dbAttributString, cdmAttributeString);
|
46
|
}
|
47
|
|
48
|
|
49
|
/**
|
50
|
* @param dbAttributString
|
51
|
* @param cdmAttributeString
|
52
|
*/
|
53
|
protected DbSingleAttributeImportMapperBase(String dbAttributString, String cdmAttributeString, Object defaultValue) {
|
54
|
super(dbAttributString, cdmAttributeString, defaultValue);
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* @param dbAttributString
|
59
|
* @param cdmAttributeString
|
60
|
*/
|
61
|
protected DbSingleAttributeImportMapperBase(String dbAttributeString, String cdmAttributeString, Object defaultValue, boolean obligatory) {
|
62
|
super(dbAttributeString, cdmAttributeString, defaultValue);
|
63
|
this.obligatory = obligatory;
|
64
|
}
|
65
|
|
66
|
|
67
|
@Override
|
68
|
public void initialize(STATE state, Class<? extends CdmBase> destinationClass) {
|
69
|
importMapperHelper.initialize(state, destinationClass);
|
70
|
try {
|
71
|
targetClass = getTargetClass(destinationClass);
|
72
|
Class<?> parameterType = getTypeClass();
|
73
|
String methodName = getMethodName(parameterType);
|
74
|
destinationMethod = targetClass.getMethod(methodName, parameterType);
|
75
|
} catch (SecurityException e) {
|
76
|
throw new RuntimeException(e);
|
77
|
} catch (NoSuchMethodException e) {
|
78
|
throw new RuntimeException(e);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
|
83
|
/**
|
84
|
* @param destinationClass
|
85
|
* @return
|
86
|
* @throws NoSuchMethodException
|
87
|
* @throws SecurityException
|
88
|
* @throws NoSuchMethodException
|
89
|
*/
|
90
|
protected Class getTargetClass(Class<?> destinationClass) throws SecurityException, NoSuchMethodException{
|
91
|
Class result = destinationClass;
|
92
|
String destinationAttribute = getDestinationAttribute();
|
93
|
if (destinationAttribute == null){
|
94
|
return null;
|
95
|
}
|
96
|
String[] splits = destinationAttribute.split("\\.");
|
97
|
//for all prefixes
|
98
|
for (int i = 0; i < splits.length - 1; i++){
|
99
|
String split = splits[i];
|
100
|
String castedResultClass = getCastedResultClass(split);
|
101
|
split = removeCast(split);
|
102
|
String methodName = ImportHelper.getGetterMethodName(split, false);
|
103
|
Method getterMethod;
|
104
|
try {
|
105
|
getterMethod = result.getMethod(methodName, null);
|
106
|
} catch (NoSuchMethodException e1) {
|
107
|
throw e1;
|
108
|
}
|
109
|
result = getterMethod.getReturnType();
|
110
|
if (castedResultClass != null){
|
111
|
try {
|
112
|
//casting works only if the subclass is in the same package
|
113
|
String packageName = result.getPackage().getName();
|
114
|
castedResultClass = packageName + "." + castedResultClass;
|
115
|
result = Class.forName(castedResultClass);
|
116
|
} catch (ClassNotFoundException e) {
|
117
|
e.printStackTrace();
|
118
|
//result = null;
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
return result;
|
123
|
}
|
124
|
|
125
|
|
126
|
/**
|
127
|
* @param split
|
128
|
* @return
|
129
|
*/
|
130
|
private String removeCast(String split) {
|
131
|
int index = split.lastIndexOf(")");
|
132
|
if (split.length() > index){
|
133
|
split = split.substring(index + 1);
|
134
|
}else{
|
135
|
split = "";
|
136
|
}
|
137
|
return split;
|
138
|
}
|
139
|
|
140
|
|
141
|
/**
|
142
|
* @param split
|
143
|
* @return
|
144
|
*/
|
145
|
private String getCastedResultClass(String split) {
|
146
|
String castString = null;
|
147
|
split = split.trim();
|
148
|
int index = split.lastIndexOf(")");
|
149
|
if (split.startsWith("(") && index > -1 ){
|
150
|
castString = split.substring(1, index);
|
151
|
castString = castString.trim();
|
152
|
}
|
153
|
return castString;
|
154
|
}
|
155
|
|
156
|
|
157
|
/**
|
158
|
* @param clazz
|
159
|
* @return
|
160
|
*/
|
161
|
private String getMethodName(Class clazz) {
|
162
|
String cdmAttributeName = getTargetClassAttribute(getDestinationAttribute());
|
163
|
String result = ImportHelper.getSetterMethodName(clazz, cdmAttributeName);
|
164
|
return result;
|
165
|
}
|
166
|
|
167
|
|
168
|
/**
|
169
|
* @param destinationAttribute
|
170
|
* @return
|
171
|
*/
|
172
|
private String getTargetClassAttribute(String destinationAttribute) {
|
173
|
if (destinationAttribute == null){
|
174
|
return null;
|
175
|
}
|
176
|
String[] splitted = destinationAttribute.split("\\.");
|
177
|
String result = splitted[splitted.length - 1]; //get last attribute
|
178
|
return result;
|
179
|
}
|
180
|
|
181
|
@Override
|
182
|
public CDM_BASE invoke(ResultSet rs, CDM_BASE cdmBase) throws SQLException {
|
183
|
if (ignore){
|
184
|
return cdmBase;
|
185
|
}
|
186
|
Object dbValue = getValue(rs);
|
187
|
|
188
|
// String dbValue = rs.getString(getSourceAttribute());
|
189
|
return doInvoke(cdmBase, dbValue);
|
190
|
}
|
191
|
|
192
|
protected CDM_BASE doInvoke(CDM_BASE cdmBase, Object value) throws SQLException {
|
193
|
Method method = getMethod();
|
194
|
try {
|
195
|
Object objectToInvoke = getObjectToInvoke(cdmBase);
|
196
|
if (objectToInvoke == null){
|
197
|
logger.warn("No object defined for invoke. Method will not be invoked");
|
198
|
}else{
|
199
|
method.invoke(objectToInvoke, value);
|
200
|
}
|
201
|
return cdmBase;
|
202
|
} catch (IllegalArgumentException e) {
|
203
|
e.printStackTrace();
|
204
|
} catch (IllegalAccessException e) {
|
205
|
e.printStackTrace();
|
206
|
} catch (InvocationTargetException e) {
|
207
|
e.printStackTrace();
|
208
|
} catch (SecurityException e) {
|
209
|
e.printStackTrace();
|
210
|
} catch (NoSuchMethodException e) {
|
211
|
e.printStackTrace();
|
212
|
}
|
213
|
throw new RuntimeException("Problems when invoking target method");
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* @param cdmBase
|
218
|
* @return
|
219
|
* @throws NoSuchMethodException
|
220
|
* @throws SecurityException
|
221
|
* @throws InvocationTargetException
|
222
|
* @throws IllegalAccessException
|
223
|
* @throws IllegalArgumentException
|
224
|
*/
|
225
|
private Object getObjectToInvoke(CDM_BASE cdmBase) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
|
226
|
Object objectToInvoke = cdmBase;
|
227
|
String destinationAttribute = getDestinationAttribute();
|
228
|
String[] splits = destinationAttribute.split("\\.");
|
229
|
for (int i = 0; i < splits.length - 1; i++){
|
230
|
String split = splits[i];
|
231
|
split = removeCast(split);
|
232
|
String methodName = ImportHelper.getGetterMethodName(split, false);
|
233
|
Method method = objectToInvoke.getClass().getMethod(methodName, null);
|
234
|
objectToInvoke = method.invoke(cdmBase, null);
|
235
|
}
|
236
|
return objectToInvoke;
|
237
|
}
|
238
|
|
239
|
|
240
|
/**
|
241
|
* @return
|
242
|
*/
|
243
|
private Method getMethod() {
|
244
|
if (destinationMethod != null){
|
245
|
return destinationMethod;
|
246
|
}else{
|
247
|
throw new RuntimeException("Missing destinationMethod not yet implemented");
|
248
|
}
|
249
|
}
|
250
|
|
251
|
|
252
|
protected Object getValue(ResultSet rs) throws SQLException{
|
253
|
return getDbValue(rs);
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Returns the database value for the attribute
|
258
|
* @param rs
|
259
|
* @return
|
260
|
* @throws SQLException
|
261
|
*/
|
262
|
protected Object getDbValue(ResultSet rs) throws SQLException{
|
263
|
String columnLabel = getSourceAttribute();
|
264
|
Object value = rs.getObject(columnLabel);
|
265
|
return value;
|
266
|
}
|
267
|
|
268
|
|
269
|
// /**
|
270
|
// * @return the index
|
271
|
// */
|
272
|
// public int getIndex() {
|
273
|
// return exportMapperHelper.getIndex();
|
274
|
// }
|
275
|
|
276
|
/**
|
277
|
* @return the state
|
278
|
*/
|
279
|
protected STATE getState() {
|
280
|
return importMapperHelper.getState();
|
281
|
}
|
282
|
|
283
|
|
284
|
/**
|
285
|
* @return the state
|
286
|
*/
|
287
|
protected String getTableName() {
|
288
|
return importMapperHelper.getTableName();
|
289
|
}
|
290
|
|
291
|
protected boolean checkDbColumnExists() throws DatabaseTypeNotSupportedException{
|
292
|
// //TODO remove cast
|
293
|
// Source source = (Source)getState().getConfig().getSource();
|
294
|
// String tableName = getTableName();
|
295
|
// String attributeName = getSourceAttribute();
|
296
|
// return source.checkColumnExists(tableName, attributeName);
|
297
|
//TODO not possible as long as tableName is not initialized
|
298
|
return true;
|
299
|
}
|
300
|
|
301
|
// protected int getPrecision(){
|
302
|
// return this.precision;
|
303
|
// }
|
304
|
|
305
|
protected int getDbColumnIntegerInfo(String selectPart){
|
306
|
//TODO remove cast
|
307
|
Source source = getState().getConfig().getSource();
|
308
|
String strQuery = "SELECT " + selectPart + " as result" +
|
309
|
" FROM sysobjects AS t " +
|
310
|
" INNER JOIN syscolumns AS c ON t.id = c.id " +
|
311
|
" WHERE (t.xtype = 'U') AND " +
|
312
|
" (t.name = '" + getTableName() + "') AND " +
|
313
|
" (c.name = '" + getSourceAttribute() + "')";
|
314
|
ResultSet rs = source.getResultSet(strQuery) ;
|
315
|
int n;
|
316
|
try {
|
317
|
rs.next();
|
318
|
n = rs.getInt("result");
|
319
|
return n;
|
320
|
} catch (SQLException e) {
|
321
|
e.printStackTrace();
|
322
|
return -1;
|
323
|
}
|
324
|
|
325
|
}
|
326
|
|
327
|
|
328
|
/**
|
329
|
* @param rs
|
330
|
* @return
|
331
|
* @throws SQLException
|
332
|
*/
|
333
|
protected String getStringDbValue(ResultSet rs, String attribute) throws SQLException {
|
334
|
Object oId = rs.getObject(attribute);
|
335
|
String id = String.valueOf(oId);
|
336
|
return id;
|
337
|
}
|
338
|
// public String toString(){
|
339
|
// String sourceAtt = CdmUtils.Nz(getSourceAttribute());
|
340
|
// String destAtt = CdmUtils.Nz(getDestinationAttribute());
|
341
|
// return this.getClass().getSimpleName() +"[" + sourceAtt + "->" + destAtt + "]";
|
342
|
// }
|
343
|
|
344
|
}
|