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