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
;
43 @SuppressWarnings("serial")
44 public class RedListGefaesspflanzenImportNames
extends DbImportBase
<RedListGefaesspflanzenImportState
, RedListGefaesspflanzenImportConfigurator
> {
48 private static final String EX
= " ex ";
50 private static final Logger logger
= Logger
.getLogger(RedListGefaesspflanzenImportNames
.class);
52 private static final String tableName
= "Rote Liste Gefäßpflanzen";
54 private static final String pluralString
= "names";
56 private static final String NAME_NAMESPACE
= "name";
58 public RedListGefaesspflanzenImportNames() {
59 super(tableName
, pluralString
);
63 protected String
getIdQuery(RedListGefaesspflanzenImportState state
) {
64 return "SELECT NAMNR "
65 + "FROM V_TAXATLAS_D20_EXPORT t "
70 protected String
getRecordQuery(RedListGefaesspflanzenImportConfigurator config
) {
71 String result
= " SELECT * "
72 + " FROM V_TAXATLAS_D20_EXPORT t "
73 + " WHERE t.NAMNR IN (@IDSET)";
74 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
79 protected void doInvoke(RedListGefaesspflanzenImportState state
) {
80 super.doInvoke(state
);
85 public boolean doPartition(ResultSetPartitioner partitioner
, RedListGefaesspflanzenImportState state
) {
86 ResultSet rs
= partitioner
.getResultSet();
87 Set
<TaxonNameBase
> namesToSave
= new HashSet
<TaxonNameBase
>();
90 makeSingleName(state
, rs
, namesToSave
);
93 } catch (SQLException e
) {
97 getNameService().saveOrUpdate(namesToSave
);
101 private void makeSingleName(RedListGefaesspflanzenImportState state
, ResultSet rs
, Set
<TaxonNameBase
> namesToSave
)
102 throws SQLException
{
103 long id
= rs
.getLong("NAMNR");
104 String taxNameString
= rs
.getString("TAXNAME");
105 String rangString
= rs
.getString("RANG");
106 String ep1String
= rs
.getString("EPI1");
107 String ep2String
= rs
.getString("EPI2");
108 String ep3String
= rs
.getString("EPI3");
109 String nomZusatzString
= rs
.getString("NOM_ZUSATZ");
110 String zusatzString
= rs
.getString("ZUSATZ");
111 String authorKombString
= rs
.getString("AUTOR_KOMB");
112 String authorBasiString
= rs
.getString("AUTOR_BASI");
114 if(CdmUtils
.isBlank(taxNameString
) && CdmUtils
.isBlank(ep1String
)){
115 logger
.error("NAMNR: "+id
+" No name found!");
118 Rank rank
= makeRank(state
, rangString
);
120 logger
.error("NAMNR: "+id
+" Rank could not be resolved.");
123 BotanicalName name
= BotanicalName
.NewInstance(rank
);
125 //ep1 should always be present
126 if(CdmUtils
.isBlank(ep1String
)){
127 logger
.error("NAMNR: "+id
+" EPI1 is empty!");
129 name
.setGenusOrUninomial(ep1String
);
130 // if(CdmUtils.isBlank(ep2String)){
131 // name.setSpecificEpithet(ep2String);
133 // if(CdmUtils.isBlank(ep3String)){
135 if(rank
.isSpecies()){
136 if(!CdmUtils
.isBlank(ep2String
)){
137 name
.setSpecificEpithet(ep2String
);
140 else if(rank
.isLower(Rank
.SPECIES())){
141 if(!CdmUtils
.isBlank(ep3String
)){
142 name
.setSpecificEpithet(ep3String
);
148 if(authorKombString
.contains(EX
)){
149 //TODO: what happens with multiple ex authors??
150 String
[] kombSplit
= authorKombString
.split(EX
);
151 for (int i
= 0; i
< kombSplit
.length
; i
++) {
153 //first author is ex author
154 TeamOrPersonBase authorKomb
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(kombSplit
[i
])), TeamOrPersonBase
.class);
155 name
.setExCombinationAuthorship(authorKomb
);
158 TeamOrPersonBase authorKomb
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(kombSplit
[i
])), TeamOrPersonBase
.class);
159 name
.setCombinationAuthorship(authorKomb
);
163 else if(!CdmUtils
.isBlank(authorKombString
)){
164 TeamOrPersonBase authorKomb
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(authorKombString
)), TeamOrPersonBase
.class);
165 name
.setCombinationAuthorship(authorKomb
);
168 if(authorBasiString
.contains(EX
)){
169 String
[] basiSplit
= authorBasiString
.split(EX
);
170 for (int i
= 0; i
< basiSplit
.length
; i
++) {
172 TeamOrPersonBase authorBasi
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(basiSplit
[i
])), TeamOrPersonBase
.class);
173 name
.setExBasionymAuthorship(authorBasi
);
176 TeamOrPersonBase authorBasi
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(basiSplit
[i
])), TeamOrPersonBase
.class);
177 name
.setBasionymAuthorship(authorBasi
);
181 else if(!CdmUtils
.isBlank(authorBasiString
)){
182 TeamOrPersonBase authorBasi
= HibernateProxyHelper
.deproxy(getAgentService().load(state
.getAuthorMap().get(authorBasiString
)), TeamOrPersonBase
.class);
183 name
.setBasionymAuthorship(authorBasi
);
186 //check authorship consistency
187 String authorString
= rs
.getString("AUTOR");
188 String authorshipCache
= name
.getAuthorshipCache();
190 if(!CdmUtils
.isBlank(zusatzString
)){
191 authorString
= authorString
.replace(", "+zusatzString
, "");
193 if(CdmUtils
.isBlank(authorKombString
) && !CdmUtils
.isBlank(authorBasiString
)){
194 authorString
= "("+authorString
+")";
196 if(!authorString
.equals(authorshipCache
)){
197 logger
.warn("NAMNR: "+id
+" Authorship inconsistent! name.authorhshipCache <-> Column AUTOR: "+authorshipCache
+" <-> "+authorString
);
201 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, NAME_NAMESPACE
);
203 namesToSave
.add(name
);
206 private Rank
makeRank(RedListGefaesspflanzenImportState state
, String rankStr
) {
209 rank
= state
.getTransformer().getRankByKey(rankStr
);
210 } catch (UndefinedTransformerMethodException e
) {
214 logger
.error(rankStr
+" could not be associated to a known rank.");
222 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
223 RedListGefaesspflanzenImportState state
) {
224 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
225 // Map<Long, AgentBase<?>> authorKombMap = new HashMap<>();
226 // Map<Long, AgentBase<?>> authorBasiMap = new HashMap<>();
229 // for(Entry<Long, UUID> entry:state.getAuthorKombMap().entrySet()){
230 // authorKombMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
232 // for(Entry<Long, UUID> entry:state.getAuthorBasiMap().entrySet()){
233 // authorBasiMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
236 // while (rs.next()){
237 // long id = rs.getLong("NAMNR");
239 // } catch (SQLException e) {
240 // e.printStackTrace();
244 // Set<UUID> uuidSet = new HashSet<>();
245 // for (String authorStr : authorKombSet){
246 // UUID uuid = state.getAuthorUuid(authorStr);
247 // uuidSet.add(uuid);
249 // List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
250 // Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
251 // for (TeamOrPersonBase<?> author : authors){
252 // authorUuidMap.put(author.getUuid(), author);
255 // for (String authorStr : authorKombSet){
256 // UUID uuid = state.getAuthorUuid(authorStr);
257 // TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
258 // authorMap.put(authorStr, author);
260 // result.put(AUTHOR_NAMESPACE, authorMap);
263 // String nameSpace = REFERENCE_NAMESPACE;
264 // Class<?> cdmClass = Reference.class;
265 // Set<String> idSet = referenceIdSet;
266 // Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
267 // result.put(nameSpace, referenceMap);
270 // UUID secUuid = state.getConfig().getSecUuid();
271 // Reference<?> secRef = getReferenceService().find(secUuid);
272 // referenceMap.put(secUuid.toString(), secRef);
278 protected boolean doCheck(RedListGefaesspflanzenImportState state
) {
283 protected boolean isIgnore(RedListGefaesspflanzenImportState state
) {