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