Project

General

Profile

Download (59.5 KB) Statistics
| Branch: | Revision:
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.phycobank;
11

    
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.UUID;
20
import java.util.regex.Matcher;
21
import java.util.regex.Pattern;
22

    
23
import org.apache.commons.lang.ArrayUtils;
24
import org.apache.commons.lang.StringEscapeUtils;
25
import org.apache.commons.lang.StringUtils;
26
import org.apache.log4j.Level;
27
import org.apache.log4j.Logger;
28
import org.joda.time.DateTimeFieldType;
29
import org.joda.time.Partial;
30
import org.joda.time.format.DateTimeFormat;
31
import org.joda.time.format.DateTimeFormatter;
32
import org.springframework.stereotype.Component;
33

    
34
import com.fasterxml.jackson.core.JsonProcessingException;
35
import com.fasterxml.jackson.databind.ObjectMapper;
36

    
37
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
38
import eu.etaxonomy.cdm.common.CdmUtils;
39
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImport;
40
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
41
import eu.etaxonomy.cdm.model.agent.Institution;
42
import eu.etaxonomy.cdm.model.agent.Person;
43
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
44
import eu.etaxonomy.cdm.model.common.Annotation;
45
import eu.etaxonomy.cdm.model.common.AnnotationType;
46
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
47
import eu.etaxonomy.cdm.model.common.Extension;
48
import eu.etaxonomy.cdm.model.common.ExtensionType;
49
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
50
import eu.etaxonomy.cdm.model.common.Language;
51
import eu.etaxonomy.cdm.model.common.LanguageString;
52
import eu.etaxonomy.cdm.model.common.Marker;
53
import eu.etaxonomy.cdm.model.common.MarkerType;
54
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
55
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
56
import eu.etaxonomy.cdm.model.name.IBotanicalName;
57
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
58
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
59
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
60
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
61
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
62
import eu.etaxonomy.cdm.model.name.Rank;
63
import eu.etaxonomy.cdm.model.name.RankClass;
64
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
65
import eu.etaxonomy.cdm.model.name.TaxonName;
66
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
67
import eu.etaxonomy.cdm.model.occurrence.Collection;
68
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
69
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
70
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
71
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
72
import eu.etaxonomy.cdm.model.reference.Reference;
73
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
74
import eu.etaxonomy.cdm.model.taxon.Classification;
75
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
76
import eu.etaxonomy.cdm.model.taxon.Synonym;
77
import eu.etaxonomy.cdm.model.taxon.SynonymType;
78
import eu.etaxonomy.cdm.model.taxon.Taxon;
79
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
80
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
81

    
82
/**
83
 * @author a.mueller
84
 * @since 05.01.2016
85
 */
86

    
87
@Component("iAPTExcelImport")
88
public class IAPTExcelImport<CONFIG extends IAPTImportConfigurator> extends SimpleExcelTaxonImport<CONFIG> {
89
    private static final long serialVersionUID = -747486709409732371L;
90
    private static final Logger logger = Logger.getLogger(IAPTExcelImport.class);
91
    public static final String ANNOTATION_MARKER_STRING = "[*]";
92

    
93

    
94
    private static UUID ROOT_UUID = UUID.fromString("4137fd2a-20f6-4e70-80b9-f296daf51d82");
95

    
96
    private static NonViralNameParserImpl nameParser = NonViralNameParserImpl.NewInstance();
97

    
98
    private final static String REGISTRATIONNO_PK= "RegistrationNo_Pk";
99
    private final static String HIGHERTAXON= "HigherTaxon";
100
    private final static String FULLNAME= "FullName";
101
    private final static String AUTHORSSPELLING= "AuthorsSpelling";
102
    private final static String LITSTRING= "LitString";
103
    private final static String REGISTRATION= "Registration";
104
    private final static String TYPE= "Type";
105
    private final static String CAVEATS= "Caveats";
106
    private final static String FULLBASIONYM= "FullBasionym";
107
    private final static String FULLSYNSUBST= "FullSynSubst";
108
    private final static String NOTESTXT= "NotesTxt";
109
    private final static String REGDATE= "RegDate";
110
    private final static String NAMESTRING= "NameString";
111
    private final static String BASIONYMSTRING= "BasionymString";
112
    private final static String SYNSUBSTSTR= "SynSubstStr";
113
    private final static String AUTHORSTRING= "AuthorString";
114

    
115
    private  static List<String> expectedKeys= Arrays.asList(new String[]{
116
            REGISTRATIONNO_PK, HIGHERTAXON, FULLNAME, AUTHORSSPELLING, LITSTRING, REGISTRATION, TYPE, CAVEATS, FULLBASIONYM, FULLSYNSUBST, NOTESTXT, REGDATE, NAMESTRING, BASIONYMSTRING, SYNSUBSTSTR, AUTHORSTRING});
117

    
118
    private static final Pattern nomRefTokenizeP = Pattern.compile("^(?<title>.*):\\s(?<detail>[^\\.:]+)\\.(?<date>.*?)(?:\\s\\((?<issue>[^\\)]*)\\)\\s*)?\\.?$");
119
    private static final Pattern[] datePatterns = new Pattern[]{
120
            // NOTE:
121
            // The order of the patterns is extremely important!!!
122
            //
123
            // all patterns cover the years 1700 - 1999
124
            Pattern.compile("^(?<year>1[7,8,9][0-9]{2})$"), // only year, like '1969'
125
            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
126
            Pattern.compile("^(?<monthName>\\p{L}+\\.?),?\\s?(?<year>(?:1[7,8,9])?[0-9]{2})$"), // April 99 or April, 1999 or Apr. 12
127
            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
128
            Pattern.compile("^(?<day>[0-9]{1,2})([\\.\\-/])(?<monthName>[IVX]{1,2})\\2(?<year>(?:1[7,8,9])?[0-9]{2})$"), // full date like 12-VI-1969
129
            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
130
            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
131
            Pattern.compile("^(?<year>(?:1[7,8,9])?[0-9]{2})([\\.\\-/])(?<month>[0-1]?[0-9])$"),//  partial date like 1999-04
132
            Pattern.compile("^(?<monthName>[IVX]{1,2})([\\.\\-/])(?<year>(?:1[7,8,9])?[0-9]{2})$"), // partial date like VI-1969
133
            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
134
        };
135
    protected static final Pattern typeSpecimenSplitPattern =  Pattern.compile("^(?:\"*[Tt]ype: (?<fieldUnit>.*?))(?:[Hh]olotype:(?<holotype>.*?)\\.?)?(?:[Ii]sotype[^:]*:(?<isotype>.*)\\.?)?\\.?$");
136

    
137
    private static final Pattern typeNameBasionymPattern =  Pattern.compile("\\([Bb]asionym\\s?\\:\\s?(?<basionymName>[^\\)]*).*$");
138
    private static final Pattern typeNameNotePattern =  Pattern.compile("\\[([^\\[]*)"); // matches the inner of '[...]'
139
    private static final Pattern typeNameSpecialSplitPattern =  Pattern.compile("(?<note>.*\\;.*?)\\:(?<agent>)\\;(<name>.*)");
140

    
141
    protected static final Pattern collectorPattern =  Pattern.compile(".*?(?<fullStr1>\\([Ll]eg\\.\\s+(?<data1>[^\\)]*)\\)).*$|.*?(?<fullStr2>\\s[Ll]eg\\.\\:?\\s+(?<data2>.*?)\\.?)$|^(?<fullStr3>[Ll]eg\\.\\:?\\s+(?<data3>.*?)\\.?)");
142
    private static final Pattern collectionDataPattern =  Pattern.compile("^(?<collector>[^,]*),\\s?(?<detail>.*?)\\.?$");
143
    private static final Pattern collectorsNumber =  Pattern.compile("^([nN]o\\.\\s.*)$");
144

    
145
    // AccessionNumbers: , #.*, n°:?, 96/3293, No..*, -?\w{1,3}-[0-9\-/]*
146
    private static final Pattern accessionNumberOnlyPattern = Pattern.compile("^(?<accNumber>(?:n°\\:?\\s?|#|No\\.?\\s?)?[\\d\\w\\-/]*)$");
147

    
148
    private static final Pattern[] specimenTypePatterns = new Pattern[]{
149
            Pattern.compile("^(?<colCode>[A-Z]+|CPC Micropaleontology Lab\\.?)\\s+(?:\\((?<institute>.*[^\\)])\\))(?<accNumber>.*)?$"), // like: GAUF (Gansu Agricultural University) No. 1207-1222
150
            Pattern.compile("^(?<colCode>[A-Z]+|CPC Micropaleontology Lab\\.?)\\s+(?:Coll\\.\\s(?<subCollection>[^\\.,;]*)(.))(?<accNumber>.*)?$"), // like KASSEL Coll. Krasske, Praep. DII 78
151
            Pattern.compile("^(?:in\\s)?(?<institute>[Cc]oll\\.\\s.*?)(?:\\s+(?<accNumber>(Praep\\.|slide|No\\.|Inv\\. Nr\\.|Nr\\.).*))?$"), // like Coll. Lange-Bertalot, Bot. Inst., Univ. Frankfurt/Main, Germany Praep. Neukaledonien OTL 62
152
            Pattern.compile("^(?<institute>Inst\\.\\s.*?)\\s+(?<accNumber>N\\s.*)?$"), // like Inst. Geological Sciences, Acad. Sci. Belarus, Minsk N 212 A
153
            Pattern.compile("^(?<colCode>[A-Z]+)(?:\\s+(?<accNumber>.*))?$"), // identifies the Collection code and takes the rest as accessionNumber if any
154
    };
155

    
156

    
157
    private static final Pattern registrationPattern = Pattern.compile("^Registration date\\:\\s(?<regdate>\\d\\d\\.\\d\\d\\.\\d\\d); no\\.\\:\\s(?<regid>\\d+);\\soffice\\:\\s(?<office>.*?)\\.(?:\\s\\[Form no\\.\\:\\s(?<formNo>d+)\\])?$"); // Registration date: 29.06.98; no.: 2922; office: Berlin.
158

    
159
    private static Map<String, Integer> monthFromNameMap = new HashMap<>();
160

    
161
    static {
162
        String[] ck = new String[]{"leden", "únor", "březen", "duben", "květen", "červen", "červenec ", "srpen", "září", "říjen", "listopad", "prosinec"};
163
        String[] fr = new String[]{"janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"};
164
        String[] de = new String[]{"januar", "februar", "märz", "april", "mai", "juni", "juli", "august", "september", "oktober", "november", "dezember"};
165
        String[] en = new String[]{"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
166
        String[] it = new String[]{"gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"};
167
        String[] sp = new String[]{"enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"};
168
        String[] de_abbrev = new String[]{"jan.", "feb.", "märz", "apr.", "mai", "jun.", "jul.", "aug.", "sept.", "okt.", "nov.", "dez."};
169
        String[] en_abbrev = new String[]{"jan.", "feb.", "mar.", "apr.", "may", "jun.", "jul.", "aug.", "sep.", "oct.", "nov.", "dec."};
170
        String[] port = new String[]{"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
171
        String[] rom_num = new String[]{"i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii"};
172

    
173
        String[][] perLang =  new String[][]{ck, de, fr, en, it, sp, port, de_abbrev, en_abbrev, rom_num};
174

    
175
        for (String[] months: perLang) {
176
            for(int m = 1; m < 13; m++){
177
                monthFromNameMap.put(months[m - 1].toLowerCase(), m);
178
            }
179
        }
180

    
181
        // special cases
182
        monthFromNameMap.put("mar", 3);
183
        monthFromNameMap.put("dec", 12);
184
        monthFromNameMap.put("februari", 2);
185
        monthFromNameMap.put("març", 3);
186
    }
187

    
188

    
189
    DateTimeFormatter formatterYear = DateTimeFormat.forPattern("yyyy");
190

    
191
    private Map<String, Collection> collectionMap = new HashMap<>();
192

    
193
    private ExtensionType extensionTypeIAPTRegData = null;
194

    
195
    private Set<String> nameSet = new HashSet<>();
196
    private DefinedTermBase duplicateRegistration = null;
197

    
198
    enum TypesName {
199
        fieldUnit, holotype, isotype;
200

    
201
        public SpecimenTypeDesignationStatus status(){
202
            switch (this) {
203
                case holotype:
204
                    return SpecimenTypeDesignationStatus.HOLOTYPE();
205
                case isotype:
206
                    return SpecimenTypeDesignationStatus.ISOTYPE();
207
                default:
208
                    return null;
209
            }
210
        }
211
    }
212

    
213
    private MarkerType markerTypeFossil = null;
214
    private Rank rankUnrankedSupraGeneric = null;
215
    private Rank familyIncertisSedis = null;
216
    private AnnotationType annotationTypeCaveats = null;
217

    
218
    private Reference bookVariedadesTradicionales = null;
219

    
220
    /**
221
     * HACK for unit simple testing
222
     */
223
    boolean _testMode = System.getProperty("TEST_MODE") != null;
224

    
225
    private Taxon makeTaxon(HashMap<String, String> record, SimpleExcelTaxonImportState<CONFIG> state,
226
                            TaxonNode higherTaxonNode, boolean isFossil) {
227

    
228
        String regNumber = getValue(record, REGISTRATIONNO_PK, false);
229
        String regStr = getValue(record, REGISTRATION, true);
230
        String titleCacheStr = getValue(record, FULLNAME, true);
231
        String nameStr = getValue(record, NAMESTRING, true);
232
        String authorStr = getValue(record, AUTHORSTRING, true);
233
        String nomRefStr = getValue(record, LITSTRING, true);
234
        String authorsSpelling = getValue(record, AUTHORSSPELLING, true);
235
        String notesTxt = getValue(record, NOTESTXT, true);
236
        String caveats = getValue(record, CAVEATS, true);
237
        String fullSynSubstStr = getValue(record, FULLSYNSUBST, true);
238
        String fullBasionymStr = getValue(record, FULLBASIONYM, true);
239
        String basionymNameStr = getValue(record, FULLBASIONYM, true);
240
        String synSubstStr = getValue(record, SYNSUBSTSTR, true);
241
        String typeStr = getValue(record, TYPE, true);
242

    
243

    
244
        String nomRefTitle = null;
245
        String nomRefDetail;
246
        String nomRefPupDate = null;
247
        String nomRefIssue = null;
248
        Partial pupDate = null;
249

    
250
        boolean restoreOriginalReference = false;
251
        boolean nameIsValid = true;
252

    
253
        // preprocess nomRef: separate citation, reference detail, publishing date
254
        if(!StringUtils.isEmpty(nomRefStr)){
255
            nomRefStr = nomRefStr.trim();
256

    
257
            // handle the special case which is hard to parse:
258
            //
259
            // Las variedades tradicionales de frutales de la Cuenca del Río Segura. Catálogo Etnobotánico (1): Frutos secos, oleaginosos, frutales de hueso, almendros y frutales de pepita: 154. 1997.
260
            if(nomRefStr.startsWith("Las variedades tradicionales de frutales ")){
261

    
262
                if(bookVariedadesTradicionales == null){
263
                    bookVariedadesTradicionales = ReferenceFactory.newBook();
264
                    bookVariedadesTradicionales.setTitle("Las variedades tradicionales de frutales de la Cuenca del Río Segura. Catálogo Etnobotánico (1): Frutos secos, oleaginosos, frutales de hueso, almendros y frutales de pepita");
265
                    bookVariedadesTradicionales.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1997));
266
                    getReferenceService().save(bookVariedadesTradicionales);
267
                }
268
                nomRefStr = nomRefStr.replaceAll("^.*?\\:.*?\\:", "Las variedades tradicionales:");
269
                restoreOriginalReference = true;
270
            }
271

    
272
            Matcher m = nomRefTokenizeP.matcher(nomRefStr);
273
            if(m.matches()){
274
                nomRefTitle = m.group("title");
275
                nomRefDetail = m.group("detail");
276
                nomRefPupDate = m.group("date").trim();
277
                nomRefIssue = m.group("issue");
278

    
279
                pupDate = parseDate(regNumber, nomRefPupDate);
280
                if (pupDate != null) {
281
                    nomRefTitle = nomRefTitle + ": " + nomRefDetail + ". " + pupDate.toString(formatterYear) + ".";
282
                } else {
283
                    logger.warn(csvReportLine(regNumber, "Pub date", nomRefPupDate, "in", nomRefStr, "not parsable"));
284
                }
285
            } else {
286
                nomRefTitle = nomRefStr;
287
            }
288
        }
289

    
290
        TaxonName taxonName = makeBotanicalName(state, regNumber, titleCacheStr, nameStr, authorStr, nomRefTitle);
291

    
292
        // always add the original strings of parsed data as annotation
293
        taxonName.addAnnotation(Annotation.NewInstance("imported and parsed data strings:" +
294
                        "\n -  '" + LITSTRING + "': "+ nomRefStr +
295
                        "\n -  '" + TYPE + "': " + typeStr +
296
                        "\n -  '" + REGISTRATION  + "': " + regStr
297
                , AnnotationType.TECHNICAL(), Language.DEFAULT()));
298

    
299
        if(restoreOriginalReference){
300
            taxonName.setNomenclaturalReference(bookVariedadesTradicionales);
301
        }
302

    
303
        if(taxonName.getNomenclaturalReference() != null){
304
            if(pupDate != null) {
305
                taxonName.getNomenclaturalReference().setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(pupDate));
306
            }
307
            if(nomRefIssue != null) {
308
                taxonName.getNomenclaturalReference().setVolume(nomRefIssue);
309
            }
310
        }
311

    
312

    
313
        if(!StringUtils.isEmpty(notesTxt)){
314
            notesTxt = notesTxt.replace("Notes: ", "").trim();
315
            taxonName.addAnnotation(Annotation.NewInstance(notesTxt, AnnotationType.EDITORIAL(), Language.DEFAULT()));
316
            nameIsValid = false;
317

    
318
        }
319
        if(!StringUtils.isEmpty(caveats)){
320
            caveats = caveats.replace("Caveats: ", "").trim();
321
            taxonName.addAnnotation(Annotation.NewInstance(caveats, annotationTypeCaveats(), Language.DEFAULT()));
322
            nameIsValid = false;
323
        }
324

    
325
        if(nameIsValid){
326
            // Status is always considered valid if no notes and cavets are set
327
            taxonName.addStatus(NomenclaturalStatus.NewInstance(NomenclaturalStatusType.VALID()));
328
        }
329

    
330
        getNameService().save(taxonName);
331

    
332
        // Namerelations
333
        if(!StringUtils.isEmpty(authorsSpelling)){
334
            authorsSpelling = authorsSpelling.replaceFirst("Author's spelling:", "").replaceAll("\"", "").trim();
335

    
336
            String[] authorSpellingTokens = StringUtils.split(authorsSpelling, " ");
337
            String[] nameStrTokens = StringUtils.split(nameStr, " ");
338

    
339
            ArrayUtils.reverse(authorSpellingTokens);
340
            ArrayUtils.reverse(nameStrTokens);
341

    
342
            for (int i = 0; i < nameStrTokens.length; i++){
343
                if(i < authorSpellingTokens.length){
344
                    nameStrTokens[i] = authorSpellingTokens[i];
345
                }
346
            }
347
            ArrayUtils.reverse(nameStrTokens);
348

    
349
            String misspelledNameStr = StringUtils.join (nameStrTokens, ' ');
350
            // build the fullnameString of the misspelled name
351
            misspelledNameStr = taxonName.getTitleCache().replace(nameStr, misspelledNameStr);
352

    
353
            TaxonName misspelledName = nameParser.parseReferencedName(misspelledNameStr, NomenclaturalCode.ICNAFP, null);
354
            misspelledName.addRelationshipToName(taxonName, NameRelationshipType.MISSPELLING(), null);
355
            getNameService().save(misspelledName);
356
        }
357

    
358
        // Replaced Synonyms
359
        if(!StringUtils.isEmpty(fullSynSubstStr)){
360
            fullSynSubstStr = fullSynSubstStr.replace("Syn. subst.: ", "");
361
            TaxonName replacedSynonymName = makeBotanicalName(state, regNumber, fullSynSubstStr, synSubstStr, null, null);
362
            replacedSynonymName.addReplacedSynonym(taxonName, null, null, null);
363
            getNameService().save(replacedSynonymName);
364
        }
365

    
366
        Reference sec = state.getConfig().getSecReference();
367
        Taxon taxon = Taxon.NewInstance(taxonName, sec);
368

    
369
        // Basionym
370
        if(fullBasionymStr != null){
371
            fullBasionymStr = fullBasionymStr.replaceAll("^\\w*:\\s", ""); // Strip off the leading 'Basionym: "
372
            basionymNameStr = basionymNameStr.replaceAll("^\\w*:\\s", ""); // Strip off the leading 'Basionym: "
373
            TaxonName basionym = makeBotanicalName(state, regNumber, fullBasionymStr, basionymNameStr, null, null);
374
            getNameService().save(basionym);
375
            taxonName.addBasionym(basionym);
376

    
377
            Synonym syn = Synonym.NewInstance(basionym, sec);
378
            taxon.addSynonym(syn, SynonymType.HOMOTYPIC_SYNONYM_OF());
379
            getTaxonService().save(syn);
380
        }
381

    
382
        // Markers
383
        if(isFossil){
384
            taxon.addMarker(Marker.NewInstance(markerTypeFossil(), true));
385
        }
386
        if(!nameSet.add(titleCacheStr)){
387
            taxonName.addMarker(Marker.NewInstance(markerDuplicateRegistration(), true));
388
            logger.warn(csvReportLine(regNumber, "Duplicate registration of", titleCacheStr));
389
        }
390

    
391

    
392
        // Types
393
        if(!StringUtils.isEmpty(typeStr)){
394

    
395
            if(taxonName.getRank().isSpecies() || taxonName.getRank().isLower(Rank.SPECIES())) {
396
                makeSpecimenTypeData(typeStr, taxonName, regNumber, state, false);
397
            } else {
398
                makeNameTypeData(typeStr, taxonName, regNumber, state);
399
            }
400
        }
401

    
402
        getTaxonService().save(taxon);
403

    
404
        if(taxonName.getRank().equals(Rank.SPECIES()) || taxonName.getRank().isLower(Rank.SPECIES())){
405
            // try to find the genus, it should have been imported already, Genera are coming first in the import file
406
            Taxon genus = ((IAPTImportState)state).getGenusTaxonMap().get(taxonName.getGenusOrUninomial());
407
            if(genus != null){
408
                higherTaxonNode = genus.getTaxonNodes().iterator().next();
409
            } else {
410
                logger.info(csvReportLine(regNumber, "Parent genus not found for", nameStr));
411
            }
412
        }
413

    
414
        if(higherTaxonNode != null){
415
            higherTaxonNode.addChildTaxon(taxon, null, null);
416
            getTaxonNodeService().save(higherTaxonNode);
417
        }
418

    
419
        if(taxonName.getRank().isGenus()){
420
            ((IAPTImportState)state).getGenusTaxonMap().put(taxonName.getGenusOrUninomial(), taxon);
421
        }
422

    
423
        return taxon;
424
    }
425

    
426
    private void makeSpecimenTypeData(String typeStr, TaxonName taxonName, String regNumber, SimpleExcelTaxonImportState<CONFIG> state, boolean isFossil) {
427

    
428
        Matcher m = typeSpecimenSplitPattern.matcher(typeStr);
429

    
430
        if(m.matches()){
431
            String fieldUnitStr = m.group(TypesName.fieldUnit.name());
432
            // boolean isFieldUnit = typeStr.matches(".*([°']|\\d+\\s?m\\s|\\d+\\s?km\\s).*"); // check for location or unit m, km // makes no sense!!!!
433
            FieldUnit fieldUnit = parseFieldUnit(fieldUnitStr, regNumber, state);
434
            if(fieldUnit == null) {
435
                // create a field unit with only a titleCache using the fieldUnitStr substring
436
                logger.warn(csvReportLine(regNumber, "Type: fieldUnitStr can not be parsed", fieldUnitStr));
437
                fieldUnit = FieldUnit.NewInstance();
438
                fieldUnit.setTitleCache(fieldUnitStr, true);
439
                getOccurrenceService().save(fieldUnit);
440
            }
441
            getOccurrenceService().save(fieldUnit);
442

    
443
            SpecimenOrObservationType specimenType;
444
            if(isFossil){
445
                specimenType = SpecimenOrObservationType.Fossil;
446
            } else {
447
                specimenType = SpecimenOrObservationType.PreservedSpecimen;
448
            }
449

    
450
            // all others ..
451
            addSpecimenTypes(taxonName, fieldUnit, m.group(TypesName.holotype.name()), TypesName.holotype, false, regNumber, specimenType);
452
            addSpecimenTypes(taxonName, fieldUnit, m.group(TypesName.isotype.name()), TypesName.isotype, true, regNumber, specimenType);
453

    
454
        } else {
455
            // create a field unit with only a titleCache using the full typeStr
456
            FieldUnit fieldUnit = FieldUnit.NewInstance();
457
            fieldUnit.setTitleCache(typeStr, true);
458
            getOccurrenceService().save(fieldUnit);
459
            logger.warn(csvReportLine(regNumber, "Type: field 'Type' can not be parsed", typeStr));
460
        }
461
        getNameService().save(taxonName);
462
    }
463

    
464
    private void makeNameTypeData(String typeStr, IBotanicalName taxonName, String regNumber, SimpleExcelTaxonImportState<CONFIG> state) {
465

    
466
        String nameStr = typeStr.replaceAll("^Type\\s?\\:\\s?", "");
467
        if(nameStr.isEmpty()) {
468
            return;
469
        }
470

    
471
        String basionymNameStr = null;
472
        String noteStr = null;
473
        String agentStr = null;
474

    
475
        Matcher m;
476

    
477
        if(typeStr.startsWith("not to be indicated")){
478
            // Special case:
479
            // Type: not to be indicated (Art. H.9.1. Tokyo Code); stated parent genera: Hechtia Klotzsch; Deuterocohnia Mez
480
            // FIXME
481
            m = typeNameSpecialSplitPattern.matcher(nameStr);
482
            if(m.matches()){
483
                nameStr = m.group("name");
484
                noteStr = m.group("note");
485
                agentStr = m.group("agent");
486
                // TODO better import of agent?
487
                if(agentStr != null){
488
                    noteStr = noteStr + ": " + agentStr;
489
                }
490
            }
491
        } else {
492
            // Generic case
493
            m = typeNameBasionymPattern.matcher(nameStr);
494
            if (m.find()) {
495
                basionymNameStr = m.group("basionymName");
496
                if (basionymNameStr != null) {
497
                    nameStr = nameStr.replace(m.group(0), "");
498
                }
499
            }
500

    
501
            m = typeNameNotePattern.matcher(nameStr);
502
            if (m.find()) {
503
                noteStr = m.group(1);
504
                if (noteStr != null) {
505
                    nameStr = nameStr.replace(m.group(0), "");
506
                }
507
            }
508
        }
509

    
510
        TaxonName typeName = (TaxonName) nameParser.parseFullName(nameStr, NomenclaturalCode.ICNAFP, null);
511

    
512
        if(typeName.isProtectedTitleCache() || typeName.getNomenclaturalReference() != null && typeName.getNomenclaturalReference().isProtectedTitleCache()) {
513
            logger.warn(csvReportLine(regNumber, "NameType not parsable", typeStr, nameStr));
514
        }
515

    
516
        if(basionymNameStr != null){
517
            TaxonName basionymName = (TaxonName) nameParser.parseFullName(nameStr, NomenclaturalCode.ICNAFP, null);
518
            getNameService().save(basionymName);
519
            typeName.addBasionym(basionymName);
520
        }
521

    
522

    
523
        NameTypeDesignation nameTypeDesignation = NameTypeDesignation.NewInstance();
524
        nameTypeDesignation.setTypeName(typeName);
525
        getNameService().save(typeName);
526

    
527
        if(noteStr != null){
528
            nameTypeDesignation.addAnnotation(Annotation.NewInstance(noteStr, AnnotationType.EDITORIAL(), Language.UNKNOWN_LANGUAGE()));
529
        }
530
        taxonName.addNameTypeDesignation(typeName, null, null, null, null, false);
531

    
532
    }
533

    
534
    /**
535
     * Currently only parses the collector, fieldNumber and the collection date.
536
     *
537
     * @param fieldUnitStr
538
     * @param regNumber
539
     * @param state
540
     * @return null if the fieldUnitStr could not be parsed
541
     */
542
    private FieldUnit parseFieldUnit(String fieldUnitStr, String regNumber, SimpleExcelTaxonImportState<CONFIG> state) {
543

    
544
        FieldUnit fieldUnit = null;
545

    
546
        Matcher m1 = collectorPattern.matcher(fieldUnitStr);
547
        if(m1.matches()){
548

    
549
            String collectorData = m1.group(2); // like ... (leg. Metzeltin, 30. 9. 1996)
550
            String removal = m1.group(1);
551
            if(collectorData == null){
552
                collectorData = m1.group(4); // like ... leg. Metzeltin, 30. 9. 1996
553
                removal = m1.group(3);
554
            }
555
            if(collectorData == null){
556
                collectorData = m1.group(6); // like ^leg. J. J. Halda 18.3.1997$
557
                removal = null;
558
            }
559
            if(collectorData == null){
560
                return null;
561
            }
562

    
563
            // the fieldUnitStr is parsable
564
            // remove all collectorData from the fieldUnitStr and use the rest as locality
565
            String locality = null;
566
            if(removal != null){
567
                locality = fieldUnitStr.replace(removal, "");
568
            }
569

    
570
            String collectorStr = null;
571
            String detailStr = null;
572
            Partial date = null;
573
            String fieldNumber = null;
574

    
575
            Matcher m2 = collectionDataPattern.matcher(collectorData);
576
            if(m2.matches()){
577
                collectorStr = m2.group("collector");
578
                detailStr = m2.group("detail");
579

    
580
                // Try to make sense of the detailStr
581
                if(detailStr != null){
582
                    detailStr = detailStr.trim();
583
                    // 1. try to parse as date
584
                    date = parseDate(regNumber, detailStr);
585
                    if(date == null){
586
                        // 2. try to parse as number
587
                        if(collectorsNumber.matcher(detailStr).matches()){
588
                            fieldNumber = detailStr;
589
                        }
590
                    }
591
                }
592
                if(date == null && fieldNumber == null){
593
                    // detailed parsing not possible, so need fo fallback
594
                    collectorStr = collectorData;
595
                }
596
            }
597

    
598
            if(collectorStr == null) {
599
                collectorStr = collectorData;
600
            }
601

    
602
            fieldUnit = FieldUnit.NewInstance();
603
            GatheringEvent ge = GatheringEvent.NewInstance();
604
            if(locality != null){
605
                ge.setLocality(LanguageString.NewInstance(locality, Language.UNKNOWN_LANGUAGE()));
606
            }
607

    
608
            TeamOrPersonBase agent =  state.getAgentBase(collectorStr);
609
            if(agent == null) {
610
                agent = Person.NewTitledInstance(collectorStr);
611
                getAgentService().save(agent);
612
                state.putAgentBase(collectorStr, agent);
613
            }
614
            ge.setCollector(agent);
615

    
616
            if(date != null){
617
                ge.setGatheringDate(date);
618
            }
619

    
620
            getEventBaseService().save(ge);
621
            fieldUnit.setGatheringEvent(ge);
622

    
623
            if(fieldNumber != null) {
624
                fieldUnit.setFieldNumber(fieldNumber);
625
            }
626
            getOccurrenceService().save(fieldUnit);
627

    
628
        }
629

    
630
        return fieldUnit;
631
    }
632

    
633
    protected Partial parseDate(String regNumber, String dateStr) {
634

    
635
        Partial pupDate = null;
636
        boolean parseError = false;
637

    
638
        String day = null;
639
        String month = null;
640
        String monthName = null;
641
        String year = null;
642

    
643
        for(Pattern p : datePatterns){
644
            Matcher m2 = p.matcher(dateStr);
645
            if(m2.matches()){
646
                try {
647
                    year = m2.group("year");
648
                } catch (IllegalArgumentException e){
649
                    // named capture group not found
650
                }
651
                try {
652
                    month = m2.group("month");
653
                } catch (IllegalArgumentException e){
654
                    // named capture group not found
655
                }
656

    
657
                try {
658
                    monthName = m2.group("monthName");
659
                    month = monthFromName(monthName, regNumber);
660
                    if(month == null){
661
                        parseError = true;
662
                    }
663
                } catch (IllegalArgumentException e){
664
                    // named capture group not found
665
                }
666
                try {
667
                    day = m2.group("day");
668
                } catch (IllegalArgumentException e){
669
                    // named capture group not found
670
                }
671

    
672
                if(year != null){
673
                    if (year.length() == 2) {
674
                        // it is an abbreviated year from the 19** years
675
                        year = "19" + year;
676
                    }
677
                    break;
678
                } else {
679
                    parseError = true;
680
                }
681
            }
682
        }
683
        if(year == null){
684
            parseError = true;
685
        }
686
        List<DateTimeFieldType> types = new ArrayList<>();
687
        List<Integer> values = new ArrayList<>();
688
        if(!parseError) {
689
            types.add(DateTimeFieldType.year());
690
            values.add(Integer.parseInt(year));
691
            if (month != null) {
692
                types.add(DateTimeFieldType.monthOfYear());
693
                values.add(Integer.parseInt(month));
694
            }
695
            if (day != null) {
696
                types.add(DateTimeFieldType.dayOfMonth());
697
                values.add(Integer.parseInt(day));
698
            }
699
            pupDate = new Partial(types.toArray(new DateTimeFieldType[types.size()]), ArrayUtils.toPrimitive(values.toArray(new Integer[values.size()])));
700
        }
701
        return pupDate;
702
    }
703

    
704
    private String monthFromName(String monthName, String regNumber) {
705

    
706
        Integer month = monthFromNameMap.get(monthName.toLowerCase());
707
        if(month == null){
708
            logger.warn(csvReportLine(regNumber, "Unknown month name", monthName));
709
            return null;
710
        } else {
711
            return month.toString();
712
        }
713
    }
714

    
715

    
716
    private void addSpecimenTypes(IBotanicalName taxonName, FieldUnit fieldUnit, String typeStr, TypesName typeName, boolean multiple, String regNumber, SpecimenOrObservationType specimenType){
717

    
718
        if(StringUtils.isEmpty(typeStr)){
719
            return;
720
        }
721
        typeStr = typeStr.trim().replaceAll("\\.$", "");
722

    
723
        Collection collection = null;
724
        DerivedUnit specimen = null;
725

    
726
        List<DerivedUnit> specimens = new ArrayList<>();
727
        if(multiple){
728
            String[] tokens = typeStr.split("\\s?,\\s?");
729
            for (String t : tokens) {
730
                // command to  list all complex parsabel types:
731
                // csvcut -t -c RegistrationNo_Pk,Type iapt.csv | csvgrep -c Type -m "Holotype" | egrep -o 'Holotype:\s([A-Z]*\s)[^.]*?'
732
                // csvcut -t -c RegistrationNo_Pk,Type iapt.csv | csvgrep -c Type -m "Holotype" | egrep -o 'Isotype[^:]*:\s([A-Z]*\s)[^.]*?'
733

    
734
                if(!t.isEmpty()){
735
                    // trying to parse the string
736
                    specimen = parseSpecimenType(fieldUnit, typeName, collection, t, regNumber);
737
                    if(specimen != null){
738
                        specimens.add(specimen);
739
                    } else {
740
                        // parsing was not successful make simple specimen
741
                        specimens.add(makeSpecimenType(fieldUnit, t, specimenType));
742
                    }
743
                }
744
            }
745
        } else {
746
            specimen = parseSpecimenType(fieldUnit, typeName, collection, typeStr, regNumber);
747
            if(specimen != null) {
748
                specimens.add(specimen);
749
                // remember current collection
750
                collection = specimen.getCollection();
751
            } else {
752
                // parsing was not successful make simple specimen
753
                specimens.add(makeSpecimenType(fieldUnit, typeStr, SpecimenOrObservationType.PreservedSpecimen));
754
            }
755
        }
756

    
757
        for(DerivedUnit s : specimens){
758
            taxonName.addSpecimenTypeDesignation(s, typeName.status(), null, null, null, false, true);
759
       }
760
    }
761

    
762
    private DerivedUnit makeSpecimenType(FieldUnit fieldUnit, String titleCache, SpecimenOrObservationType specimenType) {
763
        DerivedUnit specimen;DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimenType, fieldUnit);
764
        facade.setTitleCache(titleCache.trim(), true);
765
        specimen = facade.innerDerivedUnit();
766
        return specimen;
767
    }
768

    
769
    /**
770
     *
771
     * @param fieldUnit
772
     * @param typeName
773
     * @param collection
774
     * @param text
775
     * @param regNumber
776
     * @return
777
     */
778
    protected DerivedUnit parseSpecimenType(FieldUnit fieldUnit, TypesName typeName, Collection collection, String text, String regNumber) {
779

    
780
        DerivedUnit specimen = null;
781

    
782
        String collectionCode = null;
783
        String subCollectionStr = null;
784
        String instituteStr = null;
785
        String accessionNumber = null;
786

    
787
        boolean unusualAccessionNumber = false;
788

    
789
        text = text.trim();
790

    
791
        // 1.  For Isotypes often the accession number is noted alone if the
792
        //     preceeding entry has a collection code.
793
        if(typeName .equals(TypesName.isotype) && collection != null){
794
            Matcher m = accessionNumberOnlyPattern.matcher(text);
795
            if(m.matches()){
796
                try {
797
                    accessionNumber = m.group("accNumber");
798
                    specimen = makeSpecimenType(fieldUnit, collection, accessionNumber);
799
                } catch (IllegalArgumentException e){
800
                    // match group acc_number not found
801
                }
802
            }
803
        }
804

    
805
        //2. try it the 'normal' way
806
        if(specimen == null) {
807
            for (Pattern p : specimenTypePatterns) {
808
                Matcher m = p.matcher(text);
809
                if (m.matches()) {
810
                    // collection code or collectionTitle is mandatory
811
                    try {
812
                        collectionCode = m.group("colCode");
813
                    } catch (IllegalArgumentException e){
814
                        // match group colCode not found
815
                    }
816

    
817
                    try {
818
                        instituteStr = m.group("institute");
819
                    } catch (IllegalArgumentException e){
820
                        // match group col_name not found
821
                    }
822

    
823
                    try {
824
                        subCollectionStr = m.group("subCollection");
825
                    } catch (IllegalArgumentException e){
826
                        // match group subCollection not found
827
                    }
828
                    try {
829
                        accessionNumber = m.group("accNumber");
830

    
831
                        // try to improve the accessionNumber
832
                        if(accessionNumber!= null) {
833
                            accessionNumber = accessionNumber.trim();
834
                            Matcher m2 = accessionNumberOnlyPattern.matcher(accessionNumber);
835
                            String betterAccessionNumber = null;
836
                            if (m2.matches()) {
837
                                try {
838
                                    betterAccessionNumber = m.group("accNumber");
839
                                } catch (IllegalArgumentException e) {
840
                                    // match group acc_number not found
841
                                }
842
                            }
843
                            if (betterAccessionNumber != null) {
844
                                accessionNumber = betterAccessionNumber;
845
                            } else {
846
                                unusualAccessionNumber = true;
847
                            }
848
                        }
849

    
850
                    } catch (IllegalArgumentException e){
851
                        // match group acc_number not found
852
                    }
853

    
854
                    if(collectionCode == null && instituteStr == null){
855
                        logger.warn(csvReportLine(regNumber, "Type: neither 'collectionCode' nor 'institute' found in ", text));
856
                        continue;
857
                    }
858
                    collection = getCollection(collectionCode, instituteStr, subCollectionStr);
859
                    specimen = makeSpecimenType(fieldUnit, collection, accessionNumber);
860
                    break;
861
                }
862
            }
863
        }
864
        if(specimen == null) {
865
            logger.warn(csvReportLine(regNumber, "Type: Could not parse specimen", typeName.name().toString(), text));
866
        }
867
        if(unusualAccessionNumber){
868
            logger.warn(csvReportLine(regNumber, "Type: Unusual accession number", typeName.name().toString(), text, accessionNumber));
869
        }
870
        return specimen;
871
    }
872

    
873
    private DerivedUnit makeSpecimenType(FieldUnit fieldUnit, Collection collection, String accessionNumber) {
874

    
875
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen, fieldUnit);
876
        facade.setCollection(collection);
877
        if(accessionNumber != null){
878
            facade.setAccessionNumber(accessionNumber);
879
        }
880
        return facade.innerDerivedUnit();
881
    }
882

    
883
    private TaxonName makeBotanicalName(SimpleExcelTaxonImportState<CONFIG> state, String regNumber, String titleCacheStr, String nameStr,
884
                                            String authorStr, String nomRefTitle) {
885

    
886
        TaxonName taxonName;// cache field for the taxonName.titleCache
887
        String taxonNameTitleCache = null;
888
        Map<String, AnnotationType> nameAnnotations = new HashMap<>();
889

    
890
        // TitleCache preprocessing
891
        if(titleCacheStr.endsWith(ANNOTATION_MARKER_STRING) || (authorStr != null && authorStr.endsWith(ANNOTATION_MARKER_STRING))){
892
            nameAnnotations.put("Author abbreviation not checked.", AnnotationType.EDITORIAL());
893
            titleCacheStr = titleCacheStr.replace(ANNOTATION_MARKER_STRING, "").trim();
894
            if(authorStr != null) {
895
                authorStr = authorStr.replace(ANNOTATION_MARKER_STRING, "").trim();
896
            }
897
        }
898

    
899
        // parse the full taxon name
900
        if(!StringUtils.isEmpty(nomRefTitle)){
901
            String referenceSeparator = nomRefTitle.startsWith("in ") ? " " : ", ";
902
            String taxonFullNameStr = titleCacheStr + referenceSeparator + nomRefTitle;
903
            logger.debug(":::::" + taxonFullNameStr);
904
            taxonName = nameParser.parseReferencedName(taxonFullNameStr, NomenclaturalCode.ICNAFP, null);
905
        } else {
906
            taxonName = (TaxonName) nameParser.parseFullName(titleCacheStr, NomenclaturalCode.ICNAFP, null);
907
        }
908

    
909
        taxonNameTitleCache = taxonName.getTitleCache().trim();
910
        if (taxonName.isProtectedTitleCache()) {
911
            logger.warn(csvReportLine(regNumber, "Name could not be parsed", titleCacheStr));
912
        } else {
913

    
914
            boolean doRestoreTitleCacheStr = false;
915

    
916
            // Check if titleCache and nameCache are plausible
917
            String titleCacheCompareStr = titleCacheStr;
918
            String nameCache = taxonName.getNameCache();
919
            String nameCompareStr = nameStr;
920
            if(taxonName.isBinomHybrid()){
921
                titleCacheCompareStr = titleCacheCompareStr.replace(" x ", " ×");
922
                nameCompareStr = nameCompareStr.replace(" x ", " ×");
923
            }
924
            if(taxonName.isMonomHybrid()){
925
                titleCacheCompareStr = titleCacheCompareStr.replaceAll("^X ", "× ");
926
                nameCompareStr = nameCompareStr.replace("^X ", "× ");
927
            }
928
            if(authorStr != null && authorStr.contains(" et ")){
929
                titleCacheCompareStr = titleCacheCompareStr.replaceAll(" et ", " & ");
930
            }
931
            if (!taxonNameTitleCache.equals(titleCacheCompareStr)) {
932
                logger.warn(csvReportLine(regNumber, "The generated titleCache differs from the imported string", taxonNameTitleCache, " != ", titleCacheStr, " ==> original titleCacheStr has been restored"));
933
                doRestoreTitleCacheStr = true;
934
            }
935
            if (!nameCache.trim().equals(nameCompareStr)) {
936
                logger.warn(csvReportLine(regNumber, "The parsed nameCache differs from field '" + NAMESTRING + "'", nameCache, " != ", nameCompareStr));
937
            }
938

    
939
            //  Author
940
            //nameParser.handleAuthors(taxonName, titleCacheStr, authorStr);
941
            //if (!titleCacheStr.equals(taxonName.getTitleCache())) {
942
            //    logger.warn(regNumber + ": titleCache has changed after setting authors, will restore original titleCacheStr");
943
            //    doRestoreTitleCacheStr = true;
944
            //}
945

    
946
            if(doRestoreTitleCacheStr){
947
                taxonName.setTitleCache(titleCacheStr, true);
948
            }
949

    
950
            // deduplicate
951
            replaceAuthorNamesAndNomRef(state, taxonName);
952
        }
953

    
954
        // Annotations
955
        if(!nameAnnotations.isEmpty()){
956
            for(String text : nameAnnotations.keySet()){
957
                taxonName.addAnnotation(Annotation.NewInstance(text, nameAnnotations.get(text), Language.DEFAULT()));
958
            }
959
        }
960

    
961
        taxonName.addSource(OriginalSourceType.Import, regNumber, null, state.getConfig().getSourceReference(), null);
962

    
963
        getNameService().save(taxonName);
964

    
965
        return taxonName;
966
    }
967

    
968
    /**
969
     * @param state
970
     * @return
971
     */
972
    private TaxonNode getClassificationRootNode(IAPTImportState state) {
973

    
974
     //   Classification classification = state.getClassification();
975
     //   if (classification == null){
976
     //       IAPTImportConfigurator config = state.getConfig();
977
     //       classification = Classification.NewInstance(state.getConfig().getClassificationName());
978
     //       classification.setUuid(config.getClassificationUuid());
979
     //       classification.setReference(config.getSecReference());
980
     //       classification = getClassificationService().find(state.getConfig().getClassificationUuid());
981
     //   }
982
        TaxonNode rootNode = state.getRootNode();
983
        if (rootNode == null){
984
            rootNode = getTaxonNodeService().find(ROOT_UUID);
985
        }
986
        if (rootNode == null){
987
            Classification classification = state.getClassification();
988
            if (classification == null){
989
                Reference sec = state.getSecReference();
990
                String classificationName = state.getConfig().getClassificationName();
991
                Language language = Language.DEFAULT();
992
                classification = Classification.NewInstance(classificationName, sec, language);
993
                state.setClassification(classification);
994
                classification.setUuid(state.getConfig().getClassificationUuid());
995
                classification.getRootNode().setUuid(ROOT_UUID);
996
                getClassificationService().save(classification);
997
            }
998
            rootNode = classification.getRootNode();
999
            state.setRootNode(rootNode);
1000
        }
1001
        return rootNode;
1002
    }
1003

    
1004
    private Collection getCollection(String collectionCode, String instituteStr, String subCollectionStr){
1005

    
1006
        Collection superCollection = null;
1007
        if(subCollectionStr != null){
1008
            superCollection = getCollection(collectionCode, instituteStr, null);
1009
            collectionCode = subCollectionStr;
1010
            instituteStr = null;
1011
        }
1012

    
1013
        final String key = collectionCode + "-#i:" + StringUtils.defaultString(instituteStr);
1014

    
1015
        Collection collection = collectionMap.get(key);
1016

    
1017
        if(collection == null) {
1018
            collection = Collection.NewInstance();
1019
            collection.setCode(collectionCode);
1020
            if(instituteStr != null){
1021
                collection.setInstitute(Institution.NewNamedInstance(instituteStr));
1022
            }
1023
            if(superCollection != null){
1024
                collection.setSuperCollection(superCollection);
1025
            }
1026
            collectionMap.put(key, collection);
1027
            if(!_testMode) {
1028
                getCollectionService().save(collection);
1029
            }
1030
        }
1031

    
1032
        return collection;
1033
    }
1034

    
1035

    
1036
    /**
1037
     * @param record
1038
     * @param originalKey
1039
     * @param doUnescapeHtmlEntities
1040
     * @return
1041
     */
1042
    private String getValue(HashMap<String, String> record, String originalKey, boolean doUnescapeHtmlEntities) {
1043
        String value = record.get(originalKey);
1044

    
1045
        value = fixCharacters(value);
1046

    
1047
        if (! StringUtils.isBlank(value)) {
1048
        	if (logger.isDebugEnabled()) {
1049
        	    logger.debug(originalKey + ": " + value);
1050
        	}
1051
        	value = CdmUtils.removeDuplicateWhitespace(value.trim()).toString();
1052
            if(doUnescapeHtmlEntities){
1053
                value = StringEscapeUtils.unescapeHtml(value);
1054
            }
1055
        	return value.trim();
1056
        }else{
1057
        	return null;
1058
        }
1059
    }
1060

    
1061
    /**
1062
     * Fixes broken characters.
1063
     * For details see
1064
     * http://dev.e-taxonomy.eu/redmine/issues/6035
1065
     *
1066
     * @param value
1067
     * @return
1068
     */
1069
    private String fixCharacters(String value) {
1070

    
1071
        value = StringUtils.replace(value, "s$K", "š");
1072
        value = StringUtils.replace(value, "n$K", "ň");
1073
        value = StringUtils.replace(value, "e$K", "ě");
1074
        value = StringUtils.replace(value, "r$K", "ř");
1075
        value = StringUtils.replace(value, "c$K", "č");
1076
        value = StringUtils.replace(value, "z$K", "ž");
1077
        value = StringUtils.replace(value, "S>U$K", "Š");
1078
        value = StringUtils.replace(value, "C>U$K", "Č");
1079
        value = StringUtils.replace(value, "R>U$K", "Ř");
1080
        value = StringUtils.replace(value, "Z>U$K", "Ž");
1081
        value = StringUtils.replace(value, "g$K", "ǧ");
1082
        value = StringUtils.replace(value, "s$A", "ś");
1083
        value = StringUtils.replace(value, "n$A", "ń");
1084
        value = StringUtils.replace(value, "c$A", "ć");
1085
        value = StringUtils.replace(value, "e$E", "ę");
1086
        value = StringUtils.replace(value, "o$H", "õ");
1087
        value = StringUtils.replace(value, "s$C", "ş");
1088
        value = StringUtils.replace(value, "t$C", "ț");
1089
        value = StringUtils.replace(value, "S>U$C", "Ş");
1090
        value = StringUtils.replace(value, "a$O", "å");
1091
        value = StringUtils.replace(value, "A>U$O", "Å");
1092
        value = StringUtils.replace(value, "u$O", "ů");
1093
        value = StringUtils.replace(value, "g$B", "ğ");
1094
        value = StringUtils.replace(value, "g$B", "ĕ");
1095
        value = StringUtils.replace(value, "a$B", "ă");
1096
        value = StringUtils.replace(value, "l$/", "ł");
1097
        value = StringUtils.replace(value, ">i", "ı");
1098
        value = StringUtils.replace(value, "i$U", "ï");
1099
        // Special-cases
1100
        value = StringUtils.replace(value, "&yacute", "ý");
1101
        value = StringUtils.replace(value, ">L", "Ł"); // corrected rule
1102
        value = StringUtils.replace(value, "E>U$D", "З");
1103
        value = StringUtils.replace(value, "S>U$E", "Ş");
1104
        value = StringUtils.replace(value, "s$E", "ş");
1105

    
1106
        value = StringUtils.replace(value, "c$k", "č");
1107
        value = StringUtils.replace(value, " U$K", " Š");
1108

    
1109
        value = StringUtils.replace(value, "O>U>!", "Ø");
1110
        value = StringUtils.replace(value, "o>!", "ø");
1111
        value = StringUtils.replace(value, "S$K", "Ŝ");
1112
        value = StringUtils.replace(value, ">l", "ğ");
1113

    
1114
        value = StringUtils.replace(value, "§B>i", "ł");
1115
        value = StringUtils.replace(value, "¤", "ń");
1116

    
1117
        return value;
1118
    }
1119

    
1120

    
1121
    /**
1122
	 *  Stores taxa records in DB
1123
	 */
1124
	@Override
1125
    protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
1126

    
1127
        if(excludeFromImport(state)){
1128
            return;
1129
        }
1130

    
1131
        String lineNumber = "L#" + state.getCurrentLine() + ": ";
1132
        logger.setLevel(Level.DEBUG);
1133
        HashMap<String, String> record = state.getOriginalRecord();
1134
        logger.debug(lineNumber + record.toString());
1135

    
1136
        Set<String> keys = record.keySet();
1137
        for (String key: keys) {
1138
            if (! expectedKeys.contains(key)){
1139
                logger.warn(lineNumber + "Unexpected Key: " + key);
1140
            }
1141
        }
1142

    
1143
        String reg_id = record.get(REGISTRATIONNO_PK);
1144

    
1145
        //higherTaxon
1146
        String higherTaxaString = record.get(HIGHERTAXON);
1147
        boolean isFossil = false;
1148
        if(higherTaxaString.startsWith("FOSSIL ")){
1149
            higherTaxaString = higherTaxaString.replace("FOSSIL ", "");
1150
            isFossil = true;
1151
        }
1152
        TaxonNode higherTaxon = getHigherTaxon(higherTaxaString, (IAPTImportState)state);
1153

    
1154
       //Taxon
1155
        Taxon taxon = makeTaxon(record, state, higherTaxon, isFossil);
1156
        if (taxon == null){
1157
            logger.warn(lineNumber + "taxon could not be created and is null");
1158
            return;
1159
        }
1160
        ((IAPTImportState)state).setCurrentTaxon(taxon);
1161

    
1162
        // Registration
1163
        IAPTRegData regData = makeIAPTRegData(state);
1164
        ObjectMapper mapper = new ObjectMapper();
1165
        try {
1166
            String regdataJson = mapper.writeValueAsString(regData);
1167
            Extension.NewInstance(taxon.getName(), regdataJson, getExtensionTypeIAPTRegData());
1168
            getNameService().save(taxon.getName());
1169
        } catch (JsonProcessingException e) {
1170
            logger.error("Error on converting IAPTRegData", e);
1171
        }
1172

    
1173
        logger.info("#of imported Genera: " + ((IAPTImportState) state).getGenusTaxonMap().size());
1174
		return;
1175
    }
1176

    
1177
    private boolean excludeFromImport(SimpleExcelTaxonImportState<CONFIG> state) {
1178
        if(state.getConfig().isDoAlgeaeOnly()){
1179
            boolean include = false;
1180
            String higherTaxon = getValue(state.getOriginalRecord(), HIGHERTAXON, true);
1181
            String fullNameStr = getValue(state.getOriginalRecord(), FULLNAME, true);
1182
            include |= higherTaxon.matches(".*?PHYCEAE(?:$|\\s+)");
1183
            for(String test : new String[]{
1184
                    "Bolidophyceae ",
1185
                    "Phaeothamniophyceae ",
1186
                    "Bolidomonadales ",
1187
                    "Bolidomonadaceae ",
1188
                    "Aureoumbra ",
1189
                    "Bolidomonas ",
1190
                    "Seagriefia ",
1191
                    "Navicula "
1192
                }) {
1193
                include |= fullNameStr.startsWith(test);
1194
            }
1195
            return !include;
1196
        }
1197

    
1198
        return false;
1199
    }
1200

    
1201
    private ExtensionType getExtensionTypeIAPTRegData() {
1202
        if(extensionTypeIAPTRegData == null){
1203
            extensionTypeIAPTRegData = ExtensionType.NewInstance("IAPTRegData.json", "IAPTRegData.json", "");
1204
            getTermService().save(extensionTypeIAPTRegData);
1205
        }
1206
        return extensionTypeIAPTRegData;
1207
    }
1208

    
1209
    private IAPTRegData makeIAPTRegData(SimpleExcelTaxonImportState<CONFIG> state) {
1210

    
1211
        HashMap<String, String> record = state.getOriginalRecord();
1212
        String registrationStr = getValue(record, REGISTRATION);
1213
        String regDateStr = getValue(record, REGDATE);
1214
        String regStr = getValue(record, REGISTRATION, true);
1215

    
1216
        String dateStr = null;
1217
        String office = null;
1218
        Integer regID = null;
1219
        Integer formNo = null;
1220

    
1221
        Matcher m = registrationPattern.matcher(registrationStr);
1222
        if(m.matches()){
1223
            dateStr = m.group("regdate");
1224
            if(parseDate( regStr, dateStr) == null){
1225
                // check for valid dates
1226
                logger.warn(csvReportLine(regStr, REGISTRATION + ": could not parse date", dateStr, " in ", registrationStr));
1227
            };
1228
            office = m.group("office");
1229
            regID = Integer.valueOf(m.group("regid"));
1230
            try {
1231
                formNo = Integer.valueOf(m.group("formNo"));
1232
            } catch(IllegalArgumentException e){
1233
                // ignore
1234
            }
1235
        } else {
1236
            logger.warn(csvReportLine(regStr, REGISTRATION + ": could not be parsed", registrationStr));
1237
        }
1238
        IAPTRegData regData = new IAPTRegData(dateStr, office, regID, formNo);
1239
        return regData;
1240
    }
1241

    
1242
    private TaxonNode getHigherTaxon(String higherTaxaString, IAPTImportState state) {
1243
        String[] higherTaxaNames = higherTaxaString.toLowerCase().replaceAll("[\\[\\]]", "").split(":");
1244
        TaxonNode higherTaxonNode = null;
1245

    
1246
        ITaxonTreeNode rootNode = getClassificationRootNode(state);
1247
        for (String htn :  higherTaxaNames) {
1248
            htn = StringUtils.capitalize(htn.trim());
1249
            Taxon higherTaxon = state.getHigherTaxon(htn);
1250
            if (higherTaxon != null){
1251
                higherTaxonNode = higherTaxon.getTaxonNodes().iterator().next();
1252
            }else{
1253
                IBotanicalName name = makeHigherTaxonName(state, htn);
1254
                Reference sec = state.getSecReference();
1255
                higherTaxon = Taxon.NewInstance(name, sec);
1256
                getTaxonService().save(higherTaxon);
1257
                higherTaxonNode = rootNode.addChildTaxon(higherTaxon, sec, null);
1258
                state.putHigherTaxon(htn, higherTaxon);
1259
                getClassificationService().saveTreeNode(higherTaxonNode);
1260
            }
1261
            rootNode = higherTaxonNode;
1262
        }
1263
        return higherTaxonNode;
1264
    }
1265

    
1266
    private IBotanicalName makeHigherTaxonName(IAPTImportState state, String name) {
1267

    
1268
        Rank rank = guessRank(name);
1269

    
1270
        IBotanicalName taxonName = TaxonNameFactory.NewBotanicalInstance(rank);
1271
        taxonName.addSource(makeOriginalSource(state));
1272
        taxonName.setGenusOrUninomial(StringUtils.capitalize(name));
1273
        return taxonName;
1274
    }
1275

    
1276
    private Rank guessRank(String name) {
1277

    
1278
        // normalize
1279
        name = name.replaceAll("\\(.*\\)", "").trim();
1280

    
1281
        if(name.matches("^Plantae$|^Fungi$")){
1282
           return Rank.KINGDOM();
1283
        } else if(name.matches("^Incertae sedis$|^No group assigned$")){
1284
           return rankFamilyIncertisSedis();
1285
        } else if(name.matches(".*phyta$|.*mycota$")){
1286
           return Rank.PHYLUM();
1287
        } else if(name.matches(".*phytina$|.*mycotina$")){
1288
           return Rank.SUBPHYLUM();
1289
        } else if(name.matches("Gymnospermae$|.*ones$")){ // Monocotyledones, Dicotyledones
1290
            return rankUnrankedSupraGeneric();
1291
        } else if(name.matches(".*opsida$|.*phyceae$|.*mycetes$|.*ones$|^Musci$|^Hepaticae$")){
1292
           return Rank.CLASS();
1293
        } else if(name.matches(".*idae$|.*phycidae$|.*mycetidae$")){
1294
           return Rank.SUBCLASS();
1295
        } else if(name.matches(".*ales$")){
1296
           return Rank.ORDER();
1297
        } else if(name.matches(".*ineae$")){
1298
           return Rank.SUBORDER();
1299
        } else if(name.matches(".*aceae$")){
1300
            return Rank.FAMILY();
1301
        } else if(name.matches(".*oideae$")){
1302
           return Rank.SUBFAMILY();
1303
        } else
1304
        //    if(name.matches(".*eae$")){
1305
        //    return Rank.TRIBE();
1306
        // } else
1307
            if(name.matches(".*inae$")){
1308
           return Rank.SUBTRIBE();
1309
        } else if(name.matches(".*ae$")){
1310
           return Rank.FAMILY();
1311
        }
1312
        return Rank.UNKNOWN_RANK();
1313
    }
1314

    
1315
    private Rank rankUnrankedSupraGeneric() {
1316

    
1317
        if(rankUnrankedSupraGeneric == null){
1318
            rankUnrankedSupraGeneric = Rank.NewInstance(RankClass.Suprageneric, "Unranked supra generic", " ", " ");
1319
            getTermService().save(rankUnrankedSupraGeneric);
1320
        }
1321
        return rankUnrankedSupraGeneric;
1322
    }
1323

    
1324
    private Rank rankFamilyIncertisSedis() {
1325

    
1326
        if(familyIncertisSedis == null){
1327
            familyIncertisSedis = Rank.NewInstance(RankClass.Suprageneric, "Family incertis sedis", " ", " ");
1328
            getTermService().save(familyIncertisSedis);
1329
        }
1330
        return familyIncertisSedis;
1331
    }
1332

    
1333
    private AnnotationType annotationTypeCaveats(){
1334
        if(annotationTypeCaveats == null){
1335
            annotationTypeCaveats = AnnotationType.NewInstance("Caveats", "Caveats", "");
1336
            getTermService().save(annotationTypeCaveats);
1337
        }
1338
        return annotationTypeCaveats;
1339
    }
1340

    
1341

    
1342
    /**
1343
     * @param state
1344
     * @return
1345
     */
1346
    private IdentifiableSource makeOriginalSource(IAPTImportState state) {
1347
        return IdentifiableSource.NewDataImportInstance("line: " + state.getCurrentLine(), null, state.getConfig().getSourceReference());
1348
    }
1349

    
1350

    
1351
    private Reference makeReference(IAPTImportState state, UUID uuidRef) {
1352
        Reference ref = state.getReference(uuidRef);
1353
        if (ref == null){
1354
            ref = getReferenceService().find(uuidRef);
1355
            state.putReference(uuidRef, ref);
1356
        }
1357
        return ref;
1358
    }
1359

    
1360
    private MarkerType markerTypeFossil(){
1361
        if(this.markerTypeFossil == null){
1362
            markerTypeFossil = MarkerType.NewInstance("isFossilTaxon", "isFossil", null);
1363
            getTermService().save(this.markerTypeFossil);
1364
        }
1365
        return markerTypeFossil;
1366
    }
1367

    
1368
    private MarkerType markerDuplicateRegistration(){
1369
        if(this.duplicateRegistration == null){
1370
            duplicateRegistration = MarkerType.NewInstance("duplicateRegistration", "duplicateRegistration", null);
1371
            getTermService().save(this.duplicateRegistration);
1372
        }
1373
        return markerTypeFossil;
1374
    }
1375

    
1376
    private String csvReportLine(String regId, String message, String ... fields){
1377
        StringBuilder out = new StringBuilder("regID#");
1378
        out.append(regId).append(",\"").append(message).append('"');
1379

    
1380
        for(String f : fields){
1381
            out.append(",\"").append(f).append('"');
1382
        }
1383
        return out.toString();
1384
    }
1385

    
1386

    
1387
}
(1-1/8)