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
, 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 TaxonRelationshipType taxonRelationshipTypeByKey
= new RedListGefaesspflanzenTransformer().getTaxonRelationshipTypeByKey(relationString
);
175 if(taxonRelationshipTypeByKey
==null){
176 RedListUtil
.logMessage(id
, "Could not interpret relationship "+relationString
+" for taxon "+taxonBase
, logger
);
178 cloneTaxon(taxonBase
, name
, taxonRelationshipTypeByKey
, taxaToSave
, id
, classificationNamespace
, state
);
183 * <b>NOTE:</b> the {@link TaxonRelationshipType} passed as parameter is
184 * directed <b>from the clone</b> to the taxon
186 private void cloneTaxon(TaxonBase taxonBase
, TaxonNameBase name
, TaxonRelationshipType relationFromCloneToTaxon
, Set
<TaxonBase
> taxaToSave
, long id
, String sourceNameSpace
, RedListGefaesspflanzenImportState state
){
187 TaxonBase clone
= (TaxonBase
) taxonBase
.clone();
189 if(taxonBase
.isInstanceOf(Taxon
.class)){
190 TaxonRelationship taxonRelation
= ((Taxon
) clone
).addTaxonRelation((Taxon
) taxonBase
, relationFromCloneToTaxon
, null, null);
191 taxonRelation
.setDoubtful(true);//TODO Ist das mit " mit Fragezeichen" gemeint?
193 ImportHelper
.setOriginalSource(clone
, state
.getTransactionalSourceReference(), id
, sourceNameSpace
);
194 taxaToSave
.add(clone
);
197 private TaxonBase
importTaxon(long id
, String taxNameString
, String gueltString
, String authorBasiString
,
198 String hybString
, BotanicalName name
) {
199 TaxonBase taxonBase
= null;
200 if(authorBasiString
.trim().contains(RedListUtil
.AUCT
)){
201 taxonBase
= Taxon
.NewInstance(name
, null);
202 taxonBase
.setAppendedPhrase(RedListUtil
.AUCT
);
204 else if(gueltString
.equals(RedListUtil
.GUELT_ACCEPTED_TAXON
)){
205 taxonBase
= Taxon
.NewInstance(name
, null);
207 else if(gueltString
.equals(RedListUtil
.GUELT_SYNONYM
) || gueltString
.equals(RedListUtil
.GUELT_BASIONYM
)){
208 taxonBase
= Synonym
.NewInstance(name
, null);
214 //check taxon name consistency
215 checkTaxonNameConsistency(id
, taxNameString
, hybString
, taxonBase
);
219 private void importAuthors(RedListGefaesspflanzenImportState state
, ResultSet rs
, long id
, String nomZusatzString
,
220 String taxZusatzString
, String zusatzString
, String authorKombString
, String authorBasiString
,
221 BotanicalName name
) throws SQLException
{
223 if(authorKombString
.contains(RedListUtil
.EX
)){
224 //TODO: what happens with multiple ex authors??
225 String
[] kombSplit
= authorKombString
.split(RedListUtil
.EX
);
226 if(kombSplit
.length
!=2){
227 RedListUtil
.logMessage(id
, "Multiple ex combination authors found", logger
);
229 for (int i
= 0; i
< kombSplit
.length
; i
++) {
231 //first author is ex author
232 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
233 name
.setExCombinationAuthorship(authorKomb
);
236 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
237 name
.setCombinationAuthorship(authorKomb
);
241 else if(authorKombString
.trim().contains(RedListUtil
.AUCT
)){
242 RedListUtil
.logMessage(id
, "AUCT information in "+RedListUtil
.AUTOR_KOMB
+" column", logger
);
244 else if(CdmUtils
.isNotBlank(authorKombString
)){
245 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, authorKombString
);
246 name
.setCombinationAuthorship(authorKomb
);
249 if(authorBasiString
.contains(RedListUtil
.EX
)){
250 String
[] basiSplit
= authorBasiString
.split(RedListUtil
.EX
);
251 for (int i
= 0; i
< basiSplit
.length
; i
++) {
252 if(basiSplit
.length
!=2){
253 RedListUtil
.logMessage(id
, "Multiple ex basionymn authors found", logger
);
256 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
257 if(CdmUtils
.isBlank(authorKombString
)){
258 name
.setExCombinationAuthorship(authorBasi
);
261 name
.setExBasionymAuthorship(authorBasi
);
265 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
266 if(CdmUtils
.isBlank(authorKombString
)){
267 name
.setCombinationAuthorship(authorBasi
);
270 name
.setBasionymAuthorship(authorBasi
);
275 else if(CdmUtils
.isNotBlank(authorBasiString
)){
276 //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
277 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, authorBasiString
);
278 if(CdmUtils
.isBlank(authorKombString
)){
279 name
.setCombinationAuthorship(authorBasi
);
282 name
.setBasionymAuthorship(authorBasi
);
286 //check authorship consistency
287 String authorString
= rs
.getString(RedListUtil
.AUTOR
);
288 String authorshipCache
= name
.getAuthorshipCache();
289 checkAuthorShipConsistency(id
, nomZusatzString
, taxZusatzString
, zusatzString
, authorString
, authorshipCache
);
292 private BotanicalName
importName(RedListGefaesspflanzenImportState state
, long id
, String taxNameString
,
293 String rangString
, String ep1String
, String ep2String
, String ep3String
, String nomZusatzString
,
294 String hybString
, Set
<TaxonNameBase
> namesToSave
) {
295 if(CdmUtils
.isBlank(taxNameString
) && CdmUtils
.isBlank(ep1String
)){
296 RedListUtil
.logMessage(id
, "No name found!", logger
);
299 Rank rank
= makeRank(id
, state
, rangString
);
300 BotanicalName name
= BotanicalName
.NewInstance(rank
);
302 //ep1 should always be present
303 if(CdmUtils
.isBlank(ep1String
)){
304 RedListUtil
.logMessage(id
, RedListUtil
.EPI1
+" is empty!", logger
);
306 name
.setGenusOrUninomial(ep1String
);
307 if(CdmUtils
.isNotBlank(ep2String
)){
308 name
.setSpecificEpithet(ep2String
);
310 if(CdmUtils
.isNotBlank(ep3String
)){
311 if(rank
==Rank
.SUBSPECIES() ||
312 rank
==Rank
.VARIETY()){
313 name
.setInfraSpecificEpithet(ep3String
);
316 //nomenclatural status
317 if(CdmUtils
.isNotBlank(nomZusatzString
)){
318 NomenclaturalStatusType status
= makeNomenclaturalStatus(id
, state
, nomZusatzString
);
320 name
.addStatus(NomenclaturalStatus
.NewInstance(status
));
324 if(hybString
.equals(RedListUtil
.HYB_X
)){
325 name
.setBinomHybrid(true);
327 else if(hybString
.equals(RedListUtil
.HYB_XF
)){
328 name
.setTrinomHybrid(true);
331 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, RedListUtil
.NAME_NAMESPACE
);
333 namesToSave
.add(name
);
337 private void checkAuthorShipConsistency(long id
, String nomZusatzString
, String taxZusatzString
,
338 String zusatzString
, String authorString
, String authorshipCache
) {
339 if(CdmUtils
.isNotBlank(zusatzString
)){
340 authorString
= authorString
.replace(", "+zusatzString
, "");
342 if(CdmUtils
.isNotBlank(nomZusatzString
)){
343 authorString
= authorString
.replace(", "+nomZusatzString
, "");
345 if(CdmUtils
.isNotBlank(taxZusatzString
)){
346 authorString
= authorString
.replace(", "+taxZusatzString
, "");
348 if(authorString
.equals(RedListUtil
.AUCT
)){
351 if(!authorString
.equals(authorshipCache
)){
352 RedListUtil
.logMessage(id
, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil
.AUTOR
+": "+authorshipCache
+" <-> "+authorString
, logger
);
356 private void checkTaxonNameConsistency(long id
, String taxNameString
, String hybString
, TaxonBase taxonBase
) {
357 String nameCache
= ((BotanicalName
)taxonBase
.getName()).getNameCache().trim();
359 if(taxNameString
.endsWith("agg.")){
360 taxNameString
= taxNameString
.replace("agg.", "aggr.");
362 if(hybString
.equalsIgnoreCase(RedListUtil
.HYB_X
)){
363 taxNameString
= taxNameString
.replace("× ", "×");//hybrid sign has no space after it in titleCache for binomial hybrids
365 if(taxNameString
.endsWith(Rank
.SPECIESGROUP().toString())){
366 taxNameString
.replaceAll(Rank
.SPECIESGROUP().toString(), "- Gruppe");
367 if(!taxNameString
.trim().equals(nameCache
)){
368 taxNameString
.replaceAll(Rank
.SPECIESGROUP().toString(), "- group");
371 if(!taxNameString
.trim().equals(nameCache
)){
372 RedListUtil
.logMessage(id
, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil
.TAXNAME
+": "+nameCache
+" <-> "+taxNameString
, logger
);
376 private Rank
makeRank(long id
, RedListGefaesspflanzenImportState state
, String rankStr
) {
379 rank
= state
.getTransformer().getRankByKey(rankStr
);
380 } catch (UndefinedTransformerMethodException e
) {
384 RedListUtil
.logMessage(id
, rankStr
+" could not be associated to a known rank.", logger
);
389 private NomenclaturalStatusType
makeNomenclaturalStatus(long id
, RedListGefaesspflanzenImportState state
, String nomZusatzString
) {
390 NomenclaturalStatusType status
= null;
392 status
= state
.getTransformer().getNomenclaturalStatusByKey(nomZusatzString
);
393 } catch (UndefinedTransformerMethodException e
) {
397 RedListUtil
.logMessage(id
, nomZusatzString
+" could not be associated to a known nomenclatural status.", logger
);
405 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
406 RedListGefaesspflanzenImportState state
) {
407 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
408 Map
<String
, AgentBase
<?
>> authorMap
= new HashMap
<String
, AgentBase
<?
>>();
412 String authorKombString
= rs
.getString(RedListUtil
.AUTOR_KOMB
);
414 if(authorKombString
.contains(RedListUtil
.EX
)){
415 String
[] kombSplit
= authorKombString
.split(RedListUtil
.EX
);
416 for (int i
= 0; i
< kombSplit
.length
; i
++) {
417 if(!authorMap
.containsKey(kombSplit
[i
])){
418 authorMap
.put(kombSplit
[i
], getAgentService().load(state
.getAuthorMap().get(kombSplit
[i
])));
422 else if(CdmUtils
.isNotBlank(authorKombString
) && !authorMap
.containsKey(authorKombString
)){
423 authorMap
.put(authorKombString
, getAgentService().load(state
.getAuthorMap().get(authorKombString
)));
426 String authorBasiString
= rs
.getString(RedListUtil
.AUTOR_BASI
);
428 if(authorBasiString
.contains(RedListUtil
.EX
)){
429 String
[] basiSplit
= authorBasiString
.split(RedListUtil
.EX
);
430 for (int i
= 0; i
< basiSplit
.length
; i
++) {
431 if(!authorMap
.containsKey(basiSplit
[i
])){
432 authorMap
.put(basiSplit
[i
], getAgentService().load(state
.getAuthorMap().get(basiSplit
[i
])));
436 else if(CdmUtils
.isNotBlank(authorBasiString
) && !authorMap
.containsKey(authorBasiString
)){
437 authorMap
.put(authorBasiString
, getAgentService().load(state
.getAuthorMap().get(authorBasiString
)));
440 } catch (SQLException e
) {
443 result
.put(RedListUtil
.AUTHOR_NAMESPACE
, authorMap
);
449 protected boolean doCheck(RedListGefaesspflanzenImportState state
) {
454 protected boolean isIgnore(RedListGefaesspflanzenImportState state
) {