1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.io.edaphobase;
|
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.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.codehaus.plexus.util.StringUtils;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
|
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.Rank;
|
32
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
33
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
34
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
38
|
|
39
|
/**
|
40
|
* @author a.mueller
|
41
|
* @date 18.12.2015
|
42
|
*
|
43
|
*/
|
44
|
@Component
|
45
|
public class EdaphobaseTaxonImport extends EdaphobaseImportBase {
|
46
|
private static final long serialVersionUID = -9138378836474086070L;
|
47
|
private static final Logger logger = Logger.getLogger(EdaphobaseTaxonImport.class);
|
48
|
|
49
|
private static final String tableName = "tax_taxon";
|
50
|
|
51
|
private static final String pluralString = "taxa";
|
52
|
|
53
|
private static final Object AUTHOR_NAMESPACE = "tax_author_name";
|
54
|
|
55
|
/**
|
56
|
* @param tableName
|
57
|
* @param pluralString
|
58
|
*/
|
59
|
public EdaphobaseTaxonImport() {
|
60
|
super(tableName, pluralString);
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
protected String getIdQuery(EdaphobaseImportState state) {
|
65
|
return "SELECT DISTINCT taxon_id FROM tax_taxon t "
|
66
|
+ " ORDER BY taxon_id";
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
protected String getRecordQuery(EdaphobaseImportConfigurator config) {
|
71
|
String result = " SELECT DISTINCT t.*, r.value as rankStr, pr.value as parentRankStr, ppr.value as grandParentRankStr, "
|
72
|
+ " pt.name as parentName, ppt.name as grandParentName "
|
73
|
+ " FROM tax_taxon t "
|
74
|
+ " LEFT JOIN tax_taxon pt ON t.parent_taxon_fk = pt.taxon_id "
|
75
|
+ " LEFT JOIN tax_taxon ppt ON pt.parent_taxon_fk = ppt.taxon_id"
|
76
|
+ " LEFT OUTER JOIN tax_rank_en r ON r.element_id = t.tax_rank_fk "
|
77
|
+ " LEFT OUTER JOIN tax_rank_en pr ON pr.element_id = pt.tax_rank_fk "
|
78
|
+ " LEFT OUTER JOIN tax_rank_en ppr ON pr.element_id = ppt.tax_rank_fk "
|
79
|
+ " WHERE t.taxon_id IN (@IDSET)";
|
80
|
result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
|
81
|
return result;
|
82
|
}
|
83
|
|
84
|
@Override
|
85
|
protected void doInvoke(EdaphobaseImportState state) {
|
86
|
super.doInvoke(state);
|
87
|
}
|
88
|
|
89
|
|
90
|
@Override
|
91
|
public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
|
92
|
ResultSet rs = partitioner.getResultSet();
|
93
|
Set<TaxonBase> taxaToSave = new HashSet<>();
|
94
|
try {
|
95
|
while (rs.next()){
|
96
|
makeSingleTaxon(state, rs, taxaToSave);
|
97
|
|
98
|
}
|
99
|
} catch (SQLException e) {
|
100
|
// TODO Auto-generated catch block
|
101
|
e.printStackTrace();
|
102
|
}
|
103
|
|
104
|
getTaxonService().saveOrUpdate(taxaToSave);
|
105
|
return true;
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* @param state
|
110
|
* @param rs
|
111
|
* @param taxaToSave
|
112
|
* @throws SQLException
|
113
|
*/
|
114
|
private void makeSingleTaxon(EdaphobaseImportState state, ResultSet rs, Set<TaxonBase> taxaToSave)
|
115
|
throws SQLException {
|
116
|
Integer id = nullSafeInt(rs, "taxon_id");
|
117
|
Integer year = nullSafeInt(rs, "tax_year");
|
118
|
boolean isBrackets = rs.getBoolean("tax_brackets");
|
119
|
String remark = rs.getString("remark");
|
120
|
String nameStr = rs.getString("name");
|
121
|
String authorName = rs.getString("tax_author_name");
|
122
|
//parentTaxonFk
|
123
|
//rankFk
|
124
|
Integer nomRefId = nullSafeInt(rs, "tax_document");
|
125
|
boolean isValid = rs.getBoolean("valid");
|
126
|
boolean idDeleted = rs.getBoolean("deleted");
|
127
|
String displayString = rs.getString("display_string");
|
128
|
Integer version = nullSafeInt(rs, "versionfield");
|
129
|
String pages = rs.getString("pages");
|
130
|
String treeIndex = rs.getString("path_to_root");
|
131
|
// Integer rankFk = nullSafeInt(rs, "tax_rank_fk");
|
132
|
String nameAddition = rs.getString("name_addition");
|
133
|
String officialRemark = rs.getString("official_remark");
|
134
|
boolean isGroup = rs.getBoolean("taxonomic_group");
|
135
|
String rankStr = rs.getString("rankStr");
|
136
|
String parentRankStr = rs.getString("parentRankStr");
|
137
|
String grandParentRankStr = rs.getString("grandParentRankStr");
|
138
|
String parentNameStr = rs.getString("parentName");
|
139
|
String grandParentNameStr = rs.getString("grandParentName");
|
140
|
|
141
|
TaxonBase<?> taxonBase;
|
142
|
|
143
|
//Name etc.
|
144
|
Rank rank = makeRank(state, rankStr);
|
145
|
ZoologicalName name = ZoologicalName.NewInstance(rank);
|
146
|
setNamePart(nameStr, rank, name);
|
147
|
Rank parentRank = makeRank(state, parentRankStr);
|
148
|
setNamePart(parentNameStr, parentRank, name);
|
149
|
Rank parentParentRank = makeRank(state, grandParentRankStr);
|
150
|
setNamePart(grandParentNameStr, parentParentRank, name);
|
151
|
|
152
|
//Authors
|
153
|
if (StringUtils.isNotBlank(authorName)){
|
154
|
TeamOrPersonBase<?> author = state.getRelatedObject(AUTHOR_NAMESPACE, authorName, TeamOrPersonBase.class);
|
155
|
if (author == null){
|
156
|
logger.warn("Author not found in state: " + authorName);
|
157
|
}else{
|
158
|
if (isBrackets){
|
159
|
name.setBasionymAuthorship(author);
|
160
|
name.setOriginalPublicationYear(year);
|
161
|
}else{
|
162
|
name.setCombinationAuthorship(author);
|
163
|
name.setPublicationYear(year);
|
164
|
}
|
165
|
}
|
166
|
}
|
167
|
|
168
|
//nomRef
|
169
|
if (nomRefId != null){
|
170
|
Reference<?> nomRef = state.getRelatedObject(REFERENCE_NAMESPACE, String.valueOf(nomRefId), Reference.class);
|
171
|
if (nomRef == null){
|
172
|
logger.warn("Reference " + nomRefId + " could not be found");
|
173
|
}
|
174
|
name.setNomenclaturalReference(nomRef);
|
175
|
}
|
176
|
name.setNomenclaturalMicroReference(StringUtils.isBlank(pages)? null : pages);
|
177
|
|
178
|
|
179
|
Reference<?> secRef = state.getRelatedObject(REFERENCE_NAMESPACE, state.getConfig().getSecUuid().toString(), Reference.class);
|
180
|
if (secRef == null){
|
181
|
secRef = makeSecRef(state);
|
182
|
}
|
183
|
if (isValid){
|
184
|
taxonBase = Taxon.NewInstance(name, secRef);
|
185
|
}else{
|
186
|
taxonBase = Synonym.NewInstance(name, secRef);
|
187
|
}
|
188
|
taxaToSave.add(taxonBase);
|
189
|
|
190
|
//remarks
|
191
|
doNotes(taxonBase, remark);
|
192
|
|
193
|
//id
|
194
|
ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
|
195
|
ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
|
196
|
}
|
197
|
|
198
|
|
199
|
/**
|
200
|
* @param state
|
201
|
* @return
|
202
|
*/
|
203
|
private Reference<?> makeSecRef(EdaphobaseImportState state) {
|
204
|
Reference<?> ref = ReferenceFactory.newDatabase();
|
205
|
ref.setTitle(state.getConfig().getEdaphobaseSecundumTitle());
|
206
|
ref.setUuid(state.getConfig().getSecUuid());
|
207
|
state.addRelatedObject(REFERENCE_NAMESPACE, ref.getUuid().toString(), ref);
|
208
|
getReferenceService().save(ref);
|
209
|
return ref;
|
210
|
}
|
211
|
|
212
|
@Override
|
213
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
|
214
|
EdaphobaseImportState state) {
|
215
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
216
|
Map<String, TeamOrPersonBase<?>> authorMap = new HashMap<>();
|
217
|
Set<String> authorSet = new HashSet<>();
|
218
|
Set<String> referenceIdSet = new HashSet<String>();
|
219
|
|
220
|
try {
|
221
|
while (rs.next()){
|
222
|
String authorStr = rs.getString("tax_author_name");
|
223
|
authorSet.add(authorStr);
|
224
|
handleForeignKey(rs, referenceIdSet, "tax_document");
|
225
|
}
|
226
|
} catch (SQLException e) {
|
227
|
e.printStackTrace();
|
228
|
}
|
229
|
|
230
|
//Authors
|
231
|
Set<UUID> uuidSet = new HashSet<>();
|
232
|
for (String authorStr : authorSet){
|
233
|
UUID uuid = state.getAuthorUuid(authorStr);
|
234
|
uuidSet.add(uuid);
|
235
|
}
|
236
|
List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
|
237
|
Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
|
238
|
for (TeamOrPersonBase<?> author : authors){
|
239
|
authorUuidMap.put(author.getUuid(), author);
|
240
|
}
|
241
|
|
242
|
for (String authorStr : authorSet){
|
243
|
UUID uuid = state.getAuthorUuid(authorStr);
|
244
|
TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
|
245
|
authorMap.put(authorStr, author);
|
246
|
}
|
247
|
result.put(AUTHOR_NAMESPACE, authorMap);
|
248
|
|
249
|
//reference map
|
250
|
String nameSpace = REFERENCE_NAMESPACE;
|
251
|
Class<?> cdmClass = Reference.class;
|
252
|
Set<String> idSet = referenceIdSet;
|
253
|
Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
254
|
result.put(nameSpace, referenceMap);
|
255
|
|
256
|
//secundum
|
257
|
UUID secUuid = state.getConfig().getSecUuid();
|
258
|
Reference<?> secRef = getReferenceService().find(secUuid);
|
259
|
referenceMap.put(secUuid.toString(), secRef);
|
260
|
|
261
|
return result;
|
262
|
}
|
263
|
|
264
|
private void setNamePart(String nameStr, Rank rank, ZoologicalName name) {
|
265
|
if (rank != null){
|
266
|
if (rank.isSupraGeneric() || rank.isGenus()){
|
267
|
if (StringUtils.isBlank(name.getGenusOrUninomial())){
|
268
|
name.setGenusOrUninomial(nameStr);
|
269
|
}
|
270
|
}else if (rank.isInfraGeneric()){
|
271
|
if (StringUtils.isBlank(name.getInfraGenericEpithet())){
|
272
|
name.setInfraGenericEpithet(nameStr);
|
273
|
}
|
274
|
}else if (rank.isSpeciesAggregate() || rank.isSpecies()){
|
275
|
if (StringUtils.isBlank(name.getSpecificEpithet())){
|
276
|
name.setSpecificEpithet(nameStr);
|
277
|
}
|
278
|
}else if (rank.isInfraSpecific()){
|
279
|
if (StringUtils.isBlank(name.getInfraSpecificEpithet())){
|
280
|
name.setInfraSpecificEpithet(nameStr);
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
}
|
285
|
|
286
|
private Rank makeRank(EdaphobaseImportState state, String rankStr) {
|
287
|
Rank rank = null;
|
288
|
try {
|
289
|
rank = state.getTransformer().getRankByKey(rankStr);
|
290
|
} catch (UndefinedTransformerMethodException e) {
|
291
|
e.printStackTrace();
|
292
|
}
|
293
|
return rank;
|
294
|
}
|
295
|
|
296
|
@Override
|
297
|
protected boolean doCheck(EdaphobaseImportState state) {
|
298
|
return false;
|
299
|
}
|
300
|
|
301
|
@Override
|
302
|
protected boolean isIgnore(EdaphobaseImportState state) {
|
303
|
return ! state.getConfig().isDoTaxa();
|
304
|
}
|
305
|
|
306
|
}
|