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
{
106 long id
= rs
.getLong(RedListUtil
.NAMNR
);
107 String taxNameString
= rs
.getString(RedListUtil
.TAXNAME
);
108 String gueltString
= rs
.getString(RedListUtil
.GUELT
);
109 String rangString
= rs
.getString(RedListUtil
.RANG
);
110 String ep1String
= rs
.getString(RedListUtil
.EPI1
);
111 String ep2String
= rs
.getString(RedListUtil
.EPI2
);
112 String ep3String
= rs
.getString(RedListUtil
.EPI3
);
113 String nomZusatzString
= rs
.getString(RedListUtil
.NOM_ZUSATZ
);
114 String taxZusatzString
= rs
.getString(RedListUtil
.TAX_ZUSATZ
);
115 String zusatzString
= rs
.getString(RedListUtil
.ZUSATZ
);
116 String authorKombString
= rs
.getString(RedListUtil
.AUTOR_KOMB
);
117 String authorBasiString
= rs
.getString(RedListUtil
.AUTOR_BASI
);
120 if(CdmUtils
.isBlank(taxNameString
) && CdmUtils
.isBlank(ep1String
)){
121 RedListUtil
.logMessage(id
, "No name found!", logger
);
124 Rank rank
= makeRank(id
, state
, rangString
);
125 BotanicalName name
= BotanicalName
.NewInstance(rank
);
127 //ep1 should always be present
128 if(CdmUtils
.isBlank(ep1String
)){
129 RedListUtil
.logMessage(id
, RedListUtil
.EPI1
+" is empty!", logger
);
131 name
.setGenusOrUninomial(ep1String
);
132 if(CdmUtils
.isNotBlank(ep2String
)){
133 name
.setSpecificEpithet(ep2String
);
135 if(CdmUtils
.isNotBlank(ep3String
)){
136 if(rank
==Rank
.SUBSPECIES() ||
137 rank
==Rank
.VARIETY()){
138 name
.setInfraSpecificEpithet(ep3String
);
141 //nomenclatural status
142 if(CdmUtils
.isNotBlank(nomZusatzString
)){
143 NomenclaturalStatusType status
= makeNomenclaturalStatus(id
, state
, nomZusatzString
);
145 name
.addStatus(NomenclaturalStatus
.NewInstance(status
));
152 if(authorKombString
.contains(RedListUtil
.EX
)){
153 //TODO: what happens with multiple ex authors??
154 String
[] kombSplit
= authorKombString
.split(RedListUtil
.EX
);
155 if(kombSplit
.length
!=2){
156 RedListUtil
.logMessage(id
, "Multiple ex combination authors found", logger
);
158 for (int i
= 0; i
< kombSplit
.length
; i
++) {
160 //first author is ex author
161 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
162 name
.setExCombinationAuthorship(authorKomb
);
165 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
166 name
.setCombinationAuthorship(authorKomb
);
170 else if(authorKombString
.trim().contains(RedListUtil
.AUCT
)){
171 RedListUtil
.logMessage(id
, "AUCT information in "+RedListUtil
.AUTOR_KOMB
+" column", logger
);
173 else if(CdmUtils
.isNotBlank(authorKombString
)){
174 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, authorKombString
);
175 name
.setCombinationAuthorship(authorKomb
);
178 if(authorBasiString
.contains(RedListUtil
.EX
)){
179 String
[] basiSplit
= authorBasiString
.split(RedListUtil
.EX
);
180 for (int i
= 0; i
< basiSplit
.length
; i
++) {
181 if(basiSplit
.length
!=2){
182 RedListUtil
.logMessage(id
, "Multiple ex basionymn authors found", logger
);
185 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
186 if(CdmUtils
.isBlank(authorKombString
)){
187 name
.setExCombinationAuthorship(authorBasi
);
190 name
.setExBasionymAuthorship(authorBasi
);
194 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
195 if(CdmUtils
.isBlank(authorKombString
)){
196 name
.setCombinationAuthorship(authorBasi
);
199 name
.setBasionymAuthorship(authorBasi
);
204 else if(CdmUtils
.isNotBlank(authorBasiString
)){
205 //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
206 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, authorBasiString
);
207 if(CdmUtils
.isBlank(authorKombString
)){
208 name
.setCombinationAuthorship(authorBasi
);
211 name
.setBasionymAuthorship(authorBasi
);
215 //check authorship consistency
216 String authorString
= rs
.getString(RedListUtil
.AUTOR
);
217 String authorshipCache
= name
.getAuthorshipCache();
219 if(CdmUtils
.isNotBlank(zusatzString
)){
220 authorString
= authorString
.replace(", "+zusatzString
, "");
222 if(CdmUtils
.isNotBlank(nomZusatzString
)){
223 authorString
= authorString
.replace(", "+nomZusatzString
, "");
225 if(CdmUtils
.isNotBlank(taxZusatzString
)){
226 authorString
= authorString
.replace(", "+taxZusatzString
, "");
228 if(authorString
.equals(RedListUtil
.AUCT
)){
231 if(!authorString
.equals(authorshipCache
)){
232 RedListUtil
.logMessage(id
, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil
.AUTOR
+": "+authorshipCache
+" <-> "+authorString
, logger
);
236 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, RedListUtil
.NAME_NAMESPACE
);
237 state
.getNameMap().put(id
, name
.getUuid());
239 namesToSave
.add(name
);
242 TaxonBase taxonBase
= null;
243 if(authorBasiString
.trim().contains(RedListUtil
.AUCT
)){
244 taxonBase
= Taxon
.NewInstance(name
, null);
245 taxonBase
.setAppendedPhrase(RedListUtil
.AUCT
);
247 else if(gueltString
.equals(RedListUtil
.GUELT_ACCEPTED_TAXON
)){
248 taxonBase
= Taxon
.NewInstance(name
, null);
250 else if(gueltString
.equals(RedListUtil
.GUELT_SYNONYM
) || gueltString
.equals(RedListUtil
.GUELT_BASIONYM
)){
251 taxonBase
= Synonym
.NewInstance(name
, null);
254 RedListUtil
.logMessage(id
, "Taxon for name "+name
+" could not be created.", logger
);
258 //check taxon name consistency
259 if(taxNameString
.endsWith("agg.")){
260 taxNameString
= taxNameString
.replace("agg.", "aggr.");
262 String nameCache
= ((BotanicalName
)taxonBase
.getName()).getNameCache().trim();
263 if(!taxNameString
.trim().equals(nameCache
)){
264 RedListUtil
.logMessage(id
, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil
.TAXNAME
+": "+nameCache
+" <-> "+taxNameString
, logger
);
268 taxaToSave
.add(taxonBase
);
271 ImportHelper
.setOriginalSource(taxonBase
, state
.getTransactionalSourceReference(), id
, RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
);
272 state
.getTaxonMap().put(id
, taxonBase
.getUuid());
274 /*check if taxon/synonym is also in checklist
275 * 1. create new taxon with the same name (in the checklist classification)
276 * 2. create congruent concept relationship between both
278 String clTaxonString
= rs
.getString(RedListUtil
.CL_TAXON
);
279 if(CdmUtils
.isNotBlank(clTaxonString
) && !clTaxonString
.trim().equals("-")){
280 TaxonBase clone
= (TaxonBase
) taxonBase
.clone();
282 if(taxonBase
.isInstanceOf(Taxon
.class)){
283 TaxonRelationship taxonRelation
= ((Taxon
) taxonBase
).addTaxonRelation((Taxon
) clone
, TaxonRelationshipType
.CONGRUENT_TO(), null, null);
284 taxonRelation
.setDoubtful(true);//TODO Ist das mit " mit Fragezeichen" gemeint?
286 ImportHelper
.setOriginalSource(clone
, state
.getTransactionalSourceReference(), id
, RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
);
287 state
.getTaxonMap().put(id
, clone
.getUuid());
288 taxaToSave
.add(clone
);
293 private Rank
makeRank(long id
, RedListGefaesspflanzenImportState state
, String rankStr
) {
296 rank
= state
.getTransformer().getRankByKey(rankStr
);
297 } catch (UndefinedTransformerMethodException e
) {
301 RedListUtil
.logMessage(id
, rankStr
+" could not be associated to a known rank.", logger
);
306 private NomenclaturalStatusType
makeNomenclaturalStatus(long id
, RedListGefaesspflanzenImportState state
, String nomZusatzString
) {
307 NomenclaturalStatusType status
= null;
309 status
= state
.getTransformer().getNomenclaturalStatusByKey(nomZusatzString
);
310 } catch (UndefinedTransformerMethodException e
) {
314 RedListUtil
.logMessage(id
, nomZusatzString
+" could not be associated to a known nomenclatural status.", logger
);
322 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
323 RedListGefaesspflanzenImportState state
) {
324 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
325 Map
<String
, AgentBase
<?
>> authorMap
= new HashMap
<String
, AgentBase
<?
>>();
329 String authorKombString
= rs
.getString(RedListUtil
.AUTOR_KOMB
);
331 if(authorKombString
.contains(RedListUtil
.EX
)){
332 String
[] kombSplit
= authorKombString
.split(RedListUtil
.EX
);
333 for (int i
= 0; i
< kombSplit
.length
; i
++) {
334 if(!authorMap
.containsKey(kombSplit
[i
])){
335 authorMap
.put(kombSplit
[i
], getAgentService().load(state
.getAuthorMap().get(kombSplit
[i
])));
339 else if(CdmUtils
.isNotBlank(authorKombString
) && !authorMap
.containsKey(authorKombString
)){
340 authorMap
.put(authorKombString
, getAgentService().load(state
.getAuthorMap().get(authorKombString
)));
343 String authorBasiString
= rs
.getString(RedListUtil
.AUTOR_BASI
);
345 if(authorBasiString
.contains(RedListUtil
.EX
)){
346 String
[] basiSplit
= authorBasiString
.split(RedListUtil
.EX
);
347 for (int i
= 0; i
< basiSplit
.length
; i
++) {
348 if(!authorMap
.containsKey(basiSplit
[i
])){
349 authorMap
.put(basiSplit
[i
], getAgentService().load(state
.getAuthorMap().get(basiSplit
[i
])));
353 else if(CdmUtils
.isNotBlank(authorBasiString
) && !authorMap
.containsKey(authorBasiString
)){
354 authorMap
.put(authorBasiString
, getAgentService().load(state
.getAuthorMap().get(authorBasiString
)));
357 } catch (SQLException e
) {
360 result
.put(RedListUtil
.AUTHOR_NAMESPACE
, authorMap
);
366 protected boolean doCheck(RedListGefaesspflanzenImportState state
) {
371 protected boolean isIgnore(RedListGefaesspflanzenImportState state
) {