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
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
|
22
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
23
|
import eu.etaxonomy.cdm.io.common.DbImportBase;
|
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.AgentBase;
|
29
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
30
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
31
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
32
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
|
33
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
|
34
|
import eu.etaxonomy.cdm.model.name.Rank;
|
35
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
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 RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
|
50
|
|
51
|
private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
|
52
|
|
53
|
private static final String tableName = "Rote Liste Gefäßpflanzen";
|
54
|
|
55
|
private static final String pluralString = "names";
|
56
|
|
57
|
public RedListGefaesspflanzenImportNames() {
|
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
|
super.doInvoke(state);
|
80
|
}
|
81
|
|
82
|
|
83
|
@Override
|
84
|
public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
|
85
|
ResultSet rs = partitioner.getResultSet();
|
86
|
Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
|
87
|
Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
|
88
|
try {
|
89
|
while (rs.next()){
|
90
|
makeSingleNameAndTaxon(state, rs, namesToSave, taxaToSave);
|
91
|
|
92
|
}
|
93
|
} catch (SQLException e) {
|
94
|
e.printStackTrace();
|
95
|
}
|
96
|
|
97
|
getNameService().saveOrUpdate(namesToSave);
|
98
|
getTaxonService().saveOrUpdate(taxaToSave);
|
99
|
return true;
|
100
|
}
|
101
|
|
102
|
private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave, Set<TaxonBase> taxaToSave)
|
103
|
throws SQLException {
|
104
|
long id = rs.getLong(RedListUtil.NAMNR);
|
105
|
String taxNameString = rs.getString(RedListUtil.TAXNAME);
|
106
|
String gueltString = rs.getString(RedListUtil.GUELT);
|
107
|
String rangString = rs.getString(RedListUtil.RANG);
|
108
|
String ep1String = rs.getString(RedListUtil.EPI1);
|
109
|
String ep2String = rs.getString(RedListUtil.EPI2);
|
110
|
String ep3String = rs.getString(RedListUtil.EPI3);
|
111
|
String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
|
112
|
String zusatzString = rs.getString(RedListUtil.ZUSATZ);
|
113
|
String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
|
114
|
String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
|
115
|
|
116
|
//---NAME---
|
117
|
if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
|
118
|
RedListUtil.logMessage(id, "No name found!", logger);
|
119
|
}
|
120
|
|
121
|
Rank rank = makeRank(id, state, rangString);
|
122
|
BotanicalName name = BotanicalName.NewInstance(rank);
|
123
|
|
124
|
//ep1 should always be present
|
125
|
if(CdmUtils.isBlank(ep1String)){
|
126
|
RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
|
127
|
}
|
128
|
name.setGenusOrUninomial(ep1String);
|
129
|
if(CdmUtils.isNotBlank(ep2String)){
|
130
|
name.setSpecificEpithet(ep2String);
|
131
|
}
|
132
|
if(CdmUtils.isNotBlank(ep3String)){
|
133
|
if(rank==Rank.SUBSPECIES() ||
|
134
|
rank==Rank.VARIETY()){
|
135
|
name.setInfraSpecificEpithet(ep3String);
|
136
|
}
|
137
|
}
|
138
|
//nomenclatural status
|
139
|
if(CdmUtils.isNotBlank(nomZusatzString)){
|
140
|
NomenclaturalStatusType status = makeNomenclaturalStatus(id, state, nomZusatzString);
|
141
|
if(status!=null){
|
142
|
name.addStatus(NomenclaturalStatus.NewInstance(status));
|
143
|
}
|
144
|
}
|
145
|
|
146
|
|
147
|
//--- AUTHORS ---
|
148
|
//combination author
|
149
|
if(authorKombString.contains(RedListUtil.EX)){
|
150
|
//TODO: what happens with multiple ex authors??
|
151
|
String[] kombSplit = authorKombString.split(RedListUtil.EX);
|
152
|
if(kombSplit.length!=2){
|
153
|
RedListUtil.logMessage(id, "Multiple ex combination authors found", logger);
|
154
|
}
|
155
|
for (int i = 0; i < kombSplit.length; i++) {
|
156
|
if(i==0){
|
157
|
//first author is ex author
|
158
|
TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
|
159
|
name.setExCombinationAuthorship(authorKomb);
|
160
|
}
|
161
|
else{
|
162
|
TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
|
163
|
name.setCombinationAuthorship(authorKomb);
|
164
|
}
|
165
|
}
|
166
|
}
|
167
|
else if(authorKombString.trim().equals(RedListUtil.AUCT)){
|
168
|
RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
|
169
|
}
|
170
|
else if(CdmUtils.isNotBlank(authorKombString)){
|
171
|
TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorKombString);
|
172
|
name.setCombinationAuthorship(authorKomb);
|
173
|
}
|
174
|
//basionym author
|
175
|
if(authorBasiString.contains(RedListUtil.EX)){
|
176
|
String[] basiSplit = authorBasiString.split(RedListUtil.EX);
|
177
|
for (int i = 0; i < basiSplit.length; i++) {
|
178
|
if(basiSplit.length!=2){
|
179
|
RedListUtil.logMessage(id, "Multiple ex basionymn authors found", logger);
|
180
|
}
|
181
|
if(i==0){
|
182
|
TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
|
183
|
if(CdmUtils.isBlank(authorKombString)){
|
184
|
name.setExCombinationAuthorship(authorBasi);
|
185
|
}
|
186
|
else{
|
187
|
name.setExBasionymAuthorship(authorBasi);
|
188
|
}
|
189
|
}
|
190
|
else{
|
191
|
TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
|
192
|
if(CdmUtils.isBlank(authorKombString)){
|
193
|
name.setCombinationAuthorship(authorBasi);
|
194
|
}
|
195
|
else{
|
196
|
name.setBasionymAuthorship(authorBasi);
|
197
|
}
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
else if(CdmUtils.isNotBlank(authorBasiString)){
|
202
|
//this seems to be a convention in the source database: When there is only a single author then only the "AUTOR_BASI" column is used
|
203
|
TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorBasiString);
|
204
|
if(CdmUtils.isBlank(authorKombString)){
|
205
|
name.setCombinationAuthorship(authorBasi);
|
206
|
}
|
207
|
else{
|
208
|
name.setBasionymAuthorship(authorBasi);
|
209
|
}
|
210
|
}
|
211
|
|
212
|
//check authorship consistency
|
213
|
String authorString = rs.getString(RedListUtil.AUTOR);
|
214
|
String authorshipCache = name.getAuthorshipCache();
|
215
|
|
216
|
if(CdmUtils.isNotBlank(zusatzString)){
|
217
|
authorString = authorString.replace(", "+zusatzString, "");
|
218
|
}
|
219
|
if(authorString.equals(RedListUtil.AUCT)){
|
220
|
authorString = "";
|
221
|
}
|
222
|
if(!authorString.equals(authorshipCache)){
|
223
|
RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
|
224
|
}
|
225
|
|
226
|
//id
|
227
|
ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
|
228
|
state.getNameMap().put(id, name.getUuid());
|
229
|
|
230
|
namesToSave.add(name);
|
231
|
|
232
|
//---TAXON---
|
233
|
TaxonBase taxonBase = null;
|
234
|
if(gueltString.equals(RedListUtil.GUELT_ACCEPTED_TAXON) || (name.getAppendedPhrase()!=null && authorBasiString.trim().equals(RedListUtil.AUCT))){
|
235
|
taxonBase = Taxon.NewInstance(name, null);
|
236
|
taxonBase.setAppendedPhrase(authorBasiString);
|
237
|
}
|
238
|
else if(gueltString.equals(RedListUtil.GUELT_SYNONYM) || gueltString.equals(RedListUtil.GUELT_BASIONYM)){
|
239
|
taxonBase = Synonym.NewInstance(name, null);
|
240
|
}
|
241
|
if(taxonBase==null){
|
242
|
RedListUtil.logMessage(id, "Taxon for name "+name+" could not be created.", logger);
|
243
|
return;
|
244
|
}
|
245
|
|
246
|
taxaToSave.add(taxonBase);
|
247
|
|
248
|
//id
|
249
|
ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_NAMESPACE);
|
250
|
state.getTaxonMap().put(id, taxonBase.getUuid());
|
251
|
}
|
252
|
|
253
|
private Rank makeRank(long id, RedListGefaesspflanzenImportState state, String rankStr) {
|
254
|
Rank rank = null;
|
255
|
try {
|
256
|
rank = state.getTransformer().getRankByKey(rankStr);
|
257
|
} catch (UndefinedTransformerMethodException e) {
|
258
|
e.printStackTrace();
|
259
|
}
|
260
|
if(rank==null){
|
261
|
RedListUtil.logMessage(id, rankStr+" could not be associated to a known rank.", logger);
|
262
|
}
|
263
|
return rank;
|
264
|
}
|
265
|
|
266
|
private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
|
267
|
NomenclaturalStatusType status = null;
|
268
|
try {
|
269
|
status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
|
270
|
} catch (UndefinedTransformerMethodException e) {
|
271
|
e.printStackTrace();
|
272
|
}
|
273
|
if(status==null){
|
274
|
RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
|
275
|
}
|
276
|
return status;
|
277
|
}
|
278
|
|
279
|
|
280
|
|
281
|
@Override
|
282
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
|
283
|
RedListGefaesspflanzenImportState state) {
|
284
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
285
|
Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
|
286
|
|
287
|
try {
|
288
|
while (rs.next()){
|
289
|
String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
|
290
|
|
291
|
if(authorKombString.contains(RedListUtil.EX)){
|
292
|
String[] kombSplit = authorKombString.split(RedListUtil.EX);
|
293
|
for (int i = 0; i < kombSplit.length; i++) {
|
294
|
if(!authorMap.containsKey(kombSplit[i])){
|
295
|
authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
|
296
|
}
|
297
|
}
|
298
|
}
|
299
|
else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
|
300
|
authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
|
301
|
}
|
302
|
|
303
|
String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
|
304
|
//basionym author
|
305
|
if(authorBasiString.contains(RedListUtil.EX)){
|
306
|
String[] basiSplit = authorBasiString.split(RedListUtil.EX);
|
307
|
for (int i = 0; i < basiSplit.length; i++) {
|
308
|
if(!authorMap.containsKey(basiSplit[i])){
|
309
|
authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
|
310
|
}
|
311
|
}
|
312
|
}
|
313
|
else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
|
314
|
authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
|
315
|
}
|
316
|
}
|
317
|
} catch (SQLException e) {
|
318
|
e.printStackTrace();
|
319
|
}
|
320
|
result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
|
321
|
|
322
|
return result;
|
323
|
}
|
324
|
|
325
|
@Override
|
326
|
protected boolean doCheck(RedListGefaesspflanzenImportState state) {
|
327
|
return false;
|
328
|
}
|
329
|
|
330
|
@Override
|
331
|
protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
|
332
|
return false;
|
333
|
}
|
334
|
|
335
|
}
|