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.ArrayList;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.HashSet;
|
17
|
import java.util.List;
|
18
|
import java.util.Map;
|
19
|
import java.util.Set;
|
20
|
import java.util.UUID;
|
21
|
|
22
|
import org.apache.commons.lang.StringUtils;
|
23
|
import org.apache.log4j.Logger;
|
24
|
import org.springframework.stereotype.Component;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
|
27
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
28
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
29
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
30
|
import eu.etaxonomy.cdm.io.common.Source;
|
31
|
import eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer;
|
32
|
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
|
33
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
34
|
import eu.etaxonomy.cdm.model.common.Extension;
|
35
|
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
36
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
37
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
38
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
39
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
40
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
41
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
42
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
43
|
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
|
44
|
|
45
|
|
46
|
/**
|
47
|
* @author a.mueller
|
48
|
* @created 27.02.2012
|
49
|
*/
|
50
|
@Component
|
51
|
public class IndexFungorumSpeciesImport extends IndexFungorumImportBase {
|
52
|
private static final Logger logger = Logger.getLogger(IndexFungorumSpeciesImport.class);
|
53
|
|
54
|
private static final String pluralString = "species";
|
55
|
private static final String dbTableName = "[tblPESIfungi-IFdata]";
|
56
|
private final Set<UUID> infraspecificTaxaUUIDs = new HashSet<UUID>();
|
57
|
|
58
|
public IndexFungorumSpeciesImport(){
|
59
|
super(pluralString, dbTableName, null);
|
60
|
|
61
|
}
|
62
|
|
63
|
|
64
|
|
65
|
|
66
|
@Override
|
67
|
protected String getIdQuery() {
|
68
|
String result = " SELECT PreferredNameIFnumber FROM " + getTableName() +
|
69
|
" ORDER BY PreferredName ";
|
70
|
return result;
|
71
|
}
|
72
|
|
73
|
|
74
|
|
75
|
|
76
|
/* (non-Javadoc)
|
77
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
78
|
*/
|
79
|
@Override
|
80
|
protected String getRecordQuery(IndexFungorumImportConfigurator config) {
|
81
|
String strRecordQuery =
|
82
|
" SELECT DISTINCT distribution.PreferredNameFDCnumber, species.* , cl.[Phylum name]" +
|
83
|
" FROM tblPESIfungi AS distribution RIGHT OUTER JOIN dbo.[tblPESIfungi-IFdata] AS species ON distribution.PreferredNameIFnumber = species.PreferredNameIFnumber " +
|
84
|
" LEFT OUTER JOIN [tblPESIfungi-Classification] cl ON species.PreferredName = cl.PreferredName " +
|
85
|
" WHERE ( species.PreferredNameIFnumber IN (" + ID_LIST_TOKEN + ") )" +
|
86
|
"";
|
87
|
return strRecordQuery;
|
88
|
}
|
89
|
@Override
|
90
|
protected void doInvoke(IndexFungorumImportState state){
|
91
|
System.out.println("start make " + getPluralString() + " ...");
|
92
|
IndexFungorumImportConfigurator config = state.getConfig();
|
93
|
Source source = config.getSource();
|
94
|
|
95
|
super.doInvoke(state);
|
96
|
Classification classification = getClassification(state);
|
97
|
List<TaxonBase> infraspecificTaxa = new ArrayList<TaxonBase>();
|
98
|
for (UUID uuid: infraspecificTaxaUUIDs){
|
99
|
infraspecificTaxa.add(getTaxonService().load(uuid));
|
100
|
}
|
101
|
|
102
|
System.out.println("create infraspecific - specific relationship: " + infraspecificTaxa.size() + " taxa");
|
103
|
for (TaxonBase infraspecificTaxon: infraspecificTaxa){
|
104
|
HibernateProxyHelper.deproxy(infraspecificTaxon);
|
105
|
TaxonNameBase name = infraspecificTaxon.getName();
|
106
|
|
107
|
UUID uuid = getNameService().saveOrUpdate(name);
|
108
|
String parentNameString = getParentNameInfraSpecific(name);
|
109
|
System.out.println("Parent name string: " + parentNameString);
|
110
|
MatchingTaxonConfigurator matchingConfig = new MatchingTaxonConfigurator();
|
111
|
matchingConfig.setTaxonNameTitle(parentNameString);
|
112
|
List<String> propertyPaths = new ArrayList<String>();
|
113
|
propertyPaths.add("taxonNodes.*");
|
114
|
propertyPaths.add("taxonNodes.classification");
|
115
|
propertyPaths.add("taxonNodes.childNodes.*");
|
116
|
propertyPaths.add("taxonNodes.childNodes.taxon.*");
|
117
|
propertyPaths.add("taxonNodes.parent.*");
|
118
|
propertyPaths.add("taxonNodes.parent.taxon.*");
|
119
|
matchingConfig.setPropertyPath(propertyPaths);
|
120
|
List<TaxonBase> potentialParents = getTaxonService().findTaxaByName(matchingConfig);
|
121
|
//Taxon.class, parentNameString + "sec. ", MatchMode.BEGINNING, , pageSize, pageNumber, orderHints, propertyPaths)
|
122
|
//.searchNames(String uninomial,String infraGenericEpithet, String specificEpithet, String infraspecificEpithet, Rank rank, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
|
123
|
if (potentialParents.size()>1){
|
124
|
for (TaxonBase potentialParent:potentialParents){
|
125
|
if (potentialParent.getTitleCache().equals(parentNameString + " sec*")){
|
126
|
classification.addParentChild((Taxon)potentialParent, (Taxon)infraspecificTaxon, null, null);
|
127
|
}
|
128
|
}
|
129
|
}else if (!potentialParents.isEmpty()){
|
130
|
Taxon parent = HibernateProxyHelper.deproxy(potentialParents.get(0), Taxon.class);
|
131
|
Taxon child = (Taxon)getTaxonService().load(infraspecificTaxon.getUuid(), propertyPaths);
|
132
|
classification.addParentChild(parent, child, null, null);
|
133
|
} else{
|
134
|
System.out.println("No parent for: " + name.getTitleCache());
|
135
|
}
|
136
|
getTaxonService().saveOrUpdate(infraspecificTaxon);
|
137
|
}
|
138
|
|
139
|
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public boolean doPartition(ResultSetPartitioner partitioner, IndexFungorumImportState state) {
|
144
|
boolean success = true;
|
145
|
Reference<?> sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
|
146
|
ResultSet rs = partitioner.getResultSet();
|
147
|
Classification classification = getClassification(state);
|
148
|
|
149
|
try {
|
150
|
while (rs.next()){
|
151
|
|
152
|
//DisplayName, NomRefCache -> don't use, created by Marc
|
153
|
|
154
|
Integer id = (Integer)rs.getObject("PreferredNameIFnumber");
|
155
|
String phylumName = rs.getString("Phylum name");
|
156
|
|
157
|
String preferredName = rs.getString("PreferredName");
|
158
|
if (StringUtils.isBlank(preferredName)){
|
159
|
logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD UMBER" + CdmUtils.Nz(id));
|
160
|
}
|
161
|
|
162
|
//Rank rank = Rank.SPECIES();
|
163
|
|
164
|
NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
|
165
|
NonViralName<?> name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICNAFP, null);
|
166
|
|
167
|
Taxon taxon = Taxon.NewInstance(name, sourceReference);
|
168
|
//if name is infraspecific the parent should be the species not the genus
|
169
|
Taxon parent;
|
170
|
if (!name.isInfraSpecific()){
|
171
|
parent = getParentTaxon(state, rs);
|
172
|
if (parent == null){
|
173
|
logger.warn("parent not found for name:" +preferredName);
|
174
|
}
|
175
|
classification.addParentChild(parent, taxon, null, null);
|
176
|
}
|
177
|
|
178
|
//author + publication
|
179
|
makeAuthorAndPublication(state, rs, name);
|
180
|
//source
|
181
|
makeSource(state, taxon, id, NAMESPACE_SPECIES );
|
182
|
|
183
|
//fossil
|
184
|
if (FOSSIL_FUNGI.equalsIgnoreCase(phylumName)){
|
185
|
ExtensionType fossilExtType = getExtensionType(state, ErmsTransformer.uuidFossilStatus, "fossil status", "fossil status", "fos. stat.");
|
186
|
Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType);
|
187
|
}
|
188
|
//save
|
189
|
|
190
|
UUID uuidTaxon = getTaxonService().saveOrUpdate(taxon);
|
191
|
getNameService().saveOrUpdate(name);
|
192
|
if (name.isInfraSpecific()){
|
193
|
infraspecificTaxaUUIDs.add(uuidTaxon);
|
194
|
}
|
195
|
|
196
|
}
|
197
|
|
198
|
|
199
|
} catch (Exception e) {
|
200
|
e.printStackTrace();
|
201
|
logger.error(e.getMessage());
|
202
|
state.setSuccess(false);
|
203
|
success = false;
|
204
|
}
|
205
|
return success;
|
206
|
}
|
207
|
|
208
|
|
209
|
/**
|
210
|
* @param taxon
|
211
|
* @return
|
212
|
*/
|
213
|
private String getParentNameInfraSpecific(TaxonNameBase taxonName){
|
214
|
NonViralName<NonViralName> name = HibernateProxyHelper.deproxy(taxonName, NonViralName.class);
|
215
|
String parentName = name.getGenusOrUninomial() + " " + name.getSpecificEpithet();
|
216
|
|
217
|
return parentName;
|
218
|
}
|
219
|
|
220
|
|
221
|
|
222
|
|
223
|
private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
|
224
|
Integer genusId = rs.getInt("PreferredNameFDCnumber");
|
225
|
|
226
|
Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
|
227
|
if (taxon == null){
|
228
|
logger.warn("Taxon not found for " + genusId);
|
229
|
}
|
230
|
return taxon;
|
231
|
}
|
232
|
|
233
|
|
234
|
@Override
|
235
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
|
236
|
String nameSpace;
|
237
|
Class<?> cdmClass;
|
238
|
Set<String> idSet;
|
239
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
240
|
|
241
|
try{
|
242
|
Set<String> taxonIdSet = new HashSet<String>();
|
243
|
Set<String> taxonSpeciesNames = new HashSet<String>();
|
244
|
while (rs.next()){
|
245
|
handleForeignKey(rs, taxonIdSet,"PreferredNameFDCnumber" );
|
246
|
handleForeignKey(rs, taxonSpeciesNames, "PreferredName");
|
247
|
}
|
248
|
|
249
|
//taxon map
|
250
|
nameSpace = NAMESPACE_GENERA;
|
251
|
cdmClass = TaxonBase.class;
|
252
|
idSet = taxonIdSet;
|
253
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
254
|
result.put(nameSpace, taxonMap);
|
255
|
|
256
|
|
257
|
//sourceReference
|
258
|
Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
|
259
|
Map<String, Reference> referenceMap = new HashMap<String, Reference>();
|
260
|
referenceMap.put(SOURCE_REFERENCE, sourceReference);
|
261
|
result.put(NAMESPACE_REFERENCE, referenceMap);
|
262
|
|
263
|
} catch (SQLException e) {
|
264
|
throw new RuntimeException(e);
|
265
|
}
|
266
|
return result;
|
267
|
}
|
268
|
|
269
|
@Override
|
270
|
protected boolean doCheck(IndexFungorumImportState state){
|
271
|
return true;
|
272
|
}
|
273
|
|
274
|
@Override
|
275
|
protected boolean isIgnore(IndexFungorumImportState state){
|
276
|
return ! state.getConfig().isDoTaxa();
|
277
|
}
|
278
|
|
279
|
|
280
|
|
281
|
|
282
|
|
283
|
}
|