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.redlist;
|
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.log4j.Logger;
|
21
|
import org.springframework.stereotype.Component;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
24
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
25
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
26
|
import eu.etaxonomy.cdm.io.common.TdwgAreaProvider;
|
27
|
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
|
28
|
import eu.etaxonomy.cdm.io.common.mapping.DbImportTaxIncludedInMapper;
|
29
|
import eu.etaxonomy.cdm.io.common.mapping.IMappingImport;
|
30
|
import eu.etaxonomy.cdm.io.redlist.validation.RoteListeDbTaxonImportValidator;
|
31
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
32
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
33
|
import eu.etaxonomy.cdm.model.description.PresenceTerm;
|
34
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
35
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
36
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
37
|
import eu.etaxonomy.cdm.model.name.Rank;
|
38
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
40
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
41
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
42
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
43
|
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
|
44
|
|
45
|
|
46
|
/**
|
47
|
* @author a.mueller
|
48
|
* @created 27.08.2012
|
49
|
*/
|
50
|
@Component
|
51
|
public class RoteListeDbTaxonImport extends RoteListeDbImportBase<TaxonBase> implements IMappingImport<TaxonBase, RoteListeDbImportState>{
|
52
|
private static final Logger logger = Logger.getLogger(RoteListeDbTaxonImport.class);
|
53
|
|
54
|
private NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
|
55
|
|
56
|
private Map<UUID, Taxon> higherTaxonMap;
|
57
|
|
58
|
private Integer TREE_ID = null;
|
59
|
|
60
|
private DbImportMapping mapping;
|
61
|
|
62
|
private int modCount = 10000;
|
63
|
private static final String pluralString = "taxa";
|
64
|
private static final String dbTableName = "checklist";
|
65
|
private static final Class cdmTargetClass = TaxonBase.class;
|
66
|
private static final String strOrderBy = " ORDER BY family, genus, species ";
|
67
|
|
68
|
public RoteListeDbTaxonImport(){
|
69
|
super(pluralString, dbTableName, cdmTargetClass);
|
70
|
}
|
71
|
|
72
|
|
73
|
|
74
|
/* (non-Javadoc)
|
75
|
* @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getIdQuery()
|
76
|
*/
|
77
|
@Override
|
78
|
protected String getIdQuery() {
|
79
|
String strQuery = " SELECT pk FROM " + dbTableName +
|
80
|
strOrderBy;
|
81
|
return strQuery;
|
82
|
}
|
83
|
|
84
|
|
85
|
/* (non-Javadoc)
|
86
|
* @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getMapping()
|
87
|
*/
|
88
|
protected DbImportMapping getMapping() {
|
89
|
if (mapping == null){
|
90
|
mapping = new DbImportMapping();
|
91
|
|
92
|
// mapping.addMapper(DbImportObjectCreationMapper.NewInstance(this, "pk", TAXON_NAMESPACE)); //id + tu_status
|
93
|
//
|
94
|
// UUID uuidKew = RoteListeDbTransformer.uuidAcceptedKew;
|
95
|
// mapping.addMapper(DbImportMarkerMapper.NewInstance("accepted kew", uuidKew, "Accepted Kew", "Accepted Kew", "Kew", null));
|
96
|
|
97
|
}
|
98
|
|
99
|
return mapping;
|
100
|
}
|
101
|
|
102
|
/* (non-Javadoc)
|
103
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
104
|
*/
|
105
|
@Override
|
106
|
protected String getRecordQuery(RoteListeDbImportConfigurator config) {
|
107
|
String strSelect = " SELECT * ";
|
108
|
String strFrom = " FROM checklist";
|
109
|
String strWhere = " WHERE ( pk IN (" + ID_LIST_TOKEN + ") )";
|
110
|
String strRecordQuery = strSelect + strFrom + strWhere + strOrderBy;
|
111
|
return strRecordQuery;
|
112
|
}
|
113
|
|
114
|
|
115
|
@Override
|
116
|
public boolean doPartition(ResultSetPartitioner partitioner, RoteListeDbImportState state) {
|
117
|
// higherTaxonMap = new HashMap<UUID, Taxon>();
|
118
|
// Reference genevaReference = getReferenceService().find(state.getConfig().getUuidGenevaReference());
|
119
|
// if (genevaReference == null){
|
120
|
// genevaReference = makeGenevaReference(state);
|
121
|
// getReferenceService().save(genevaReference);
|
122
|
// }
|
123
|
// state.setGenevaReference(genevaReference);
|
124
|
boolean success = super.doPartition(partitioner, state);
|
125
|
// higherTaxonMap = new HashMap<UUID, Taxon>();
|
126
|
// state.setGenevaReference(null);
|
127
|
return success;
|
128
|
}
|
129
|
|
130
|
|
131
|
@Override
|
132
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, RoteListeDbImportState state) {
|
133
|
String nameSpace;
|
134
|
Class<?> cdmClass;
|
135
|
Set<String> idSet;
|
136
|
Set<String> referenceIdSet = new HashSet<String>();
|
137
|
|
138
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
139
|
|
140
|
try{
|
141
|
while (rs.next()){
|
142
|
handleForeignKey(rs, referenceIdSet, "source");
|
143
|
}
|
144
|
|
145
|
// //reference map
|
146
|
// nameSpace = REFERENCE_NAMESPACE;
|
147
|
// cdmClass = Reference.class;
|
148
|
// idSet = referenceIdSet;
|
149
|
// Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, referenceIdSet, nameSpace);
|
150
|
// result.put(REFERENCE_NAMESPACE, referenceMap);
|
151
|
|
152
|
} catch (SQLException e) {
|
153
|
throw new RuntimeException(e);
|
154
|
}
|
155
|
return result;
|
156
|
}
|
157
|
|
158
|
|
159
|
@Override
|
160
|
public TaxonBase createObject(ResultSet rs, RoteListeDbImportState state) throws SQLException {
|
161
|
BotanicalName speciesName = BotanicalName.NewInstance(Rank.SPECIES());
|
162
|
|
163
|
// Reference sec = state.getConfig().getSourceReference();
|
164
|
// getReferenceService().saveOrUpdate(sec);
|
165
|
//
|
166
|
// String familyString = rs.getString("family");
|
167
|
// String genusString = rs.getString("genus");
|
168
|
// String speciesString = rs.getString("species");
|
169
|
// String authorityString = rs.getString("authority");
|
170
|
//
|
171
|
// if (logger.isDebugEnabled()){
|
172
|
// System.out.println(familyString + " " + genusString + " " + speciesString);
|
173
|
// }
|
174
|
//
|
175
|
// Taxon speciesTaxon = Taxon.NewInstance(speciesName, sec);;
|
176
|
// speciesName.setGenusOrUninomial(genusString);
|
177
|
// speciesName.setSpecificEpithet(speciesString);
|
178
|
// parser.handleAuthors(speciesName, CdmUtils.concat(" ", new String[] {"", genusString, speciesString, authorityString}), authorityString);
|
179
|
//
|
180
|
// //family
|
181
|
// Taxon familyTaxon = null;
|
182
|
// if (StringUtils.isNotBlank(familyString)){
|
183
|
// familyTaxon = getHigherTaxon(state, familyString, null);
|
184
|
// if (familyTaxon == null){
|
185
|
// BotanicalName familyName = BotanicalName.NewInstance(Rank.FAMILY());
|
186
|
// familyName.setGenusOrUninomial(familyString);
|
187
|
// familyTaxon = Taxon.NewInstance(familyName, sec);
|
188
|
// saveHigherTaxon(state, familyTaxon, familyString, null);
|
189
|
// }
|
190
|
// getTaxonService().saveOrUpdate(familyTaxon);
|
191
|
// }
|
192
|
//
|
193
|
//
|
194
|
// //genus
|
195
|
// Taxon genusTaxon = getHigherTaxon(state, familyString, genusString);
|
196
|
// if (genusTaxon == null){
|
197
|
// BotanicalName genusName = BotanicalName.NewInstance(Rank.GENUS());
|
198
|
// genusName.setGenusOrUninomial(genusString);
|
199
|
// genusTaxon = Taxon.NewInstance(genusName, sec);
|
200
|
// saveHigherTaxon(state, genusTaxon, familyString, genusString);
|
201
|
// if (familyTaxon != null){
|
202
|
// makeTaxonomicallyIncluded(state, TREE_ID, genusTaxon, familyTaxon, null, null);
|
203
|
// }
|
204
|
// }
|
205
|
// makeTaxonomicallyIncluded(state, TREE_ID, speciesTaxon, genusTaxon, null, null);
|
206
|
// getTaxonService().saveOrUpdate(genusTaxon);
|
207
|
//
|
208
|
// String sourceString = rs.getString("source");
|
209
|
// String sourceId = rs.getString("source_id");
|
210
|
//
|
211
|
// Reference sourceRef = state.getRelatedObject(REFERENCE_NAMESPACE, sourceString, Reference.class);
|
212
|
// speciesTaxon.addSource(sourceId, REFERENCE_NAMESPACE, sourceRef, null);
|
213
|
//
|
214
|
//
|
215
|
// //geneva id
|
216
|
// Reference genevaReference = state.getGenevaReference();
|
217
|
// Object genevaId = rs.getObject("geneva_ID");
|
218
|
// speciesTaxon.addSource(String.valueOf(genevaId), null, genevaReference, null);
|
219
|
//
|
220
|
// //distribution
|
221
|
// handleDistribution(rs, speciesTaxon);
|
222
|
//
|
223
|
// return speciesTaxon;
|
224
|
|
225
|
return null;
|
226
|
}
|
227
|
|
228
|
private void handleDistribution(ResultSet rs, Taxon speciesTaxon) throws SQLException {
|
229
|
TaxonDescription description = TaxonDescription.NewInstance(speciesTaxon);
|
230
|
|
231
|
Boolean isCongo = rs.getBoolean("drc");
|
232
|
Boolean isBurundi = rs.getBoolean("burundi");
|
233
|
Boolean isRwanda = rs.getBoolean("rwanda");
|
234
|
|
235
|
addDistribution(description, isCongo, "ZAI");
|
236
|
addDistribution(description, isBurundi, "BUR");
|
237
|
addDistribution(description, isRwanda, "RWA");
|
238
|
|
239
|
}
|
240
|
|
241
|
|
242
|
|
243
|
/**
|
244
|
* @param description
|
245
|
* @param isCongo
|
246
|
*/
|
247
|
private void addDistribution(TaxonDescription description, Boolean exists, String label) {
|
248
|
if (exists == true){
|
249
|
NamedArea namedArea = TdwgAreaProvider.getAreaByTdwgAbbreviation(label);
|
250
|
Distribution distribution = Distribution.NewInstance(namedArea, PresenceTerm.PRESENT());
|
251
|
description.addElement(distribution);
|
252
|
}
|
253
|
}
|
254
|
|
255
|
|
256
|
|
257
|
//TODO use Mapper
|
258
|
private boolean makeTaxonomicallyIncluded(RoteListeDbImportState state, Integer treeRefFk, Taxon child, Taxon parent, Reference citation, String microCitation){
|
259
|
String treeKey;
|
260
|
UUID treeUuid;
|
261
|
if (treeRefFk == null){
|
262
|
treeKey = "1"; // there is only one tree and it gets the map key '1'
|
263
|
treeUuid = state.getConfig().getClassificationUuid();
|
264
|
}else{
|
265
|
treeKey =String.valueOf(treeRefFk);
|
266
|
treeUuid = state.getTreeUuidByTreeKey(treeKey);
|
267
|
}
|
268
|
Classification tree = (Classification)state.getRelatedObject(DbImportTaxIncludedInMapper.TAXONOMIC_TREE_NAMESPACE, treeKey);
|
269
|
if (tree == null){
|
270
|
IClassificationService service = state.getCurrentIO().getClassificationService();
|
271
|
tree = service.find(treeUuid);
|
272
|
if (tree == null){
|
273
|
String treeName = state.getConfig().getClassificationName();
|
274
|
tree = Classification.NewInstance(treeName);
|
275
|
tree.setUuid(treeUuid);
|
276
|
//FIXME tree reference
|
277
|
//tree.setReference(ref);
|
278
|
service.save(tree);
|
279
|
}
|
280
|
state.addRelatedObject(DbImportTaxIncludedInMapper.TAXONOMIC_TREE_NAMESPACE, treeKey, tree);
|
281
|
}
|
282
|
|
283
|
TaxonNode childNode = tree.addParentChild(parent, child, citation, microCitation);
|
284
|
return (childNode != null);
|
285
|
}
|
286
|
|
287
|
|
288
|
// private Reference makeGenevaReference(RoteListeDbImportState state) {
|
289
|
// Reference result = ReferenceFactory.newDatabase();
|
290
|
// result.setTitleCache(state.getConfig().getGenevaReferenceTitle(), true);
|
291
|
// result.setUuid(state.getConfig().getUuidGenevaReference());
|
292
|
// return result;
|
293
|
// }
|
294
|
|
295
|
/* (non-Javadoc)
|
296
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
297
|
*/
|
298
|
@Override
|
299
|
protected boolean doCheck(RoteListeDbImportState state){
|
300
|
IOValidator<RoteListeDbImportState> validator = new RoteListeDbTaxonImportValidator();
|
301
|
return validator.validate(state);
|
302
|
}
|
303
|
|
304
|
|
305
|
/* (non-Javadoc)
|
306
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
307
|
*/
|
308
|
protected boolean isIgnore(RoteListeDbImportState state){
|
309
|
return ! state.getConfig().isDoTaxa();
|
310
|
}
|
311
|
|
312
|
|
313
|
|
314
|
}
|