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