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;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
|
18
|
import eu.etaxonomy.cdm.io.common.ICdmIO;
|
19
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
20
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
21
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
22
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
23
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
24
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
25
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
26
|
|
27
|
/**
|
28
|
* TODO synonyms are no relationships anymore, maybe we need to change something here
|
29
|
* @author a.mueller
|
30
|
* @since 02.03.2010
|
31
|
* @param <CDM_BASE>
|
32
|
* @param <STATE>
|
33
|
*/
|
34
|
public class DbImportSynonymMapper<STATE extends DbImportStateBase<?,?>> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
|
35
|
private static final Logger logger = Logger.getLogger(DbImportSynonymMapper.class);
|
36
|
|
37
|
//******************************** FACTORY METHOD ***************************************************/
|
38
|
|
39
|
/**
|
40
|
* Creates a new instance of SynonymMapper.
|
41
|
* @param dbFromAttribute
|
42
|
* @param dbToAttribute
|
43
|
* @param relatedObjectNamespace
|
44
|
* @param relTypeAttribute
|
45
|
* @param taxonRelationshipType this relationship type is taken for accepted taxa being synonyms (may be the case if data are dirty)
|
46
|
* @return
|
47
|
*/
|
48
|
public static DbImportSynonymMapper<?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String relTypeAttribute, TaxonRelationshipType taxonRelationshipType){
|
49
|
return new DbImportSynonymMapper(dbFromAttribute, dbToAttribute, taxonRelationshipType, relatedObjectNamespace, relTypeAttribute);
|
50
|
}
|
51
|
|
52
|
//******************************* ATTRIBUTES ***************************************/
|
53
|
private String fromAttribute;
|
54
|
private String toAttribute;
|
55
|
private TaxonRelationshipType relType;
|
56
|
private String relatedObjectNamespace;
|
57
|
private String citationAttribute;
|
58
|
private String microCitationAttribute;
|
59
|
private String relationshipTypeAttribute;
|
60
|
private boolean useTaxonRelationship;
|
61
|
|
62
|
|
63
|
//********************************* CONSTRUCTOR ****************************************/
|
64
|
/**
|
65
|
* @param relatedObjectNamespace
|
66
|
* @param mappingImport
|
67
|
*/
|
68
|
protected DbImportSynonymMapper(String fromAttribute, String toAttribute, TaxonRelationshipType relType, String relatedObjectNamespace, String relTypeAttribute) {
|
69
|
super();
|
70
|
//TODO make it a single attribute mapper
|
71
|
this.fromAttribute = fromAttribute;
|
72
|
this.toAttribute = toAttribute;
|
73
|
this.relType = relType;
|
74
|
this.relatedObjectNamespace = relatedObjectNamespace;
|
75
|
this.relationshipTypeAttribute = relTypeAttribute;
|
76
|
if (relTypeAttribute != null){
|
77
|
logger.warn("Synonymrelationship type not yet implemented");
|
78
|
}
|
79
|
this.useTaxonRelationship = (relType != null);
|
80
|
}
|
81
|
|
82
|
//************************************ METHODS *******************************************/
|
83
|
|
84
|
@Override
|
85
|
public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
|
86
|
STATE state = getState();
|
87
|
ICdmIO<?> currentImport = state.getCurrentIO();
|
88
|
if (currentImport instanceof ICheckIgnoreMapper){
|
89
|
boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
|
90
|
if (ignoreRecord){
|
91
|
return cdmBase;
|
92
|
}
|
93
|
}
|
94
|
|
95
|
TaxonBase<?> fromObject = (TaxonBase<?>)getRelatedObject(rs, fromAttribute);
|
96
|
TaxonBase<?> toObject = (TaxonBase<?>)getRelatedObject(rs, toAttribute);
|
97
|
String fromId = rs.getObject(fromAttribute)== null ? null: String.valueOf(rs.getObject(fromAttribute));
|
98
|
String toId = rs.getObject(toAttribute) == null? null : String.valueOf(rs.getObject(toAttribute));
|
99
|
|
100
|
if (toId == null){
|
101
|
return fromObject;
|
102
|
}
|
103
|
|
104
|
Reference citation = CdmBase.deproxy(getRelatedObject(rs, citationAttribute), Reference.class);
|
105
|
String microCitation = null;
|
106
|
if (citationAttribute != null){
|
107
|
microCitation = rs.getString(microCitationAttribute);
|
108
|
}
|
109
|
|
110
|
|
111
|
if (fromObject == null){
|
112
|
String warning = "The synonym (" + fromId + ") could not be found. Synonym not added to accepted taxon";
|
113
|
logger.warn(warning);
|
114
|
return cdmBase;
|
115
|
}
|
116
|
checkSynonymType(fromObject, fromId);
|
117
|
|
118
|
if (toObject == null){
|
119
|
String warning = "The accepted taxon (" + toId + ") could not be found. Synonym not added to accepted taxon";
|
120
|
logger.warn(warning);
|
121
|
return cdmBase;
|
122
|
}
|
123
|
Taxon taxon = checkTaxonType(toObject, "Accepted taxon", toId);
|
124
|
|
125
|
|
126
|
if (fromObject.isInstanceOf(Synonym.class)){
|
127
|
SynonymType relType = SynonymType.SYNONYM_OF();
|
128
|
Synonym synonym = CdmBase.deproxy(fromObject, Synonym.class);
|
129
|
taxon.addSynonym(synonym, relType); //citation and micro citation not in use anymore as we do not have synonym relationships anymore
|
130
|
}else if (fromObject.isInstanceOf(Taxon.class) && this.useTaxonRelationship){
|
131
|
TaxonRelationshipType type = relType;
|
132
|
Taxon synonymTaxon = CdmBase.deproxy(fromObject, Taxon.class);
|
133
|
synonymTaxon.addTaxonRelation(taxon, type, citation, microCitation);
|
134
|
|
135
|
}else{
|
136
|
logger.warn("Taxon is not a synonym and accepted taxa are not allowed as synonyms: " + fromObject.getTitleCache() + "; " + fromObject.getId());
|
137
|
}
|
138
|
return fromObject;
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* //TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
|
143
|
* @param rs
|
144
|
* @param dbAttribute
|
145
|
* @return
|
146
|
* @throws SQLException
|
147
|
*/
|
148
|
protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute) throws SQLException {
|
149
|
CdmBase result = null;
|
150
|
if (dbAttribute != null){
|
151
|
Object dbValue = rs.getObject(dbAttribute);
|
152
|
String id = String.valueOf(dbValue);
|
153
|
DbImportStateBase<?,?> state = importMapperHelper.getState();
|
154
|
result = state.getRelatedObject(relatedObjectNamespace, id);
|
155
|
}
|
156
|
return result;
|
157
|
}
|
158
|
|
159
|
|
160
|
/**
|
161
|
* Checks if cdmBase is of type Taxon
|
162
|
* @param fromObject
|
163
|
*/
|
164
|
private Taxon checkTaxonType(TaxonBase<?> taxonBase, String typeString, String id) {
|
165
|
if (! taxonBase.isInstanceOf(Taxon.class)){
|
166
|
String warning = typeString + " (" + id + ") is not of type Taxon but of type " + taxonBase.getClass().getSimpleName();
|
167
|
logger.warn(warning);
|
168
|
throw new IllegalArgumentException(warning);
|
169
|
}
|
170
|
return (CdmBase.deproxy(taxonBase, Taxon.class));
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* Checks if cdmBase is of type Synonym
|
175
|
* @param fromObject
|
176
|
*/
|
177
|
private TaxonBase<?> checkSynonymType(CdmBase cdmBase, String id) {
|
178
|
if (! cdmBase.isInstanceOf(Synonym.class)){
|
179
|
String warning = "Synonym (" + id + ") is not of type Synonym but of type " + cdmBase.getClass().getSimpleName();
|
180
|
if (! this.useTaxonRelationship){
|
181
|
logger.warn(warning);
|
182
|
throw new IllegalArgumentException(warning);
|
183
|
}else{
|
184
|
logger.info(warning);
|
185
|
}
|
186
|
}
|
187
|
return (CdmBase.deproxy(cdmBase, TaxonBase.class));
|
188
|
}
|
189
|
|
190
|
|
191
|
}
|