Enhance name check
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / redlist / gefaesspflanzen / RedListGefaesspflanzenImportNames.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.io.redlist.gefaesspflanzen;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.apache.log4j.Logger;
20 import org.springframework.stereotype.Component;
21
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;
37
38 /**
39 *
40 * @author pplitzner
41 * @date Mar 1, 2016
42 *
43 */
44
45 @Component
46 @SuppressWarnings("serial")
47 public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
48
49 private static final String EX = " ex ";
50
51 private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
52
53 private static final String tableName = "Rote Liste Gefäßpflanzen";
54
55 private static final String pluralString = "names";
56
57 public RedListGefaesspflanzenImportNames() {
58 super(tableName, pluralString);
59 }
60
61 @Override
62 protected String getIdQuery(RedListGefaesspflanzenImportState state) {
63 return "SELECT NAMNR "
64 + "FROM V_TAXATLAS_D20_EXPORT t "
65 + " ORDER BY NAMNR";
66 }
67
68 @Override
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);
74 return result;
75 }
76
77 @Override
78 protected void doInvoke(RedListGefaesspflanzenImportState state) {
79 super.doInvoke(state);
80 }
81
82
83 @Override
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>();
88 try {
89 while (rs.next()){
90 makeSingleNameAndTaxon(state, rs, namesToSave, taxaToSave);
91
92 }
93 } catch (SQLException e) {
94 e.printStackTrace();
95 }
96
97 getNameService().saveOrUpdate(namesToSave);
98 getTaxonService().saveOrUpdate(taxaToSave);
99 return true;
100 }
101
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");
115
116 if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
117 logger.error("NAMNR: "+id+" No name found!");
118 }
119
120 Rank rank = makeRank(state, rangString);
121 if(rank==null){
122 logger.error("NAMNR: "+id+" Rank could not be resolved.");
123 }
124 BotanicalName name = BotanicalName.NewInstance(rank);
125
126 //ep1 should always be present
127 if(CdmUtils.isBlank(ep1String)){
128 logger.error("NAMNR: "+id+" EPI1 is empty!");
129 }
130 name.setGenusOrUninomial(ep1String);
131 if(!CdmUtils.isBlank(ep2String)){
132 name.setSpecificEpithet(ep2String);
133 }
134 if(!CdmUtils.isBlank(ep3String)){
135 if(rank==Rank.SUBSPECIES() ||
136 rank==Rank.VARIETY()){
137 name.setInfraSpecificEpithet(ep3String);
138 }
139 }
140
141 //--- AUTHORS ---
142 //combination author
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");
148 }
149 for (int i = 0; i < kombSplit.length; i++) {
150 if(i==0){
151 //first author is ex author
152 TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(Namespace.AUTHOR_NAMESPACE, kombSplit[i]);
153 name.setExCombinationAuthorship(authorKomb);
154 }
155 else{
156 TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(Namespace.AUTHOR_NAMESPACE, kombSplit[i]);
157 name.setCombinationAuthorship(authorKomb);
158 }
159 }
160 }
161 else if(authorKombString.trim().equals("auct.")){
162 logger.warn("NAMNR: "+id+" AUCT information in AUTOR_KOMB column");
163 }
164 else if(!CdmUtils.isBlank(authorKombString)){
165 TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(Namespace.AUTHOR_NAMESPACE, authorKombString);
166 name.setCombinationAuthorship(authorKomb);
167 }
168 //basionym author
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");
174 }
175 if(i==0){
176 TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(Namespace.AUTHOR_NAMESPACE, basiSplit[i]);
177 if(CdmUtils.isBlank(authorKombString)){
178 name.setExCombinationAuthorship(authorBasi);
179 }
180 else{
181 name.setExBasionymAuthorship(authorBasi);
182 }
183 }
184 else{
185 TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(Namespace.AUTHOR_NAMESPACE, basiSplit[i]);
186 if(CdmUtils.isBlank(authorKombString)){
187 name.setCombinationAuthorship(authorBasi);
188 }
189 else{
190 name.setBasionymAuthorship(authorBasi);
191 }
192 }
193 }
194 }
195 else if(authorBasiString.trim().equals("auct.")){
196 name.setAppendedPhrase(authorBasiString);
197 }
198 else if(!CdmUtils.isBlank(authorBasiString)){
199 //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
200 TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(Namespace.AUTHOR_NAMESPACE, authorBasiString);
201 if(CdmUtils.isBlank(authorKombString)){
202 name.setCombinationAuthorship(authorBasi);
203 }
204 else{
205 name.setBasionymAuthorship(authorBasi);
206 }
207 }
208
209 //check authorship consistency
210 String authorString = rs.getString("AUTOR");
211 String authorshipCache = name.getAuthorshipCache();
212
213 if(!CdmUtils.isBlank(zusatzString)){
214 authorString = authorString.replace(", "+zusatzString, "");
215 }
216 // if(CdmUtils.isBlank(authorKombString) && !CdmUtils.isBlank(authorBasiString)){
217 // authorString = "("+authorString+")";
218 // }
219 if(authorString.equals("auct.")){
220 authorString = "";
221 }
222 if(!authorString.equals(authorshipCache)){
223 logger.warn("NAMNR: "+id+" Authorship inconsistent! name.authorhshipCache <-> Column AUTOR: "+authorshipCache+" <-> "+authorString);
224 }
225
226 //id
227 ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, Namespace.NAME_NAMESPACE);
228 state.getNameMap().put(id, name.getUuid());
229
230 namesToSave.add(name);
231
232 //---TAXON---
233 TaxonBase taxonBase = null;
234 if(gueltString.equals("1")){
235 taxonBase = Taxon.NewInstance(name, null);
236 }
237 else if(gueltString.equals("x") || gueltString.equals("b")){
238 taxonBase = Synonym.NewInstance(name, null);
239 }
240 if(taxonBase==null){
241 logger.error("NAMNR: "+id+" Taxon for name "+name+" could not be created.");
242 return;
243 }
244
245 taxaToSave.add(taxonBase);
246
247 //id
248 ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, Namespace.TAXON_NAMESPACE);
249 state.getTaxonMap().put(id, taxonBase.getUuid());
250 }
251
252 private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
253 Rank rank = null;
254 try {
255 rank = state.getTransformer().getRankByKey(rankStr);
256 } catch (UndefinedTransformerMethodException e) {
257 e.printStackTrace();
258 }
259 if(rank==null){
260 logger.error(rankStr+" could not be associated to a known rank.");
261 }
262 return rank;
263 }
264
265
266
267 @Override
268 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
269 RedListGefaesspflanzenImportState state) {
270 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
271 Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
272
273 try {
274 while (rs.next()){
275 String authorKombString = rs.getString("AUTOR_KOMB");
276
277 if(authorKombString.contains(EX)){
278 String[] kombSplit = authorKombString.split(EX);
279 for (int i = 0; i < kombSplit.length; i++) {
280 if(!authorMap.containsKey(kombSplit[i])){
281 authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
282 }
283 }
284 }
285 else if(!CdmUtils.isBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
286 authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
287 }
288
289 String authorBasiString = rs.getString("AUTOR_BASI");
290 //basionym author
291 if(authorBasiString.contains(EX)){
292 String[] basiSplit = authorBasiString.split(EX);
293 for (int i = 0; i < basiSplit.length; i++) {
294 if(!authorMap.containsKey(basiSplit[i])){
295 authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
296 }
297 }
298 }
299 else if(!CdmUtils.isBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
300 authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
301 }
302 }
303 } catch (SQLException e) {
304 e.printStackTrace();
305 }
306 result.put(Namespace.AUTHOR_NAMESPACE, authorMap);
307
308 return result;
309 }
310
311 @Override
312 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
313 return false;
314 }
315
316 @Override
317 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
318 return false;
319 }
320
321 }