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.description.Feature;
|
42
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
43
|
import eu.etaxonomy.cdm.model.description.TextData;
|
44
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
45
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
46
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
47
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
48
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
49
|
|
50
|
|
51
|
/**
|
52
|
* @author a.mueller
|
53
|
* @created 20.03.2008
|
54
|
*/
|
55
|
@Component
|
56
|
public class BerlinModelTaxonImport extends BerlinModelImportBase {
|
57
|
private static final long serialVersionUID = -1186364983750790695L;
|
58
|
|
59
|
private static final Logger logger = Logger.getLogger(BerlinModelTaxonImport.class);
|
60
|
|
61
|
public static final String NAMESPACE = "Taxon";
|
62
|
|
63
|
private static final String pluralString = "Taxa";
|
64
|
private static final String dbTableName = "PTaxon";
|
65
|
|
66
|
/**
|
67
|
* How should the publish flag in table PTaxon be interpreted
|
68
|
* NO_MARKER: No marker is set
|
69
|
* ONLY_FALSE:
|
70
|
*/
|
71
|
public enum PublishMarkerChooser{
|
72
|
NO_MARKER,
|
73
|
ONLY_FALSE,
|
74
|
ONLY_TRUE,
|
75
|
ALL;
|
76
|
|
77
|
boolean doMark(boolean value){
|
78
|
if (value == true){
|
79
|
return this == ALL || this == ONLY_TRUE;
|
80
|
}else{
|
81
|
return this == ALL || this == ONLY_FALSE;
|
82
|
}
|
83
|
}
|
84
|
}
|
85
|
|
86
|
public BerlinModelTaxonImport(){
|
87
|
super(dbTableName, pluralString);
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
protected String getIdQuery(BerlinModelImportState state) {
|
92
|
String sqlSelect = " SELECT RIdentifier";
|
93
|
String taxonTable = state.getConfig().getTaxonTable();
|
94
|
String sqlFrom = String.format(" FROM %s ", taxonTable);
|
95
|
String sqlWhere = "";
|
96
|
|
97
|
String sql = sqlSelect + " " + sqlFrom + " " + sqlWhere ;
|
98
|
return sql;
|
99
|
}
|
100
|
|
101
|
@Override
|
102
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
103
|
String sqlSelect = " SELECT pt.* ";
|
104
|
String sqlFrom = " FROM PTaxon pt ";
|
105
|
if (isEuroMed(config) ){
|
106
|
sqlFrom = " FROM PTaxon AS pt " +
|
107
|
" INNER JOIN v_cdm_exp_taxaAll AS em ON pt.RIdentifier = em.RIdentifier " +
|
108
|
" LEFT OUTER JOIN Reference r ON pt.LastScrutinyFk = r.RefId ";
|
109
|
sqlSelect += " , em.MA, r.RefCache as LastScrutiny ";
|
110
|
}
|
111
|
|
112
|
|
113
|
String sqlWhere = " WHERE ( pt.RIdentifier IN (" + ID_LIST_TOKEN + ") )";
|
114
|
|
115
|
String strRecordQuery =sqlSelect + " " + sqlFrom + " " + sqlWhere ;
|
116
|
// " SELECT * " +
|
117
|
// " FROM PTaxon " + state.getConfig().getTaxonTable();
|
118
|
// " WHERE ( RIdentifier IN (" + ID_LIST_TOKEN + ") )";
|
119
|
return strRecordQuery;
|
120
|
}
|
121
|
|
122
|
private boolean isEuroMed(BerlinModelImportConfigurator config) {
|
123
|
return config.getTaxonTable().trim().equals("v_cdm_exp_taxaAll");
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
protected boolean doCheck(BerlinModelImportState state){
|
128
|
IOValidator<BerlinModelImportState> validator = new BerlinModelTaxonImportValidator();
|
129
|
return validator.validate(state);
|
130
|
}
|
131
|
|
132
|
@Override
|
133
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
|
134
|
boolean success = true ;
|
135
|
|
136
|
BerlinModelImportConfigurator config = state.getConfig();
|
137
|
Set<TaxonBase> taxaToSave = new HashSet<>();
|
138
|
Map<String, TaxonName> taxonNameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
|
139
|
Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
|
140
|
|
141
|
ResultSet rs = partitioner.getResultSet();
|
142
|
try{
|
143
|
boolean publishFlagExists = state.getConfig().getSource().checkColumnExists("PTaxon", "PublishFlag");
|
144
|
boolean isEuroMed = isEuroMed(state.getConfig());
|
145
|
while (rs.next()){
|
146
|
|
147
|
// if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
|
148
|
|
149
|
//create TaxonName element
|
150
|
int taxonId = rs.getInt("RIdentifier");
|
151
|
int statusFk = rs.getInt("statusFk");
|
152
|
|
153
|
int nameFk = rs.getInt("PTNameFk");
|
154
|
int refFkInt = rs.getInt("PTRefFk");
|
155
|
String doubtful = rs.getString("DoubtfulFlag");
|
156
|
String uuid = null;
|
157
|
if (resultSetHasColumn(rs,"UUID")){
|
158
|
uuid = rs.getString("UUID");
|
159
|
}
|
160
|
|
161
|
TaxonName<?,?> taxonName = null;
|
162
|
taxonName = taxonNameMap.get(String.valueOf(nameFk));
|
163
|
|
164
|
Reference reference = null;
|
165
|
String refFk = String.valueOf(refFkInt);
|
166
|
reference = refMap.get(refFk);
|
167
|
|
168
|
if(! config.isIgnoreNull()){
|
169
|
if (taxonName == null ){
|
170
|
logger.warn("TaxonName belonging to taxon (RIdentifier = " + taxonId + ") could not be found in store. Taxon will not be imported");
|
171
|
success = false;
|
172
|
continue; //next taxon
|
173
|
}else if (reference == null ){
|
174
|
logger.warn("Reference belonging to taxon could not be found in store. Taxon will not be imported");
|
175
|
success = false;
|
176
|
continue; //next taxon
|
177
|
}
|
178
|
}
|
179
|
TaxonBase<?> taxonBase;
|
180
|
Synonym synonym;
|
181
|
Taxon taxon;
|
182
|
try {
|
183
|
logger.debug(statusFk);
|
184
|
if (statusFk == T_STATUS_ACCEPTED || statusFk == T_STATUS_UNRESOLVED ){
|
185
|
taxon = Taxon.NewInstance(taxonName, reference);
|
186
|
taxonBase = taxon;
|
187
|
if (statusFk == T_STATUS_UNRESOLVED){
|
188
|
taxon.setTaxonStatusUnknown(true);
|
189
|
}
|
190
|
}else if (statusFk == T_STATUS_SYNONYM || statusFk == T_STATUS_PRO_PARTE_SYN || statusFk == T_STATUS_PARTIAL_SYN){
|
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 (StringUtils.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 (StringUtils.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
|
ExtensionType extensionTypeSpeciesExpert = getExtensionType(state, BerlinModelTransformer.uuidSpeciesExpertName, "Species Expert", "Species Expert", "Species Expert");
|
257
|
taxonBase.addExtension(lastScrutiny, extensionTypeSpeciesExpert);
|
258
|
ExtensionType extensionTypeExpert = getExtensionType(state, BerlinModelTransformer.uuidExpertName, "Expert", "Expert for a taxonomic group", "Expert");
|
259
|
taxonBase.addExtension(lastScrutiny, extensionTypeExpert);
|
260
|
}
|
261
|
|
262
|
//Notes
|
263
|
boolean excludeNotes = state.getConfig().isTaxonNoteAsFeature() && taxonBase.isInstanceOf(Taxon.class);
|
264
|
doIdCreatedUpdatedNotes(state, taxonBase, rs, taxonId, NAMESPACE, false, excludeNotes);
|
265
|
if (excludeNotes){
|
266
|
makeTaxonomicNote(state, CdmBase.deproxy(taxonBase, Taxon.class), rs.getString("Notes"));
|
267
|
}
|
268
|
|
269
|
//external url
|
270
|
if (config.getMakeUrlForTaxon() != null){
|
271
|
Method urlMethod = config.getMakeUrlForTaxon();
|
272
|
urlMethod.invoke(null, taxonBase, rs);
|
273
|
}
|
274
|
|
275
|
partitioner.startDoSave();
|
276
|
taxaToSave.add(taxonBase);
|
277
|
} catch (Exception e) {
|
278
|
logger.warn("An exception (" +e.getMessage()+") occurred when creating taxon with id " + taxonId + ". Taxon could not be saved.");
|
279
|
success = false;
|
280
|
}
|
281
|
}
|
282
|
} catch (DatabaseTypeNotSupportedException e) {
|
283
|
logger.error("MethodNotSupportedException:" + e);
|
284
|
return false;
|
285
|
} catch (Exception e) {
|
286
|
logger.error("SQLException:" + e);
|
287
|
return false;
|
288
|
}
|
289
|
|
290
|
|
291
|
// logger.info( i + " names handled");
|
292
|
getTaxonService().save(taxaToSave);
|
293
|
return success;
|
294
|
}
|
295
|
|
296
|
/**
|
297
|
* @param state
|
298
|
* @param taxonBase
|
299
|
* @param notes
|
300
|
*/
|
301
|
private void makeTaxonomicNote(BerlinModelImportState state, Taxon taxon, String notes) {
|
302
|
if (isNotBlank(notes)){
|
303
|
TaxonDescription desc = getTaxonDescription(taxon, false, true);
|
304
|
desc.setDefault(true); //hard coded for Salvador, not used elsewhere as far as I can see
|
305
|
TextData textData = TextData.NewInstance(Feature.NOTES() , notes, Language.SPANISH_CASTILIAN(), null);
|
306
|
desc.addElement(textData);
|
307
|
}
|
308
|
}
|
309
|
|
310
|
@Override
|
311
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
312
|
String nameSpace;
|
313
|
Class<?> cdmClass;
|
314
|
Set<String> idSet;
|
315
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
316
|
|
317
|
try{
|
318
|
Set<String> nameIdSet = new HashSet<String>();
|
319
|
Set<String> referenceIdSet = new HashSet<String>();
|
320
|
while (rs.next()){
|
321
|
handleForeignKey(rs, nameIdSet, "PTNameFk");
|
322
|
handleForeignKey(rs, referenceIdSet, "PTRefFk");
|
323
|
}
|
324
|
|
325
|
//name map
|
326
|
nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
|
327
|
cdmClass = TaxonName.class;
|
328
|
idSet = nameIdSet;
|
329
|
Map<String, Person> nameMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
330
|
result.put(nameSpace, nameMap);
|
331
|
|
332
|
//reference map
|
333
|
nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
|
334
|
cdmClass = Reference.class;
|
335
|
idSet = referenceIdSet;
|
336
|
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
337
|
result.put(nameSpace, referenceMap);
|
338
|
|
339
|
} catch (SQLException e) {
|
340
|
throw new RuntimeException(e);
|
341
|
}
|
342
|
return result;
|
343
|
}
|
344
|
|
345
|
@Override
|
346
|
protected String getTableName() {
|
347
|
return dbTableName;
|
348
|
}
|
349
|
|
350
|
@Override
|
351
|
public String getPluralString() {
|
352
|
return pluralString;
|
353
|
}
|
354
|
|
355
|
@Override
|
356
|
protected boolean isIgnore(BerlinModelImportState state){
|
357
|
return ! state.getConfig().isDoTaxa();
|
358
|
}
|
359
|
|
360
|
|
361
|
|
362
|
}
|