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
.hibernate
.HibernateProxyHelper
;
24 import eu
.etaxonomy
.cdm
.io
.common
.DbImportBase
;
25 import eu
.etaxonomy
.cdm
.io
.common
.IPartitionedIO
;
26 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
27 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
28 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.UndefinedTransformerMethodException
;
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
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(kombSplit
[i
])), TeamOrPersonBase
.class);
153 name
.setExCombinationAuthorship(authorKomb
);
156 TeamOrPersonBase authorKomb
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(kombSplit
[i
])), TeamOrPersonBase
.class);
157 name
.setCombinationAuthorship(authorKomb
);
161 else if(!CdmUtils
.isBlank(authorKombString
)){
162 TeamOrPersonBase authorKomb
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(authorKombString
)), TeamOrPersonBase
.class);
163 name
.setCombinationAuthorship(authorKomb
);
166 if(authorBasiString
.contains(EX
)){
167 String
[] basiSplit
= authorBasiString
.split(EX
);
168 for (int i
= 0; i
< basiSplit
.length
; i
++) {
169 if(basiSplit
.length
!=2){
170 logger
.error("NAMNR: "+id
+" Multiple ex basionymn authors found");
173 TeamOrPersonBase authorBasi
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(basiSplit
[i
])), TeamOrPersonBase
.class);
174 name
.setExBasionymAuthorship(authorBasi
);
177 TeamOrPersonBase authorBasi
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(basiSplit
[i
])), TeamOrPersonBase
.class);
178 name
.setBasionymAuthorship(authorBasi
);
182 else if(!CdmUtils
.isBlank(authorBasiString
)){
183 //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
184 TeamOrPersonBase authorBasi
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(authorBasiString
)), TeamOrPersonBase
.class);
185 if(CdmUtils
.isBlank(authorKombString
)){
186 name
.setCombinationAuthorship(authorBasi
);
189 name
.setBasionymAuthorship(authorBasi
);
193 //check authorship consistency
194 String authorString
= rs
.getString("AUTOR");
195 String authorshipCache
= name
.getAuthorshipCache();
197 if(!CdmUtils
.isBlank(zusatzString
)){
198 authorString
= authorString
.replace(", "+zusatzString
, "");
200 if(CdmUtils
.isBlank(authorKombString
) && !CdmUtils
.isBlank(authorBasiString
)){
201 authorString
= "("+authorString
+")";
203 if(!authorString
.equals(authorshipCache
)){
204 logger
.warn("NAMNR: "+id
+" Authorship inconsistent! name.authorhshipCache <-> Column AUTOR: "+authorshipCache
+" <-> "+authorString
);
208 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, Namespace
.NAME_NAMESPACE
);
209 state
.getNameMap().put(id
, name
.getUuid());
211 namesToSave
.add(name
);
214 TaxonBase taxonBase
= null;
215 if(gueltString
.equals("1")){
216 taxonBase
= Taxon
.NewInstance(name
, null);
218 else if(gueltString
.equals("x")){
219 taxonBase
= Synonym
.NewInstance(name
, null);
221 else if(gueltString
.equals("b")){
222 taxonBase
= Synonym
.NewInstance(name
, null);
225 logger
.error("NAMNR: "+id
+" Taxon for name "+name
+" could not be created.");
229 taxaToSave
.add(taxonBase
);
232 ImportHelper
.setOriginalSource(taxonBase
, state
.getTransactionalSourceReference(), id
, Namespace
.TAXON_NAMESPACE
);
233 state
.getTaxonMap().put(id
, taxonBase
.getUuid());
236 private Rank
makeRank(RedListGefaesspflanzenImportState state
, String rankStr
) {
239 rank
= state
.getTransformer().getRankByKey(rankStr
);
240 } catch (UndefinedTransformerMethodException e
) {
244 logger
.error(rankStr
+" could not be associated to a known rank.");
252 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
253 RedListGefaesspflanzenImportState state
) {
254 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
255 // Map<Long, AgentBase<?>> authorKombMap = new HashMap<>();
256 // Map<Long, AgentBase<?>> authorBasiMap = new HashMap<>();
259 // for(Entry<Long, UUID> entry:state.getAuthorKombMap().entrySet()){
260 // authorKombMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
262 // for(Entry<Long, UUID> entry:state.getAuthorBasiMap().entrySet()){
263 // authorBasiMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
266 // while (rs.next()){
267 // long id = rs.getLong("NAMNR");
269 // } catch (SQLException e) {
270 // e.printStackTrace();
274 // Set<UUID> uuidSet = new HashSet<>();
275 // for (String authorStr : authorKombSet){
276 // UUID uuid = state.getAuthorUuid(authorStr);
277 // uuidSet.add(uuid);
279 // List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
280 // Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
281 // for (TeamOrPersonBase<?> author : authors){
282 // authorUuidMap.put(author.getUuid(), author);
285 // for (String authorStr : authorKombSet){
286 // UUID uuid = state.getAuthorUuid(authorStr);
287 // TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
288 // authorMap.put(authorStr, author);
290 // result.put(AUTHOR_NAMESPACE, authorMap);
293 // String nameSpace = REFERENCE_NAMESPACE;
294 // Class<?> cdmClass = Reference.class;
295 // Set<String> idSet = referenceIdSet;
296 // Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
297 // result.put(nameSpace, referenceMap);
300 // UUID secUuid = state.getConfig().getSecUuid();
301 // Reference<?> secRef = getReferenceService().find(secUuid);
302 // referenceMap.put(secUuid.toString(), secRef);
308 protected boolean doCheck(RedListGefaesspflanzenImportState state
) {
313 protected boolean isIgnore(RedListGefaesspflanzenImportState state
) {