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.hibernate.HibernateProxyHelper;
|
24
|
import eu.etaxonomy.cdm.io.common.DbImportBase;
|
25
|
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
|
26
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
27
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
28
|
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
|
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.Rank;
|
33
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
34
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
36
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
37
|
|
38
|
/**
|
39
|
*
|
40
|
* @author pplitzner
|
41
|
* @date Mar 1, 2016
|
42
|
*
|
43
|
*/
|
44
|
|
45
|
@Component
|
46
|
@SuppressWarnings("serial")
|
47
|
public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
|
48
|
|
49
|
private static final String EX = " ex ";
|
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("NAMNR");
|
105
|
String taxNameString = rs.getString("TAXNAME");
|
106
|
String gueltString = rs.getString("GUELT");
|
107
|
String rangString = rs.getString("RANG");
|
108
|
String ep1String = rs.getString("EPI1");
|
109
|
String ep2String = rs.getString("EPI2");
|
110
|
String ep3String = rs.getString("EPI3");
|
111
|
String nomZusatzString = rs.getString("NOM_ZUSATZ");
|
112
|
String zusatzString = rs.getString("ZUSATZ");
|
113
|
String authorKombString = rs.getString("AUTOR_KOMB");
|
114
|
String authorBasiString = rs.getString("AUTOR_BASI");
|
115
|
|
116
|
if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
|
117
|
logger.error("NAMNR: "+id+" No name found!");
|
118
|
}
|
119
|
|
120
|
Rank rank = makeRank(state, rangString);
|
121
|
if(rank==null){
|
122
|
logger.error("NAMNR: "+id+" Rank could not be resolved.");
|
123
|
}
|
124
|
BotanicalName name = BotanicalName.NewInstance(rank);
|
125
|
|
126
|
//ep1 should always be present
|
127
|
if(CdmUtils.isBlank(ep1String)){
|
128
|
logger.error("NAMNR: "+id+" EPI1 is empty!");
|
129
|
}
|
130
|
name.setGenusOrUninomial(ep1String);
|
131
|
if(!CdmUtils.isBlank(ep2String)){
|
132
|
name.setSpecificEpithet(ep2String);
|
133
|
}
|
134
|
if(!CdmUtils.isBlank(ep3String)){
|
135
|
if(rank==Rank.SUBSPECIES() ||
|
136
|
rank==Rank.VARIETY()){
|
137
|
name.setInfraSpecificEpithet(ep3String);
|
138
|
}
|
139
|
}
|
140
|
|
141
|
//--- AUTHORS ---
|
142
|
//combination author
|
143
|
if(authorKombString.contains(EX)){
|
144
|
//TODO: what happens with multiple ex authors??
|
145
|
String[] kombSplit = authorKombString.split(EX);
|
146
|
if(kombSplit.length!=2){
|
147
|
logger.error("NAMNR: "+id+" Multiple ex combination authors found");
|
148
|
}
|
149
|
for (int i = 0; i < kombSplit.length; i++) {
|
150
|
if(i==0){
|
151
|
//first author is ex author
|
152
|
TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(kombSplit[i])), TeamOrPersonBase.class);
|
153
|
name.setExCombinationAuthorship(authorKomb);
|
154
|
}
|
155
|
else{
|
156
|
TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(kombSplit[i])), TeamOrPersonBase.class);
|
157
|
name.setCombinationAuthorship(authorKomb);
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
else if(!CdmUtils.isBlank(authorKombString)){
|
162
|
TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(authorKombString)), TeamOrPersonBase.class);
|
163
|
name.setCombinationAuthorship(authorKomb);
|
164
|
}
|
165
|
//basionym author
|
166
|
if(authorBasiString.contains(EX)){
|
167
|
String[] basiSplit = authorBasiString.split(EX);
|
168
|
for (int i = 0; i < basiSplit.length; i++) {
|
169
|
if(basiSplit.length!=2){
|
170
|
logger.error("NAMNR: "+id+" Multiple ex basionymn authors found");
|
171
|
}
|
172
|
if(i==0){
|
173
|
TeamOrPersonBase authorBasi = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(basiSplit[i])), TeamOrPersonBase.class);
|
174
|
name.setExBasionymAuthorship(authorBasi);
|
175
|
}
|
176
|
else{
|
177
|
TeamOrPersonBase authorBasi = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(basiSplit[i])), TeamOrPersonBase.class);
|
178
|
name.setBasionymAuthorship(authorBasi);
|
179
|
}
|
180
|
}
|
181
|
}
|
182
|
else if(!CdmUtils.isBlank(authorBasiString)){
|
183
|
//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
|
184
|
TeamOrPersonBase authorBasi = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(authorBasiString)), TeamOrPersonBase.class);
|
185
|
if(CdmUtils.isBlank(authorKombString)){
|
186
|
name.setCombinationAuthorship(authorBasi);
|
187
|
}
|
188
|
else{
|
189
|
name.setBasionymAuthorship(authorBasi);
|
190
|
}
|
191
|
}
|
192
|
|
193
|
//check authorship consistency
|
194
|
String authorString = rs.getString("AUTOR");
|
195
|
String authorshipCache = name.getAuthorshipCache();
|
196
|
|
197
|
if(!CdmUtils.isBlank(zusatzString)){
|
198
|
authorString = authorString.replace(", "+zusatzString, "");
|
199
|
}
|
200
|
if(CdmUtils.isBlank(authorKombString) && !CdmUtils.isBlank(authorBasiString)){
|
201
|
authorString = "("+authorString+")";
|
202
|
}
|
203
|
if(!authorString.equals(authorshipCache)){
|
204
|
logger.warn("NAMNR: "+id+" Authorship inconsistent! name.authorhshipCache <-> Column AUTOR: "+authorshipCache+" <-> "+authorString);
|
205
|
}
|
206
|
|
207
|
//id
|
208
|
ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, Namespace.NAME_NAMESPACE);
|
209
|
state.getNameMap().put(id, name.getUuid());
|
210
|
|
211
|
namesToSave.add(name);
|
212
|
|
213
|
//---TAXON---
|
214
|
TaxonBase taxonBase = null;
|
215
|
if(gueltString.equals("1")){
|
216
|
taxonBase = Taxon.NewInstance(name, null);
|
217
|
}
|
218
|
else if(gueltString.equals("x")){
|
219
|
taxonBase = Synonym.NewInstance(name, null);
|
220
|
}
|
221
|
else if(gueltString.equals("b")){
|
222
|
taxonBase = Synonym.NewInstance(name, null);
|
223
|
}
|
224
|
if(taxonBase==null){
|
225
|
logger.error("NAMNR: "+id+" Taxon for name "+name+" could not be created.");
|
226
|
return;
|
227
|
}
|
228
|
|
229
|
taxaToSave.add(taxonBase);
|
230
|
|
231
|
//id
|
232
|
ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, Namespace.TAXON_NAMESPACE);
|
233
|
state.getTaxonMap().put(id, taxonBase.getUuid());
|
234
|
}
|
235
|
|
236
|
private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
|
237
|
Rank rank = null;
|
238
|
try {
|
239
|
rank = state.getTransformer().getRankByKey(rankStr);
|
240
|
} catch (UndefinedTransformerMethodException e) {
|
241
|
e.printStackTrace();
|
242
|
}
|
243
|
if(rank==null){
|
244
|
logger.error(rankStr+" could not be associated to a known rank.");
|
245
|
}
|
246
|
return rank;
|
247
|
}
|
248
|
|
249
|
|
250
|
|
251
|
@Override
|
252
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
|
253
|
RedListGefaesspflanzenImportState state) {
|
254
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
255
|
// Map<Long, AgentBase<?>> authorKombMap = new HashMap<>();
|
256
|
// Map<Long, AgentBase<?>> authorBasiMap = new HashMap<>();
|
257
|
//
|
258
|
// //load authors
|
259
|
// for(Entry<Long, UUID> entry:state.getAuthorKombMap().entrySet()){
|
260
|
// authorKombMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
|
261
|
// }
|
262
|
// for(Entry<Long, UUID> entry:state.getAuthorBasiMap().entrySet()){
|
263
|
// authorBasiMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
|
264
|
// }
|
265
|
// try {
|
266
|
// while (rs.next()){
|
267
|
// long id = rs.getLong("NAMNR");
|
268
|
// }
|
269
|
// } catch (SQLException e) {
|
270
|
// e.printStackTrace();
|
271
|
// }
|
272
|
//
|
273
|
// //Authors
|
274
|
// Set<UUID> uuidSet = new HashSet<>();
|
275
|
// for (String authorStr : authorKombSet){
|
276
|
// UUID uuid = state.getAuthorUuid(authorStr);
|
277
|
// uuidSet.add(uuid);
|
278
|
// }
|
279
|
// List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
|
280
|
// Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
|
281
|
// for (TeamOrPersonBase<?> author : authors){
|
282
|
// authorUuidMap.put(author.getUuid(), author);
|
283
|
// }
|
284
|
//
|
285
|
// for (String authorStr : authorKombSet){
|
286
|
// UUID uuid = state.getAuthorUuid(authorStr);
|
287
|
// TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
|
288
|
// authorMap.put(authorStr, author);
|
289
|
// }
|
290
|
// result.put(AUTHOR_NAMESPACE, authorMap);
|
291
|
//
|
292
|
// //reference map
|
293
|
// String nameSpace = REFERENCE_NAMESPACE;
|
294
|
// Class<?> cdmClass = Reference.class;
|
295
|
// Set<String> idSet = referenceIdSet;
|
296
|
// Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
297
|
// result.put(nameSpace, referenceMap);
|
298
|
//
|
299
|
// //secundum
|
300
|
// UUID secUuid = state.getConfig().getSecUuid();
|
301
|
// Reference<?> secRef = getReferenceService().find(secUuid);
|
302
|
// referenceMap.put(secUuid.toString(), secRef);
|
303
|
|
304
|
return result;
|
305
|
}
|
306
|
|
307
|
@Override
|
308
|
protected boolean doCheck(RedListGefaesspflanzenImportState state) {
|
309
|
return false;
|
310
|
}
|
311
|
|
312
|
@Override
|
313
|
protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
|
314
|
return false;
|
315
|
}
|
316
|
|
317
|
}
|