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.HashSet;
|
16
|
import java.util.Map;
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.apache.commons.lang.StringUtils;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
25
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
26
|
import eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer;
|
27
|
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.common.Extension;
|
30
|
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
31
|
import eu.etaxonomy.cdm.model.name.INonViralName;
|
32
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
33
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
34
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
36
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
37
|
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
|
38
|
|
39
|
/**
|
40
|
* @author a.mueller
|
41
|
* @since 27.02.2012
|
42
|
*/
|
43
|
@Component
|
44
|
public class IndexFungorumSpeciesImport extends IndexFungorumImportBase {
|
45
|
|
46
|
private static final long serialVersionUID = -1148034079632876980L;
|
47
|
private static final Logger logger = Logger.getLogger(IndexFungorumSpeciesImport.class);
|
48
|
|
49
|
private static final String pluralString = "species";
|
50
|
private static final String dbTableName = "[tblPESIfungi-IFdata]";
|
51
|
|
52
|
public IndexFungorumSpeciesImport(){
|
53
|
super(pluralString, dbTableName, null);
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
protected String getIdQuery() {
|
58
|
String result = " SELECT PreferredNameIFnumber "
|
59
|
+ " FROM " + getTableName()
|
60
|
+ " ORDER BY PreferredName ";
|
61
|
return result;
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
protected String getRecordQuery(IndexFungorumImportConfigurator config) {
|
66
|
String strRecordQuery =
|
67
|
" SELECT DISTINCT distribution.PreferredNameFDCnumber, species.* , cl.[Phylum name]"
|
68
|
+ " FROM tblPESIfungi AS distribution "
|
69
|
+ " RIGHT OUTER JOIN dbo.[tblPESIfungi-IFdata] AS species ON distribution.PreferredNameIFnumber = species.PreferredNameIFnumber " +
|
70
|
" LEFT OUTER JOIN [tblPESIfungi-Classification] cl ON species.PreferredName = cl.PreferredName "
|
71
|
+ " WHERE ( species.PreferredNameIFnumber IN (" + ID_LIST_TOKEN + ") )" +
|
72
|
"";
|
73
|
return strRecordQuery;
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner,
|
78
|
IndexFungorumImportState state) {
|
79
|
|
80
|
boolean success = true;
|
81
|
Reference sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
|
82
|
ResultSet rs = partitioner.getResultSet();
|
83
|
Classification classification = getClassification(state);
|
84
|
|
85
|
try {
|
86
|
while (rs.next()){
|
87
|
|
88
|
//DisplayName, NomRefCache -> don't use, created by Marc
|
89
|
|
90
|
Integer id = (Integer)rs.getObject("PreferredNameIFnumber");
|
91
|
String phylumName = rs.getString("Phylum name");
|
92
|
|
93
|
String preferredName = rs.getString("PreferredName");
|
94
|
if (StringUtils.isBlank(preferredName)){
|
95
|
logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD UMBER" + CdmUtils.Nz(id));
|
96
|
}
|
97
|
|
98
|
//Rank rank = Rank.SPECIES();
|
99
|
|
100
|
NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
|
101
|
INonViralName name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICNAFP, null);
|
102
|
|
103
|
Taxon taxon = Taxon.NewInstance(name, sourceReference);
|
104
|
//if name is infraspecific the parent should be the species not the genus
|
105
|
Taxon parent;
|
106
|
if (!name.isInfraSpecific()){
|
107
|
parent = getParentTaxon(state, rs);
|
108
|
if (parent == null){
|
109
|
logger.warn("parent not found for name:" +preferredName);
|
110
|
}
|
111
|
classification.addParentChild(parent, taxon, null, null);
|
112
|
}
|
113
|
|
114
|
//author + publication
|
115
|
makeAuthorAndPublication(state, rs, name);
|
116
|
//source
|
117
|
makeSource(state, taxon, id, NAMESPACE_SPECIES );
|
118
|
|
119
|
//fossil
|
120
|
if (FOSSIL_FUNGI.equalsIgnoreCase(phylumName)){
|
121
|
ExtensionType fossilExtType = getExtensionType(state, ErmsTransformer.uuidExtFossilStatus, "fossil status", "fossil status", "fos. stat.");
|
122
|
Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType);
|
123
|
}
|
124
|
//save
|
125
|
|
126
|
UUID uuidTaxon = getTaxonService().saveOrUpdate(taxon);
|
127
|
//getNameService().saveOrUpdate(name);
|
128
|
if (name.isInfraSpecific()){
|
129
|
state.getInfraspecificTaxaUUIDs().add(uuidTaxon);
|
130
|
}
|
131
|
}
|
132
|
} catch (Exception e) {
|
133
|
e.printStackTrace();
|
134
|
logger.error(e.getMessage());
|
135
|
state.setSuccess(false);
|
136
|
success = false;
|
137
|
}
|
138
|
return success;
|
139
|
}
|
140
|
|
141
|
private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
|
142
|
Integer genusId = rs.getInt("PreferredNameFDCnumber");
|
143
|
|
144
|
Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
|
145
|
if (taxon == null){
|
146
|
logger.warn("Taxon not found for " + genusId);
|
147
|
}
|
148
|
return taxon;
|
149
|
}
|
150
|
|
151
|
@Override
|
152
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
|
153
|
String nameSpace;
|
154
|
Class<?> cdmClass;
|
155
|
Set<String> idSet;
|
156
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
157
|
|
158
|
try{
|
159
|
Set<String> taxonIdSet = new HashSet<>();
|
160
|
Set<String> taxonSpeciesNames = new HashSet<>();
|
161
|
while (rs.next()){
|
162
|
handleForeignKey(rs, taxonIdSet,"PreferredNameFDCnumber" );
|
163
|
handleForeignKey(rs, taxonSpeciesNames, "PreferredName");
|
164
|
}
|
165
|
|
166
|
//taxon map
|
167
|
nameSpace = NAMESPACE_GENERA;
|
168
|
idSet = taxonIdSet;
|
169
|
@SuppressWarnings({ "rawtypes" })
|
170
|
Map<String, TaxonBase> taxonMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, nameSpace);
|
171
|
result.put(nameSpace, taxonMap);
|
172
|
|
173
|
//sourceReference
|
174
|
Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
|
175
|
Map<String, Reference> referenceMap = new HashMap<>();
|
176
|
referenceMap.put(SOURCE_REFERENCE, sourceReference);
|
177
|
result.put(NAMESPACE_REFERENCE, referenceMap);
|
178
|
|
179
|
} catch (SQLException e) {
|
180
|
throw new RuntimeException(e);
|
181
|
}
|
182
|
return result;
|
183
|
}
|
184
|
|
185
|
@Override
|
186
|
protected boolean doCheck(IndexFungorumImportState state){
|
187
|
return true;
|
188
|
}
|
189
|
|
190
|
@Override
|
191
|
protected boolean isIgnore(IndexFungorumImportState state){
|
192
|
return ! state.getConfig().isDoTaxa();
|
193
|
}
|
194
|
}
|