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.pesi.indexFungorum;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
import org.springframework.transaction.TransactionStatus;
|
22
|
|
23
|
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
|
24
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
25
|
import eu.etaxonomy.cdm.model.common.Marker;
|
26
|
import eu.etaxonomy.cdm.model.name.Rank;
|
27
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
28
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
29
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
30
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
31
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
32
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
33
|
|
34
|
/**
|
35
|
* @author a.mueller
|
36
|
* @since 27.02.2012
|
37
|
*/
|
38
|
@Component
|
39
|
public class IndexFungorumHigherClassificationImport extends IndexFungorumImportBase {
|
40
|
|
41
|
private static final long serialVersionUID = -6841466146506309309L;
|
42
|
private static final Logger logger = Logger.getLogger(IndexFungorumHigherClassificationImport.class);
|
43
|
|
44
|
private static final String pluralString = "higher classifications";
|
45
|
private static final String dbTableName = "tblPESIfungi-Classification";
|
46
|
|
47
|
public IndexFungorumHigherClassificationImport(){
|
48
|
super(pluralString, dbTableName, null);
|
49
|
}
|
50
|
|
51
|
@Override
|
52
|
protected String getRecordQuery(IndexFungorumImportConfigurator config) {
|
53
|
String strRecordQuery =
|
54
|
" SELECT DISTINCT [Kingdom name], [Phylum name], [Subphylum name], [Class name], [Subclass name], [Order name], [Family name], g.[NAME OF FUNGUS] as GenusName, c.PreferredName as SpeciesName " +
|
55
|
" FROM [tblPESIfungi-Classification] c LEFT OUTER JOIN " +
|
56
|
" tblGenera g ON c.PreferredNameFDCnumber = g.[RECORD NUMBER]" +
|
57
|
" ORDER BY [Kingdom name], [Phylum name], [Subphylum name], [Class name], [Subclass name], [Order name], [Family name], GenusName, SpeciesName ";
|
58
|
return strRecordQuery;
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
protected void doInvoke(IndexFungorumImportState state) {
|
63
|
|
64
|
logger.info("Start higher classification ...");
|
65
|
|
66
|
String sql = getRecordQuery(state.getConfig());
|
67
|
ResultSet rs = state.getConfig().getSource().getResultSet(sql);
|
68
|
|
69
|
//only 1 partition here
|
70
|
|
71
|
String lastKingdom = "";
|
72
|
String lastPhylum = "";
|
73
|
String lastSubphylum = "";
|
74
|
String lastClassname = "";
|
75
|
String lastSubclass = "";
|
76
|
String lastOrder = "";
|
77
|
String lastFamily = "";
|
78
|
|
79
|
Taxon taxonKingdom = null;
|
80
|
Taxon taxonPhylum = null;
|
81
|
Taxon taxonSubphylum = null;
|
82
|
Taxon taxonClass = null;
|
83
|
Taxon taxonSubclass = null;
|
84
|
Taxon taxonOrder = null;
|
85
|
Taxon taxonFamily = null;
|
86
|
|
87
|
Taxon higherTaxon = null;
|
88
|
|
89
|
TransactionStatus tx = startTransaction();
|
90
|
ResultSet rsRelatedObjects = state.getConfig().getSource().getResultSet(sql);
|
91
|
state.setRelatedObjects(getRelatedObjectsForPartition(rsRelatedObjects, state));
|
92
|
|
93
|
Classification classification = getClassification(state);
|
94
|
|
95
|
try {
|
96
|
while (rs.next()){
|
97
|
String kingdom = rs.getString("Kingdom name");
|
98
|
String phylum = rs.getString("Phylum name");
|
99
|
String subphylum = rs.getString("Subphylum name");
|
100
|
String classname = rs.getString("Class name");
|
101
|
String subclass = rs.getString("Subclass name");
|
102
|
String order = rs.getString("Order name");
|
103
|
String family = rs.getString("Family name");
|
104
|
|
105
|
if (isNewTaxon(family, lastFamily)){
|
106
|
if (isNewTaxon(order,lastOrder)){
|
107
|
if (isNewTaxon(subclass,lastSubclass)){
|
108
|
if (isNewTaxon(classname,lastClassname)){
|
109
|
if (isNewTaxon(subphylum, lastSubphylum)){
|
110
|
if (isNewTaxon(phylum,lastPhylum)){
|
111
|
if (isNewTaxon(kingdom,lastKingdom)){
|
112
|
taxonKingdom = makeTaxon(state, kingdom, Rank.KINGDOM());
|
113
|
lastKingdom = kingdom;
|
114
|
logger.info("Import kingdom " + kingdom);
|
115
|
getTaxonService().saveOrUpdate(taxonKingdom);
|
116
|
}else{
|
117
|
higherTaxon = taxonKingdom;
|
118
|
}
|
119
|
higherTaxon = isIncertisSedis(kingdom) ? higherTaxon : taxonKingdom;
|
120
|
Rank newRank = (lastKingdom.equals("Fungi") ? null : Rank.PHYLUM());
|
121
|
taxonPhylum = makeTaxon(state, phylum, newRank);
|
122
|
if (taxonPhylum != null){
|
123
|
classification.addParentChild(higherTaxon, taxonPhylum, null, null);
|
124
|
}
|
125
|
higherTaxon = isIncertisSedis(phylum) ? higherTaxon : taxonPhylum;
|
126
|
lastPhylum = phylum;
|
127
|
logger.info("Import Phylum " + phylum);
|
128
|
}else{
|
129
|
higherTaxon = taxonPhylum;
|
130
|
}
|
131
|
Rank newRank = (lastKingdom.equals("Fungi") ? null : Rank.SUBPHYLUM());
|
132
|
taxonSubphylum = makeTaxon(state, subphylum, newRank);
|
133
|
if (taxonSubphylum != null){
|
134
|
getClassification(state).addParentChild(higherTaxon,taxonSubphylum, null, null);
|
135
|
}
|
136
|
higherTaxon = isIncertisSedis(subphylum) ? higherTaxon : taxonSubphylum;
|
137
|
lastSubphylum = subphylum;
|
138
|
}else{
|
139
|
higherTaxon = taxonSubphylum;
|
140
|
}
|
141
|
taxonClass = makeTaxon(state, classname, Rank.CLASS());
|
142
|
if (taxonClass != null){
|
143
|
getClassification(state).addParentChild(higherTaxon, taxonClass, null, null);
|
144
|
}
|
145
|
higherTaxon = isIncertisSedis(classname) ? higherTaxon : taxonClass;
|
146
|
lastClassname = classname;
|
147
|
}else{
|
148
|
higherTaxon = taxonClass;
|
149
|
}
|
150
|
taxonSubclass = makeTaxon(state, subclass, Rank.SUBCLASS());
|
151
|
if (taxonSubclass != null){
|
152
|
getClassification(state).addParentChild(higherTaxon, taxonSubclass,null, null);
|
153
|
}
|
154
|
higherTaxon = isIncertisSedis(subclass) ? higherTaxon : taxonSubclass;
|
155
|
lastSubclass = subclass;
|
156
|
}else{
|
157
|
higherTaxon = taxonSubclass;
|
158
|
}
|
159
|
taxonOrder = makeTaxon(state, order, Rank.ORDER());
|
160
|
if (taxonOrder != null){
|
161
|
getClassification(state).addParentChild(higherTaxon, taxonOrder, null, null);
|
162
|
}
|
163
|
higherTaxon = isIncertisSedis(order) ? higherTaxon : taxonOrder;
|
164
|
lastOrder = order;
|
165
|
}else{
|
166
|
higherTaxon = taxonOrder;
|
167
|
}
|
168
|
taxonFamily = makeTaxon(state, family, Rank.FAMILY());
|
169
|
if (taxonFamily != null){
|
170
|
try{
|
171
|
//if this shows a warning see single issue in #2826 about Glomerellaceae (which has 2 different parents)
|
172
|
getClassification(state).addParentChild(higherTaxon, taxonFamily, null, null);
|
173
|
}catch(IllegalStateException e){
|
174
|
if (e.getMessage().startsWith("The child taxon is already part of the tree")){
|
175
|
//TaxonNode node = getClassification(state).getNode(taxonFamily);
|
176
|
logger.warn(e.getMessage() + taxonFamily.getTitleCache() + " " + higherTaxon.getTitleCache());
|
177
|
}
|
178
|
}
|
179
|
}
|
180
|
higherTaxon = isIncertisSedis(family) ? higherTaxon : taxonFamily;
|
181
|
lastFamily = family;
|
182
|
getTaxonService().saveOrUpdate(higherTaxon);
|
183
|
}
|
184
|
getTaxonService().saveOrUpdate(higherTaxon);
|
185
|
}
|
186
|
} catch (SQLException e) {
|
187
|
e.printStackTrace();
|
188
|
logger.error(e.getMessage());
|
189
|
tx.setRollbackOnly();
|
190
|
state.setSuccess(false);
|
191
|
}
|
192
|
commitTransaction(tx);
|
193
|
logger.info("End higher classification ...");
|
194
|
|
195
|
return;
|
196
|
}
|
197
|
|
198
|
private boolean isIncertisSedis(String uninomial) {
|
199
|
return uninomial.equalsIgnoreCase(INCERTAE_SEDIS) || uninomial.equalsIgnoreCase(FOSSIL_FUNGI);
|
200
|
}
|
201
|
|
202
|
private boolean isNewTaxon(String uninomial, String lastUninomial) {
|
203
|
boolean result = !uninomial.equalsIgnoreCase(lastUninomial);
|
204
|
result |= lastUninomial.equalsIgnoreCase(INCERTAE_SEDIS);
|
205
|
result |= lastUninomial.equalsIgnoreCase(FOSSIL_FUNGI);
|
206
|
return result;
|
207
|
}
|
208
|
|
209
|
private Taxon makeTaxon(IndexFungorumImportState state, String uninomial, Rank newRank) {
|
210
|
if (uninomial.equalsIgnoreCase(INCERTAE_SEDIS) || uninomial.equalsIgnoreCase(FOSSIL_FUNGI)){
|
211
|
return null;
|
212
|
}
|
213
|
Taxon taxon = state.getRelatedObject(IndexFungorumImportBase.NAMESPACE_SUPRAGENERIC_NAMES, uninomial, Taxon.class);
|
214
|
if (taxon == null){
|
215
|
if (! newRank.equals(Rank.KINGDOM())){
|
216
|
logger.warn("Taxon not found for uninomial " + uninomial);
|
217
|
}
|
218
|
TaxonName name = TaxonNameFactory.NewBotanicalInstance(newRank);
|
219
|
name.setGenusOrUninomial(uninomial);
|
220
|
Reference sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
|
221
|
taxon = Taxon.NewInstance(name, sourceReference);
|
222
|
taxon.addMarker(Marker.NewInstance(getMissingGUIDMarkerType(state), true));
|
223
|
}else if (newRank != null){
|
224
|
taxon.getName().setRank(newRank);
|
225
|
}
|
226
|
return taxon;
|
227
|
}
|
228
|
|
229
|
@Override
|
230
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
|
231
|
String nameSpace;
|
232
|
Class<?> cdmClass;
|
233
|
Set<String> idSet;
|
234
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
235
|
|
236
|
try{
|
237
|
// Set<String> taxonNameSet = new HashSet<>();
|
238
|
// while (rs.next()){
|
239
|
// handleForeignKey(rs, taxonIdSet,"tu_accfinal" );
|
240
|
// }
|
241
|
|
242
|
//taxon map
|
243
|
nameSpace = IndexFungorumImportBase.NAMESPACE_SUPRAGENERIC_NAMES ;
|
244
|
cdmClass = TaxonBase.class;
|
245
|
// idSet = taxonNameSet;
|
246
|
Map<String, TaxonBase<?>> taxonMap = new HashMap<>();
|
247
|
List<Taxon> list = getTaxonService().list(Taxon.class, null, null, null, null);
|
248
|
for (Taxon taxon : list){
|
249
|
taxonMap.put(CdmBase.deproxy(taxon.getName()).getGenusOrUninomial(), taxon);
|
250
|
}
|
251
|
result.put(nameSpace, taxonMap);
|
252
|
|
253
|
//source reference
|
254
|
Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
|
255
|
Map<String, Reference> referenceMap = new HashMap<>();
|
256
|
referenceMap.put(SOURCE_REFERENCE, sourceReference);
|
257
|
result.put(NAMESPACE_REFERENCE, referenceMap);
|
258
|
|
259
|
} catch (Exception e) {
|
260
|
throw new RuntimeException(e);
|
261
|
}
|
262
|
return result;
|
263
|
}
|
264
|
|
265
|
@Override
|
266
|
protected boolean doCheck(IndexFungorumImportState state){
|
267
|
return true;
|
268
|
}
|
269
|
|
270
|
@Override
|
271
|
protected boolean isIgnore(IndexFungorumImportState state){
|
272
|
return ! state.getConfig().isDoRelTaxa();
|
273
|
}
|
274
|
}
|