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
.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
;
46 @SuppressWarnings("serial")
47 public class RedListGefaesspflanzenImportNames
extends DbImportBase
<RedListGefaesspflanzenImportState
, RedListGefaesspflanzenImportConfigurator
> {
49 private static final Logger logger
= Logger
.getLogger(RedListGefaesspflanzenImportNames
.class);
51 private static final String tableName
= "Rote Liste Gefäßpflanzen";
53 private static final String pluralString
= "names";
55 public RedListGefaesspflanzenImportNames() {
56 super(tableName
, pluralString
);
60 protected String
getIdQuery(RedListGefaesspflanzenImportState state
) {
61 return "SELECT NAMNR "
62 + "FROM V_TAXATLAS_D20_EXPORT t "
67 protected String
getRecordQuery(RedListGefaesspflanzenImportConfigurator config
) {
68 String result
= " SELECT * "
69 + " FROM V_TAXATLAS_D20_EXPORT t "
70 + " WHERE t.NAMNR IN (@IDSET)";
71 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
76 protected void doInvoke(RedListGefaesspflanzenImportState state
) {
77 super.doInvoke(state
);
82 public boolean doPartition(ResultSetPartitioner partitioner
, RedListGefaesspflanzenImportState state
) {
83 ResultSet rs
= partitioner
.getResultSet();
84 Set
<TaxonNameBase
> namesToSave
= new HashSet
<TaxonNameBase
>();
85 Set
<TaxonBase
> taxaToSave
= new HashSet
<TaxonBase
>();
88 makeSingleNameAndTaxon(state
, rs
, namesToSave
, taxaToSave
);
91 } catch (SQLException e
) {
95 getNameService().saveOrUpdate(namesToSave
);
96 getTaxonService().saveOrUpdate(taxaToSave
);
100 private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state
, ResultSet rs
, Set
<TaxonNameBase
> namesToSave
, Set
<TaxonBase
> taxaToSave
)
101 throws SQLException
{
102 long id
= rs
.getLong(RedListUtil
.NAMNR
);
103 String taxNameString
= rs
.getString(RedListUtil
.TAXNAME
);
104 String gueltString
= rs
.getString(RedListUtil
.GUELT
);
105 String rangString
= rs
.getString(RedListUtil
.RANG
);
106 String ep1String
= rs
.getString(RedListUtil
.EPI1
);
107 String ep2String
= rs
.getString(RedListUtil
.EPI2
);
108 String ep3String
= rs
.getString(RedListUtil
.EPI3
);
109 String nomZusatzString
= rs
.getString(RedListUtil
.NOM_ZUSATZ
);
110 String zusatzString
= rs
.getString(RedListUtil
.ZUSATZ
);
111 String authorKombString
= rs
.getString(RedListUtil
.AUTOR_KOMB
);
112 String authorBasiString
= rs
.getString(RedListUtil
.AUTOR_BASI
);
114 if(CdmUtils
.isBlank(taxNameString
) && CdmUtils
.isBlank(ep1String
)){
115 RedListUtil
.logMessage(id
, "No name found!", logger
);
118 Rank rank
= makeRank(state
, rangString
);
120 RedListUtil
.logMessage(id
, "Rank could not be resolved.", logger
);
122 BotanicalName name
= BotanicalName
.NewInstance(rank
);
124 //ep1 should always be present
125 if(CdmUtils
.isBlank(ep1String
)){
126 RedListUtil
.logMessage(id
, RedListUtil
.EPI1
+" is empty!", logger
);
128 name
.setGenusOrUninomial(ep1String
);
129 if(!CdmUtils
.isBlank(ep2String
)){
130 name
.setSpecificEpithet(ep2String
);
132 if(!CdmUtils
.isBlank(ep3String
)){
133 if(rank
==Rank
.SUBSPECIES() ||
134 rank
==Rank
.VARIETY()){
135 name
.setInfraSpecificEpithet(ep3String
);
141 if(authorKombString
.contains(RedListUtil
.EX
)){
142 //TODO: what happens with multiple ex authors??
143 String
[] kombSplit
= authorKombString
.split(RedListUtil
.EX
);
144 if(kombSplit
.length
!=2){
145 RedListUtil
.logMessage(id
, "Multiple ex combination authors found", logger
);
147 for (int i
= 0; i
< kombSplit
.length
; i
++) {
149 //first author is ex author
150 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
151 name
.setExCombinationAuthorship(authorKomb
);
154 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
155 name
.setCombinationAuthorship(authorKomb
);
159 else if(authorKombString
.trim().equals(RedListUtil
.AUCT
)){
160 RedListUtil
.logMessage(id
, "AUCT information in "+RedListUtil
.AUTOR_KOMB
+" column", logger
);
162 else if(!CdmUtils
.isBlank(authorKombString
)){
163 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, authorKombString
);
164 name
.setCombinationAuthorship(authorKomb
);
167 if(authorBasiString
.contains(RedListUtil
.EX
)){
168 String
[] basiSplit
= authorBasiString
.split(RedListUtil
.EX
);
169 for (int i
= 0; i
< basiSplit
.length
; i
++) {
170 if(basiSplit
.length
!=2){
171 RedListUtil
.logMessage(id
, "Multiple ex basionymn authors found", logger
);
174 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
175 if(CdmUtils
.isBlank(authorKombString
)){
176 name
.setExCombinationAuthorship(authorBasi
);
179 name
.setExBasionymAuthorship(authorBasi
);
183 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
184 if(CdmUtils
.isBlank(authorKombString
)){
185 name
.setCombinationAuthorship(authorBasi
);
188 name
.setBasionymAuthorship(authorBasi
);
193 else if(authorBasiString
.trim().equals(RedListUtil
.AUCT
)){
194 name
.setAppendedPhrase(authorBasiString
);
196 else if(!CdmUtils
.isBlank(authorBasiString
)){
197 //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
198 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(RedListUtil
.AUTHOR_NAMESPACE
, authorBasiString
);
199 if(CdmUtils
.isBlank(authorKombString
)){
200 name
.setCombinationAuthorship(authorBasi
);
203 name
.setBasionymAuthorship(authorBasi
);
207 //check authorship consistency
208 String authorString
= rs
.getString(RedListUtil
.AUTOR
);
209 String authorshipCache
= name
.getAuthorshipCache();
211 if(!CdmUtils
.isBlank(zusatzString
)){
212 authorString
= authorString
.replace(", "+zusatzString
, "");
214 // if(CdmUtils.isBlank(authorKombString) && !CdmUtils.isBlank(authorBasiString)){
215 // authorString = "("+authorString+")";
217 if(authorString
.equals(RedListUtil
.AUCT
)){
220 if(!authorString
.equals(authorshipCache
)){
221 RedListUtil
.logMessage(id
, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil
.AUTOR
+": "+authorshipCache
+" <-> "+authorString
, logger
);
225 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, RedListUtil
.NAME_NAMESPACE
);
226 state
.getNameMap().put(id
, name
.getUuid());
228 namesToSave
.add(name
);
231 TaxonBase taxonBase
= null;
232 if(gueltString
.equals(RedListUtil
.GUELT_ACCEPTED_TAXON
) || (name
.getAppendedPhrase()!=null && name
.getAppendedPhrase().equals(RedListUtil
.AUCT
))){
233 taxonBase
= Taxon
.NewInstance(name
, null);
235 else if(gueltString
.equals(RedListUtil
.GUELT_SYNONYM
) || gueltString
.equals(RedListUtil
.GUELT_BASIONYM
)){
236 taxonBase
= Synonym
.NewInstance(name
, null);
239 RedListUtil
.logMessage(id
, "Taxon for name "+name
+" could not be created.", logger
);
243 taxaToSave
.add(taxonBase
);
246 ImportHelper
.setOriginalSource(taxonBase
, state
.getTransactionalSourceReference(), id
, RedListUtil
.TAXON_NAMESPACE
);
247 state
.getTaxonMap().put(id
, taxonBase
.getUuid());
250 private Rank
makeRank(RedListGefaesspflanzenImportState state
, String rankStr
) {
253 rank
= state
.getTransformer().getRankByKey(rankStr
);
254 } catch (UndefinedTransformerMethodException e
) {
258 logger
.error(rankStr
+" could not be associated to a known rank.");
266 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
267 RedListGefaesspflanzenImportState state
) {
268 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
269 Map
<String
, AgentBase
<?
>> authorMap
= new HashMap
<String
, AgentBase
<?
>>();
273 String authorKombString
= rs
.getString(RedListUtil
.AUTOR_KOMB
);
275 if(authorKombString
.contains(RedListUtil
.EX
)){
276 String
[] kombSplit
= authorKombString
.split(RedListUtil
.EX
);
277 for (int i
= 0; i
< kombSplit
.length
; i
++) {
278 if(!authorMap
.containsKey(kombSplit
[i
])){
279 authorMap
.put(kombSplit
[i
], getAgentService().load(state
.getAuthorMap().get(kombSplit
[i
])));
283 else if(!CdmUtils
.isBlank(authorKombString
) && !authorMap
.containsKey(authorKombString
)){
284 authorMap
.put(authorKombString
, getAgentService().load(state
.getAuthorMap().get(authorKombString
)));
287 String authorBasiString
= rs
.getString(RedListUtil
.AUTOR_BASI
);
289 if(authorBasiString
.contains(RedListUtil
.EX
)){
290 String
[] basiSplit
= authorBasiString
.split(RedListUtil
.EX
);
291 for (int i
= 0; i
< basiSplit
.length
; i
++) {
292 if(!authorMap
.containsKey(basiSplit
[i
])){
293 authorMap
.put(basiSplit
[i
], getAgentService().load(state
.getAuthorMap().get(basiSplit
[i
])));
297 else if(!CdmUtils
.isBlank(authorBasiString
) && !authorMap
.containsKey(authorBasiString
)){
298 authorMap
.put(authorBasiString
, getAgentService().load(state
.getAuthorMap().get(authorBasiString
)));
301 } catch (SQLException e
) {
304 result
.put(RedListUtil
.AUTHOR_NAMESPACE
, authorMap
);
310 protected boolean doCheck(RedListGefaesspflanzenImportState state
) {
315 protected boolean isIgnore(RedListGefaesspflanzenImportState state
) {