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.Method;
|
13
|
import java.sql.PreparedStatement;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
20
|
|
21
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
22
|
import eu.etaxonomy.cdm.io.common.mapping.CdmAttributeMapperBase;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
|
25
|
/**
|
26
|
* Changes the object to be handled (usually the object to be mapped stays the same for all mappers of a mapping).
|
27
|
* @author a.mueller
|
28
|
* @since 12.05.2009
|
29
|
*/
|
30
|
public class ObjectChangeMapper extends CdmAttributeMapperBase implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer> {
|
31
|
private static final Logger logger = LogManager.getLogger(ObjectChangeMapper.class);
|
32
|
|
33
|
private DbExportStateBase<?, IExportTransformer> state; //for possible later use
|
34
|
|
35
|
private Class<? extends CdmBase> oldClass;
|
36
|
private Class<? extends CdmBase> newClass;
|
37
|
private String cdmAttribute;
|
38
|
|
39
|
private Method method;
|
40
|
// private Class<?>[] parameterTypes;
|
41
|
|
42
|
public static ObjectChangeMapper NewInstance(Class<? extends CdmBase> oldClass, Class<? extends CdmBase> newClass, String cdmAttribute){
|
43
|
String methodName = "get" + cdmAttribute;
|
44
|
return new ObjectChangeMapper(oldClass, newClass, methodName, (Class<?>[])null);
|
45
|
}
|
46
|
|
47
|
protected ObjectChangeMapper(Class<? extends CdmBase>oldClazz,Class<? extends CdmBase>newClazz, String methodName, Class<?>... parameterTypes) {
|
48
|
try {
|
49
|
// this.parameterTypes = parameterTypes;
|
50
|
oldClass = oldClazz;
|
51
|
newClass = newClazz;
|
52
|
method = oldClazz.getDeclaredMethod(methodName, parameterTypes);
|
53
|
method.setAccessible(true);
|
54
|
} catch (SecurityException e) {
|
55
|
logger.error("SecurityException", e);
|
56
|
} catch (NoSuchMethodException e) {
|
57
|
logger.error("NoSuchMethodException", e);
|
58
|
}
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public void initialize(PreparedStatement stmt, IndexCounter index, DbExportStateBase<?, IExportTransformer> state, String tableName) {
|
63
|
this.state = state;
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public boolean invoke(CdmBase cdmBase) {
|
68
|
throw new RuntimeException("Invoke must not be called for " + this.getClass().getSimpleName() + ". Return type is still incompatible. Use getNewObject instead.");
|
69
|
}
|
70
|
|
71
|
public CdmBase getNewObject(CdmBase oldCdmBase){
|
72
|
try {
|
73
|
if (oldCdmBase.isInstanceOf(oldClass)){
|
74
|
return (CdmBase)method.invoke(oldCdmBase, (Object[])null);
|
75
|
}else if (oldCdmBase.isInstanceOf(newClass)){
|
76
|
return oldCdmBase;
|
77
|
}else{
|
78
|
logger.warn("ObjectChangeMapper "+this.toString()+"not applicable for CdmBase " + oldCdmBase.getClass());
|
79
|
return oldCdmBase;
|
80
|
}
|
81
|
} catch (Exception e) {
|
82
|
throw new RuntimeException(e);
|
83
|
}
|
84
|
}
|
85
|
|
86
|
@Override
|
87
|
public Set<String> getSourceAttributes() {
|
88
|
Set<String> result = new HashSet<>();
|
89
|
if (cdmAttribute != null){
|
90
|
result.add(cdmAttribute);
|
91
|
}
|
92
|
return result;
|
93
|
}
|
94
|
|
95
|
@Override
|
96
|
public Set<String> getDestinationAttributes() {
|
97
|
return new HashSet<>();
|
98
|
}
|
99
|
|
100
|
@Override
|
101
|
public List<String> getSourceAttributeList() {
|
102
|
ArrayList<String> result = new ArrayList<>();
|
103
|
if (cdmAttribute != null){
|
104
|
result.add(cdmAttribute);
|
105
|
}
|
106
|
return result;
|
107
|
}
|
108
|
|
109
|
@Override
|
110
|
public List<String> getDestinationAttributeList() {
|
111
|
return new ArrayList<>();
|
112
|
}
|
113
|
}
|