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
|
package eu.etaxonomy.cdm.io.mexico;
|
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.logging.log4j.LogManager;
|
21
|
import org.apache.logging.log4j.Logger;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
27
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
28
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
30
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
31
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
32
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
33
|
|
34
|
/**
|
35
|
* @author a.mueller
|
36
|
* @since 08.02.2022
|
37
|
*/
|
38
|
@Component
|
39
|
public class MexicoEfloraTaxonRelationImport extends MexicoEfloraImportBase {
|
40
|
|
41
|
private static final long serialVersionUID = 8616047381536678637L;
|
42
|
private static final Logger logger = LogManager.getLogger();
|
43
|
|
44
|
protected static final String NAMESPACE = "TaxonRelation";
|
45
|
|
46
|
private static final String pluralString = "Taxon relations";
|
47
|
private static final String dbTableName = "EFlora_Taxonomia4CDM2";
|
48
|
|
49
|
|
50
|
public MexicoEfloraTaxonRelationImport(){
|
51
|
super(dbTableName, pluralString);
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
protected String getIdQuery(MexicoEfloraImportState state) {
|
56
|
String sql = " SELECT IdCAT "
|
57
|
+ " FROM " + dbTableName + " t "
|
58
|
+ " LEFT JOIN cv1_Controlled_vocabulary_for_name_Ranks r ON t.CategoriaTaxonomica = r.NombreCategoriaTaxonomica "
|
59
|
+ " WHERE t.IdCAT_AscendenteHerarquico4CDM NOT IN ('2PLANT') "
|
60
|
+ " ORDER BY r.Nivel1, IdCAT ";
|
61
|
return sql;
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
protected String getRecordQuery(MexicoEfloraImportConfigurator config) {
|
66
|
String sqlSelect = " SELECT t.*, acc.uuid accUuid, p.uuid pUuid, bas.uuid basUuid, p.IDCat pID ";
|
67
|
String sqlFrom = " FROM " + dbTableName + " t "
|
68
|
+ " LEFT JOIN "+dbTableName+" acc ON acc.IdCat = t.IdCATRel "
|
69
|
+ " LEFT JOIN "+dbTableName+" p ON p.IdCat = t.IdCAT_AscendenteHerarquico4CDM "
|
70
|
+ " LEFT JOIN "+dbTableName+" bas ON bas.IdCat = t.IdCAT_BasNomOrig "
|
71
|
;
|
72
|
String sqlWhere = " WHERE ( t.IdCAT IN (" + ID_LIST_TOKEN + ") )";
|
73
|
|
74
|
String strRecordQuery =sqlSelect + " " + sqlFrom + " " + sqlWhere ;
|
75
|
return strRecordQuery;
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, MexicoEfloraImportState state) {
|
80
|
classification = null;
|
81
|
boolean success = true ;
|
82
|
@SuppressWarnings("rawtypes")
|
83
|
Set<TaxonBase> taxaToSave = new HashSet<>();
|
84
|
@SuppressWarnings("unchecked")
|
85
|
Map<String, TaxonBase<?>> taxonMap = partitioner.getObjectMap(MexicoEfloraTaxonImport.NAMESPACE);
|
86
|
|
87
|
ResultSet rs = partitioner.getResultSet();
|
88
|
try{
|
89
|
while (rs.next()){
|
90
|
|
91
|
// if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
|
92
|
|
93
|
//create TaxonName element
|
94
|
String taxonId = rs.getString("IdCAT");
|
95
|
UUID uuid = UUID.fromString(rs.getString("uuid"));
|
96
|
String accUuidStr = rs.getString("accUuid"); //accepted for synonym
|
97
|
String parentUuidStr = rs.getString("pUuid"); //parent
|
98
|
String basUuidStr = rs.getString("basUuid"); //basionyms of accepted taxa
|
99
|
String parentId = rs.getString("pID");
|
100
|
|
101
|
if ("2PLANT".equals(parentId) || "79217TRACH".equals(parentId)) {
|
102
|
parentUuidStr = null;
|
103
|
}
|
104
|
|
105
|
TaxonBase<?> taxonBase = taxonMap.get(uuid.toString());
|
106
|
|
107
|
try {
|
108
|
if (taxonBase == null) {
|
109
|
logger.warn(taxonId + ": Taxon does not exist");
|
110
|
continue;
|
111
|
}else if (taxonBase.isInstanceOf(Synonym.class) && accUuidStr != null) {
|
112
|
Synonym syn = CdmBase.deproxy(taxonBase, Synonym.class);
|
113
|
TaxonBase<?> related = taxonMap.get(accUuidStr);
|
114
|
if (!related.isInstanceOf(Taxon.class)){
|
115
|
logger.warn(taxonId + ": Accepted taxon for synonym is not accepted: " + accUuidStr);
|
116
|
}else {
|
117
|
Taxon acc = CdmBase.deproxy(related, Taxon.class);
|
118
|
acc.addSynonym(syn, SynonymType.SYNONYM_OF);
|
119
|
}
|
120
|
}else if (taxonBase.isInstanceOf(Taxon.class) && parentUuidStr != null) {
|
121
|
Taxon child = CdmBase.deproxy(taxonBase, Taxon.class);
|
122
|
TaxonBase<?> parentBase = taxonMap.get(parentUuidStr);
|
123
|
if (!parentBase.isInstanceOf(Taxon.class)){
|
124
|
logger.warn(taxonId + ": Parent is not accepted: " + parentUuidStr);
|
125
|
}else {
|
126
|
Taxon parent = CdmBase.deproxy(parentBase, Taxon.class);
|
127
|
Reference parentChildReference = null;
|
128
|
getClassification(state).addParentChild(parent, child, parentChildReference, null);
|
129
|
}
|
130
|
}else {
|
131
|
logger.warn(taxonId + ": Taxon has no valid relationship: " + taxonBase.getName().getTitleCache());
|
132
|
if (taxonBase.isInstanceOf(Taxon.class)) {
|
133
|
Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
134
|
getClassification(state).addChildTaxon(taxon, null);
|
135
|
}
|
136
|
}
|
137
|
|
138
|
if (basUuidStr != null) {
|
139
|
TaxonName name = taxonBase.getName();
|
140
|
TaxonBase<?> basionymTaxon = taxonMap.get(basUuidStr);
|
141
|
name.addBasionym(basionymTaxon.getName());
|
142
|
}
|
143
|
|
144
|
partitioner.startDoSave();
|
145
|
taxaToSave.add(taxonBase);
|
146
|
} catch (Exception e) {
|
147
|
logger.warn("An exception (" +e.getMessage()+") occurred when trying to create relation for id " + taxonId + ". Relation could not be saved.");
|
148
|
e.printStackTrace();
|
149
|
success = false;
|
150
|
}
|
151
|
}
|
152
|
} catch (Exception e) {
|
153
|
logger.error("SQLException:" + e);
|
154
|
return false;
|
155
|
}
|
156
|
|
157
|
getTaxonService().save(taxaToSave);
|
158
|
logger.warn("Partition finished");
|
159
|
return success;
|
160
|
}
|
161
|
|
162
|
Classification classification;
|
163
|
private Classification getClassification(MexicoEfloraImportState state) {
|
164
|
if (classification == null) {
|
165
|
classification = getClassificationService().find(state.getConfig().getClassificationUuid());
|
166
|
if (classification == null) {
|
167
|
classification = Classification.NewInstance(state.getConfig().getClassificationName());
|
168
|
classification.setUuid(state.getConfig().getClassificationUuid());
|
169
|
classification.setTitleCache(state.getConfig().getClassificationName(), true);
|
170
|
getClassificationService().save(classification);
|
171
|
}
|
172
|
}
|
173
|
return classification;
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
|
178
|
|
179
|
String nameSpace;
|
180
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
181
|
|
182
|
try{
|
183
|
Set<UUID> taxonIdSet = new HashSet<>();
|
184
|
while (rs.next()){
|
185
|
handleForeignUuidKey(rs, taxonIdSet, "uuid");
|
186
|
handleForeignUuidKey(rs, taxonIdSet, "accUuid");
|
187
|
handleForeignUuidKey(rs, taxonIdSet, "pUuid");
|
188
|
handleForeignUuidKey(rs, taxonIdSet, "basUuid");
|
189
|
}
|
190
|
|
191
|
//taxon map
|
192
|
nameSpace = MexicoEfloraTaxonImport.NAMESPACE;
|
193
|
@SuppressWarnings("rawtypes")
|
194
|
Map<String, TaxonBase> taxonMap = new HashMap<>();
|
195
|
@SuppressWarnings("rawtypes")
|
196
|
List<TaxonBase> taxa = getTaxonService().find(taxonIdSet);
|
197
|
taxa.stream().forEach(t->taxonMap.put(t.getUuid().toString(), t));
|
198
|
result.put(nameSpace, taxonMap);
|
199
|
|
200
|
} catch (SQLException e) {
|
201
|
throw new RuntimeException(e);
|
202
|
}
|
203
|
return result;
|
204
|
}
|
205
|
|
206
|
@Override
|
207
|
protected String getTableName() {
|
208
|
return dbTableName;
|
209
|
}
|
210
|
|
211
|
@Override
|
212
|
public String getPluralString() {
|
213
|
return pluralString;
|
214
|
}
|
215
|
|
216
|
@Override
|
217
|
protected boolean doCheck(MexicoEfloraImportState state){
|
218
|
return true;
|
219
|
}
|
220
|
|
221
|
@Override
|
222
|
protected boolean isIgnore(MexicoEfloraImportState state){
|
223
|
return ! state.getConfig().isDoTaxa();
|
224
|
}
|
225
|
}
|