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.berlinModel.in;
|
11
|
|
12
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.T_STATUS_ACCEPTED;
|
13
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.T_STATUS_PARTIAL_SYN;
|
14
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.T_STATUS_PRO_PARTE_SYN;
|
15
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.T_STATUS_SYNONYM;
|
16
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.T_STATUS_UNRESOLVED;
|
17
|
|
18
|
import java.lang.reflect.Method;
|
19
|
import java.sql.ResultSet;
|
20
|
import java.sql.SQLException;
|
21
|
import java.util.HashMap;
|
22
|
import java.util.HashSet;
|
23
|
import java.util.Map;
|
24
|
import java.util.Set;
|
25
|
import java.util.UUID;
|
26
|
|
27
|
import org.apache.commons.lang.StringUtils;
|
28
|
import org.apache.log4j.Logger;
|
29
|
import org.springframework.stereotype.Component;
|
30
|
|
31
|
import eu.etaxonomy.cdm.database.update.DatabaseTypeNotSupportedException;
|
32
|
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
|
33
|
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelTaxonImportValidator;
|
34
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
35
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
36
|
import eu.etaxonomy.cdm.model.agent.Person;
|
37
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
38
|
import eu.etaxonomy.cdm.model.common.Extension;
|
39
|
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
40
|
import eu.etaxonomy.cdm.model.common.Language;
|
41
|
import eu.etaxonomy.cdm.model.common.Marker;
|
42
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
43
|
import eu.etaxonomy.cdm.model.description.Feature;
|
44
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
45
|
import eu.etaxonomy.cdm.model.description.TextData;
|
46
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
47
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
48
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
49
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
50
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
51
|
|
52
|
|
53
|
/**
|
54
|
* @author a.mueller
|
55
|
* @since 20.03.2008
|
56
|
*/
|
57
|
@Component
|
58
|
public class BerlinModelTaxonImport extends BerlinModelImportBase {
|
59
|
private static final long serialVersionUID = -1186364983750790695L;
|
60
|
|
61
|
private static final Logger logger = Logger.getLogger(BerlinModelTaxonImport.class);
|
62
|
|
63
|
public static final String NAMESPACE = "Taxon";
|
64
|
|
65
|
private static final String pluralString = "Taxa";
|
66
|
private static final String dbTableName = "PTaxon";
|
67
|
|
68
|
/**
|
69
|
* How should the publish flag in table PTaxon be interpreted
|
70
|
* NO_MARKER: No marker is set
|
71
|
* ONLY_FALSE:
|
72
|
*/
|
73
|
public enum PublishMarkerChooser{
|
74
|
NO_MARKER,
|
75
|
ONLY_FALSE,
|
76
|
ONLY_TRUE,
|
77
|
ALL;
|
78
|
|
79
|
boolean doMark(boolean value){
|
80
|
if (value == true){
|
81
|
return this == ALL || this == ONLY_TRUE;
|
82
|
}else{
|
83
|
return this == ALL || this == ONLY_FALSE;
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
|
88
|
public BerlinModelTaxonImport(){
|
89
|
super(dbTableName, pluralString);
|
90
|
}
|
91
|
|
92
|
@Override
|
93
|
protected String getIdQuery(BerlinModelImportState state) {
|
94
|
String sqlSelect = " SELECT RIdentifier";
|
95
|
String taxonTable = state.getConfig().getTaxonTable();
|
96
|
String sqlFrom = String.format(" FROM %s ", taxonTable);
|
97
|
String sqlWhere = "";
|
98
|
|
99
|
String sql = sqlSelect + " " + sqlFrom + " " + sqlWhere ;
|
100
|
return sql;
|
101
|
}
|
102
|
|
103
|
@Override
|
104
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
105
|
String sqlSelect = " SELECT pt.* ";
|
106
|
String sqlFrom = " FROM PTaxon pt ";
|
107
|
if (config.isEuroMed()){
|
108
|
sqlFrom = " FROM PTaxon AS pt " +
|
109
|
" INNER JOIN v_cdm_exp_taxaAll AS em ON pt.RIdentifier = em.RIdentifier " +
|
110
|
" LEFT OUTER JOIN Reference r ON pt.LastScrutinyFk = r.RefId ";
|
111
|
sqlSelect += " , em.MA, r.RefCache as LastScrutiny ";
|
112
|
}
|
113
|
|
114
|
|
115
|
String sqlWhere = " WHERE ( pt.RIdentifier IN (" + ID_LIST_TOKEN + ") )";
|
116
|
|
117
|
String strRecordQuery =sqlSelect + " " + sqlFrom + " " + sqlWhere ;
|
118
|
// " SELECT * " +
|
119
|
// " FROM PTaxon " + state.getConfig().getTaxonTable();
|
120
|
// " WHERE ( RIdentifier IN (" + ID_LIST_TOKEN + ") )";
|
121
|
return strRecordQuery;
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
protected boolean doCheck(BerlinModelImportState state){
|
126
|
IOValidator<BerlinModelImportState> validator = new BerlinModelTaxonImportValidator();
|
127
|
return validator.validate(state);
|
128
|
}
|
129
|
|
130
|
@Override
|
131
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
|
132
|
boolean success = true ;
|
133
|
|
134
|
BerlinModelImportConfigurator config = state.getConfig();
|
135
|
Set<TaxonBase> taxaToSave = new HashSet<>();
|
136
|
Map<String, TaxonName> taxonNameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
|
137
|
Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
|
138
|
|
139
|
ResultSet rs = partitioner.getResultSet();
|
140
|
try{
|
141
|
boolean publishFlagExists = state.getConfig().getSource().checkColumnExists("PTaxon", "PublishFlag");
|
142
|
boolean isEuroMed = config.isEuroMed();
|
143
|
while (rs.next()){
|
144
|
|
145
|
// if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
|
146
|
|
147
|
//create TaxonName element
|
148
|
int taxonId = rs.getInt("RIdentifier");
|
149
|
int statusFk = rs.getInt("statusFk");
|
150
|
|
151
|
int nameFk = rs.getInt("PTNameFk");
|
152
|
int refFkInt = rs.getInt("PTRefFk");
|
153
|
String doubtful = rs.getString("DoubtfulFlag");
|
154
|
String uuid = null;
|
155
|
if (resultSetHasColumn(rs,"UUID")){
|
156
|
uuid = rs.getString("UUID");
|
157
|
}
|
158
|
|
159
|
TaxonName taxonName = null;
|
160
|
taxonName = taxonNameMap.get(String.valueOf(nameFk));
|
161
|
|
162
|
Reference reference = null;
|
163
|
String refFk = String.valueOf(refFkInt);
|
164
|
reference = refMap.get(refFk);
|
165
|
|
166
|
if(! config.isIgnoreNull()){
|
167
|
if (taxonName == null ){
|
168
|
logger.warn("TaxonName belonging to taxon (RIdentifier = " + taxonId + ") could not be found in store. Taxon will not be imported");
|
169
|
success = false;
|
170
|
continue; //next taxon
|
171
|
}else if (reference == null ){
|
172
|
logger.warn("Reference belonging to taxon could not be found in store. Taxon will not be imported");
|
173
|
success = false;
|
174
|
continue; //next taxon
|
175
|
}
|
176
|
}
|
177
|
TaxonBase<?> taxonBase;
|
178
|
Synonym synonym;
|
179
|
Taxon taxon;
|
180
|
try {
|
181
|
logger.debug(statusFk);
|
182
|
if (statusFk == T_STATUS_ACCEPTED || statusFk == T_STATUS_UNRESOLVED
|
183
|
|| statusFk == T_STATUS_PRO_PARTE_SYN || statusFk == T_STATUS_PARTIAL_SYN ){
|
184
|
taxon = Taxon.NewInstance(taxonName, reference);
|
185
|
taxonBase = taxon;
|
186
|
if (statusFk == T_STATUS_UNRESOLVED){
|
187
|
taxon.setTaxonStatusUnknown(true);
|
188
|
}
|
189
|
//TODO marker for pp and partial?
|
190
|
}else if (statusFk == T_STATUS_SYNONYM ){
|
191
|
synonym = Synonym.NewInstance(taxonName, reference);
|
192
|
taxonBase = synonym;
|
193
|
// if (statusFk == T_STATUS_PRO_PARTE_SYN){
|
194
|
// synonym.setProParte(true);
|
195
|
// }
|
196
|
// if (statusFk == T_STATUS_PARTIAL_SYN){
|
197
|
// synonym.setPartial(true);
|
198
|
// }
|
199
|
}else{
|
200
|
logger.warn("TaxonStatus " + statusFk + " not yet implemented. Taxon (RIdentifier = " + taxonId + ") left out.");
|
201
|
success = false;
|
202
|
continue;
|
203
|
}
|
204
|
if (uuid != null){
|
205
|
taxonBase.setUuid(UUID.fromString(uuid));
|
206
|
}
|
207
|
|
208
|
//doubtful
|
209
|
if (doubtful.equals("a")){
|
210
|
taxonBase.setDoubtful(false);
|
211
|
}else if(doubtful.equals("d")){
|
212
|
taxonBase.setDoubtful(true);
|
213
|
}else if(doubtful.equals("i")){
|
214
|
taxonBase.setDoubtful(false);
|
215
|
logger.warn("Doubtful = i (inactivated) does not exist in CDM. Doubtful set to false. RIdentifier: " + taxonId);
|
216
|
}
|
217
|
|
218
|
//detail
|
219
|
String detail = rs.getString("Detail");
|
220
|
if (isNotBlank(detail)){
|
221
|
ExtensionType detailExtensionType = getExtensionType(state, BerlinModelTransformer.DETAIL_EXT_UUID, "micro reference","micro reference","micro ref.");
|
222
|
Extension.NewInstance(taxonBase, detail, detailExtensionType);
|
223
|
}
|
224
|
//idInSource
|
225
|
String idInSource = rs.getString("IdInSource");
|
226
|
if (isNotBlank(idInSource)){
|
227
|
ExtensionType detailExtensionType = getExtensionType(state, BerlinModelTransformer.ID_IN_SOURCE_EXT_UUID, "Berlin Model IdInSource","Berlin Model IdInSource","BM source id");
|
228
|
Extension.NewInstance(taxonBase, idInSource, detailExtensionType);
|
229
|
}
|
230
|
//namePhrase
|
231
|
String namePhrase = rs.getString("NamePhrase");
|
232
|
if (StringUtils.isNotBlank(namePhrase)){
|
233
|
taxonBase.setAppendedPhrase(namePhrase);
|
234
|
}
|
235
|
//useNameCache
|
236
|
Boolean useNameCacheFlag = rs.getBoolean("UseNameCacheFlag");
|
237
|
if (useNameCacheFlag){
|
238
|
taxonBase.setUseNameCache(true);
|
239
|
}
|
240
|
//publisheFlag
|
241
|
if (publishFlagExists){
|
242
|
Boolean publishFlag = rs.getBoolean("PublishFlag");
|
243
|
Boolean misapplied = false;
|
244
|
if (isEuroMed){
|
245
|
misapplied = rs.getBoolean("MA");
|
246
|
}
|
247
|
|
248
|
if ( ! misapplied){
|
249
|
taxonBase.setPublish(publishFlag);
|
250
|
}
|
251
|
}
|
252
|
|
253
|
//
|
254
|
if (resultSetHasColumn(rs, "LastScrutiny")){
|
255
|
String lastScrutiny = rs.getString("LastScrutiny");
|
256
|
//TODO strange, why not Extension last scrutiny? To match PESI? Is there a difference
|
257
|
//to LastScrutinyFK and SpeciesExpertFK?
|
258
|
if (isNotBlank(lastScrutiny)){
|
259
|
ExtensionType extensionTypeSpeciesExpert = getExtensionType(state, BerlinModelTransformer.uuidSpeciesExpertName, "Species Expert", "Species Expert", "Species Expert");
|
260
|
taxonBase.addExtension(lastScrutiny, extensionTypeSpeciesExpert);
|
261
|
ExtensionType extensionTypeExpert = getExtensionType(state, BerlinModelTransformer.uuidExpertName, "Expert", "Expert for a taxonomic group", "Expert");
|
262
|
taxonBase.addExtension(lastScrutiny, extensionTypeExpert);
|
263
|
}
|
264
|
}
|
265
|
//
|
266
|
if (resultSetHasColumn(rs, "IsExcludedMarker")){
|
267
|
boolean isExcluded = rs.getBoolean("IsExcludedMarker");
|
268
|
if (isExcluded){
|
269
|
String extension = rs.getString("IsExcludedExtension");
|
270
|
String valueless = "not accepted: taxonomically valueless local or singular biotype";
|
271
|
String provisional = "provisional: probably a taxonomically valueless local or singular biotype";
|
272
|
|
273
|
MarkerType markerType = null;
|
274
|
if (valueless.equals(extension)){
|
275
|
markerType = getMarkerType(state, BerlinModelTransformer.uuidTaxonomicallyValueless, "taxonomically valueless", valueless, "valueless");
|
276
|
}else if (provisional.equals(extension)){
|
277
|
markerType = getMarkerType(state, BerlinModelTransformer.uuidProbablyTaxonomicallyValueless, "probably taxonomically valueless", provisional, "provisional");
|
278
|
}
|
279
|
if (markerType != null){
|
280
|
taxonBase.addMarker(Marker.NewInstance(markerType, true));
|
281
|
}else{
|
282
|
logger.warn("IsExcludedExtension not regonized for taxon " + taxonId + "; " + extension);
|
283
|
}
|
284
|
}
|
285
|
}
|
286
|
|
287
|
//Notes
|
288
|
boolean excludeNotes = state.getConfig().isTaxonNoteAsFeature() && taxonBase.isInstanceOf(Taxon.class);
|
289
|
doIdCreatedUpdatedNotes(state, taxonBase, rs, taxonId, NAMESPACE, false, excludeNotes);
|
290
|
if (excludeNotes){
|
291
|
makeTaxonomicNote(state, CdmBase.deproxy(taxonBase, Taxon.class), rs.getString("Notes"));
|
292
|
}
|
293
|
|
294
|
//external url
|
295
|
if (config.getMakeUrlForTaxon() != null){
|
296
|
Method urlMethod = config.getMakeUrlForTaxon();
|
297
|
urlMethod.invoke(null, taxonBase, rs);
|
298
|
}
|
299
|
|
300
|
partitioner.startDoSave();
|
301
|
taxaToSave.add(taxonBase);
|
302
|
} catch (Exception e) {
|
303
|
logger.warn("An exception (" +e.getMessage()+") occurred when creating taxon with id " + taxonId + ". Taxon could not be saved.");
|
304
|
success = false;
|
305
|
}
|
306
|
}
|
307
|
} catch (DatabaseTypeNotSupportedException e) {
|
308
|
logger.error("MethodNotSupportedException:" + e);
|
309
|
return false;
|
310
|
} catch (Exception e) {
|
311
|
logger.error("SQLException:" + e);
|
312
|
return false;
|
313
|
}
|
314
|
|
315
|
|
316
|
// logger.info( i + " names handled");
|
317
|
getTaxonService().save(taxaToSave);
|
318
|
return success;
|
319
|
}
|
320
|
|
321
|
/**
|
322
|
* @param state
|
323
|
* @param taxonBase
|
324
|
* @param notes
|
325
|
*/
|
326
|
private void makeTaxonomicNote(BerlinModelImportState state, Taxon taxon, String notes) {
|
327
|
if (isNotBlank(notes)){
|
328
|
TaxonDescription desc = getTaxonDescription(taxon, false, true);
|
329
|
desc.setDefault(true); //hard coded for Salvador, not used elsewhere as far as I can see
|
330
|
TextData textData = TextData.NewInstance(Feature.NOTES() , notes, Language.SPANISH_CASTILIAN(), null);
|
331
|
desc.addElement(textData);
|
332
|
}
|
333
|
}
|
334
|
|
335
|
@Override
|
336
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
337
|
String nameSpace;
|
338
|
Class<?> cdmClass;
|
339
|
Set<String> idSet;
|
340
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
341
|
|
342
|
try{
|
343
|
Set<String> nameIdSet = new HashSet<>();
|
344
|
Set<String> referenceIdSet = new HashSet<>();
|
345
|
while (rs.next()){
|
346
|
handleForeignKey(rs, nameIdSet, "PTNameFk");
|
347
|
handleForeignKey(rs, referenceIdSet, "PTRefFk");
|
348
|
}
|
349
|
|
350
|
//name map
|
351
|
nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
|
352
|
cdmClass = TaxonName.class;
|
353
|
idSet = nameIdSet;
|
354
|
Map<String, Person> nameMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
355
|
result.put(nameSpace, nameMap);
|
356
|
|
357
|
//reference map
|
358
|
nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
|
359
|
cdmClass = Reference.class;
|
360
|
idSet = referenceIdSet;
|
361
|
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
362
|
result.put(nameSpace, referenceMap);
|
363
|
|
364
|
} catch (SQLException e) {
|
365
|
throw new RuntimeException(e);
|
366
|
}
|
367
|
return result;
|
368
|
}
|
369
|
|
370
|
@Override
|
371
|
protected String getTableName() {
|
372
|
return dbTableName;
|
373
|
}
|
374
|
|
375
|
@Override
|
376
|
public String getPluralString() {
|
377
|
return pluralString;
|
378
|
}
|
379
|
|
380
|
@Override
|
381
|
protected boolean isIgnore(BerlinModelImportState state){
|
382
|
return ! state.getConfig().isDoTaxa();
|
383
|
}
|
384
|
|
385
|
|
386
|
|
387
|
}
|