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.sql.Types;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
17
|
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
|
18
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
19
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
20
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
21
|
|
22
|
/**
|
23
|
* Maps distribution to a database foreign key or cache field.
|
24
|
* Requires according transformer implementation.
|
25
|
* @author a.mueller
|
26
|
* @created 06.02.2012
|
27
|
*/
|
28
|
public class DbDistributionStatusMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer>{
|
29
|
@SuppressWarnings("unused")
|
30
|
private static final Logger logger = Logger.getLogger(DbDistributionStatusMapper.class);
|
31
|
|
32
|
private boolean isCache = false;
|
33
|
|
34
|
public static DbDistributionStatusMapper NewInstance(String dbAttributeString, boolean isCache){
|
35
|
return new DbDistributionStatusMapper(dbAttributeString, isCache, null);
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* @param dbAttributeString
|
40
|
* @param cdmAttributeString
|
41
|
*/
|
42
|
protected DbDistributionStatusMapper(String dbAttributeString, boolean isCache, Object defaultValue) {
|
43
|
super("status", dbAttributeString, defaultValue);
|
44
|
this.isCache = isCache;
|
45
|
}
|
46
|
|
47
|
/* (non-Javadoc)
|
48
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValue(eu.etaxonomy.cdm.model.common.CdmBase)
|
49
|
*/
|
50
|
@Override
|
51
|
protected Object getValue(CdmBase cdmBase) {
|
52
|
if (cdmBase.isInstanceOf(Distribution.class)){
|
53
|
Distribution distribution = CdmBase.deproxy(cdmBase, Distribution.class);
|
54
|
PresenceAbsenceTerm status = distribution.getStatus();
|
55
|
IExportTransformer transformer = getState().getTransformer();
|
56
|
try {
|
57
|
if (isCache){
|
58
|
return transformer.getCacheByPresenceAbsenceTerm(status);
|
59
|
}else{
|
60
|
return transformer.getKeyByPresenceAbsenceTerm(status);
|
61
|
}
|
62
|
} catch (UndefinedTransformerMethodException e) {
|
63
|
throw new RuntimeException(e);
|
64
|
}
|
65
|
|
66
|
}else{
|
67
|
throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type Distribution, but was " + cdmBase.getClass());
|
68
|
}
|
69
|
}
|
70
|
|
71
|
/* (non-Javadoc)
|
72
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
|
73
|
*/
|
74
|
@Override
|
75
|
protected int getSqlType() {
|
76
|
if (isCache){
|
77
|
return Types.VARCHAR;
|
78
|
}else{
|
79
|
return Types.INTEGER;
|
80
|
}
|
81
|
}
|
82
|
|
83
|
|
84
|
/* (non-Javadoc)
|
85
|
* @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
|
86
|
*/
|
87
|
@Override
|
88
|
public Class<?> getTypeClass() {
|
89
|
if (isCache){
|
90
|
return String.class;
|
91
|
}else{
|
92
|
return Integer.class;
|
93
|
}
|
94
|
}
|
95
|
}
|