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.Types;
|
14
|
|
15
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
18
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
19
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
20
|
|
21
|
/**
|
22
|
* Maps an area to a database key or cache field.
|
23
|
* @author a.mueller
|
24
|
* @since 06.02.2012
|
25
|
*/
|
26
|
public class DbAreaMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer>{
|
27
|
private static final Logger logger = LogManager.getLogger(DbAreaMapper.class);
|
28
|
|
29
|
private Method method;
|
30
|
private boolean isCache;
|
31
|
private Class<? extends CdmBase> cdmClass;
|
32
|
|
33
|
public static DbAreaMapper NewInstance(Class<? extends CdmBase> cdmClass, String cdmAttribute, String dbAttributeString, boolean isCache){
|
34
|
String methodName = "get" + cdmAttribute;
|
35
|
return new DbAreaMapper(cdmClass, methodName, (Class<?>[])null, cdmAttribute, dbAttributeString, isCache, null);
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* @param clazz
|
40
|
* @param parameterTypes
|
41
|
* @param dbAttributeString
|
42
|
* @param cdmAttributeString
|
43
|
*/
|
44
|
protected DbAreaMapper(Class<? extends CdmBase> clazz, String methodName, Class<?>[] parameterTypes, String cdmAttribute, String dbAttributeString, boolean isCache, Object defaultValue) {
|
45
|
super(cdmAttribute, dbAttributeString, defaultValue);
|
46
|
cdmClass = clazz;
|
47
|
this.isCache = isCache;
|
48
|
try {
|
49
|
// this.parameterTypes = parameterTypes;
|
50
|
method = clazz.getDeclaredMethod(methodName, parameterTypes);
|
51
|
method.setAccessible(true);
|
52
|
cdmClass = clazz;
|
53
|
} catch (NoSuchMethodException e) {
|
54
|
try {
|
55
|
method = clazz.getMethod(methodName, parameterTypes);
|
56
|
} catch (NoSuchMethodException e1) {
|
57
|
logger.error("NoSuchMethodException", e);
|
58
|
return;
|
59
|
}
|
60
|
} catch (SecurityException e) {
|
61
|
logger.error("SecurityException", e);
|
62
|
return;
|
63
|
}
|
64
|
method.setAccessible(true);
|
65
|
cdmClass = clazz;
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
protected Object getValue(CdmBase cdmBase) {
|
70
|
if (cdmBase.isInstanceOf(cdmClass)){
|
71
|
try {
|
72
|
NamedArea area = (NamedArea)method.invoke(cdmBase, (Object[])null);
|
73
|
IExportTransformer transformer = getState().getTransformer();
|
74
|
if (isCache){
|
75
|
return transformer.getCacheByNamedArea(area);
|
76
|
}else{
|
77
|
return transformer.getKeyByNamedArea(area);
|
78
|
}
|
79
|
} catch (Exception e) {
|
80
|
logger.error("Exception when invoking method: " + e.getMessage());
|
81
|
return null;
|
82
|
}
|
83
|
|
84
|
}else{
|
85
|
throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type "+cdmClass.getName()+", but was " + cdmBase.getClass());
|
86
|
}
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
protected int getSqlType() {
|
91
|
if (isCache){
|
92
|
return Types.VARCHAR;
|
93
|
}else{
|
94
|
return Types.INTEGER;
|
95
|
}
|
96
|
}
|
97
|
|
98
|
|
99
|
@Override
|
100
|
public Class<?> getTypeClass() {
|
101
|
if (isCache){
|
102
|
return String.class;
|
103
|
}else{
|
104
|
return Integer.class;
|
105
|
}
|
106
|
}
|
107
|
}
|