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