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 String EX
= " ex ";
51 private static final Logger logger
= Logger
.getLogger(RedListGefaesspflanzenImportNames
.class);
53 private static final String tableName
= "Rote Liste Gefäßpflanzen";
55 private static final String pluralString
= "names";
57 public RedListGefaesspflanzenImportNames() {
58 super(tableName
, pluralString
);
62 protected String
getIdQuery(RedListGefaesspflanzenImportState state
) {
63 return "SELECT NAMNR "
64 + "FROM V_TAXATLAS_D20_EXPORT t "
69 protected String
getRecordQuery(RedListGefaesspflanzenImportConfigurator config
) {
70 String result
= " SELECT * "
71 + " FROM V_TAXATLAS_D20_EXPORT t "
72 + " WHERE t.NAMNR IN (@IDSET)";
73 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
78 protected void doInvoke(RedListGefaesspflanzenImportState state
) {
79 super.doInvoke(state
);
84 public boolean doPartition(ResultSetPartitioner partitioner
, RedListGefaesspflanzenImportState state
) {
85 ResultSet rs
= partitioner
.getResultSet();
86 Set
<TaxonNameBase
> namesToSave
= new HashSet
<TaxonNameBase
>();
87 Set
<TaxonBase
> taxaToSave
= new HashSet
<TaxonBase
>();
90 makeSingleNameAndTaxon(state
, rs
, namesToSave
, taxaToSave
);
93 } catch (SQLException e
) {
97 getNameService().saveOrUpdate(namesToSave
);
98 getTaxonService().saveOrUpdate(taxaToSave
);
102 private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state
, ResultSet rs
, Set
<TaxonNameBase
> namesToSave
, Set
<TaxonBase
> taxaToSave
)
103 throws SQLException
{
104 long id
= rs
.getLong("NAMNR");
105 String taxNameString
= rs
.getString("TAXNAME");
106 String gueltString
= rs
.getString("GUELT");
107 String rangString
= rs
.getString("RANG");
108 String ep1String
= rs
.getString("EPI1");
109 String ep2String
= rs
.getString("EPI2");
110 String ep3String
= rs
.getString("EPI3");
111 String nomZusatzString
= rs
.getString("NOM_ZUSATZ");
112 String zusatzString
= rs
.getString("ZUSATZ");
113 String authorKombString
= rs
.getString("AUTOR_KOMB");
114 String authorBasiString
= rs
.getString("AUTOR_BASI");
116 if(CdmUtils
.isBlank(taxNameString
) && CdmUtils
.isBlank(ep1String
)){
117 logger
.error("NAMNR: "+id
+" No name found!");
120 Rank rank
= makeRank(state
, rangString
);
122 logger
.error("NAMNR: "+id
+" Rank could not be resolved.");
124 BotanicalName name
= BotanicalName
.NewInstance(rank
);
126 //ep1 should always be present
127 if(CdmUtils
.isBlank(ep1String
)){
128 logger
.error("NAMNR: "+id
+" EPI1 is empty!");
130 name
.setGenusOrUninomial(ep1String
);
131 if(!CdmUtils
.isBlank(ep2String
)){
132 name
.setSpecificEpithet(ep2String
);
134 if(!CdmUtils
.isBlank(ep3String
)){
135 if(rank
==Rank
.SUBSPECIES() ||
136 rank
==Rank
.VARIETY()){
137 name
.setInfraSpecificEpithet(ep3String
);
143 if(authorKombString
.contains(EX
)){
144 //TODO: what happens with multiple ex authors??
145 String
[] kombSplit
= authorKombString
.split(EX
);
146 if(kombSplit
.length
!=2){
147 logger
.error("NAMNR: "+id
+" Multiple ex combination authors found");
149 for (int i
= 0; i
< kombSplit
.length
; i
++) {
151 //first author is ex author
152 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(Namespace
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
153 name
.setExCombinationAuthorship(authorKomb
);
156 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(Namespace
.AUTHOR_NAMESPACE
, kombSplit
[i
]);
157 name
.setCombinationAuthorship(authorKomb
);
161 else if(authorKombString
.trim().equals("auct.")){
162 logger
.warn("NAMNR: "+id
+" AUCT information in AUTOR_KOMB column");
164 else if(!CdmUtils
.isBlank(authorKombString
)){
165 TeamOrPersonBase authorKomb
= (TeamOrPersonBase
) state
.getRelatedObject(Namespace
.AUTHOR_NAMESPACE
, authorKombString
);
166 name
.setCombinationAuthorship(authorKomb
);
169 if(authorBasiString
.contains(EX
)){
170 String
[] basiSplit
= authorBasiString
.split(EX
);
171 for (int i
= 0; i
< basiSplit
.length
; i
++) {
172 if(basiSplit
.length
!=2){
173 logger
.error("NAMNR: "+id
+" Multiple ex basionymn authors found");
176 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(Namespace
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
177 name
.setExBasionymAuthorship(authorBasi
);
180 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(Namespace
.AUTHOR_NAMESPACE
, basiSplit
[i
]);
181 name
.setBasionymAuthorship(authorBasi
);
185 else if(authorBasiString
.trim().equals("auct.")){
186 name
.setAppendedPhrase(authorBasiString
);
188 else if(!CdmUtils
.isBlank(authorBasiString
)){
189 //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
190 TeamOrPersonBase authorBasi
= (TeamOrPersonBase
) state
.getRelatedObject(Namespace
.AUTHOR_NAMESPACE
, authorBasiString
);
191 if(CdmUtils
.isBlank(authorKombString
)){
192 name
.setCombinationAuthorship(authorBasi
);
195 name
.setBasionymAuthorship(authorBasi
);
199 //check authorship consistency
200 String authorString
= rs
.getString("AUTOR");
201 String authorshipCache
= name
.getAuthorshipCache();
203 if(!CdmUtils
.isBlank(zusatzString
)){
204 authorString
= authorString
.replace(", "+zusatzString
, "");
206 // if(CdmUtils.isBlank(authorKombString) && !CdmUtils.isBlank(authorBasiString)){
207 // authorString = "("+authorString+")";
209 if(!authorString
.equals(authorshipCache
)){
210 logger
.warn("NAMNR: "+id
+" Authorship inconsistent! name.authorhshipCache <-> Column AUTOR: "+authorshipCache
+" <-> "+authorString
);
214 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, Namespace
.NAME_NAMESPACE
);
215 state
.getNameMap().put(id
, name
.getUuid());
217 namesToSave
.add(name
);
220 TaxonBase taxonBase
= null;
221 if(gueltString
.equals("1")){
222 taxonBase
= Taxon
.NewInstance(name
, null);
224 else if(gueltString
.equals("x") || gueltString
.equals("b")){
225 taxonBase
= Synonym
.NewInstance(name
, null);
228 logger
.error("NAMNR: "+id
+" Taxon for name "+name
+" could not be created.");
232 taxaToSave
.add(taxonBase
);
235 ImportHelper
.setOriginalSource(taxonBase
, state
.getTransactionalSourceReference(), id
, Namespace
.TAXON_NAMESPACE
);
236 state
.getTaxonMap().put(id
, taxonBase
.getUuid());
239 private Rank
makeRank(RedListGefaesspflanzenImportState state
, String rankStr
) {
242 rank
= state
.getTransformer().getRankByKey(rankStr
);
243 } catch (UndefinedTransformerMethodException e
) {
247 logger
.error(rankStr
+" could not be associated to a known rank.");
255 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
256 RedListGefaesspflanzenImportState state
) {
257 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
258 Map
<String
, AgentBase
<?
>> authorMap
= new HashMap
<String
, AgentBase
<?
>>();
262 String authorKombString
= rs
.getString("AUTOR_KOMB");
264 if(authorKombString
.contains(EX
)){
265 String
[] kombSplit
= authorKombString
.split(EX
);
266 for (int i
= 0; i
< kombSplit
.length
; i
++) {
267 if(!authorMap
.containsKey(kombSplit
[i
])){
268 authorMap
.put(kombSplit
[i
], getAgentService().load(state
.getAuthorMap().get(kombSplit
[i
])));
272 else if(!CdmUtils
.isBlank(authorKombString
) && !authorMap
.containsKey(authorKombString
)){
273 authorMap
.put(authorKombString
, getAgentService().load(state
.getAuthorMap().get(authorKombString
)));
276 String authorBasiString
= rs
.getString("AUTOR_BASI");
278 if(authorBasiString
.contains(EX
)){
279 String
[] basiSplit
= authorBasiString
.split(EX
);
280 for (int i
= 0; i
< basiSplit
.length
; i
++) {
281 if(!authorMap
.containsKey(basiSplit
[i
])){
282 authorMap
.put(basiSplit
[i
], getAgentService().load(state
.getAuthorMap().get(basiSplit
[i
])));
286 else if(!CdmUtils
.isBlank(authorBasiString
) && !authorMap
.containsKey(authorBasiString
)){
287 authorMap
.put(authorBasiString
, getAgentService().load(state
.getAuthorMap().get(authorBasiString
)));
290 } catch (SQLException e
) {
293 result
.put(Namespace
.AUTHOR_NAMESPACE
, authorMap
);
299 protected boolean doCheck(RedListGefaesspflanzenImportState state
) {
304 protected boolean isIgnore(RedListGefaesspflanzenImportState state
) {