2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.redlist
.gefaesspflanzen
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
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 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
50 @SuppressWarnings("serial")
51 public class RedListGefaesspflanzenImportNames
extends DbImportBase
<RedListGefaesspflanzenImportState
, RedListGefaesspflanzenImportConfigurator
> {
53 private static final Logger logger
= Logger
.getLogger(RedListGefaesspflanzenImportNames
.class);
55 private static final String tableName
= "Rote Liste Gefäßpflanzen";
57 private static final String pluralString
= "names";
59 public RedListGefaesspflanzenImportNames() {
60 super(tableName
, pluralString
);
64 protected String
getIdQuery(RedListGefaesspflanzenImportState state
) {
65 return "SELECT NAMNR "
66 + "FROM V_TAXATLAS_D20_EXPORT t "
71 protected String
getRecordQuery(RedListGefaesspflanzenImportConfigurator config
) {
72 String result
= " SELECT * "
73 + " FROM V_TAXATLAS_D20_EXPORT t "
74 + " WHERE t.NAMNR IN (@IDSET)";
75 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
80 protected void doInvoke(RedListGefaesspflanzenImportState state
) {
81 super.doInvoke(state
);
86 public boolean doPartition(ResultSetPartitioner partitioner
, RedListGefaesspflanzenImportState state
) {
87 ResultSet rs
= partitioner
.getResultSet();
88 Set
<TaxonNameBase
> namesToSave
= new HashSet
<TaxonNameBase
>();
89 Set
<TaxonBase
> taxaToSave
= new HashSet
<TaxonBase
>();
92 makeSingleNameAndTaxon(state
, rs
, namesToSave
, taxaToSave
);
95 } catch (SQLException e
) {
99 getNameService().saveOrUpdate(namesToSave
);
100 getTaxonService().saveOrUpdate(taxaToSave
);
104 private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state
, ResultSet rs
, Set
<TaxonNameBase
> namesToSave
, Set
<TaxonBase
> taxaToSave
)
105 throws SQLException
{
107 long id
= rs
.getLong(RedListUtil
.NAMNR
);
108 String taxNameString
= rs
.getString(RedListUtil
.TAXNAME
);
109 String gueltString
= rs
.getString(RedListUtil
.GUELT
);
110 String rangString
= rs
.getString(RedListUtil
.RANG
);
111 String ep1String
= rs
.getString(RedListUtil
.EPI1
);
112 String ep2String
= rs
.getString(RedListUtil
.EPI2
);
113 String ep3String
= rs
.getString(RedListUtil
.EPI3
);
114 String nomZusatzString
= rs
.getString(RedListUtil
.NOM_ZUSATZ
);
115 String taxZusatzString
= rs
.getString(RedListUtil
.TAX_ZUSATZ
);
116 String zusatzString
= rs
.getString(RedListUtil
.ZUSATZ
);
117 String nonString
= rs
.getString(RedListUtil
.NON
);
118 String sensuString
= rs
.getString(RedListUtil
.SENSU
);
119 String authorKombString
= rs
.getString(RedListUtil
.AUTOR_KOMB
);
120 String authorBasiString
= rs
.getString(RedListUtil
.AUTOR_BASI
);
121 String hybString
= rs
.getString(RedListUtil
.HYB
);
122 String clTaxonString
= rs
.getString(RedListUtil
.CL_TAXON
);
123 String relationE
= rs
.getString(RedListUtil
.E
);
124 String relationW
= rs
.getString(RedListUtil
.W
);
125 String relationK
= rs
.getString(RedListUtil
.K
);
126 String relationAW
= rs
.getString(RedListUtil
.AW
);
127 String relationAO
= rs
.getString(RedListUtil
.AO
);
128 String relationR
= rs
.getString(RedListUtil
.R
);
129 String relationO
= rs
.getString(RedListUtil
.O
);
130 String relationS
= rs
.getString(RedListUtil
.S
);
133 BotanicalName name
= importName(state
, id
, taxNameString
, rangString
, ep1String
, ep2String
, ep3String
,
134 nomZusatzString
, hybString
, namesToSave
);
138 importAuthors(state
, rs
, id
, nomZusatzString
, taxZusatzString
, zusatzString
, authorKombString
,
139 authorBasiString
, name
);
142 TaxonBase taxonBase
= importTaxon(id
, taxNameString
, gueltString
, authorBasiString
, hybString
, name
);
144 RedListUtil
.logMessage(id
, "Taxon for name "+name
+" could not be created.", logger
);
148 //---CONCEPT RELATIONSHIPS---
149 /*check if taxon/synonym also exists in other classification
150 * 1. create new taxon with the same name (in that classification)
151 * 2. create concept relationship between both
154 if(CdmUtils
.isNotBlank(clTaxonString
) && !clTaxonString
.trim().equals("-")){
155 cloneTaxon(taxonBase
, name
, TaxonRelationshipType
.CONGRUENT_TO(), taxaToSave
, id
, RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
, false, true, state
);
157 //E, W, K, AW, AO, R, O, S
158 addConceptRelation(relationE
, RedListUtil
.CLASSIFICATION_NAMESPACE_E
, taxonBase
, name
, taxaToSave
, id
, state
);
159 addConceptRelation(relationW
, RedListUtil
.CLASSIFICATION_NAMESPACE_W
, taxonBase
, name
, taxaToSave
, id
, state
);
160 addConceptRelation(relationK
, RedListUtil
.CLASSIFICATION_NAMESPACE_K
, taxonBase
, name
, taxaToSave
, id
, state
);
161 addConceptRelation(relationAW
, RedListUtil
.CLASSIFICATION_NAMESPACE_AW
, taxonBase
, name
, taxaToSave
, id
, state
);
162 addConceptRelation(relationAO
, RedListUtil
.CLASSIFICATION_NAMESPACE_AO
, taxonBase
, name
, taxaToSave
, id
, state
);
163 addConceptRelation(relationR
, RedListUtil
.CLASSIFICATION_NAMESPACE_R
, taxonBase
, name
, taxaToSave
, id
, state
);
164 addConceptRelation(relationO
, RedListUtil
.CLASSIFICATION_NAMESPACE_O
, taxonBase
, name
, taxaToSave
, id
, state
);
165 addConceptRelation(relationS
, RedListUtil
.CLASSIFICATION_NAMESPACE_S
, taxonBase
, name
, taxaToSave
, id
, state
);
167 //NOTE: the source has to be added after cloning or otherwise the clone would also get the source
168 ImportHelper
.setOriginalSource(taxonBase
, state
.getTransactionalSourceReference(), id
, RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
);
169 taxaToSave
.add(taxonBase
);
172 private void addConceptRelation(String relationString
, String classificationNamespace
, TaxonBase taxonBase
, TaxonNameBase name
, Set
<TaxonBase
> taxaToSave
, long id
, RedListGefaesspflanzenImportState state
){
173 if(CdmUtils
.isNotBlank(relationString
) && !relationString
.equals(".")){
174 String substring
= relationString
.substring(relationString
.length()-1, relationString
.length());
175 TaxonRelationshipType taxonRelationshipTypeByKey
= new RedListGefaesspflanzenTransformer().getTaxonRelationshipTypeByKey(substring
);
176 if(taxonRelationshipTypeByKey
==null){
177 RedListUtil
.logMessage(id
, "Could not interpret relationship "+relationString
+" for taxon "+taxonBase
.generateTitle(), logger
);
179 //there is no type "included in" so we have to reverse the direction
180 if(substring
.equals("<")){
181 cloneTaxon(taxonBase
, name
, taxonRelationshipTypeByKey
, taxaToSave
, id
, classificationNamespace
, true, false, state
);
184 cloneTaxon(taxonBase
, name
, taxonRelationshipTypeByKey
, taxaToSave
, id
, classificationNamespace
, false, false, state
);
190 * <b>NOTE:</b> the {@link TaxonRelationshipType} passed as parameter is
191 * directed <b>from the clone</b> to the taxon.<br>
192 * This can be changed with parameter <i>reverseRelation</i>
194 private void cloneTaxon(TaxonBase taxonBase
, TaxonNameBase name
, TaxonRelationshipType relationFromCloneToTaxon
, Set
<TaxonBase
> taxaToSave
, long id
, String sourceNameSpace
, boolean reverseRelation
, boolean doubtful
, RedListGefaesspflanzenImportState state
){
195 TaxonBase clone
= (TaxonBase
) taxonBase
.clone();
197 if(taxonBase
.isInstanceOf(Taxon
.class)){
198 TaxonRelationship taxonRelation
;
200 taxonRelation
= ((Taxon
) taxonBase
).addTaxonRelation((Taxon
) clone
, relationFromCloneToTaxon
, null, null);
203 taxonRelation
= ((Taxon
) clone
).addTaxonRelation((Taxon
) taxonBase
, relationFromCloneToTaxon
, null, null);
205 taxonRelation
.setDoubtful(doubtful
);
207 ImportHelper
.setOriginalSource(clone
, state
.getTransactionalSourceReference(), id
, sourceNameSpace
);
208 taxaToSave
.add(clone
);
211 private TaxonBase
importTaxon(long id
, String taxNameString
, String gueltString
, String authorBasiString
,
212 String hybString
, BotanicalName name
) {
213 TaxonBase taxonBase
= null;
214 if(authorBasiString
.trim().contains(RedListUtil
.AUCT
)){
215 taxonBase
= Taxon
.NewInstance(name
, null);
216 taxonBase
.setAppendedPhrase(RedListUtil
.AUCT
);
218 else if(gueltString
.equals(RedListUtil
.GUELT_ACCEPTED_TAXON
)){
219 taxonBase
= Taxon
.NewInstance(name
, null);
221 else if(gueltString
.equals(RedListUtil
.GUELT_SYNONYM
) || gueltString
.equals(RedListUtil
.GUELT_BASIONYM
)){
222 taxonBase
= Synonym
.NewInstance(name
, null);
228 //check taxon name consistency
229 checkTaxonNameConsistency(id
, taxNameString
, hybString
, taxonBase
);
233 private void importAuthors(RedListGefaesspflanzenImportState state
, ResultSet rs
, long id
, String nomZusatzString
,
234 String taxZusatzString
, String zusatzString
, String authorKombString
, String authorBasiString
,
235 BotanicalName name
) throws SQLException
{
237 if(authorKombString
.contains(RedListUtil
.EX
)){
238 //TODO: what happens with multiple ex authors??
239 String
[] kombSplit
= authorKombString
.split(RedListUtil
.EX
);
240 if(kombSplit
.length
!=2){
241 RedListUtil
.logMessage(id
, "Multiple ex combination authors found", logger
);
243 for (int i
= 0; i
< kombSplit
.length
; i
++) {
245 //first author is ex author
246 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
247 name
.setExCombinationAuthorship(authorKomb
);
250 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
251 name
.setCombinationAuthorship(authorKomb
);
255 else if(authorKombString
.trim().contains(RedListUtil
.AUCT
)){
256 RedListUtil
.logMessage(id
, "AUCT information in "+RedListUtil
.AUTOR_KOMB
+" column", logger
);
258 else if(CdmUtils
.isNotBlank(authorKombString
)){
259 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, authorKombString
);
260 name
.setCombinationAuthorship(authorKomb
);
263 if(authorBasiString
.contains(RedListUtil
.EX
)){
264 String
[] basiSplit
= authorBasiString
.split(RedListUtil
.EX
);
265 for (int i
= 0; i
< basiSplit
.length
; i
++) {
266 if(basiSplit
.length
!=2){
267 RedListUtil
.logMessage(id
, "Multiple ex basionymn authors found", logger
);
270 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
271 if(CdmUtils
.isBlank(authorKombString
)){
272 name
.setExCombinationAuthorship(authorBasi
);
275 name
.setExBasionymAuthorship(authorBasi
);
279 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
280 if(CdmUtils
.isBlank(authorKombString
)){
281 name
.setCombinationAuthorship(authorBasi
);
284 name
.setBasionymAuthorship(authorBasi
);
289 else if(CdmUtils
.isNotBlank(authorBasiString
)){
290 //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
291 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, authorBasiString
);
292 if(CdmUtils
.isBlank(authorKombString
)){
293 name
.setCombinationAuthorship(authorBasi
);
296 name
.setBasionymAuthorship(authorBasi
);
300 //check authorship consistency
301 String authorString
= rs
.getString(RedListUtil
.AUTOR
);
302 String authorshipCache
= name
.getAuthorshipCache();
303 checkAuthorShipConsistency(id
, nomZusatzString
, taxZusatzString
, zusatzString
, authorString
, authorshipCache
);
306 private BotanicalName
importName(RedListGefaesspflanzenImportState state
, long id
, String taxNameString
,
307 String rangString
, String ep1String
, String ep2String
, String ep3String
, String nomZusatzString
,
308 String hybString
, Set
<TaxonNameBase
> namesToSave
) {
309 if(CdmUtils
.isBlank(taxNameString
) && CdmUtils
.isBlank(ep1String
)){
310 RedListUtil
.logMessage(id
, "No name found!", logger
);
313 Rank rank
= makeRank(id
, state
, rangString
);
314 BotanicalName name
= BotanicalName
.NewInstance(rank
);
316 //ep1 should always be present
317 if(CdmUtils
.isBlank(ep1String
)){
318 RedListUtil
.logMessage(id
, RedListUtil
.EPI1
+" is empty!", logger
);
320 name
.setGenusOrUninomial(ep1String
);
321 if(CdmUtils
.isNotBlank(ep2String
)){
322 name
.setSpecificEpithet(ep2String
);
324 if(CdmUtils
.isNotBlank(ep3String
)){
325 if(rank
==Rank
.SUBSPECIES() ||
326 rank
==Rank
.VARIETY()){
327 name
.setInfraSpecificEpithet(ep3String
);
330 //nomenclatural status
331 if(CdmUtils
.isNotBlank(nomZusatzString
)){
332 NomenclaturalStatusType status
= makeNomenclaturalStatus(id
, state
, nomZusatzString
);
334 name
.addStatus(NomenclaturalStatus
.NewInstance(status
));
338 if(hybString
.equals(RedListUtil
.HYB_X
)){
339 name
.setBinomHybrid(true);
341 else if(hybString
.equals(RedListUtil
.HYB_XF
)){
342 name
.setTrinomHybrid(true);
345 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, RedListUtil
.NAME_NAMESPACE
);
347 namesToSave
.add(name
);
351 private void checkAuthorShipConsistency(long id
, String nomZusatzString
, String taxZusatzString
,
352 String zusatzString
, String authorString
, String authorshipCache
) {
353 if(CdmUtils
.isNotBlank(zusatzString
)){
354 authorString
= authorString
.replace(", "+zusatzString
, "");
356 if(CdmUtils
.isNotBlank(nomZusatzString
)){
357 authorString
= authorString
.replace(", "+nomZusatzString
, "");
359 if(CdmUtils
.isNotBlank(taxZusatzString
)){
360 authorString
= authorString
.replace(", "+taxZusatzString
, "");
362 if(authorString
.equals(RedListUtil
.AUCT
)){
365 if(!authorString
.equals(authorshipCache
)){
366 RedListUtil
.logMessage(id
, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil
.AUTOR
+": "+authorshipCache
+" <-> "+authorString
, logger
);
370 private void checkTaxonNameConsistency(long id
, String taxNameString
, String hybString
, TaxonBase taxonBase
) {
371 String nameCache
= ((BotanicalName
)taxonBase
.getName()).getNameCache().trim();
373 if(taxNameString
.endsWith("agg.")){
374 taxNameString
= taxNameString
.replace("agg.", "aggr.");
376 if(hybString
.equalsIgnoreCase(RedListUtil
.HYB_X
)){
377 taxNameString
= taxNameString
.replace("× ", "×");//hybrid sign has no space after it in titleCache for binomial hybrids
379 if(taxNameString
.endsWith(Rank
.SPECIESGROUP().toString())){
380 taxNameString
.replaceAll(Rank
.SPECIESGROUP().toString(), "- Gruppe");
381 if(!taxNameString
.trim().equals(nameCache
)){
382 taxNameString
.replaceAll(Rank
.SPECIESGROUP().toString(), "- group");
385 if(!taxNameString
.trim().equals(nameCache
)){
386 RedListUtil
.logMessage(id
, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil
.TAXNAME
+": "+nameCache
+" <-> "+taxNameString
, logger
);
390 private Rank
makeRank(long id
, RedListGefaesspflanzenImportState state
, String rankStr
) {
393 rank
= state
.getTransformer().getRankByKey(rankStr
);
394 } catch (UndefinedTransformerMethodException e
) {
398 RedListUtil
.logMessage(id
, rankStr
+" could not be associated to a known rank.", logger
);
403 private NomenclaturalStatusType
makeNomenclaturalStatus(long id
, RedListGefaesspflanzenImportState state
, String nomZusatzString
) {
404 NomenclaturalStatusType status
= null;
406 status
= state
.getTransformer().getNomenclaturalStatusByKey(nomZusatzString
);
407 } catch (UndefinedTransformerMethodException e
) {
411 RedListUtil
.logMessage(id
, nomZusatzString
+" could not be associated to a known nomenclatural status.", logger
);
419 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
420 RedListGefaesspflanzenImportState state
) {
421 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
422 Map
<String
, AgentBase
<?
>> authorMap
= new HashMap
<String
, AgentBase
<?
>>();
426 String authorKombString
= rs
.getString(RedListUtil
.AUTOR_KOMB
);
428 if(authorKombString
.contains(RedListUtil
.EX
)){
429 String
[] kombSplit
= authorKombString
.split(RedListUtil
.EX
);
430 for (int i
= 0; i
< kombSplit
.length
; i
++) {
431 if(!authorMap
.containsKey(kombSplit
[i
])){
432 authorMap
.put(kombSplit
[i
], getAgentService().load(state
.getAuthorMap().get(kombSplit
[i
])));
436 else if(CdmUtils
.isNotBlank(authorKombString
) && !authorMap
.containsKey(authorKombString
)){
437 authorMap
.put(authorKombString
, getAgentService().load(state
.getAuthorMap().get(authorKombString
)));
440 String authorBasiString
= rs
.getString(RedListUtil
.AUTOR_BASI
);
442 if(authorBasiString
.contains(RedListUtil
.EX
)){
443 String
[] basiSplit
= authorBasiString
.split(RedListUtil
.EX
);
444 for (int i
= 0; i
< basiSplit
.length
; i
++) {
445 if(!authorMap
.containsKey(basiSplit
[i
])){
446 authorMap
.put(basiSplit
[i
], getAgentService().load(state
.getAuthorMap().get(basiSplit
[i
])));
450 else if(CdmUtils
.isNotBlank(authorBasiString
) && !authorMap
.containsKey(authorBasiString
)){
451 authorMap
.put(authorBasiString
, getAgentService().load(state
.getAuthorMap().get(authorBasiString
)));
454 } catch (SQLException e
) {
457 result
.put(RedListUtil
.AUTHOR_NAMESPACE
, authorMap
);
463 protected boolean doCheck(RedListGefaesspflanzenImportState state
) {
468 protected boolean isIgnore(RedListGefaesspflanzenImportState state
) {