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
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
17
|
|
18
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
19
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
20
|
import eu.etaxonomy.cdm.io.common.DbImportConfiguratorBase;
|
21
|
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
|
22
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
23
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
24
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
25
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
26
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
28
|
|
29
|
/**
|
30
|
* @author a.mueller
|
31
|
* @since 02.03.2010
|
32
|
*
|
33
|
* @see DbImportSynonymMapper
|
34
|
*/
|
35
|
public class DbImportTaxIncludedInMapper<STATE extends DbImportStateBase<DbImportConfiguratorBase<STATE>,?>>
|
36
|
extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
|
37
|
|
38
|
private static final Logger logger = LogManager.getLogger(DbImportTaxIncludedInMapper.class);
|
39
|
|
40
|
//******************************** FACTORY METHOD ***************************************************/
|
41
|
|
42
|
public static DbImportTaxIncludedInMapper<?> NewInstance(String dbChildAttribute, String dbChildNamespace, String dbParentAttribute, String parentNamespace, String dbTreeAttribute){
|
43
|
String citationNamespace = null;
|
44
|
String citationAttribute = null;
|
45
|
return new DbImportTaxIncludedInMapper<>(dbChildAttribute, dbChildNamespace, dbParentAttribute, parentNamespace, null, null, dbTreeAttribute, citationAttribute, citationNamespace);
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* @param dbChildAttribute
|
50
|
* @param dbChildNamespace
|
51
|
* @param dbParentAttribute
|
52
|
* @param parentNamespace
|
53
|
* @param dbAlternativeParentAttribute if the object represented by dbParentAttribute is of class
|
54
|
* Synonym the alternative parent is taken
|
55
|
* @param alternativeParentNamespace
|
56
|
* @param dbTreeAttribute
|
57
|
* @return
|
58
|
*/
|
59
|
public static DbImportTaxIncludedInMapper<?> NewInstance(String dbChildAttribute, String dbChildNamespace, String dbParentAttribute, String parentNamespace, String dbAlternativeParentAttribute, String alternativeParentNamespace, String dbTreeAttribute){
|
60
|
String citationNamespace = null;
|
61
|
String citationAttribute = null;
|
62
|
return new DbImportTaxIncludedInMapper<>(dbChildAttribute, dbChildNamespace, dbParentAttribute, parentNamespace, dbAlternativeParentAttribute, alternativeParentNamespace, dbTreeAttribute, citationAttribute, citationNamespace);
|
63
|
}
|
64
|
|
65
|
//******************************* ATTRIBUTES ***************************************/
|
66
|
private String fromAttribute;
|
67
|
private String toAttribute;
|
68
|
|
69
|
private String fromNamespace;
|
70
|
private String toNamespace;
|
71
|
|
72
|
private String citationAttribute;
|
73
|
private String citationNamespace;
|
74
|
|
75
|
private String microCitationAttribute;
|
76
|
private String treeAttribute;
|
77
|
private String alternativeAttribute;
|
78
|
private String alternativeNamespace;
|
79
|
|
80
|
//********************************* CONSTRUCTOR ****************************************/
|
81
|
|
82
|
protected DbImportTaxIncludedInMapper(String fromAttribute, String fromNamespace,
|
83
|
String toAttribute, String toNamespace, String alternativeAttribute, String alternativeNamespace, String treeAttribute, String citationAttribute, String citationNamespace) {
|
84
|
//TODO make it a single attribute mapper
|
85
|
this.fromAttribute = fromAttribute;
|
86
|
this.fromNamespace = fromNamespace;
|
87
|
this.toAttribute = toAttribute;
|
88
|
this.toNamespace = toNamespace;
|
89
|
this.treeAttribute = treeAttribute;
|
90
|
this.alternativeAttribute = alternativeAttribute;
|
91
|
this.alternativeNamespace = alternativeNamespace;
|
92
|
this.citationAttribute = citationAttribute;
|
93
|
this.citationNamespace = citationNamespace;
|
94
|
}
|
95
|
|
96
|
//************************************ METHODS *******************************************/
|
97
|
|
98
|
@Override
|
99
|
public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
|
100
|
STATE state = getState();
|
101
|
CdmImportBase<?,?> currentImport = state.getCurrentIO();
|
102
|
if (currentImport instanceof ICheckIgnoreMapper){
|
103
|
boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
|
104
|
if (ignoreRecord){
|
105
|
return cdmBase;
|
106
|
}
|
107
|
}
|
108
|
|
109
|
TaxonBase<?> fromObject = (TaxonBase<?>)getRelatedObject(rs, fromAttribute, fromNamespace);
|
110
|
TaxonBase<?> toObject = (TaxonBase<?>)getRelatedObject(rs, toAttribute, toNamespace);
|
111
|
TaxonBase<?> alternativeToObject = (TaxonBase<?>)getRelatedObject(rs, alternativeAttribute, alternativeNamespace);
|
112
|
|
113
|
String fromId = String.valueOf(rs.getObject(fromAttribute));
|
114
|
String toId = rs.getObject(toAttribute) == null ? null : String.valueOf(rs.getObject(toAttribute));
|
115
|
String alternativeToId = rs.getObject(alternativeAttribute) == null ? null : String.valueOf(rs.getObject(alternativeAttribute));
|
116
|
|
117
|
if (toId == null){
|
118
|
return fromObject;
|
119
|
}
|
120
|
|
121
|
Reference citation = (Reference)getRelatedObject(rs, citationAttribute, citationNamespace);
|
122
|
String microCitation = null;
|
123
|
if (citationAttribute != null){
|
124
|
microCitation = rs.getString(microCitationAttribute);
|
125
|
}
|
126
|
//TODO check int
|
127
|
Integer treeFk = null;
|
128
|
if (treeAttribute != null){
|
129
|
treeFk = rs.getInt(treeAttribute);
|
130
|
}
|
131
|
|
132
|
if (fromObject == null){
|
133
|
String warning = "The child taxon could not be found. Child taxon not added to the tree";
|
134
|
logger.warn(warning);
|
135
|
return cdmBase;
|
136
|
}
|
137
|
Taxon fromTaxon;
|
138
|
try {
|
139
|
fromTaxon = checkTaxonType(fromObject, "Child", fromId);
|
140
|
} catch (IllegalArgumentException e2) {
|
141
|
//fromTaxon is null
|
142
|
return cdmBase;
|
143
|
}
|
144
|
|
145
|
if (toObject == null){
|
146
|
String warning = "The parent taxon could not be found. Child taxon (" + fromTaxon.getTitleCache() + "; " + fromTaxon.getUuid() + ") not added to the tree";
|
147
|
logger.warn(warning);
|
148
|
return cdmBase;
|
149
|
}
|
150
|
|
151
|
Taxon toTaxon;
|
152
|
try {
|
153
|
toTaxon = checkTaxonType(toObject, "Parent", toId);
|
154
|
} catch (IllegalArgumentException e) {
|
155
|
if (alternativeToObject != null){
|
156
|
try {
|
157
|
toTaxon = checkTaxonType(alternativeToObject, "Alternative parent", alternativeToId);
|
158
|
} catch (IllegalArgumentException e1) {
|
159
|
logger.warn("Alternative taxon is of wrong type: " + alternativeToObject.getTitleCache() + "; " + alternativeToObject.getUuid());
|
160
|
return cdmBase;
|
161
|
}
|
162
|
}else{
|
163
|
|
164
|
return cdmBase;
|
165
|
}
|
166
|
}
|
167
|
|
168
|
if (fromTaxon.equals(toTaxon)){
|
169
|
String warning = "A taxon may not be a child of itself. Taxon not added to the tree: " + toTaxon.getTitleCache() + ", " + toTaxon.getLsid().toString();
|
170
|
logger.warn(warning);
|
171
|
return cdmBase;
|
172
|
}
|
173
|
//maps the reference
|
174
|
makeTaxonomicallyIncluded(state, treeFk, fromTaxon, toTaxon, citation, microCitation);
|
175
|
return fromTaxon;
|
176
|
}
|
177
|
|
178
|
|
179
|
|
180
|
|
181
|
|
182
|
/**
|
183
|
* TODO copied from BM import. May be more generic
|
184
|
* @param state
|
185
|
* @param classificationMap
|
186
|
* @param treeRefFk
|
187
|
* @param child
|
188
|
* @param parent
|
189
|
* @param citation
|
190
|
* @param microCitation
|
191
|
* @return
|
192
|
*/
|
193
|
|
194
|
public static final String TAXONOMIC_TREE_NAMESPACE = "Classification";
|
195
|
|
196
|
private boolean makeTaxonomicallyIncluded(STATE state, Integer classificationRefFk, Taxon child, Taxon parent, Reference citation, String microCitation){
|
197
|
String treeKey;
|
198
|
UUID treeUuid;
|
199
|
if (classificationRefFk == null){
|
200
|
treeKey = "1"; // there is only one tree and it gets the key '1'
|
201
|
treeUuid = state.getConfig().getClassificationUuid();
|
202
|
}else{
|
203
|
treeKey =String.valueOf(classificationRefFk);
|
204
|
treeUuid = state.getTreeUuidByTreeKey(treeKey);
|
205
|
}
|
206
|
Classification tree = (Classification)state.getRelatedObject(TAXONOMIC_TREE_NAMESPACE, treeKey);
|
207
|
if (tree == null){
|
208
|
IClassificationService service = state.getCurrentIO().getClassificationService();
|
209
|
tree = service.find(treeUuid);
|
210
|
if (tree == null){
|
211
|
String treeName = state.getConfig().getClassificationName();
|
212
|
tree = Classification.NewInstance(treeName);
|
213
|
tree.setUuid(treeUuid);
|
214
|
//FIXME tree reference
|
215
|
//tree.setReference(ref);
|
216
|
service.save(tree);
|
217
|
}
|
218
|
state.addRelatedObject(TAXONOMIC_TREE_NAMESPACE, treeKey, tree);
|
219
|
}
|
220
|
|
221
|
TaxonNode childNode = tree.addParentChild(parent, child, citation, microCitation);
|
222
|
return (childNode != null);
|
223
|
}
|
224
|
|
225
|
|
226
|
/**
|
227
|
* //TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
|
228
|
* @param rs
|
229
|
* @param dbAttribute
|
230
|
* @return
|
231
|
* @throws SQLException
|
232
|
*/
|
233
|
@Override
|
234
|
protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute, String namespace) throws SQLException {
|
235
|
CdmBase result = null;
|
236
|
if (dbAttribute != null){
|
237
|
Object dbValue = rs.getObject(dbAttribute);
|
238
|
String id = String.valueOf(dbValue);
|
239
|
DbImportStateBase<?,?> state = importMapperHelper.getState();
|
240
|
result = state.getRelatedObject(namespace, id);
|
241
|
}
|
242
|
return result;
|
243
|
}
|
244
|
|
245
|
|
246
|
/**
|
247
|
* Checks if cdmBase is of type Taxon
|
248
|
* @param taxonBase
|
249
|
* @param typeString
|
250
|
* @param id
|
251
|
* @return
|
252
|
*/
|
253
|
private Taxon checkTaxonType(TaxonBase<?> taxonBase, String typeString, String id) throws IllegalArgumentException{
|
254
|
if (! taxonBase.isInstanceOf(Taxon.class)){
|
255
|
String warning = typeString + " (" + id + ") is not of type Taxon but of type " + taxonBase.getClass().getSimpleName();
|
256
|
logger.warn(warning);
|
257
|
throw new IllegalArgumentException(warning);
|
258
|
}
|
259
|
return (CdmBase.deproxy(taxonBase, Taxon.class));
|
260
|
}
|
261
|
}
|