1
|
/**
|
2
|
* Copyright (C) 2015 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
|
package eu.etaxonomy.cdm.io.edaphobase;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.List;
|
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.codehaus.plexus.util.StringUtils;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
|
25
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
26
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
27
|
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
|
28
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
29
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
30
|
import eu.etaxonomy.cdm.model.common.Language;
|
31
|
import eu.etaxonomy.cdm.model.common.Marker;
|
32
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
33
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
34
|
import eu.etaxonomy.cdm.model.common.Representation;
|
35
|
import eu.etaxonomy.cdm.model.name.IZoologicalName;
|
36
|
import eu.etaxonomy.cdm.model.name.Rank;
|
37
|
import eu.etaxonomy.cdm.model.name.RankClass;
|
38
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
39
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
40
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
41
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
42
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
43
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
44
|
|
45
|
/**
|
46
|
* @author a.mueller
|
47
|
* @date 18.12.2015
|
48
|
*
|
49
|
*/
|
50
|
@Component
|
51
|
public class EdaphobaseTaxonImport extends EdaphobaseImportBase {
|
52
|
private static final long serialVersionUID = -9138378836474086070L;
|
53
|
private static final Logger logger = Logger.getLogger(EdaphobaseTaxonImport.class);
|
54
|
|
55
|
private static final String tableName = "tax_taxon";
|
56
|
|
57
|
private static final String pluralString = "taxa";
|
58
|
|
59
|
private static final Object AUTHOR_NAMESPACE = "tax_author_name";
|
60
|
|
61
|
/**
|
62
|
* @param tableName
|
63
|
* @param pluralString
|
64
|
*/
|
65
|
public EdaphobaseTaxonImport() {
|
66
|
super(tableName, pluralString);
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
protected String getIdQuery(EdaphobaseImportState state) {
|
71
|
return "SELECT DISTINCT taxon_id FROM tax_taxon t "
|
72
|
+ " ORDER BY taxon_id";
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
protected String getRecordQuery(EdaphobaseImportConfigurator config) {
|
77
|
String result = " SELECT DISTINCT t.*, r.value_summary as rankStr, pr.value_summary as parentRankStr, ppr.value_summary as grandParentRankStr, pppr.value_summary as grandGrandParentRankStr, "
|
78
|
+ " pt.name as parentName, ppt.name as grandParentName, pppt.name as grandGrandParentName "
|
79
|
+ " FROM tax_taxon t "
|
80
|
+ " LEFT JOIN tax_taxon pt ON t.parent_taxon_fk = pt.taxon_id "
|
81
|
+ " LEFT JOIN tax_taxon ppt ON pt.parent_taxon_fk = ppt.taxon_id "
|
82
|
+ " LEFT JOIN tax_taxon pppt ON ppt.parent_taxon_fk = pppt.taxon_id "
|
83
|
+ " LEFT OUTER JOIN selective_list.element r ON r.element_id = t.tax_rank_fk "
|
84
|
+ " LEFT OUTER JOIN selective_list.element pr ON pr.element_id = pt.tax_rank_fk "
|
85
|
+ " LEFT OUTER JOIN selective_list.element ppr ON ppr.element_id = ppt.tax_rank_fk "
|
86
|
+ " LEFT OUTER JOIN selective_list.element pppr ON pppr.element_id = pppt.tax_rank_fk "
|
87
|
+ " WHERE t.taxon_id IN (@IDSET)";
|
88
|
result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
|
89
|
return result;
|
90
|
}
|
91
|
|
92
|
@Override
|
93
|
protected void doInvoke(EdaphobaseImportState state) {
|
94
|
super.doInvoke(state);
|
95
|
}
|
96
|
|
97
|
|
98
|
@Override
|
99
|
public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
|
100
|
ResultSet rs = partitioner.getResultSet();
|
101
|
Set<TaxonBase> taxaToSave = new HashSet<>();
|
102
|
try {
|
103
|
while (rs.next()){
|
104
|
makeSingleTaxon(state, rs, taxaToSave);
|
105
|
}
|
106
|
} catch (SQLException | UndefinedTransformerMethodException e) {
|
107
|
e.printStackTrace();
|
108
|
}
|
109
|
|
110
|
getTaxonService().saveOrUpdate(taxaToSave);
|
111
|
return true;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* @param state
|
116
|
* @param rs
|
117
|
* @param taxaToSave
|
118
|
* @throws SQLException
|
119
|
* @throws UndefinedTransformerMethodException
|
120
|
*/
|
121
|
private void makeSingleTaxon(EdaphobaseImportState state, ResultSet rs, Set<TaxonBase> taxaToSave)
|
122
|
throws SQLException, UndefinedTransformerMethodException {
|
123
|
Integer id = nullSafeInt(rs, "taxon_id");
|
124
|
Integer year = nullSafeInt(rs, "tax_year");
|
125
|
boolean isBrackets = rs.getBoolean("tax_brackets");
|
126
|
String remark = rs.getString("remark");
|
127
|
String nameStr = rs.getString("name");
|
128
|
String authorName = rs.getString("tax_author_name");
|
129
|
//parentTaxonFk
|
130
|
//rankFk
|
131
|
Integer nomRefId = nullSafeInt(rs, "tax_document");
|
132
|
boolean isValid = rs.getBoolean("valid");
|
133
|
boolean idDeleted = rs.getBoolean("deleted");
|
134
|
String displayString = rs.getString("display_string");
|
135
|
Integer version = nullSafeInt(rs, "versionfield");
|
136
|
String pages = rs.getString("pages");
|
137
|
String treeIndex = rs.getString("path_to_root");
|
138
|
// Integer rankFk = nullSafeInt(rs, "tax_rank_fk");
|
139
|
String nameAddition = rs.getString("name_addition");
|
140
|
String officialRemark = rs.getString("official_remark");
|
141
|
boolean isGroup = rs.getBoolean("taxonomic_group");
|
142
|
String rankStr = rs.getString("rankStr");
|
143
|
String parentRankStr = rs.getString("parentRankStr");
|
144
|
String grandParentRankStr = rs.getString("grandParentRankStr");
|
145
|
String grandGrandParentRankStr = rs.getString("grandGrandParentRankStr");
|
146
|
String parentNameStr = rs.getString("parentName");
|
147
|
String grandParentNameStr = rs.getString("grandParentName");
|
148
|
String grandGrandParentNameStr = rs.getString("grandGrandParentName");
|
149
|
|
150
|
TaxonBase<?> taxonBase;
|
151
|
|
152
|
rankStr= extractEnglish(rankStr);
|
153
|
parentRankStr= extractEnglish(parentRankStr);
|
154
|
grandParentRankStr= extractEnglish(grandParentRankStr);
|
155
|
grandGrandParentRankStr= extractEnglish(grandGrandParentRankStr);
|
156
|
|
157
|
//Name etc.
|
158
|
Rank rank = makeRank(state, rankStr);
|
159
|
checkRankMarker(state, rank);
|
160
|
IZoologicalName name = TaxonNameFactory.NewZoologicalInstance(rank);
|
161
|
setNamePart(nameStr, rank, name);
|
162
|
Rank parentRank = makeRank(state, parentRankStr);
|
163
|
setNamePart(parentNameStr, parentRank, name);
|
164
|
Rank parentParentRank = makeRank(state, grandParentRankStr);
|
165
|
setNamePart(grandParentNameStr, parentParentRank, name);
|
166
|
Rank grandParentParentRank = makeRank(state, grandGrandParentRankStr);
|
167
|
setNamePart(grandGrandParentNameStr, grandParentParentRank, name);
|
168
|
if (grandParentParentRank != null && grandParentParentRank.isLower(Rank.GENUS()) || isBlank(name.getGenusOrUninomial()) ){
|
169
|
logger.warn("Grand-Grandparent rank is lower than genus for " +
|
170
|
name.getTitleCache() + " (edapho-id: " + id + "; cdm-id: " + name.getId() + ")");
|
171
|
}
|
172
|
|
173
|
//Authors
|
174
|
if (StringUtils.isNotBlank(authorName)){
|
175
|
TeamOrPersonBase<?> author = state.getRelatedObject(AUTHOR_NAMESPACE, authorName, TeamOrPersonBase.class);
|
176
|
if (author == null){
|
177
|
logger.warn("Author not found in state: " + authorName);
|
178
|
}else{
|
179
|
if (isBrackets){
|
180
|
name.setBasionymAuthorship(author);
|
181
|
name.setOriginalPublicationYear(year);
|
182
|
}else{
|
183
|
name.setCombinationAuthorship(author);
|
184
|
name.setPublicationYear(year);
|
185
|
}
|
186
|
}
|
187
|
}
|
188
|
|
189
|
//nomRef
|
190
|
if (nomRefId != null){
|
191
|
Reference nomRef = state.getRelatedObject(REFERENCE_NAMESPACE, String.valueOf(nomRefId), Reference.class);
|
192
|
if (nomRef == null){
|
193
|
logger.warn("Reference " + nomRefId + " could not be found");
|
194
|
}
|
195
|
name.setNomenclaturalReference(nomRef);
|
196
|
}
|
197
|
name.setNomenclaturalMicroReference(StringUtils.isBlank(pages)? null : pages);
|
198
|
|
199
|
//taxon
|
200
|
Reference secRef = state.getRelatedObject(REFERENCE_NAMESPACE, state.getConfig().getSecUuid().toString(), Reference.class);
|
201
|
if (secRef == null){
|
202
|
secRef = makeSecRef(state);
|
203
|
}
|
204
|
if (isValid){
|
205
|
taxonBase = Taxon.NewInstance(name, secRef);
|
206
|
}else{
|
207
|
taxonBase = Synonym.NewInstance(name, secRef);
|
208
|
}
|
209
|
handleTaxonomicGroupMarker(state, taxonBase, isGroup);
|
210
|
taxaToSave.add(taxonBase);
|
211
|
|
212
|
//remarks
|
213
|
doNotes(taxonBase, remark);
|
214
|
|
215
|
//id
|
216
|
ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
|
217
|
ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
|
218
|
handleExampleIdentifiers(taxonBase, id);
|
219
|
}
|
220
|
|
221
|
|
222
|
/**
|
223
|
* @param rankStr
|
224
|
* @return
|
225
|
*/
|
226
|
private String extractEnglish(String rankStr) {
|
227
|
if (rankStr == null){
|
228
|
return null;
|
229
|
}
|
230
|
String[] splits = rankStr.split(", ");
|
231
|
if (splits.length != 3){
|
232
|
String message = "Wrong rank format: "+ rankStr;
|
233
|
logger.error(message);
|
234
|
return null;
|
235
|
}
|
236
|
return splits[1].trim();
|
237
|
}
|
238
|
|
239
|
|
240
|
static Map<Integer,UUID> idMap = new HashMap<>();
|
241
|
static{
|
242
|
idMap.put(86594, UUID.fromString("715c2370-45a4-450c-99f7-e196758979ca")); //Aporrectodea caliginosa
|
243
|
idMap.put(86593, UUID.fromString("230f1a69-5dcd-4829-a01c-17490a2fdf34")); //Aporrectodea
|
244
|
idMap.put(86684, UUID.fromString("0982dc0e-1a79-45a0-8abc-8166625b94b8")); //Achaeta
|
245
|
idMap.put(104328, UUID.fromString("15f0b5f8-44e4-4ae1-8b40-f36f0a049b27")); //Chamaedrilus
|
246
|
idMap.put(97537, UUID.fromString("899c62e3-a116-4c5b-b22a-c76e761cc32e")); //Araeolaimoides caecus
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* @param taxonBase
|
251
|
* @param id
|
252
|
*/
|
253
|
private void handleExampleIdentifiers(TaxonBase<?> taxonBase, Integer id) {
|
254
|
if (idMap.get(id) != null){
|
255
|
taxonBase.setUuid(idMap.get(id));
|
256
|
}
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* @param state
|
261
|
* @param rank
|
262
|
* @throws UndefinedTransformerMethodException
|
263
|
*/
|
264
|
private void checkRankMarker(EdaphobaseImportState state, Rank rank) throws UndefinedTransformerMethodException {
|
265
|
|
266
|
if (rank != null){
|
267
|
Set<Marker> markers = rank.getMarkers();
|
268
|
if ( markers.size() == 0){ //we assume that no markers exist, at least not for markers of unused ranks
|
269
|
UUID edaphoRankMarkerTypeUuid = state.getTransformer().getMarkerTypeUuid("EdaphoRankMarker");
|
270
|
MarkerType marker = getMarkerType(state, edaphoRankMarkerTypeUuid, "Edaphobase rank", "Rank used in Edaphobase", "EdaRk" );
|
271
|
Representation rep = Representation.NewInstance("Rang, verwendet in Edaphobase", "Edaphobase Rang", "EdaRg", Language.GERMAN());
|
272
|
marker.addRepresentation(rep);
|
273
|
rank.addMarker(Marker.NewInstance(marker, true));
|
274
|
getTermService().saveOrUpdate(rank);
|
275
|
}
|
276
|
}else{
|
277
|
logger.info("Rank is null and marker can not be set");
|
278
|
}
|
279
|
}
|
280
|
|
281
|
/**
|
282
|
* @param state
|
283
|
* @param isGroup
|
284
|
* @param taxonBase
|
285
|
*/
|
286
|
private void handleTaxonomicGroupMarker(EdaphobaseImportState state, TaxonBase<?> taxonBase, boolean isGroup) {
|
287
|
if (! isGroup){
|
288
|
return;
|
289
|
}else{
|
290
|
try {
|
291
|
MarkerType markerType = getMarkerType(state, state.getTransformer().getMarkerTypeUuid("TaxGrossgruppe"), "Tax. Gruppe", "Taxonomische Grossgruppe", "TGG", null, Language.GERMAN());
|
292
|
if (taxonBase.isInstanceOf(Synonym.class)){
|
293
|
logger.warn("Syonym is marked as 'taxonomische Grossgruppe'");
|
294
|
}
|
295
|
taxonBase.addMarker(Marker.NewInstance(markerType, true));
|
296
|
} catch (UndefinedTransformerMethodException e) {
|
297
|
}
|
298
|
}
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* @param state
|
303
|
* @return
|
304
|
*/
|
305
|
private Reference makeSecRef(EdaphobaseImportState state) {
|
306
|
Reference ref = ReferenceFactory.newDatabase();
|
307
|
ref.setTitle(state.getConfig().getEdaphobaseSecundumTitle());
|
308
|
ref.setUuid(state.getConfig().getSecUuid());
|
309
|
state.addRelatedObject(REFERENCE_NAMESPACE, ref.getUuid().toString(), ref);
|
310
|
getReferenceService().save(ref);
|
311
|
return ref;
|
312
|
}
|
313
|
|
314
|
@Override
|
315
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
|
316
|
EdaphobaseImportState state) {
|
317
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
318
|
Map<String, TeamOrPersonBase<?>> authorMap = new HashMap<>();
|
319
|
Set<String> authorSet = new HashSet<>();
|
320
|
Set<String> referenceIdSet = new HashSet<>();
|
321
|
|
322
|
try {
|
323
|
while (rs.next()){
|
324
|
String authorStr = rs.getString("tax_author_name");
|
325
|
authorSet.add(authorStr);
|
326
|
handleForeignKey(rs, referenceIdSet, "tax_document");
|
327
|
}
|
328
|
} catch (SQLException e) {
|
329
|
e.printStackTrace();
|
330
|
}
|
331
|
|
332
|
//Authors
|
333
|
Set<UUID> uuidSet = new HashSet<>();
|
334
|
for (String authorStr : authorSet){
|
335
|
UUID uuid = state.getAuthorUuid(authorStr);
|
336
|
uuidSet.add(uuid);
|
337
|
}
|
338
|
List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
|
339
|
Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
|
340
|
for (TeamOrPersonBase<?> author : authors){
|
341
|
authorUuidMap.put(author.getUuid(), author);
|
342
|
}
|
343
|
|
344
|
for (String authorStr : authorSet){
|
345
|
UUID uuid = state.getAuthorUuid(authorStr);
|
346
|
TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
|
347
|
authorMap.put(authorStr, author);
|
348
|
}
|
349
|
result.put(AUTHOR_NAMESPACE, authorMap);
|
350
|
|
351
|
//reference map
|
352
|
String nameSpace = REFERENCE_NAMESPACE;
|
353
|
Class<?> cdmClass = Reference.class;
|
354
|
Set<String> idSet = referenceIdSet;
|
355
|
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
356
|
result.put(nameSpace, referenceMap);
|
357
|
|
358
|
//secundum
|
359
|
UUID secUuid = state.getConfig().getSecUuid();
|
360
|
Reference secRef = getReferenceService().find(secUuid);
|
361
|
referenceMap.put(secUuid.toString(), secRef);
|
362
|
|
363
|
return result;
|
364
|
}
|
365
|
|
366
|
private void setNamePart(String nameStr, Rank rank, IZoologicalName name) {
|
367
|
if (rank != null){
|
368
|
if (rank.isSupraGeneric() || rank.isGenus()){
|
369
|
if (isBlank(name.getGenusOrUninomial())){
|
370
|
name.setGenusOrUninomial(nameStr);
|
371
|
}
|
372
|
}else if (rank.isInfraGenericButNotSpeciesGroup()){
|
373
|
if (isBlank(name.getInfraGenericEpithet())){
|
374
|
name.setInfraGenericEpithet(nameStr);
|
375
|
}
|
376
|
}else if (rank.isSpeciesAggregate() || rank.isSpecies()){
|
377
|
if (isBlank(name.getSpecificEpithet())){
|
378
|
name.setSpecificEpithet(nameStr);
|
379
|
}
|
380
|
}else if (rank.isInfraSpecific()){
|
381
|
if (isBlank(name.getInfraSpecificEpithet())){
|
382
|
name.setInfraSpecificEpithet(nameStr);
|
383
|
}
|
384
|
}
|
385
|
}
|
386
|
}
|
387
|
|
388
|
private Rank makeRank(EdaphobaseImportState state, String rankStr) {
|
389
|
Rank rank = null;
|
390
|
try {
|
391
|
rank = state.getTransformer().getRankByKey(rankStr);
|
392
|
if (rank == null && rankStr != null){
|
393
|
if (rankStr.equals("Cohort")){
|
394
|
//position not really clear #7285
|
395
|
Rank lowerRank = Rank.SUPERORDER();
|
396
|
rank = this.getRank(state, Rank.uuidCohort, "Cohort", "Cohort", null,
|
397
|
(OrderedTermVocabulary<Rank>)Rank.GENUS().getVocabulary(),
|
398
|
lowerRank, RankClass.Suprageneric);
|
399
|
}else if (rankStr.equals("Hyporder")){
|
400
|
rank = this.getRank(state, Rank.uuidHyporder, "Hyporder", "Hyporder", null,
|
401
|
(OrderedTermVocabulary<Rank>)Rank.GENUS().getVocabulary(),
|
402
|
Rank.SUBORDER(), RankClass.Suprageneric);
|
403
|
}
|
404
|
}
|
405
|
} catch (UndefinedTransformerMethodException e) {
|
406
|
e.printStackTrace();
|
407
|
}
|
408
|
return rank;
|
409
|
}
|
410
|
|
411
|
@Override
|
412
|
protected boolean doCheck(EdaphobaseImportState state) {
|
413
|
return true;
|
414
|
}
|
415
|
|
416
|
@Override
|
417
|
protected boolean isIgnore(EdaphobaseImportState state) {
|
418
|
return ! state.getConfig().isDoTaxa();
|
419
|
}
|
420
|
|
421
|
}
|