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
.iapt
;
12 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
13 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
14 import eu
.etaxonomy
.cdm
.io
.mexico
.SimpleExcelTaxonImport
;
15 import eu
.etaxonomy
.cdm
.io
.mexico
.SimpleExcelTaxonImportState
;
16 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
17 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
18 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
19 import eu
.etaxonomy
.cdm
.model
.common
.*;
20 import eu
.etaxonomy
.cdm
.model
.name
.*;
21 import eu
.etaxonomy
.cdm
.model
.occurrence
.*;
22 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
23 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
24 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.*;
26 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
27 import org
.apache
.commons
.lang
.ArrayUtils
;
28 import org
.apache
.commons
.lang
.StringEscapeUtils
;
29 import org
.apache
.commons
.lang
.StringUtils
;
30 import org
.apache
.log4j
.Level
;
31 import org
.apache
.log4j
.Logger
;
32 import org
.joda
.time
.DateTimeFieldType
;
33 import org
.joda
.time
.Partial
;
34 import org
.joda
.time
.format
.DateTimeFormat
;
35 import org
.joda
.time
.format
.DateTimeFormatter
;
36 import org
.springframework
.stereotype
.Component
;
39 import java
.util
.regex
.Matcher
;
40 import java
.util
.regex
.Pattern
;
47 @Component("iAPTExcelImport")
48 public class IAPTExcelImport
<CONFIG
extends IAPTImportConfigurator
> extends SimpleExcelTaxonImport
<CONFIG
> {
49 private static final long serialVersionUID
= -747486709409732371L;
50 private static final Logger logger
= Logger
.getLogger(IAPTExcelImport
.class);
51 public static final String ANNOTATION_MARKER_STRING
= "[*]";
54 private static UUID ROOT_UUID
= UUID
.fromString("4137fd2a-20f6-4e70-80b9-f296daf51d82");
56 private static NonViralNameParserImpl nameParser
= NonViralNameParserImpl
.NewInstance();
58 private final static String REGISTRATIONNO_PK
= "RegistrationNo_Pk";
59 private final static String HIGHERTAXON
= "HigherTaxon";
60 private final static String FULLNAME
= "FullName";
61 private final static String AUTHORSSPELLING
= "AuthorsSpelling";
62 private final static String LITSTRING
= "LitString";
63 private final static String REGISTRATION
= "Registration";
64 private final static String TYPE
= "Type";
65 private final static String CAVEATS
= "Caveats";
66 private final static String FULLBASIONYM
= "FullBasionym";
67 private final static String FULLSYNSUBST
= "FullSynSubst";
68 private final static String NOTESTXT
= "NotesTxt";
69 private final static String REGDATE
= "RegDate";
70 private final static String NAMESTRING
= "NameString";
71 private final static String BASIONYMSTRING
= "BasionymString";
72 private final static String SYNSUBSTSTR
= "SynSubstStr";
73 private final static String AUTHORSTRING
= "AuthorString";
75 private static List
<String
> expectedKeys
= Arrays
.asList(new String
[]{
76 REGISTRATIONNO_PK
, HIGHERTAXON
, FULLNAME
, AUTHORSSPELLING
, LITSTRING
, REGISTRATION
, TYPE
, CAVEATS
, FULLBASIONYM
, FULLSYNSUBST
, NOTESTXT
, REGDATE
, NAMESTRING
, BASIONYMSTRING
, SYNSUBSTSTR
, AUTHORSTRING
});
78 private static final Pattern nomRefTokenizeP
= Pattern
.compile("^(?<title>.*):\\s(?<detail>[^\\.:]+)\\.(?<date>.*?)(?:\\s\\((?<issue>[^\\)]*)\\)\\s*)\\.?$");
79 private static final Pattern
[] datePatterns
= new Pattern
[]{
81 // The order of the patterns is extremely important!!!
83 // all patterns cover the years 1700 - 1999
84 Pattern
.compile("^(?<year>1[7,8,9][0-9]{2})$"), // only year, like '1969'
85 Pattern
.compile("^(?<monthName>\\p{L}+\\.?)\\s(?<day>[0-9]{1,2})(?:st|rd|th)?\\.?,?\\s(?<year>(?:1[7,8,9])?[0-9]{2})$"), // full date like April 12, 1969 or april 12th 1999
86 Pattern
.compile("^(?<monthName>\\p{L}+\\.?),?\\s?(?<year>(?:1[7,8,9])?[0-9]{2})$"), // April 99 or April, 1999 or Apr. 12
87 Pattern
.compile("^(?<day>[0-9]{1,2})([\\.\\-/])(\\s?)(?<month>[0-1]?[0-9])\\2\\3(?<year>(?:1[7,8,9])?[0-9]{2})$"), // full date like 12.04.1969 or 12. 04. 1969 or 12/04/1969 or 12-04-1969
88 Pattern
.compile("^(?<day>[0-9]{1,2})([\\.\\-/])(?<month>[IVX]{1,2})\\2(?<year>(?:1[7,8,9])?[0-9]{2})$"), // full date like 12-VI-1969
89 Pattern
.compile("^(?:(?<day>[0-9]{1,2})(?:\\sde)\\s)(?<monthName>\\p{L}+)\\sde\\s(?<year>(?:1[7,8,9])?[0-9]{2})$"), // full and partial date like 12 de Enero de 1999 or Enero de 1999
90 Pattern
.compile("^(?<month>[0-1]?[0-9])([\\.\\-/])(?<year>(?:1[7,8,9])?[0-9]{2})$"), // partial date like 04.1969 or 04/1969 or 04-1969
91 Pattern
.compile("^(?<year>(?:1[7,8,9])?[0-9]{2})([\\.\\-/])(?<month>[0-1]?[0-9])$"),// partial date like 1999-04
92 Pattern
.compile("^(?<month>[IVX]{1,2})([\\.\\-/])(?<year>(?:1[7,8,9])?[0-9]{2})$"), // partial date like VI-1969
93 Pattern
.compile("^(?<day>[0-9]{1,2})(?:[\\./]|th|rd|st)?\\s(?<monthName>\\p{L}+\\.?),?\\s?(?<year>(?:1[7,8,9])?[0-9]{2})$"), // full date like 12. April 1969 or april 1999 or 22 Dec.1999
95 private static final Pattern typeSplitPattern
= Pattern
.compile("^(?:\"*[Tt]ype: (?<fieldUnit>.*?))(?:[Hh]olotype:(?<holotype>.*?)\\.?)?(?:[Ii]sotype[^:]*:(?<isotype>.*)\\.?)?\\.?$");
97 private static final Pattern collectorPattern
= Pattern
.compile(".*?(?<fullStr1>\\(leg\\.\\s+(?<data1>[^\\)]*)\\))|.*?(?<fullStr2>\\sleg\\.\\s+(?<data2>.*?)\\.?)$");
98 private static final Pattern collectionDataPattern
= Pattern
.compile("^(?<collector>[^,]*),\\s?(?<detail>.*?)\\.?$");
99 private static final Pattern collectorsNumber
= Pattern
.compile("^([nN]o\\.\\s.*)$");
101 // AccessionNumbers: , #.*, n°:?, 96/3293, No..*, -?\w{1,3}-[0-9\-/]*
102 private static final Pattern accessionNumberOnlyPattern
= Pattern
.compile("^(?<accNumber>(?:n°\\:?\\s?|#|No\\.?\\s?)?[\\d\\w\\-/]*)$");
104 private static final Pattern
[] specimenTypePatterns
= new Pattern
[]{
105 Pattern
.compile("^(?<colCode>[A-Z]+|CPC Micropaleontology Lab\\.?)\\s+(?:\\((?<institute>.*[^\\)])\\))(?<accNumber>.*)?$"), // like: GAUF (Gansu Agricultural University) No. 1207-1222
106 Pattern
.compile("^(?<colCode>[A-Z]+|CPC Micropaleontology Lab\\.?)\\s+(?:Coll\\.\\s(?<subCollection>[^\\.,;]*)(.))(?<accNumber>.*)?$"), // like KASSEL Coll. Krasske, Praep. DII 78
107 Pattern
.compile("^(?:Coll\\.\\s(?<subCollection>[^\\.,;]*)(.))(?<institute>.*?)(?<accNumber>Praep\\..*)?$"), // like Coll. Lange-Bertalot, Bot. Inst., Univ. Frankfurt/Main, Germany Praep. Neukaledonien OTL 62
108 Pattern
.compile("^(?<colCode>[A-Z]+)(?:\\s+(?<accNumber>.*))?$"), // identifies the Collection code and takes the rest as accessionNumber if any
111 private static Map
<String
, Integer
> monthFromNameMap
= new HashMap
<>();
114 String
[] ck
= new String
[]{"leden", "únor", "březen", "duben", "květen", "červen", "červenec ", "srpen", "září", "říjen", "listopad", "prosinec"};
115 String
[] fr
= new String
[]{"janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"};
116 String
[] de
= new String
[]{"januar", "februar", "märz", "april", "mai", "juni", "juli", "august", "september", "oktober", "november", "dezember"};
117 String
[] en
= new String
[]{"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
118 String
[] it
= new String
[]{"gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"};
119 String
[] sp
= new String
[]{"enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"};
120 String
[] de_abbrev
= new String
[]{"jan.", "feb.", "märz", "apr.", "mai", "jun.", "jul.", "aug.", "sept.", "okt.", "nov.", "dez."};
121 String
[] en_abbrev
= new String
[]{"jan.", "feb.", "mar.", "apr.", "may", "jun.", "jul.", "aug.", "sep.", "oct.", "nov.", "dec."};
122 String
[] port
= new String
[]{"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
123 String
[] rom_num
= new String
[]{"i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii"};
125 String
[][] perLang
= new String
[][]{ck
, de
, fr
, en
, it
, sp
, port
, de_abbrev
, en_abbrev
, rom_num
};
127 for (String
[] months
: perLang
) {
128 for(int m
= 1; m
< 13; m
++){
129 monthFromNameMap
.put(months
[m
- 1].toLowerCase(), m
);
134 monthFromNameMap
.put("mar", 3);
135 monthFromNameMap
.put("dec", 12);
136 monthFromNameMap
.put("Februari", 2);
140 DateTimeFormatter formatterYear
= DateTimeFormat
.forPattern("yyyy");
142 private Map
<String
, Collection
> collectionMap
= new HashMap
<>();
146 fieldUnit
, holotype
, isotype
;
148 public SpecimenTypeDesignationStatus
status(){
151 return SpecimenTypeDesignationStatus
.HOLOTYPE();
153 return SpecimenTypeDesignationStatus
.ISOTYPE();
160 private MarkerType markerTypeFossil
= null;
161 private Rank rankUnrankedSupraGeneric
= null;
162 private Rank familyIncertisSedis
= null;
163 private AnnotationType annotationTypeCaveats
= null;
165 private Taxon
makeTaxon(HashMap
<String
, String
> record
, SimpleExcelTaxonImportState
<CONFIG
> state
,
166 TaxonNode higherTaxonNode
, boolean isFossil
) {
168 String regNumber
= getValue(record
, REGISTRATIONNO_PK
, false);
169 String regStr
= getValue(record
, REGISTRATION
, true);
170 String titleCacheStr
= getValue(record
, FULLNAME
, true);
171 String nameStr
= getValue(record
, NAMESTRING
, true);
172 String authorStr
= getValue(record
, AUTHORSTRING
, true);
173 String nomRefStr
= getValue(record
, LITSTRING
, true);
174 String authorsSpelling
= getValue(record
, AUTHORSSPELLING
, true);
175 String notesTxt
= getValue(record
, NOTESTXT
, true);
176 String caveats
= getValue(record
, CAVEATS
, true);
177 String fullSynSubstStr
= getValue(record
, FULLSYNSUBST
, true);
178 String fullBasionymStr
= getValue(record
, FULLBASIONYM
, true);
179 String basionymNameStr
= getValue(record
, FULLBASIONYM
, true);
180 String synSubstStr
= getValue(record
, SYNSUBSTSTR
, true);
181 String typeStr
= getValue(record
, TYPE
, true);
184 String nomRefTitle
= null;
186 String nomRefPupDate
= null;
187 String nomRefIssue
= null;
188 Partial pupDate
= null;
190 // preprocess nomRef: separate citation, reference detail, publishing date
191 if(!StringUtils
.isEmpty(nomRefStr
)){
192 nomRefStr
= nomRefStr
.trim();
193 Matcher m
= nomRefTokenizeP
.matcher(nomRefStr
);
195 nomRefTitle
= m
.group("title");
196 nomRefDetail
= m
.group("detail");
197 nomRefPupDate
= m
.group("date").trim();
198 nomRefIssue
= m
.group("issue");
200 pupDate
= parseDate(regNumber
, nomRefPupDate
);
201 if (pupDate
!= null) {
202 nomRefTitle
= nomRefTitle
+ ": " + nomRefDetail
+ ". " + pupDate
.toString(formatterYear
) + ".";
204 logger
.warn(csvReportLine(regNumber
, "Pub date", nomRefPupDate
, "in", nomRefStr
, "not parsable"));
207 nomRefTitle
= nomRefStr
;
211 BotanicalName taxonName
= makeBotanicalName(state
, regNumber
, titleCacheStr
, nameStr
, authorStr
, nomRefTitle
);
213 // always add the original strings of parsed data as annotation
214 taxonName
.addAnnotation(Annotation
.NewInstance("imported and parsed data strings:" +
215 "\n - '" + LITSTRING
+ "': "+ nomRefStr
+
216 "\n - '" + TYPE
+ "': " + typeStr
+
217 "\n - '" + REGISTRATION
+ "': " + regStr
218 , AnnotationType
.TECHNICAL(), Language
.DEFAULT()));
220 if(pupDate
!= null) {
221 taxonName
.getNomenclaturalReference().setDatePublished(TimePeriod
.NewInstance(pupDate
));
223 if(nomRefIssue
!= null) {
224 taxonName
.getNomenclaturalReference().setType(ReferenceType
.Book
);
225 ((Reference
)taxonName
.getNomenclaturalReference()).setVolume(nomRefIssue
);
228 if(!StringUtils
.isEmpty(notesTxt
)){
229 notesTxt
= notesTxt
.replace("Notes: ", "").trim();
230 taxonName
.addAnnotation(Annotation
.NewInstance(notesTxt
, AnnotationType
.EDITORIAL(), Language
.DEFAULT()));
232 if(!StringUtils
.isEmpty(caveats
)){
233 caveats
= caveats
.replace("Caveats: ", "").trim();
234 taxonName
.addAnnotation(Annotation
.NewInstance(caveats
, annotationTypeCaveats(), Language
.DEFAULT()));
237 getNameService().save(taxonName
);
240 if(!StringUtils
.isEmpty(authorsSpelling
)){
241 authorsSpelling
= authorsSpelling
.replaceFirst("Author's spelling:", "").replaceAll("\"", "").trim();
243 String
[] authorSpellingTokens
= StringUtils
.split(authorsSpelling
, " ");
244 String
[] nameStrTokens
= StringUtils
.split(nameStr
, " ");
246 ArrayUtils
.reverse(authorSpellingTokens
);
247 ArrayUtils
.reverse(nameStrTokens
);
249 for (int i
= 0; i
< nameStrTokens
.length
; i
++){
250 if(i
< authorSpellingTokens
.length
){
251 nameStrTokens
[i
] = authorSpellingTokens
[i
];
254 ArrayUtils
.reverse(nameStrTokens
);
256 String misspelledNameStr
= StringUtils
.join (nameStrTokens
, ' ');
257 // build the fullnameString of the misspelled name
258 misspelledNameStr
= taxonName
.getTitleCache().replace(nameStr
, misspelledNameStr
);
260 TaxonNameBase misspelledName
= (BotanicalName
) nameParser
.parseReferencedName(misspelledNameStr
, NomenclaturalCode
.ICNAFP
, null);
261 misspelledName
.addRelationshipToName(taxonName
, NameRelationshipType
.MISSPELLING(), null);
262 getNameService().save(misspelledName
);
266 if(!StringUtils
.isEmpty(fullSynSubstStr
)){
267 fullSynSubstStr
= fullSynSubstStr
.replace("Syn. subst.: ", "");
268 BotanicalName replacedSynonymName
= makeBotanicalName(state
, regNumber
, fullSynSubstStr
, synSubstStr
, null, null);
269 replacedSynonymName
.addReplacedSynonym(taxonName
, null, null, null);
270 getNameService().save(replacedSynonymName
);
273 Reference sec
= state
.getConfig().getSecReference();
274 Taxon taxon
= Taxon
.NewInstance(taxonName
, sec
);
277 if(fullBasionymStr
!= null){
278 fullBasionymStr
= fullBasionymStr
.replaceAll("^\\w*:\\s", ""); // Strip off the leading 'Basionym: "
279 BotanicalName basionym
= makeBotanicalName(state
, regNumber
, fullBasionymStr
, basionymNameStr
, null, null);
280 getNameService().save(basionym
);
281 taxonName
.addBasionym(basionym
);
283 Synonym syn
= Synonym
.NewInstance(basionym
, sec
);
284 taxon
.addSynonym(syn
, SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF());
285 getTaxonService().save(syn
);
290 taxon
.addMarker(Marker
.NewInstance(markerTypeFossil(), true));
294 if(!StringUtils
.isEmpty(typeStr
)){
295 makeTypeData(typeStr
, taxonName
, regNumber
, state
);
298 getTaxonService().save(taxon
);
299 if(higherTaxonNode
!= null){
300 higherTaxonNode
.addChildTaxon(taxon
, null, null);
301 getTaxonNodeService().save(higherTaxonNode
);
307 private void makeTypeData(String typeStr
, BotanicalName taxonName
, String regNumber
, SimpleExcelTaxonImportState
<CONFIG
> state
) {
309 Matcher m
= typeSplitPattern
.matcher(typeStr
);
312 String fieldUnitStr
= m
.group(TypesName
.fieldUnit
.name());
313 // boolean isFieldUnit = typeStr.matches(".*([°']|\\d+\\s?m\\s|\\d+\\s?km\\s).*"); // check for location or unit m, km // makes no sense!!!!
314 FieldUnit fieldUnit
= parseFieldUnit(fieldUnitStr
, regNumber
, state
);
315 if(fieldUnit
== null) {
316 // create a field unit with only a titleCache using the fieldUnitStr substring
317 logger
.warn(csvReportLine(regNumber
, "Type: fieldUnitStr can not be parsed", fieldUnitStr
));
318 fieldUnit
= FieldUnit
.NewInstance();
319 fieldUnit
.setTitleCache(fieldUnitStr
, true);
320 getOccurrenceService().save(fieldUnit
);
322 getOccurrenceService().save(fieldUnit
);
325 addSpecimenTypes(taxonName
, fieldUnit
, m
.group(TypesName
.holotype
.name()), TypesName
.holotype
, false, regNumber
);
326 addSpecimenTypes(taxonName
, fieldUnit
, m
.group(TypesName
.isotype
.name()), TypesName
.isotype
, true, regNumber
);
329 // create a field unit with only a titleCache using the full typeStr
330 FieldUnit fieldUnit
= FieldUnit
.NewInstance();
331 fieldUnit
.setTitleCache(typeStr
, true);
332 getOccurrenceService().save(fieldUnit
);
333 logger
.warn(csvReportLine(regNumber
, "Type: field 'Type' can not be parsed", typeStr
));
335 getNameService().save(taxonName
);
339 * Currently only parses the collector, fieldNumber and the collection date.
341 * @param fieldUnitStr
344 * @return null if the fieldUnitStr could not be parsed
346 private FieldUnit
parseFieldUnit(String fieldUnitStr
, String regNumber
, SimpleExcelTaxonImportState
<CONFIG
> state
) {
348 FieldUnit fieldUnit
= null;
350 Matcher m1
= collectorPattern
.matcher(fieldUnitStr
);
353 String collectorData
= m1
.group(2); // like (leg. Metzeltin, 30. 9. 1996)
354 String removal
= m1
.group(1);
355 if(collectorData
== null){
356 collectorData
= m1
.group(4); // like leg. Metzeltin, 30. 9. 1996
357 removal
= m1
.group(3);
359 if(collectorData
== null){
363 // the fieldUnitStr is parsable
364 // remove all collectorData from the fieldUnitStr and use the rest as locality
365 String locality
= fieldUnitStr
.replace(removal
, "");
367 String collectorStr
= null;
368 String detailStr
= null;
370 String fieldNumber
= null;
372 Matcher m2
= collectionDataPattern
.matcher(collectorData
);
374 collectorStr
= m2
.group("collector");
375 detailStr
= m2
.group("detail");
377 // Try to make sense of the detailStr
378 if(detailStr
!= null){
379 detailStr
= detailStr
.trim();
380 // 1. try to parse as date
381 date
= parseDate(regNumber
, detailStr
);
383 // 2. try to parse as number
384 if(collectorsNumber
.matcher(detailStr
).matches()){
385 fieldNumber
= detailStr
;
389 if(date
== null && fieldNumber
== null){
390 // detailed parsing not possible, so need fo fallback
391 collectorStr
= collectorData
;
395 if(collectorStr
== null) {
396 collectorStr
= collectorData
;
399 fieldUnit
= FieldUnit
.NewInstance();
400 GatheringEvent ge
= GatheringEvent
.NewInstance();
401 ge
.setLocality(LanguageString
.NewInstance(locality
, Language
.UNKNOWN_LANGUAGE()));
403 TeamOrPersonBase agent
= state
.getAgentBase(collectorStr
);
405 agent
= Person
.NewTitledInstance(collectorStr
);
406 getAgentService().save(agent
);
407 state
.putAgentBase(collectorStr
, agent
);
409 ge
.setCollector(agent
);
412 ge
.setGatheringDate(date
);
415 getEventBaseService().save(ge
);
416 fieldUnit
.setGatheringEvent(ge
);
418 if(fieldNumber
!= null) {
419 fieldUnit
.setFieldNumber(fieldNumber
);
421 getOccurrenceService().save(fieldUnit
);
428 private Partial
parseDate(String regNumber
, String dateStr
) {
430 Partial pupDate
= null;
431 boolean parseError
= false;
435 String monthName
= null;
438 for(Pattern p
: datePatterns
){
439 Matcher m2
= p
.matcher(dateStr
);
442 year
= m2
.group("year");
443 } catch (IllegalArgumentException e
){
444 // named capture group not found
447 month
= m2
.group("month");
448 } catch (IllegalArgumentException e
){
449 // named capture group not found
453 monthName
= m2
.group("monthName");
454 month
= monthFromName(monthName
, regNumber
);
458 } catch (IllegalArgumentException e
){
459 // named capture group not found
462 day
= m2
.group("day");
463 } catch (IllegalArgumentException e
){
464 // named capture group not found
468 if (year
.length() == 2) {
469 // it is an abbreviated year from the 19** years
481 List
<DateTimeFieldType
> types
= new ArrayList
<>();
482 List
<Integer
> values
= new ArrayList
<>();
484 types
.add(DateTimeFieldType
.year());
485 values
.add(Integer
.parseInt(year
));
487 types
.add(DateTimeFieldType
.monthOfYear());
488 values
.add(Integer
.parseInt(month
));
491 types
.add(DateTimeFieldType
.dayOfMonth());
492 values
.add(Integer
.parseInt(day
));
494 pupDate
= new Partial(types
.toArray(new DateTimeFieldType
[types
.size()]), ArrayUtils
.toPrimitive(values
.toArray(new Integer
[values
.size()])));
499 private String
monthFromName(String monthName
, String regNumber
) {
501 Integer month
= monthFromNameMap
.get(monthName
.toLowerCase());
503 logger
.warn(csvReportLine(regNumber
, "Unknown month name", monthName
));
506 return month
.toString();
511 private void addSpecimenTypes(BotanicalName taxonName
, FieldUnit fieldUnit
, String typeStr
, TypesName typeName
, boolean multiple
, String regNumber
){
513 if(StringUtils
.isEmpty(typeStr
)){
516 typeStr
= typeStr
.trim().replaceAll("\\.$", "");
518 Collection collection
= null;
519 DerivedUnit specimen
= null;
521 List
<DerivedUnit
> specimens
= new ArrayList
<>();
523 String
[] tokens
= typeStr
.split("\\s?,\\s?");
524 for (String t
: tokens
) {
525 // command to list all complex parsabel types:
526 // csvcut -t -c RegistrationNo_Pk,Type iapt.csv | csvgrep -c Type -m "Holotype" | egrep -o 'Holotype:\s([A-Z]*\s)[^.]*?'
527 // csvcut -t -c RegistrationNo_Pk,Type iapt.csv | csvgrep -c Type -m "Holotype" | egrep -o 'Isotype[^:]*:\s([A-Z]*\s)[^.]*?'
530 // trying to parse the string
531 specimen
= parseSpecimenType(fieldUnit
, typeName
, collection
, t
, regNumber
);
532 if(specimen
!= null){
533 specimens
.add(specimen
);
535 // parsing was not successful make simple specimen
536 specimens
.add(makeSpecimenType(fieldUnit
, t
));
541 specimen
= parseSpecimenType(fieldUnit
, typeName
, collection
, typeStr
, regNumber
);
542 if(specimen
!= null) {
543 specimens
.add(specimen
);
544 // remember current collection
545 collection
= specimen
.getCollection();
547 // parsing was not successful make simple specimen
548 specimens
.add(makeSpecimenType(fieldUnit
, typeStr
));
552 for(DerivedUnit s
: specimens
){
553 taxonName
.addSpecimenTypeDesignation(s
, typeName
.status(), null, null, null, false, true);
557 private DerivedUnit
makeSpecimenType(FieldUnit fieldUnit
, String titleCache
) {
558 DerivedUnit specimen
;DerivedUnitFacade facade
= DerivedUnitFacade
.NewInstance(SpecimenOrObservationType
.PreservedSpecimen
, fieldUnit
);
559 facade
.setTitleCache(titleCache
.trim(), true);
560 specimen
= facade
.innerDerivedUnit();
573 private DerivedUnit
parseSpecimenType(FieldUnit fieldUnit
, TypesName typeName
, Collection collection
, String text
, String regNumber
) {
575 DerivedUnit specimen
= null;
577 String collectionCode
= null;
578 String subCollectionStr
= null;
579 String instituteStr
= null;
580 String accessionNumber
= null;
582 boolean unusualAccessionNumber
= false;
586 // 1. For Isotypes often the accession number is noted alone if the
587 // preceeding entry has a collection code.
588 if(typeName
.equals(TypesName
.isotype
) && collection
!= null){
589 Matcher m
= accessionNumberOnlyPattern
.matcher(text
);
592 accessionNumber
= m
.group("accNumber");
593 specimen
= makeSpecimenType(fieldUnit
, collection
, accessionNumber
);
594 } catch (IllegalArgumentException e
){
595 // match group acc_number not found
600 //2. try it the 'normal' way
601 if(specimen
== null) {
602 for (Pattern p
: specimenTypePatterns
) {
603 Matcher m
= p
.matcher(text
);
605 // collection code is mandatory
607 collectionCode
= m
.group("colCode");
608 } catch (IllegalArgumentException e
){
609 // match group colCode not found
612 subCollectionStr
= m
.group("subCollection");
613 } catch (IllegalArgumentException e
){
614 // match group subCollection not found
617 instituteStr
= m
.group("institute");
618 } catch (IllegalArgumentException e
){
619 // match group col_name not found
622 accessionNumber
= m
.group("accNumber");
624 // try to improve the accessionNumber
625 if(accessionNumber
!= null) {
626 accessionNumber
= accessionNumber
.trim();
627 Matcher m2
= accessionNumberOnlyPattern
.matcher(accessionNumber
);
628 String betterAccessionNumber
= null;
631 betterAccessionNumber
= m
.group("accNumber");
632 } catch (IllegalArgumentException e
) {
633 // match group acc_number not found
636 if (betterAccessionNumber
!= null) {
637 accessionNumber
= betterAccessionNumber
;
639 unusualAccessionNumber
= true;
643 } catch (IllegalArgumentException e
){
644 // match group acc_number not found
647 if(collectionCode
== null && instituteStr
== null){
648 logger
.warn(csvReportLine(regNumber
, "Type: neither 'collectionCode' nor 'institute' found in ", text
));
651 collection
= getCollection(collectionCode
, instituteStr
, subCollectionStr
);
652 specimen
= makeSpecimenType(fieldUnit
, collection
, accessionNumber
);
657 if(specimen
== null) {
658 logger
.warn(csvReportLine(regNumber
, "Type: Could not parse specimen", typeName
.name().toString(), text
));
660 if(unusualAccessionNumber
){
661 logger
.warn(csvReportLine(regNumber
, "Type: Unusual accession number", typeName
.name().toString(), text
, accessionNumber
));
666 private DerivedUnit
makeSpecimenType(FieldUnit fieldUnit
, Collection collection
, String accessionNumber
) {
668 DerivedUnitFacade facade
= DerivedUnitFacade
.NewInstance(SpecimenOrObservationType
.PreservedSpecimen
, fieldUnit
);
669 facade
.setCollection(collection
);
670 if(accessionNumber
!= null){
671 facade
.setAccessionNumber(accessionNumber
);
673 return facade
.innerDerivedUnit();
676 private BotanicalName
makeBotanicalName(SimpleExcelTaxonImportState
<CONFIG
> state
, String regNumber
, String titleCacheStr
, String nameStr
,
677 String authorStr
, String nomRefTitle
) {
679 BotanicalName taxonName
;// cache field for the taxonName.titleCache
680 String taxonNameTitleCache
= null;
681 Map
<String
, AnnotationType
> nameAnnotations
= new HashMap
<>();
683 // TitleCache preprocessing
684 if(titleCacheStr
.endsWith(ANNOTATION_MARKER_STRING
) || (authorStr
!= null && authorStr
.endsWith(ANNOTATION_MARKER_STRING
))){
685 nameAnnotations
.put("Author abbreviation not checked.", AnnotationType
.EDITORIAL());
686 titleCacheStr
= titleCacheStr
.replace(ANNOTATION_MARKER_STRING
, "").trim();
687 if(authorStr
!= null) {
688 authorStr
= authorStr
.replace(ANNOTATION_MARKER_STRING
, "").trim();
692 // parse the full taxon name
693 if(!StringUtils
.isEmpty(nomRefTitle
)){
694 String referenceSeparator
= nomRefTitle
.startsWith("in ") ?
" " : ", ";
695 String taxonFullNameStr
= titleCacheStr
+ referenceSeparator
+ nomRefTitle
;
696 logger
.debug(":::::" + taxonFullNameStr
);
697 taxonName
= (BotanicalName
) nameParser
.parseReferencedName(taxonFullNameStr
, NomenclaturalCode
.ICNAFP
, null);
699 taxonName
= (BotanicalName
) nameParser
.parseFullName(titleCacheStr
, NomenclaturalCode
.ICNAFP
, null);
702 taxonNameTitleCache
= taxonName
.getTitleCache().trim();
703 if (taxonName
.isProtectedTitleCache()) {
704 logger
.warn(csvReportLine(regNumber
, "Name could not be parsed", titleCacheStr
));
707 boolean doRestoreTitleCacheStr
= false;
709 // Check if titleCache and nameCache are plausible
710 String titleCacheCompareStr
= titleCacheStr
;
711 String nameCache
= taxonName
.getNameCache();
712 String nameCompareStr
= nameStr
;
713 if(taxonName
.isBinomHybrid()){
714 titleCacheCompareStr
= titleCacheCompareStr
.replace(" x ", " ×");
715 nameCompareStr
= nameCompareStr
.replace(" x ", " ×");
717 if(taxonName
.isMonomHybrid()){
718 titleCacheCompareStr
= titleCacheCompareStr
.replaceAll("^X ", "× ");
719 nameCompareStr
= nameCompareStr
.replace("^X ", "× ");
721 if(authorStr
!= null && authorStr
.contains(" et ")){
722 titleCacheCompareStr
= titleCacheCompareStr
.replaceAll(" et ", " & ");
724 if (!taxonNameTitleCache
.equals(titleCacheCompareStr
)) {
725 logger
.warn(csvReportLine(regNumber
, "The generated titleCache differs from the imported string", taxonNameTitleCache
, " != ", titleCacheStr
, " ==> original titleCacheStr has been restored"));
726 doRestoreTitleCacheStr
= true;
728 if (!nameCache
.trim().equals(nameCompareStr
)) {
729 logger
.warn(csvReportLine(regNumber
, "The parsed nameCache differs from field '" + NAMESTRING
+ "'", nameCache
, " != ", nameCompareStr
));
733 //nameParser.handleAuthors(taxonName, titleCacheStr, authorStr);
734 //if (!titleCacheStr.equals(taxonName.getTitleCache())) {
735 // logger.warn(regNumber + ": titleCache has changed after setting authors, will restore original titleCacheStr");
736 // doRestoreTitleCacheStr = true;
739 if(doRestoreTitleCacheStr
){
740 taxonName
.setTitleCache(titleCacheStr
, true);
744 replaceAuthorNamesAndNomRef(state
, taxonName
);
748 if(!nameAnnotations
.isEmpty()){
749 for(String text
: nameAnnotations
.keySet()){
750 taxonName
.addAnnotation(Annotation
.NewInstance(text
, nameAnnotations
.get(text
), Language
.DEFAULT()));
752 getNameService().save(taxonName
);
761 private TaxonNode
getClassificationRootNode(IAPTImportState state
) {
763 // Classification classification = state.getClassification();
764 // if (classification == null){
765 // IAPTImportConfigurator config = state.getConfig();
766 // classification = Classification.NewInstance(state.getConfig().getClassificationName());
767 // classification.setUuid(config.getClassificationUuid());
768 // classification.setReference(config.getSecReference());
769 // classification = getClassificationService().find(state.getConfig().getClassificationUuid());
771 TaxonNode rootNode
= state
.getRootNode();
772 if (rootNode
== null){
773 rootNode
= getTaxonNodeService().find(ROOT_UUID
);
775 if (rootNode
== null){
776 Classification classification
= state
.getClassification();
777 if (classification
== null){
778 Reference sec
= state
.getSecReference();
779 String classificationName
= state
.getConfig().getClassificationName();
780 Language language
= Language
.DEFAULT();
781 classification
= Classification
.NewInstance(classificationName
, sec
, language
);
782 state
.setClassification(classification
);
783 classification
.setUuid(state
.getConfig().getClassificationUuid());
784 classification
.getRootNode().setUuid(ROOT_UUID
);
785 getClassificationService().save(classification
);
787 rootNode
= classification
.getRootNode();
788 state
.setRootNode(rootNode
);
793 private Collection
getCollection(String collectionCode
, String instituteStr
, String subCollectionStr
){
795 Collection superCollection
= null;
796 if(subCollectionStr
!= null){
797 superCollection
= getCollection(collectionCode
, instituteStr
, null);
798 collectionCode
= subCollectionStr
;
802 final String key
= collectionCode
+ "-#i:" + StringUtils
.defaultString(instituteStr
);
804 Collection collection
= collectionMap
.get(key
);
806 if(collection
== null) {
807 collection
= Collection
.NewInstance();
808 collection
.setCode(collectionCode
);
809 if(instituteStr
!= null){
810 collection
.setInstitute(Institution
.NewNamedInstance(instituteStr
));
812 if(superCollection
!= null){
813 collection
.setSuperCollection(superCollection
);
815 collectionMap
.put(key
, collection
);
816 getCollectionService().save(collection
);
826 * @param doUnescapeHtmlEntities
829 private String
getValue(HashMap
<String
, String
> record
, String originalKey
, boolean doUnescapeHtmlEntities
) {
830 String value
= record
.get(originalKey
);
832 value
= fixCharacters(value
);
834 if (! StringUtils
.isBlank(value
)) {
835 if (logger
.isDebugEnabled()) {
836 logger
.debug(originalKey
+ ": " + value
);
838 value
= CdmUtils
.removeDuplicateWhitespace(value
.trim()).toString();
839 if(doUnescapeHtmlEntities
){
840 value
= StringEscapeUtils
.unescapeHtml(value
);
849 * Fixes broken characters.
851 * http://dev.e-taxonomy.eu/redmine/issues/6035
856 private String
fixCharacters(String value
) {
858 value
= StringUtils
.replace(value
, "s$K", "š");
859 value
= StringUtils
.replace(value
, "n$K", "ň");
860 value
= StringUtils
.replace(value
, "e$K", "ě");
861 value
= StringUtils
.replace(value
, "r$K", "ř");
862 value
= StringUtils
.replace(value
, "c$K", "č");
863 value
= StringUtils
.replace(value
, "z$K", "ž");
864 value
= StringUtils
.replace(value
, "S>U$K", "Š");
865 value
= StringUtils
.replace(value
, "C>U$K", "Č");
866 value
= StringUtils
.replace(value
, "R>U$K", "Ř");
867 value
= StringUtils
.replace(value
, "Z>U$K", "Ž");
868 value
= StringUtils
.replace(value
, "g$K", "ǧ");
869 value
= StringUtils
.replace(value
, "s$A", "ś");
870 value
= StringUtils
.replace(value
, "n$A", "ń");
871 value
= StringUtils
.replace(value
, "c$A", "ć");
872 value
= StringUtils
.replace(value
, "e$E", "ę");
873 value
= StringUtils
.replace(value
, "o$H", "õ");
874 value
= StringUtils
.replace(value
, "s$C", "ş");
875 value
= StringUtils
.replace(value
, "t$C", "ț");
876 value
= StringUtils
.replace(value
, "S>U$C", "Ş");
877 value
= StringUtils
.replace(value
, "a$O", "å");
878 value
= StringUtils
.replace(value
, "A>U$O", "Å");
879 value
= StringUtils
.replace(value
, "u$O", "ů");
880 value
= StringUtils
.replace(value
, "g$B", "ğ");
881 value
= StringUtils
.replace(value
, "g$B", "ĕ");
882 value
= StringUtils
.replace(value
, "a$B", "ă");
883 value
= StringUtils
.replace(value
, "l$/", "ł");
884 value
= StringUtils
.replace(value
, ">i", "ı");
885 value
= StringUtils
.replace(value
, "i$U", "ï");
887 value
= StringUtils
.replace(value
, "ý", "ý");
888 value
= StringUtils
.replace(value
, ">L", "Ł"); // corrected rule
889 value
= StringUtils
.replace(value
, "E>U$D", "З");
890 value
= StringUtils
.replace(value
, "S>U$E", "Ş");
891 value
= StringUtils
.replace(value
, "s$E", "ş");
893 value
= StringUtils
.replace(value
, "c$k", "č");
894 value
= StringUtils
.replace(value
, " U$K", " Š");
901 * Stores taxa records in DB
904 protected void firstPass(SimpleExcelTaxonImportState
<CONFIG
> state
) {
906 String lineNumber
= "L#" + state
.getCurrentLine() + ": ";
907 logger
.setLevel(Level
.DEBUG
);
908 HashMap
<String
, String
> record
= state
.getOriginalRecord();
909 logger
.debug(lineNumber
+ record
.toString());
911 Set
<String
> keys
= record
.keySet();
912 for (String key
: keys
) {
913 if (! expectedKeys
.contains(key
)){
914 logger
.warn(lineNumber
+ "Unexpected Key: " + key
);
918 String reg_id
= record
.get(REGISTRATIONNO_PK
);
921 String higherTaxaString
= record
.get(HIGHERTAXON
);
922 boolean isFossil
= false;
923 if(higherTaxaString
.startsWith("FOSSIL ")){
924 higherTaxaString
= higherTaxaString
.replace("FOSSIL ", "");
927 TaxonNode higherTaxon
= getHigherTaxon(higherTaxaString
, (IAPTImportState
)state
);
930 Taxon taxon
= makeTaxon(record
, state
, higherTaxon
, isFossil
);
932 logger
.warn(lineNumber
+ "taxon could not be created and is null");
935 ((IAPTImportState
)state
).setCurrentTaxon(taxon
);
941 private TaxonNode
getHigherTaxon(String higherTaxaString
, IAPTImportState state
) {
942 String
[] higherTaxaNames
= higherTaxaString
.toLowerCase().replaceAll("[\\[\\]]", "").split(":");
943 TaxonNode higherTaxonNode
= null;
945 ITaxonTreeNode rootNode
= getClassificationRootNode(state
);
946 for (String htn
: higherTaxaNames
) {
947 htn
= StringUtils
.capitalize(htn
.trim());
948 Taxon higherTaxon
= state
.getHigherTaxon(htn
);
949 if (higherTaxon
!= null){
950 higherTaxonNode
= higherTaxon
.getTaxonNodes().iterator().next();
952 BotanicalName name
= makeHigherTaxonName(state
, htn
);
953 Reference sec
= state
.getSecReference();
954 higherTaxon
= Taxon
.NewInstance(name
, sec
);
955 getTaxonService().save(higherTaxon
);
956 higherTaxonNode
= rootNode
.addChildTaxon(higherTaxon
, sec
, null);
957 state
.putHigherTaxon(htn
, higherTaxon
);
958 getClassificationService().saveTreeNode(higherTaxonNode
);
960 rootNode
= higherTaxonNode
;
962 return higherTaxonNode
;
965 private BotanicalName
makeHigherTaxonName(IAPTImportState state
, String name
) {
967 Rank rank
= guessRank(name
);
969 BotanicalName taxonName
= BotanicalName
.NewInstance(rank
);
970 taxonName
.addSource(makeOriginalSource(state
));
971 taxonName
.setGenusOrUninomial(StringUtils
.capitalize(name
));
975 private Rank
guessRank(String name
) {
978 name
= name
.replaceAll("\\(.*\\)", "").trim();
980 if(name
.matches("^Plantae$|^Fungi$")){
981 return Rank
.KINGDOM();
982 } else if(name
.matches("^Incertae sedis$|^No group assigned$")){
983 return rankFamilyIncertisSedis();
984 } else if(name
.matches(".*phyta$|.*mycota$")){
985 return Rank
.SECTION_BOTANY();
986 } else if(name
.matches(".*phytina$|.*mycotina$")){
987 return Rank
.SUBSECTION_BOTANY();
988 } else if(name
.matches("Gymnospermae$|.*ones$")){ // Monocotyledones, Dicotyledones
989 return rankUnrankedSupraGeneric();
990 } else if(name
.matches(".*opsida$|.*phyceae$|.*mycetes$|.*ones$|^Musci$|^Hepaticae$")){
992 } else if(name
.matches(".*idae$|.*phycidae$|.*mycetidae$")){
993 return Rank
.SUBCLASS();
994 } else if(name
.matches(".*ales$")){
996 } else if(name
.matches(".*ineae$")){
997 return Rank
.SUBORDER();
998 } else if(name
.matches(".*aceae$")){
999 return Rank
.FAMILY();
1000 } else if(name
.matches(".*oideae$")){
1001 return Rank
.SUBFAMILY();
1003 // if(name.matches(".*eae$")){
1004 // return Rank.TRIBE();
1006 if(name
.matches(".*inae$")){
1007 return Rank
.SUBTRIBE();
1008 } else if(name
.matches(".*ae$")){
1009 return Rank
.FAMILY();
1011 return Rank
.UNKNOWN_RANK();
1014 private Rank
rankUnrankedSupraGeneric() {
1016 if(rankUnrankedSupraGeneric
== null){
1017 rankUnrankedSupraGeneric
= Rank
.NewInstance(RankClass
.Suprageneric
, "Unranked supra generic", " ", " ");
1018 getTermService().save(rankUnrankedSupraGeneric
);
1020 return rankUnrankedSupraGeneric
;
1023 private Rank
rankFamilyIncertisSedis() {
1025 if(familyIncertisSedis
== null){
1026 familyIncertisSedis
= Rank
.NewInstance(RankClass
.Suprageneric
, "Family incertis sedis", " ", " ");
1027 getTermService().save(familyIncertisSedis
);
1029 return familyIncertisSedis
;
1032 private AnnotationType
annotationTypeCaveats(){
1033 if(annotationTypeCaveats
== null){
1034 annotationTypeCaveats
= AnnotationType
.NewInstance("Caveats", "Caveats", "");
1035 getTermService().save(annotationTypeCaveats
);
1037 return annotationTypeCaveats
;
1045 private IdentifiableSource
makeOriginalSource(IAPTImportState state
) {
1046 return IdentifiableSource
.NewDataImportInstance("line: " + state
.getCurrentLine(), null, state
.getConfig().getSourceReference());
1050 private Reference
makeReference(IAPTImportState state
, UUID uuidRef
) {
1051 Reference ref
= state
.getReference(uuidRef
);
1053 ref
= getReferenceService().find(uuidRef
);
1054 state
.putReference(uuidRef
, ref
);
1059 private MarkerType
markerTypeFossil(){
1060 if(this.markerTypeFossil
== null){
1061 markerTypeFossil
= MarkerType
.NewInstance("isFossilTaxon", "isFossil", null);
1062 getTermService().save(this.markerTypeFossil
);
1064 return markerTypeFossil
;
1067 private String
csvReportLine(String regId
, String message
, String
... fields
){
1068 StringBuilder out
= new StringBuilder("regID#");
1069 out
.append(regId
).append(",\"").append(message
).append('"');
1071 for(String f
: fields
){
1072 out
.append(",\"").append(f
).append('"');
1074 return out
.toString();