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
|
import java.util.EnumSet;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.apache.commons.lang.StringUtils;
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
21
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
24
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
25
|
|
26
|
|
27
|
/**
|
28
|
* Maps distribution to a database foreign key or cache field.
|
29
|
* Requires according transformer implementation.
|
30
|
* @author a.mueller
|
31
|
* @created 06.02.2012
|
32
|
*/
|
33
|
public class DbSingleSourceMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer>{
|
34
|
private static final Logger logger = Logger.getLogger(DbSingleSourceMapper.class);
|
35
|
|
36
|
public enum EXCLUDE{
|
37
|
NONE,
|
38
|
WITH_ID,
|
39
|
WITH_NAMESPACE
|
40
|
}
|
41
|
public static int EXCLUDE_NONE = 0;
|
42
|
public static int EXCLUDE_WITH_ID = 1;
|
43
|
public static int EXCLUDE_WITH_NAMESPACE = 2;
|
44
|
|
45
|
private boolean isCache = false;
|
46
|
private final EnumSet<EXCLUDE> exclude;
|
47
|
|
48
|
public static DbSingleSourceMapper NewInstance(String dbAttributeString, EnumSet<EXCLUDE> exclude, boolean isCache){
|
49
|
return new DbSingleSourceMapper(dbAttributeString, exclude, isCache, null);
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* @param dbAttributeString
|
54
|
* @param cdmAttributeString
|
55
|
*/
|
56
|
protected DbSingleSourceMapper(String dbAttributeString, EnumSet<EXCLUDE> exclude, boolean isCache, Object defaultValue) {
|
57
|
super("sources", dbAttributeString, defaultValue);
|
58
|
this.isCache = isCache;
|
59
|
this.exclude = exclude;
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
protected Object getValue(CdmBase cdmBase) {
|
64
|
//TODO implement also for Identifiable sources
|
65
|
if (cdmBase.isInstanceOf(DescriptionElementBase.class)){
|
66
|
//find source candidates
|
67
|
DescriptionElementBase el = CdmBase.deproxy(cdmBase, DescriptionElementBase.class);
|
68
|
Set<DescriptionElementSource> sourceCandidates = el.getSources();
|
69
|
Set<DescriptionElementSource> filteredSources = new HashSet<DescriptionElementSource>();
|
70
|
for (DescriptionElementSource sourceCandidate : sourceCandidates){
|
71
|
if (isPesiSource(sourceCandidate)){ //TODO pesi should not appear here
|
72
|
filteredSources.add(sourceCandidate);
|
73
|
}
|
74
|
}
|
75
|
//filter
|
76
|
if (filteredSources.size() == 0 ){
|
77
|
return null;
|
78
|
}else if (filteredSources.size() > 1){
|
79
|
logger.warn("There is more than 1 accepted source for description element " + el.getUuid() + ". Arbitrary first source is used.");
|
80
|
}
|
81
|
DescriptionElementSource source = filteredSources.iterator().next();
|
82
|
Reference ref = source.getCitation();
|
83
|
if (ref == null){
|
84
|
logger.warn("Citation is missing for description element (" + el.getUuid() + ") source.");
|
85
|
return null;
|
86
|
}
|
87
|
if (isCache){
|
88
|
return ref.getTitleCache();
|
89
|
}else{
|
90
|
return getState().getDbId(ref);
|
91
|
}
|
92
|
}else{
|
93
|
throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type DescriptionElementBase, but was " + cdmBase.getClass());
|
94
|
}
|
95
|
}
|
96
|
|
97
|
private boolean isPesiSource(DescriptionElementSource source) {
|
98
|
boolean result = true;
|
99
|
if ( exclude.contains(EXCLUDE.NONE)){
|
100
|
return true;
|
101
|
}
|
102
|
if ( exclude.contains(EXCLUDE.WITH_ID)){
|
103
|
result &= StringUtils.isBlank(source.getIdInSource());
|
104
|
}else if ( exclude.contains(EXCLUDE.WITH_NAMESPACE)){
|
105
|
result &= StringUtils.isBlank(source.getIdNamespace());
|
106
|
}else{
|
107
|
logger.warn("isPesiSource case not yet handled for " + this.exclude);
|
108
|
}
|
109
|
return result;
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
protected int getSqlType() {
|
114
|
if (isCache){
|
115
|
return Types.VARCHAR;
|
116
|
}else{
|
117
|
return Types.INTEGER;
|
118
|
}
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public Class<?> getTypeClass() {
|
123
|
if (isCache){
|
124
|
return String.class;
|
125
|
}else{
|
126
|
return Integer.class;
|
127
|
}
|
128
|
}
|
129
|
}
|