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.gefaesspflanzen;
|
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.common.CdmUtils;
|
24
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
25
|
import eu.etaxonomy.cdm.io.common.DbImportBase;
|
26
|
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
|
27
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.common.Language;
|
30
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
32
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
33
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
34
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
|
35
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
38
|
|
39
|
/**
|
40
|
*
|
41
|
* @author pplitzner
|
42
|
* @date Mar 1, 2016
|
43
|
*
|
44
|
*/
|
45
|
|
46
|
@Component
|
47
|
@SuppressWarnings("serial")
|
48
|
public class RedListGefaesspflanzenImportClassification extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
|
49
|
|
50
|
private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportClassification.class);
|
51
|
|
52
|
private static final String tableName = "Rote Liste Gefäßpflanzen";
|
53
|
|
54
|
private static final String pluralString = "classifications";
|
55
|
|
56
|
public RedListGefaesspflanzenImportClassification() {
|
57
|
super(tableName, pluralString);
|
58
|
}
|
59
|
|
60
|
@Override
|
61
|
protected String getIdQuery(RedListGefaesspflanzenImportState state) {
|
62
|
return "SELECT NAMNR "
|
63
|
+ "FROM V_TAXATLAS_D20_EXPORT t "
|
64
|
+ " ORDER BY NAMNR";
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
|
69
|
String result = " SELECT * "
|
70
|
+ " FROM V_TAXATLAS_D20_EXPORT t "
|
71
|
+ " WHERE t.NAMNR IN (@IDSET)";
|
72
|
result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
|
73
|
return result;
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
protected void doInvoke(RedListGefaesspflanzenImportState state) {
|
78
|
makeClassification("Gesamtliste", state.getConfig().getClassificationUuid(), "Gesamtliste_ref", RedListUtil.gesamtListeReferenceUuid, state);
|
79
|
makeClassification("Checkliste", RedListUtil.checkListClassificationUuid, "Checkliste_ref", RedListUtil.checkListReferenceUuid, state);
|
80
|
makeClassification("E", RedListUtil.uuidClassificationE, "E_ref", RedListUtil.uuidClassificationReferenceE, state);
|
81
|
makeClassification("W", RedListUtil.uuidClassificationW, "W_ref", RedListUtil.uuidClassificationReferenceW, state);
|
82
|
makeClassification("K", RedListUtil.uuidClassificationK, "K_ref", RedListUtil.uuidClassificationReferenceK, state);
|
83
|
makeClassification("AW", RedListUtil.uuidClassificationAW, "AW_ref", RedListUtil.uuidClassificationReferenceAW, state);
|
84
|
makeClassification("AO", RedListUtil.uuidClassificationAO, "AO_ref", RedListUtil.uuidClassificationReferenceAO, state);
|
85
|
makeClassification("R", RedListUtil.uuidClassificationR, "R_ref", RedListUtil.uuidClassificationReferenceR, state);
|
86
|
makeClassification("O", RedListUtil.uuidClassificationO, "O_ref", RedListUtil.uuidClassificationReferenceO, state);
|
87
|
makeClassification("S", RedListUtil.uuidClassificationS, "S_ref", RedListUtil.uuidClassificationReferenceS, state);
|
88
|
super.doInvoke(state);
|
89
|
}
|
90
|
|
91
|
|
92
|
@Override
|
93
|
public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
|
94
|
ResultSet rs = partitioner.getResultSet();
|
95
|
Classification gesamtListeClassification = getClassificationService().load(state.getConfig().getClassificationUuid());
|
96
|
Classification checklistClassification = getClassificationService().load(RedListUtil.checkListClassificationUuid);
|
97
|
Classification classificationE = getClassificationService().load(RedListUtil.uuidClassificationE);
|
98
|
Classification classificationW = getClassificationService().load(RedListUtil.uuidClassificationW);
|
99
|
Classification classificationK = getClassificationService().load(RedListUtil.uuidClassificationK);
|
100
|
Classification classificationAW = getClassificationService().load(RedListUtil.uuidClassificationAW);
|
101
|
Classification classificationAO = getClassificationService().load(RedListUtil.uuidClassificationAO);
|
102
|
Classification classificationR = getClassificationService().load(RedListUtil.uuidClassificationR);
|
103
|
Classification classificationO = getClassificationService().load(RedListUtil.uuidClassificationO);
|
104
|
Classification classificationS = getClassificationService().load(RedListUtil.uuidClassificationS);
|
105
|
try {
|
106
|
while (rs.next()){
|
107
|
makeSingleTaxonNode(state, rs, gesamtListeClassification, checklistClassification,
|
108
|
classificationE, classificationW, classificationK, classificationAW
|
109
|
, classificationAO, classificationR, classificationO, classificationS);
|
110
|
|
111
|
}
|
112
|
} catch (SQLException e) {
|
113
|
e.printStackTrace();
|
114
|
}
|
115
|
|
116
|
logger.info("Update classification (1000 nodes)");
|
117
|
getClassificationService().saveOrUpdate(gesamtListeClassification);
|
118
|
getClassificationService().saveOrUpdate(checklistClassification);
|
119
|
return true;
|
120
|
}
|
121
|
|
122
|
private void makeSingleTaxonNode(RedListGefaesspflanzenImportState state, ResultSet rs,
|
123
|
Classification gesamtListeClassification, Classification checklistClassification,
|
124
|
Classification classificationE, Classification classificationW, Classification classificationK,
|
125
|
Classification classificationAW, Classification classificationAO, Classification classificationR,
|
126
|
Classification classificationO, Classification classificationS)
|
127
|
throws SQLException {
|
128
|
long id = rs.getLong(RedListUtil.NAMNR);
|
129
|
String parentId = String.valueOf(rs.getLong(RedListUtil.LOWER));
|
130
|
String gueltString = rs.getString(RedListUtil.GUELT);
|
131
|
String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
|
132
|
|
133
|
//Gesamtliste
|
134
|
TaxonBase<?> taxonBaseGL = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
|
135
|
TaxonBase<?> parentBaseGL = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, TaxonBase.class);
|
136
|
if(parentBaseGL!=null && !parentBaseGL.isInstanceOf(Taxon.class)){
|
137
|
RedListUtil.logMessage(id, parentBaseGL+" is no taxon but is a parent of "+taxonBaseGL+" (Gesamtliste)", logger);
|
138
|
}
|
139
|
Taxon parentGL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, TaxonBase.class);
|
140
|
createParentChildNodes(gesamtListeClassification, id, gueltString, taxZusatzString, taxonBaseGL, parentGL);
|
141
|
|
142
|
//Checkliste
|
143
|
TaxonBase<?> taxonBaseCL = state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
|
144
|
TaxonBase<?> parentBaseCL = state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, parentId, TaxonBase.class);
|
145
|
if(parentBaseCL!=null && !parentBaseCL.isInstanceOf(Taxon.class)){
|
146
|
RedListUtil.logMessage(id, parentBaseCL+" is no taxon but is a parent of "+taxonBaseCL+" (Checkliste)", logger);
|
147
|
}
|
148
|
Taxon parentCL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, parentId, TaxonBase.class);
|
149
|
if(taxonBaseCL!=null){//null check necessary because not all taxa exist in the checklist
|
150
|
createParentChildNodes(checklistClassification, id, gueltString, taxZusatzString, taxonBaseCL, parentCL);
|
151
|
}
|
152
|
|
153
|
if(taxonBaseGL!= null && taxonBaseCL!=null
|
154
|
&& taxonBaseGL.getUuid().equals(taxonBaseCL.getUuid())){
|
155
|
RedListUtil.logMessage(id, "Same UUID for "+taxonBaseGL+ " (Gesamtliste) and "+taxonBaseCL+" (Checkliste)", logger);
|
156
|
}
|
157
|
if(parentGL!=null && parentCL!=null && parentGL.getUuid().equals(parentCL.getUuid())){
|
158
|
RedListUtil.logMessage(id, "Same UUID for "+parentGL+ " (Gesamtliste) and "+parentCL+" (Checkliste)", logger);
|
159
|
}
|
160
|
|
161
|
//add taxa for concept relationships to E, W, K, AW, AO, R, O, S
|
162
|
addTaxonToClassification(classificationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, id, state);
|
163
|
addTaxonToClassification(classificationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, id, state);
|
164
|
addTaxonToClassification(classificationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, id, state);
|
165
|
addTaxonToClassification(classificationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, id, state);
|
166
|
addTaxonToClassification(classificationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, id, state);
|
167
|
addTaxonToClassification(classificationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, id, state);
|
168
|
addTaxonToClassification(classificationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, id, state);
|
169
|
addTaxonToClassification(classificationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, id, state);
|
170
|
}
|
171
|
|
172
|
private void addTaxonToClassification(Classification classification, String classificationNamespace, long id, RedListGefaesspflanzenImportState state){
|
173
|
Taxon taxon = HibernateProxyHelper.deproxy(state.getRelatedObject(classificationNamespace, String.valueOf(id), TaxonBase.class), Taxon.class);
|
174
|
classification.addChildTaxon(taxon, null, null);
|
175
|
}
|
176
|
|
177
|
private void createParentChildNodes(Classification classification, long id, String gueltString,
|
178
|
String taxZusatzString, TaxonBase<?> taxonBase, Taxon parent) {
|
179
|
if(taxonBase==null){
|
180
|
RedListUtil.logMessage(id, "child taxon/synonym of "+parent+" is null. ("+classification.generateTitle()+")" , logger);
|
181
|
return;
|
182
|
}
|
183
|
//taxon
|
184
|
if(taxonBase.isInstanceOf(Taxon.class)){
|
185
|
//misapplied name
|
186
|
String appendedPhrase = taxonBase.getAppendedPhrase();
|
187
|
if(appendedPhrase!=null && appendedPhrase.equals(RedListUtil.AUCT)){
|
188
|
if(parent==null){
|
189
|
RedListUtil.logMessage(id, "parent taxon of misapplied name "+taxonBase+" is null. ("+classification.getTitleCache()+")" , logger);
|
190
|
return;
|
191
|
}
|
192
|
parent.addMisappliedName((Taxon) taxonBase, null, null);
|
193
|
}
|
194
|
else{
|
195
|
classification.addParentChild(parent, (Taxon)taxonBase, null, null);
|
196
|
}
|
197
|
|
198
|
if(CdmUtils.isNotBlank(taxZusatzString)){
|
199
|
if(taxZusatzString.trim().equals("p. p.")){
|
200
|
RedListUtil.logMessage(id, "pro parte for accepted taxon "+taxonBase, logger);
|
201
|
}
|
202
|
}
|
203
|
}
|
204
|
//synonym
|
205
|
else if(taxonBase.isInstanceOf(Synonym.class)){
|
206
|
if(parent==null){
|
207
|
RedListUtil.logMessage(id, "parent taxon of synonym "+taxonBase+" is null. ("+classification.getTitleCache()+")" , logger);
|
208
|
return;
|
209
|
}
|
210
|
//basionym
|
211
|
if(gueltString.equals(RedListUtil.GUELT_BASIONYM)){
|
212
|
parent.addHomotypicSynonym((Synonym) taxonBase, null, null);
|
213
|
parent.getName().addBasionym(taxonBase.getName());
|
214
|
}
|
215
|
//regular synonym
|
216
|
else{
|
217
|
SynonymRelationship synonymRelationship = parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null);
|
218
|
|
219
|
//TAX_ZUSATZ
|
220
|
if(CdmUtils.isNotBlank(taxZusatzString)){
|
221
|
if(taxZusatzString.trim().equals("p. p.")){
|
222
|
synonymRelationship.setProParte(true);
|
223
|
}
|
224
|
else if(taxZusatzString.trim().equals("s. l. p. p.")){
|
225
|
synonymRelationship.setProParte(true);
|
226
|
taxonBase.setAppendedPhrase("s. l.");
|
227
|
}
|
228
|
else if(taxZusatzString.trim().equals("s. str. p. p.")){
|
229
|
synonymRelationship.setProParte(true);
|
230
|
taxonBase.setAppendedPhrase("s. str.");
|
231
|
}
|
232
|
else if(taxZusatzString.trim().equals("s. l.")
|
233
|
|| taxZusatzString.trim().equals("s. str.")){
|
234
|
taxonBase.setAppendedPhrase(taxZusatzString);
|
235
|
}
|
236
|
else{
|
237
|
RedListUtil.logMessage(id, "unknown value "+taxZusatzString+" for column "+RedListUtil.TAX_ZUSATZ, logger);
|
238
|
}
|
239
|
}
|
240
|
}
|
241
|
}
|
242
|
//set sec reference
|
243
|
taxonBase.setSec(classification.getReference());
|
244
|
taxonBase.setTitleCache(null, false);//refresh title cache
|
245
|
}
|
246
|
|
247
|
@Override
|
248
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
|
249
|
RedListGefaesspflanzenImportState state) {
|
250
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
251
|
|
252
|
Set<String> idSet = new HashSet<String>();
|
253
|
try {
|
254
|
while (rs.next()){
|
255
|
idSet.add(String.valueOf(rs.getLong(RedListUtil.NAMNR)));
|
256
|
idSet.add(String.valueOf(rs.getLong(RedListUtil.LOWER)));
|
257
|
}
|
258
|
} catch (SQLException e) {
|
259
|
e.printStackTrace();
|
260
|
}
|
261
|
Map<String, TaxonBase> taxonMapGesamtListe = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
|
262
|
result.put(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, taxonMapGesamtListe);
|
263
|
Map<String, TaxonBase> taxonMapCheckliste = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
|
264
|
result.put(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, taxonMapCheckliste);
|
265
|
return result;
|
266
|
}
|
267
|
|
268
|
private void makeClassification(String classificationName, UUID classificationUuid, String referenceName, UUID referenceUuid, RedListGefaesspflanzenImportState state) {
|
269
|
Classification classification = Classification.NewInstance(classificationName, Language.DEFAULT());
|
270
|
classification.setUuid(classificationUuid);
|
271
|
Reference gesamtListeReference = ReferenceFactory.newGeneric();
|
272
|
gesamtListeReference.setTitle(referenceName);
|
273
|
gesamtListeReference.setUuid(referenceUuid);
|
274
|
classification.setReference(gesamtListeReference);
|
275
|
getClassificationService().save(classification);
|
276
|
}
|
277
|
|
278
|
@Override
|
279
|
protected boolean doCheck(RedListGefaesspflanzenImportState state) {
|
280
|
return false;
|
281
|
}
|
282
|
|
283
|
@Override
|
284
|
protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
|
285
|
return false;
|
286
|
}
|
287
|
|
288
|
}
|