#5448 Import genus hybrid formulas
[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 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.apache.log4j.Logger;
22 import org.springframework.stereotype.Component;
23
24 import eu.etaxonomy.cdm.common.CdmUtils;
25 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26 import eu.etaxonomy.cdm.io.common.DbImportBase;
27 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
28 import eu.etaxonomy.cdm.io.common.ImportHelper;
29 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
30 import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
31 import eu.etaxonomy.cdm.model.agent.AgentBase;
32 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
34 import eu.etaxonomy.cdm.model.common.Annotation;
35 import eu.etaxonomy.cdm.model.common.AnnotationType;
36 import eu.etaxonomy.cdm.model.common.CdmBase;
37 import eu.etaxonomy.cdm.model.common.Language;
38 import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
39 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
40 import eu.etaxonomy.cdm.model.description.TaxonDescription;
41 import eu.etaxonomy.cdm.model.name.BotanicalName;
42 import eu.etaxonomy.cdm.model.name.CultivarPlantName;
43 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
44 import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
45 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
46 import eu.etaxonomy.cdm.model.name.NonViralName;
47 import eu.etaxonomy.cdm.model.name.Rank;
48 import eu.etaxonomy.cdm.model.name.RankClass;
49 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
50 import eu.etaxonomy.cdm.model.taxon.Synonym;
51 import eu.etaxonomy.cdm.model.taxon.Taxon;
52 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
53 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
54 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
55 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
56
57 /**
58 *
59 * @author pplitzner
60 * @date Mar 1, 2016
61 *
62 */
63
64 @Component
65 @SuppressWarnings("serial")
66 public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
67
68 private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
69
70 private static final String tableName = "Rote Liste Gefäßpflanzen";
71
72 private static final String pluralString = "names";
73
74 private static final boolean STRICT_TITLE_CHECK = false;
75
76 public RedListGefaesspflanzenImportNames() {
77 super(tableName, pluralString);
78 }
79
80 @Override
81 protected String getIdQuery(RedListGefaesspflanzenImportState state) {
82 return "SELECT NAMNR "
83 + "FROM V_TAXATLAS_D20_EXPORT t "
84 + " ORDER BY NAMNR";
85 }
86
87 @Override
88 protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
89 String result = " SELECT * "
90 + " FROM V_TAXATLAS_D20_EXPORT t "
91 + " WHERE t.NAMNR IN (@IDSET)";
92 result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
93 return result;
94 }
95
96 @Override
97 protected void doInvoke(RedListGefaesspflanzenImportState state) {
98 super.doInvoke(state);
99 }
100
101
102 @Override
103 public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
104 ResultSet rs = partitioner.getResultSet();
105 Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
106 Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
107 try {
108 while (rs.next()){
109 makeSingleNameAndTaxon(state, rs, namesToSave, taxaToSave);
110
111 }
112 } catch (SQLException e) {
113 e.printStackTrace();
114 }
115
116 getNameService().saveOrUpdate(namesToSave);
117 getTaxonService().saveOrUpdate(taxaToSave);
118 return true;
119 }
120
121 private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave, Set<TaxonBase> taxaToSave)
122 throws SQLException {
123 long id = rs.getLong(RedListUtil.NAMNR);
124 String clTaxonString = rs.getString(RedListUtil.CL_TAXON);
125 String relationE = rs.getString(RedListUtil.E);
126 String relationW = rs.getString(RedListUtil.W);
127 String relationK = rs.getString(RedListUtil.K);
128 String relationAW = rs.getString(RedListUtil.AW);
129 String relationAO = rs.getString(RedListUtil.AO);
130 String relationR = rs.getString(RedListUtil.R);
131 String relationO = rs.getString(RedListUtil.O);
132 String relationS = rs.getString(RedListUtil.S);
133
134 //---NAME---
135 NonViralName<?> name = importName(state, rs, namesToSave);
136
137
138 //--- AUTHORS ---
139 importAuthors(state, rs, name);
140
141 //---TAXON---
142 TaxonBase<?> taxonBase = importTaxon(rs, name, state);
143 if(taxonBase==null){
144 RedListUtil.logMessage(id, "Taxon for name "+name+" could not be created.", logger);
145 return;
146 }
147
148 //---CONCEPT RELATIONSHIPS---
149 //E, W, K, AW, AO, R, O, S
150 cloneTaxon(taxonBase, relationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, taxaToSave, id, state);
151 cloneTaxon(taxonBase, relationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, taxaToSave, id, state);
152 cloneTaxon(taxonBase, relationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, taxaToSave, id, state);
153 cloneTaxon(taxonBase, relationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, taxaToSave, id, state);
154 cloneTaxon(taxonBase, relationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, taxaToSave, id, state);
155 cloneTaxon(taxonBase, relationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, taxaToSave, id, state);
156 cloneTaxon(taxonBase, relationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, taxaToSave, id, state);
157 cloneTaxon(taxonBase, relationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, taxaToSave, id, state);
158 //checklist
159 TaxonBase<?> checklistTaxon = null;
160 if(CdmUtils.isNotBlank(clTaxonString) && !clTaxonString.trim().equals("-")){
161 checklistTaxon = (TaxonBase<?>) taxonBase.clone();
162 if(checklistTaxon.isInstanceOf(Taxon.class)){
163 TaxonRelationship relation = HibernateProxyHelper.deproxy(checklistTaxon, Taxon.class).addTaxonRelation(HibernateProxyHelper.deproxy(taxonBase, Taxon.class), TaxonRelationshipType.CONGRUENT_TO(), null, null);
164 relation.setDoubtful(true);
165 }
166
167 ImportHelper.setOriginalSource(checklistTaxon, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
168 taxaToSave.add(checklistTaxon);
169 }
170
171 //NOTE: the source has to be added after cloning or otherwise the clone would also get the source
172 ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
173 taxaToSave.add(taxonBase);
174 }
175
176 private void cloneTaxon(final TaxonBase<?> gesamtListeTaxon, String relationString, String sourceNameSpace, Set<TaxonBase> taxaToSave, long id, RedListGefaesspflanzenImportState state){
177 if(CdmUtils.isNotBlank(relationString) && !relationString.equals(".")){
178 Taxon clonedTaxon = null;
179
180 if(gesamtListeTaxon.isInstanceOf(Taxon.class)){
181 clonedTaxon = HibernateProxyHelper.deproxy(gesamtListeTaxon.clone(), Taxon.class);
182 }
183 else if(gesamtListeTaxon.isInstanceOf(Synonym.class)){
184 clonedTaxon = Taxon.NewInstance(gesamtListeTaxon.getName(), gesamtListeTaxon.getSec());
185 }
186 else{
187 RedListUtil.logMessage(id, "Taxon base "+gesamtListeTaxon+" is neither taxon nor synonym! Taxon could not be cloned", logger);
188 return;
189 }
190 ImportHelper.setOriginalSource(clonedTaxon, state.getTransactionalSourceReference(), id, sourceNameSpace);
191 taxaToSave.add(clonedTaxon);
192 }
193 }
194
195 private TaxonBase<?> importTaxon(ResultSet rs, NonViralName<?> name, RedListGefaesspflanzenImportState state) throws SQLException {
196
197 long id = rs.getLong(RedListUtil.NAMNR);
198 String taxNameString = rs.getString(RedListUtil.TAXNAME);
199 String epi1String = rs.getString(RedListUtil.EPI1);
200 String gueltString = rs.getString(RedListUtil.GUELT);
201 String trivialString = rs.getString(RedListUtil.TRIVIAL);
202 String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
203 String hybString = rs.getString(RedListUtil.HYB);
204 String florString = rs.getString(RedListUtil.FLOR);
205 String atlasIdxString = rs.getString(RedListUtil.ATLAS_IDX);
206 String kartString = rs.getString(RedListUtil.KART);
207 String rl2015String = rs.getString(RedListUtil.RL2015);
208 String ehrdString = rs.getString(RedListUtil.EHRD);
209 String wisskString = rs.getString(RedListUtil.WISSK);
210
211 TaxonBase<?> taxonBase = null;
212 if(authorBasiString.trim().contains(RedListUtil.AUCT)){
213 taxonBase = Taxon.NewInstance(name, null);
214 taxonBase.setAppendedPhrase(RedListUtil.AUCT);
215 }
216 else if(gueltString.equals(RedListUtil.GUELT_ACCEPTED_TAXON)){
217 taxonBase = Taxon.NewInstance(name, null);
218 }
219 else if(gueltString.equals(RedListUtil.GUELT_SYNONYM) || gueltString.equals(RedListUtil.GUELT_BASIONYM)){
220 taxonBase = Synonym.NewInstance(name, null);
221 }
222 else{
223 return null;
224 }
225
226 //common name
227 if(taxonBase.isInstanceOf(Taxon.class) && trivialString!=null){
228 Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
229 TaxonDescription description = TaxonDescription.NewInstance(taxon);
230 description.addElement(CommonTaxonName.NewInstance(trivialString, Language.GERMAN()));
231 }
232
233 //add annotations
234 addAnnotation(RedListUtil.FLOR+": "+florString, taxonBase);
235 addAnnotation(RedListUtil.ATLAS_IDX+": "+atlasIdxString, taxonBase);
236 addAnnotation(RedListUtil.KART+": "+kartString, taxonBase);
237 addAnnotation(RedListUtil.RL2015+": "+rl2015String, taxonBase);
238 addAnnotation(RedListUtil.EHRD+": "+ehrdString, taxonBase);
239 addAnnotation(RedListUtil.WISSK+": "+wisskString, taxonBase);
240
241 //check taxon name consistency
242 checkTaxonConsistency(id, taxNameString, hybString, epi1String, taxonBase, state);
243 return taxonBase;
244 }
245
246 private void addAnnotation(String string, AnnotatableEntity entity) {
247 if(CdmUtils.isNotBlank(string)){
248 entity.addAnnotation(Annotation.NewInstance(string, AnnotationType.TECHNICAL(), Language.GERMAN()));
249 }
250 }
251
252 private void importAuthors(RedListGefaesspflanzenImportState state, ResultSet rs, NonViralName<?> name) throws SQLException {
253
254 long id = rs.getLong(RedListUtil.NAMNR);
255 String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
256 String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
257 String zusatzString = rs.getString(RedListUtil.ZUSATZ);
258 String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
259 String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
260 String hybString = rs.getString(RedListUtil.HYB);
261
262 //combination author
263 if(authorKombString.contains(RedListUtil.EX)){
264 //TODO: what happens with multiple ex authors??
265 String[] kombSplit = authorKombString.split(RedListUtil.EX);
266 if(kombSplit.length!=2){
267 RedListUtil.logMessage(id, "Multiple ex combination authors found", logger);
268 }
269 for (int i = 0; i < kombSplit.length; i++) {
270 if(i==0){
271 //first author is ex author
272 TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
273 name.setExCombinationAuthorship(authorKomb);
274 }
275 else{
276 TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
277 name.setCombinationAuthorship(authorKomb);
278 }
279 }
280 }
281 else if(authorKombString.trim().contains(RedListUtil.AUCT)){
282 RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
283 }
284 else if(CdmUtils.isNotBlank(authorKombString)){
285 TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorKombString);
286 name.setCombinationAuthorship(authorKomb);
287 }
288 //basionym author
289 if(authorBasiString.contains(RedListUtil.EX)){
290 String[] basiSplit = authorBasiString.split(RedListUtil.EX);
291 for (int i = 0; i < basiSplit.length; i++) {
292 if(basiSplit.length!=2){
293 RedListUtil.logMessage(id, "Multiple ex basionymn authors found", logger);
294 }
295 if(i==0){
296 TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
297 if(CdmUtils.isBlank(authorKombString)){
298 name.setExCombinationAuthorship(authorBasi);
299 }
300 else{
301 name.setExBasionymAuthorship(authorBasi);
302 }
303 }
304 else{
305 TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
306 if(CdmUtils.isBlank(authorKombString)){
307 name.setCombinationAuthorship(authorBasi);
308 }
309 else{
310 name.setBasionymAuthorship(authorBasi);
311 }
312 }
313 }
314 }
315 else if(CdmUtils.isNotBlank(authorBasiString)){
316 //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
317 TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorBasiString);
318 if(CdmUtils.isBlank(authorKombString)){
319 name.setCombinationAuthorship(authorBasi);
320 }
321 else{
322 name.setBasionymAuthorship(authorBasi);
323 }
324 }
325
326 //check authorship consistency
327 String authorString = rs.getString(RedListUtil.AUTOR);
328 checkNameConsistency(id, nomZusatzString, taxZusatzString, zusatzString, authorString, hybString, name);
329 }
330
331 private NonViralName<?> importName(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave) throws SQLException {
332
333 long id = rs.getLong(RedListUtil.NAMNR);
334 String taxNameString = rs.getString(RedListUtil.TAXNAME);
335 String rangString = rs.getString(RedListUtil.RANG);
336 String ep1String = rs.getString(RedListUtil.EPI1);
337 String ep2String = rs.getString(RedListUtil.EPI2);
338 String ep3String = rs.getString(RedListUtil.EPI3);
339 String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
340 String hybString = rs.getString(RedListUtil.HYB);
341 String formelString = rs.getString(RedListUtil.FORMEL);
342
343 if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
344 RedListUtil.logMessage(id, "No name found!", logger);
345 }
346
347 NonViralName<?> name = null;
348 Rank rank = makeRank(id, state, rangString, ep3String!=null);
349 //cultivar
350 if(rank!= null && rank.equals(Rank.CULTIVAR())){
351 CultivarPlantName cultivar = CultivarPlantName.NewInstance(rank);
352 cultivar.setGenusOrUninomial(ep1String);
353 cultivar.setSpecificEpithet(ep2String);
354 cultivar.setCultivarName(ep3String);
355 name = cultivar;
356 }
357 //botanical names
358 else{
359 name = BotanicalName.NewInstance(rank);
360
361 //ep1 should always be present
362 if(CdmUtils.isBlank(ep1String)){
363 RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
364 }
365 name.setGenusOrUninomial(ep1String);
366 if(CdmUtils.isNotBlank(ep2String)){
367 if(rank!=null && rank.isInfraGenericButNotSpeciesGroup()){
368 name.setInfraGenericEpithet(ep2String);
369 }
370 else{
371 name.setSpecificEpithet(ep2String);
372 }
373 }
374 if(CdmUtils.isNotBlank(ep3String)){
375 name.setInfraSpecificEpithet(ep3String);
376 }
377
378
379 //nomenclatural status
380 if(CdmUtils.isNotBlank(nomZusatzString)){
381 NomenclaturalStatusType statusType = makeNomenclaturalStatus(id, state, nomZusatzString);
382 if(statusType!=null){
383 NomenclaturalStatus status = NomenclaturalStatus.NewInstance(statusType);
384 //special case for invalid names where the DB entry contains
385 //additional information in brackets e.g. "nom. inval. (sine basion.)"
386 if(statusType.equals(NomenclaturalStatusType.INVALID()) || statusType.equals(NomenclaturalStatusType.REJECTED()) ){
387 Pattern pattern = Pattern.compile("\\((.*?)\\)");
388 Matcher matcher = pattern.matcher(nomZusatzString);
389 if (matcher.find()){
390 status.setRuleConsidered(matcher.group(1));
391 }
392 }
393 name.addStatus(status);
394 }
395 }
396 //hybrid
397 if(CdmUtils.isNotBlank(hybString)){
398 //save hybrid formula
399 if(CdmUtils.isNotBlank(formelString)){
400 Annotation annotation = Annotation.NewDefaultLanguageInstance(formelString);
401 annotation.setAnnotationType(AnnotationType.TECHNICAL());
402 name.addAnnotation(annotation);
403 }
404 //more than two hybrids not yet handled by name parser
405 //TODO: use parser when implemented to fully support hybrids
406 if(taxNameString.split(RedListUtil.HYB_SIGN).length>2){
407 name = BotanicalName.NewInstance(rank);
408 name.setTitleCache(taxNameString, true);
409 }
410 else if(hybString.equals(RedListUtil.HYB_X)){
411 name.setBinomHybrid(true);
412 }
413 else if(hybString.equals(RedListUtil.HYB_G)){
414 name.setMonomHybrid(true);
415 }
416 else if(hybString.equals(RedListUtil.HYB_XF) || hybString.equals(RedListUtil.HYB_XU)){
417 name.setHybridFormula(true);
418 if(ep1String.contains(RedListUtil.HYB_SIGN)){
419 name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String, NomenclaturalCode.ICNAFP, rank);
420 }
421 else if(ep2String.contains(RedListUtil.HYB_SIGN)){
422 String[] split = ep2String.split(RedListUtil.HYB_SIGN);
423 String hybridFormula1 = ep1String+" "+split[0].trim();
424 String hybridFormula2 = ep1String+" "+split[1].trim();
425 //check if the genus is mentioned in EP2 or not
426 String[] secondHybrid = split[1].trim().split(" ");
427 //check if the genus is abbreviated like e.g. Centaurea jacea × C. decipiens
428 if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z]\\.")){
429 hybridFormula2 = ep1String+" "+split[1].trim().substring(2);
430 }
431 else if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z].*")){
432 hybridFormula2 = split[1];
433 }
434 if(CdmUtils.isNotBlank(ep3String)){
435 hybridFormula1 += " "+ep3String;
436 hybridFormula2 += " "+ep3String;
437 }
438 String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
439 name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula, NomenclaturalCode.ICNAFP, rank);
440 }
441 else if(ep3String.contains(RedListUtil.HYB_SIGN)){
442 String[] split = ep3String.split(RedListUtil.HYB_SIGN);
443 String hybridFormula1 = ep1String+" "+ep2String+" "+split[0];
444 String hybridFormula2 = ep1String+" "+ep2String+" "+split[1];
445 //check if the genus is mentioned in EP3 or not
446 String[] secondHybrid = split[1].trim().split(" ");
447 //check if the genus is abbreviated like e.g. Centaurea jacea jacea × C. jacea subsp. decipiens
448 if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z]\\.")){
449 hybridFormula2 = ep1String+" "+split[1].trim().substring(2);
450 }
451 else if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z].*")){
452 hybridFormula2 = split[1];
453 }
454 String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
455 name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula, NomenclaturalCode.ICNAFP, rank);
456 }
457 }
458 else if(hybString.equals(RedListUtil.HYB_N)){
459 name = NonViralNameParserImpl.NewInstance().parseFullName(taxNameString, NomenclaturalCode.ICNAFP, rank);
460 }
461 else if(hybString.equals(RedListUtil.HYB_GF)){
462 if(ep1String.contains(RedListUtil.HYB_SIGN)){
463 name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String, NomenclaturalCode.ICNAFP, rank);
464 }
465 else{
466 RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
467 }
468 }
469 else if(hybString.equals(RedListUtil.HYB_XS)){
470 //nothing to do
471 }
472 else{
473 logger.error("HYB value "+hybString+" not yet handled");
474 }
475 }
476 }
477
478 //add source
479 ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
480
481 namesToSave.add(name);
482 return name;
483 }
484
485 private void checkNameConsistency(long id, String nomZusatzString, String taxZusatzString,
486 String zusatzString, String authorString, String hybString, NonViralName<?> name) {
487 String authorshipCache = name.getAuthorshipCache();
488 //FIXME: remove split length check when name parser can parse multiple hybrid parents
489 if(hybString.equals(RedListUtil.HYB_XF) && name.getTitleCache().split(RedListUtil.HYB_SIGN).length==2){
490 if(name.getHybridChildRelations().isEmpty()){
491 RedListUtil.logMessage(id, "Hybrid formula but no hybrid child relations: "+name.getTitleCache(), logger);
492 return;
493 }
494 return;
495 }
496
497 if(CdmUtils.isNotBlank(zusatzString)){
498 authorString = authorString.replace(", "+zusatzString, "");
499 }
500 if(CdmUtils.isNotBlank(nomZusatzString)){
501 authorString = authorString.replace(", "+nomZusatzString, "");
502 }
503 if(CdmUtils.isNotBlank(taxZusatzString)){
504 authorString = authorString.replace(", "+taxZusatzString, "");
505 }
506 if(authorString.equals(RedListUtil.AUCT)){
507 authorString = "";
508 }
509 if(STRICT_TITLE_CHECK){
510 if(!authorString.equals(authorshipCache)){
511 RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
512 }
513 }
514 else{
515 if(CdmUtils.isNotBlank(authorString) && !authorString.startsWith(authorshipCache)){
516 RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
517 }
518 }
519 }
520
521 private void checkTaxonConsistency(long id, String taxNameString, String hybString, String epi1String, TaxonBase<?> taxonBase, RedListGefaesspflanzenImportState state) {
522 if(taxNameString.split(RedListUtil.HYB_SIGN).length>2){
523 RedListUtil.logInfoMessage(id, "multiple hybrid signs. No name check for "+taxNameString, logger);
524 return;
525 }
526
527 String nameCache = HibernateProxyHelper.deproxy(taxonBase.getName(), NonViralName.class).getNameCache().trim();
528 taxNameString = taxNameString.trim();
529 taxNameString = taxNameString.replaceAll(" +", " ");
530
531
532 if(hybString.equals(RedListUtil.HYB_X) || hybString.equals(RedListUtil.HYB_N)){
533 taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN+" ", " "+RedListUtil.HYB_SIGN);//hybrid sign has no space after it in titleCache for binomial hybrids
534 taxNameString = taxNameString.replace(" x ", " "+RedListUtil.HYB_SIGN);//in some cases a standard 'x' is used
535 }
536 else if(hybString.equals(RedListUtil.HYB_G)){
537 taxNameString = taxNameString.replace("X ", RedListUtil.HYB_SIGN);
538 }
539 else if(hybString.equals(RedListUtil.HYB_GF)){
540 taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN+" ", " "+RedListUtil.HYB_SIGN);
541 }
542 else if(hybString.equals(RedListUtil.HYB_XF)){
543 nameCache = taxonBase.getName().getTitleCache();
544 if(nameCache.contains("sec")){
545 nameCache = nameCache.substring(0, nameCache.indexOf("sec"));
546 }
547 if(taxNameString.matches((".*[A-Z]\\..*"))){
548 taxNameString = taxNameString.replaceAll("[A-Z]\\.", epi1String);
549 }
550 if(taxNameString.matches((".*"+RedListUtil.HYB_SIGN+"\\s[a-z].*"))){
551 taxNameString = taxNameString.replaceAll(RedListUtil.HYB_SIGN+" ", RedListUtil.HYB_SIGN+" "+epi1String+" ");
552 }
553 }
554
555 if(taxNameString.endsWith("- Gruppe")){
556 taxNameString = taxNameString.replaceAll("- Gruppe", "species group");
557 }
558 if(taxNameString.endsWith("- group")){
559 taxNameString = taxNameString.replaceAll("- group", "species group");
560 }
561
562 taxNameString = taxNameString.replace("agg.", "aggr.");
563 taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
564
565 if(taxonBase.getName().getRank()!=null){
566 if(taxonBase.getName().getRank().equals(Rank.PROLES())){
567 taxNameString = taxNameString.replace("proles", "prol.");
568 }
569 else if(taxonBase.getName().getRank().equals(state.getRank(RedListUtil.uuidRankCollectionSpecies))){
570 taxNameString = taxNameString.replace("\"Sammelart\"", "\"Coll. Species\"");
571 }
572 }
573 if(STRICT_TITLE_CHECK){
574 if(!taxNameString.trim().equals(nameCache)){
575 RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
576 }
577 }
578 else{
579 if(!taxNameString.startsWith(nameCache)){
580 RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
581 }
582 }
583 }
584
585 private Rank makeRank(long id, RedListGefaesspflanzenImportState state, String rankStr, boolean hasSpecificEpithet) {
586 Rank rank = null;
587 try {
588 if(rankStr.equals("ORA")){
589 //special handling for ORA because of two possibilities
590 if(hasSpecificEpithet){
591 return Rank.UNRANKED_INFRASPECIFIC();
592 }
593 else{
594 return Rank.UNRANKED_INFRAGENERIC();
595 }
596 }
597 else if(rankStr.equals("SAM")){
598 return getRank(state, RedListUtil.uuidRankCollectionSpecies, "Collective Species", "Collective Species", "\"Coll. Species\"", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.SpeciesGroup);
599 }
600 else if(rankStr.equals("SPR")){
601 return getRank(state, RedListUtil.uuidRankSubproles, "Subproles", "Subproles", "subproles", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
602 }
603 else if(rankStr.equals("MOD")){
604 return getRank(state, RedListUtil.uuidRankModification, "Modification", "Modification", "modificatio", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
605 }
606 else if(rankStr.equals("LUS")){
607 return getRank(state, RedListUtil.uuidRankLusus, "Lusus", "Lusus", "lusus", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
608 }
609 else if(rankStr.equals("SPI")){
610 return getRank(state, RedListUtil.uuidRankSubspeciesPrincipes, "Subspecies principes", "Subspecies principes", "subsp. princ.", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
611 }
612 else if(rankStr.equals("KMB")){
613 return getRank(state, RedListUtil.uuidRankCombination, "Combination", "Combination", "", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
614 }
615 else{
616 rank = state.getTransformer().getRankByKey(rankStr);
617 }
618 } catch (UndefinedTransformerMethodException e) {
619 e.printStackTrace();
620 }
621 if(rank==null){
622 RedListUtil.logMessage(id, rankStr+" could not be associated to a known rank.", logger);
623 }
624 return rank;
625 }
626
627 private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
628 NomenclaturalStatusType status = null;
629 try {
630 status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
631 } catch (UndefinedTransformerMethodException e) {
632 e.printStackTrace();
633 }
634 if(status==null){
635 RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
636 }
637 return status;
638 }
639
640
641
642 @Override
643 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
644 RedListGefaesspflanzenImportState state) {
645 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
646 Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
647
648 try {
649 while (rs.next()){
650 String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
651
652 if(authorKombString.contains(RedListUtil.EX)){
653 String[] kombSplit = authorKombString.split(RedListUtil.EX);
654 for (int i = 0; i < kombSplit.length; i++) {
655 if(!authorMap.containsKey(kombSplit[i])){
656 authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
657 }
658 }
659 }
660 else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
661 authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
662 }
663
664 String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
665 //basionym author
666 if(authorBasiString.contains(RedListUtil.EX)){
667 String[] basiSplit = authorBasiString.split(RedListUtil.EX);
668 for (int i = 0; i < basiSplit.length; i++) {
669 if(!authorMap.containsKey(basiSplit[i])){
670 authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
671 }
672 }
673 }
674 else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
675 authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
676 }
677 }
678 } catch (SQLException e) {
679 e.printStackTrace();
680 }
681 result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
682
683 return result;
684 }
685
686 @Override
687 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
688 return false;
689 }
690
691 @Override
692 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
693 return false;
694 }
695
696 }