Project

General

Profile

Download (50.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2013 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
package eu.etaxonomy.cdm.io.taxonx2013;
10

    
11
import java.awt.Dimension;
12
import java.io.StringWriter;
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.Scanner;
20
import java.util.Set;
21
import java.util.UUID;
22

    
23
import javax.swing.JFrame;
24
import javax.swing.JOptionPane;
25
import javax.swing.JScrollPane;
26
import javax.swing.JTextArea;
27
import javax.swing.UIManager;
28
import javax.xml.transform.OutputKeys;
29
import javax.xml.transform.Transformer;
30
import javax.xml.transform.TransformerException;
31
import javax.xml.transform.TransformerFactory;
32
import javax.xml.transform.TransformerFactoryConfigurationError;
33
import javax.xml.transform.dom.DOMSource;
34
import javax.xml.transform.stream.StreamResult;
35

    
36
import org.apache.commons.lang.StringUtils;
37
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
38
import org.w3c.dom.Node;
39
import org.w3c.dom.NodeList;
40

    
41
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
42
import eu.etaxonomy.cdm.api.service.IAgentService;
43
import eu.etaxonomy.cdm.io.specimen.UnitsGatheringArea;
44
import eu.etaxonomy.cdm.io.specimen.UnitsGatheringEvent;
45
import eu.etaxonomy.cdm.model.agent.AgentBase;
46
import eu.etaxonomy.cdm.model.agent.Person;
47
import eu.etaxonomy.cdm.model.common.CdmBase;
48
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
49
import eu.etaxonomy.cdm.model.common.Language;
50
import eu.etaxonomy.cdm.model.common.TimePeriod;
51
import eu.etaxonomy.cdm.model.description.Feature;
52
import eu.etaxonomy.cdm.model.description.TaxonDescription;
53
import eu.etaxonomy.cdm.model.description.TextData;
54
import eu.etaxonomy.cdm.model.location.NamedArea;
55
import eu.etaxonomy.cdm.model.name.INonViralName;
56
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
57
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
58
import eu.etaxonomy.cdm.model.name.Rank;
59
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
60
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
61
import eu.etaxonomy.cdm.model.name.TaxonName;
62
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
63
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
64
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
65
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
66
import eu.etaxonomy.cdm.model.reference.IBook;
67
import eu.etaxonomy.cdm.model.reference.IBookSection;
68
import eu.etaxonomy.cdm.model.reference.Reference;
69
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
70
import eu.etaxonomy.cdm.model.taxon.Classification;
71
import eu.etaxonomy.cdm.model.taxon.Taxon;
72
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
73
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
74
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
75
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
76
import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
77

    
78
/**
79
 * @author pkelbert
80
 * @since 2 avr. 2013
81
 */
82
public class TaxonXExtractor {
83

    
84
    protected TaxonXImport importer;
85
    protected TaxonXImportState state2;
86
    private final Map<String,String> namesAsked = new HashMap<>();
87
    private final Map<String,Rank>ranksAsked = new HashMap<>();
88

    
89
    Logger logger = LogManager.getLogger(TaxonXExtractor.class);
90

    
91
    public class ReferenceBuilder{
92
        private int nbRef=0;
93
        private boolean foundBibref=false;
94
        private final TaxonXAddSources sourceHandler;
95

    
96

    
97
        public ReferenceBuilder(TaxonXAddSources sourceHandler) {
98
            this.sourceHandler=sourceHandler;
99
        }
100

    
101
        public boolean isFoundBibref() {
102
            return foundBibref;
103
        }
104
        public void setFoundBibref(boolean foundBibref) {
105
            this.foundBibref = foundBibref;
106
        }
107

    
108
        public void builReference(String mref, String treatmentMainName, NomenclaturalCode nomenclaturalCode,
109
                Taxon acceptedTaxon, Reference refMods) {
110
            // System.out.println("builReference "+mref);
111
            this.setFoundBibref(true);
112

    
113
            String ref= mref;
114
            if ( (ref.endsWith(";") ||ref.endsWith(",")  ) && ((ref.length())>1)) {
115
                ref=ref.substring(0, ref.length()-1)+".";
116
            }
117
            if (ref.startsWith(treatmentMainName) && !ref.endsWith(treatmentMainName)) {
118
                ref=ref.replace(treatmentMainName, "");
119
                ref=ref.trim();
120
                while (ref.startsWith(".") || ref.startsWith(",")) {
121
                    ref=ref.replace(".","").replace(",","").trim();
122
                }
123
            }
124

    
125
            //                        logger.info("Current reference :"+nbRef+", "+ref+", "+treatmentMainName+"--"+ref.indexOf(treatmentMainName));
126
            Reference reference = ReferenceFactory.newGeneric();
127
            reference.setTitleCache(ref, true);
128

    
129
            //only add the first one if there is no nomenclatural reference yet
130
            if (nbRef==0){
131
                if(acceptedTaxon.getName().getNomenclaturalReference()==null){
132
                    acceptedTaxon.getName().setNomenclaturalReference(reference);
133
                    sourceHandler.addSource(refMods, acceptedTaxon);
134
                }
135
            }
136
            //add all other references as Feature.Citation
137
            TaxonDescription taxonDescription =importer.getTaxonDescription(acceptedTaxon, false, true);
138
            acceptedTaxon.addDescription(taxonDescription);
139
            sourceHandler.addSource(refMods, acceptedTaxon);
140

    
141
            TextData textData = TextData.NewInstance(Feature.CITATION());
142
            Language language = Language.DEFAULT();
143
            textData.putText(language, ref);
144
            sourceHandler.addSource(reference, textData,acceptedTaxon.getName(),refMods);
145
            taxonDescription.addElement(textData);
146

    
147
            sourceHandler.addSource(refMods, taxonDescription);
148

    
149
            importer.getTaxonService().saveOrUpdate(acceptedTaxon);
150
            //                        logger.warn("BWAAHHHH: "+nameToBeFilled.getParsingProblems()+", "+ref);
151
            nbRef++;
152

    
153
        }
154
    }
155

    
156
    public class MySpecimenOrObservation{
157
        String descr="";
158
        DerivedUnit derivedUnitBase=null;
159

    
160
        public String getDescr() {
161
            return descr;
162
        }
163
        public void setDescr(String descr) {
164
            this.descr = descr;
165
        }
166
        public DerivedUnit getDerivedUnitBase() {
167
            return derivedUnitBase;
168
        }
169
        public void setDerivedUnitBase(DerivedUnit derivedUnitBase) {
170
            this.derivedUnitBase = derivedUnitBase;
171
        }
172
    }
173

    
174
    @SuppressWarnings({ "unused", "rawtypes" })
175
    protected MySpecimenOrObservation extractSpecimenOrObservation(Node specimenObservationNode, DerivedUnit derivedUnitBase,
176
            SpecimenOrObservationType defaultAssociation, TaxonName typifiableName) {
177
        String country=null;
178
        String locality=null;
179
        String stateprov=null;
180
        String collector=null;
181
        String fieldNumber=null;
182
        Double latitude=null,longitude=null;
183
        TimePeriod tp =null;
184
        String day,month,year="";
185
        String descr="not available";
186
        String type="";
187
        boolean asso=false;
188
        NodeList eventContent =null;
189
        // create facade
190
        DerivedUnitFacade derivedUnitFacade = null;
191

    
192
        UnitsGatheringEvent unitsGatheringEvent;
193
        UnitsGatheringArea unitsGatheringArea;
194
        DefinedTermBase areaCountry;
195

    
196
        MySpecimenOrObservation specimenOrObservation = new MySpecimenOrObservation();
197

    
198
        NodeList xmldata= specimenObservationNode.getChildNodes();
199
        for (int n=0;n<xmldata.getLength();n++){
200
            eventContent=xmldata.item(n).getChildNodes();
201
            if (xmldata.item(n).getNodeName().equalsIgnoreCase("tax:xmldata")){
202
                asso=true;
203
                country=null;
204
                locality=null;
205
                stateprov=null;
206
                collector=null;
207
                fieldNumber=null;
208
                latitude=null;
209
                longitude=null;
210
                day="";
211
                month="";
212
                year="";
213
                type="";
214
                for (int j=0;j<eventContent.getLength();j++){
215
                    if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:country")){
216
                        country=eventContent.item(j).getTextContent().trim();
217
                    }
218
                    else if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:locality")){
219
                        locality=eventContent.item(j).getTextContent().trim();
220
                    }
221
                    else  if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:stateprovince")){
222
                        stateprov=eventContent.item(j).getTextContent().trim();
223
                    }
224
                    else  if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:collector")){
225
                        collector=eventContent.item(j).getTextContent().trim();
226
                    }
227
                    else  if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:yearcollected")){
228
                        year=eventContent.item(j).getTextContent().trim();
229
                    }
230
                    else  if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:monthcollected")){
231
                        month=eventContent.item(j).getTextContent().trim();
232
                    }
233
                    else  if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:daycollected")){
234
                        day=eventContent.item(j).getTextContent().trim();
235
                    }
236
                    else  if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:decimallongitude")){
237
                        String tmp = eventContent.item(j).getTextContent().trim();
238
                        try{longitude=Double.valueOf(tmp);}catch(Exception e){logger.warn("longitude is not a number");}
239
                    }
240
                    else  if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:decimallatitude")){
241
                        String tmp = eventContent.item(j).getTextContent().trim();
242
                        try{latitude=Double.valueOf(tmp);}catch(Exception e){logger.warn("latitude is not a number");}
243
                    }else if(eventContent.item(j).getNodeName().equalsIgnoreCase("dwc:TypeStatus")){
244
                        type = eventContent.item(j).getTextContent().trim();
245
                    }else if(eventContent.item(j).getNodeName().equalsIgnoreCase("#text") && StringUtils.isBlank(eventContent.item(j).getTextContent())){
246
                        //do nothing
247
                    }
248
                    else {
249
                        logger.info("UNEXTRACTED FIELD FOR SPECIMEN "+eventContent.item(j).getNodeName()+", "+eventContent.item(j).getTextContent()) ;
250
                    }
251
                }
252
                if (!day.isEmpty() || !month.isEmpty() || !year.isEmpty()){
253
                    try{
254
                        if (!year.isEmpty()) {
255
                            tp = TimePeriod.NewInstance(Integer.parseInt(year));
256
                            if (!month.isEmpty()) {
257
                                tp.setStartMonth(Integer.parseInt(month));
258
                                if (!day.isEmpty()) {
259
                                    tp.setStartDay(Integer.parseInt(day));
260
                                }
261
                            }
262

    
263
                        }
264
                    }catch(Exception e){
265
                        logger.warn("Collection date error "+e);
266
                    }
267
                }
268
            }
269
            if(xmldata.item(n).getNodeName().equalsIgnoreCase("#text")){
270
                descr=xmldata.item(n).getTextContent().replaceAll(";","").trim();
271
                if (descr.length()>1 && containsDistinctLetters(descr)) {
272
                    specimenOrObservation.setDescr(descr);
273
                    asso=true;
274
                }
275
            }
276
            if(xmldata.item(n).getNodeName().equalsIgnoreCase("tax:p")){
277
                descr=xmldata.item(n).getTextContent().replaceAll(";","").trim();
278
                if (descr.length()>1 && containsDistinctLetters(descr)) {
279
                    specimenOrObservation.setDescr(descr);
280
                    asso=true;
281
                }
282
            }
283
        }
284
        //        if(asso && descr.length()>1){
285

    
286
        //            logger.info("DESCR: "+descr);
287
        if (!type.isEmpty()) {
288
            if (!containsDistinctLetters(type)) {
289
                type="no description text";
290
            }
291
            derivedUnitFacade = getFacade(type.replaceAll(";",""), defaultAssociation);
292
            SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
293

    
294
            if (typifiableName != null){
295
            	typifiableName.addTypeDesignation(designation, true);
296
            }else{
297
            	logger.warn("No typifiable name available");
298
            }
299
            SpecimenTypeDesignationStatus stds= getSpecimenTypeDesignationStatusByKey(type);
300
            if (stds !=null) {
301
                stds = (SpecimenTypeDesignationStatus) importer.getTermService().find(stds.getUuid());
302
            }
303

    
304
            designation.setTypeStatus(stds);
305
            derivedUnitFacade.innerDerivedUnit().addSpecimenTypeDesignation(designation);
306

    
307
            derivedUnitBase = derivedUnitFacade.innerDerivedUnit();
308
            // System.out.println("derivedUnitBase: "+derivedUnitBase);
309
            //                designation.setTypeSpecimen(derivedUnitBase);
310
            //                TaxonName name = taxon.getName();
311
            //                name.addTypeDesignation(designation, true);
312
        } else {
313
            if (!containsDistinctLetters(descr.replaceAll(";",""))) {
314
                descr="no description text";
315
            }
316

    
317
            derivedUnitFacade = getFacade(descr.replaceAll(";",""), defaultAssociation);
318
            derivedUnitBase = derivedUnitFacade.innerDerivedUnit();
319
            // System.out.println("derivedUnitBase2: "+derivedUnitBase);
320
        }
321

    
322
        unitsGatheringEvent = new UnitsGatheringEvent(importer.getTermService(), locality,collector,longitude, latitude,
323
                state2.getConfig(),importer.getAgentService());
324

    
325
        if(tp!=null) {
326
            unitsGatheringEvent.setGatheringDate(tp);
327
        }
328

    
329
        // country
330
        unitsGatheringArea = new UnitsGatheringArea();
331
        unitsGatheringArea.setParams(null, country, state2.getConfig(), importer.getTermService(), importer.getVocabularyService());
332
        //TODO other areas
333
        if (StringUtils.isNotBlank(stateprov)){
334
        	Map<String, String> namedAreas = new HashMap<>();
335
        	namedAreas.put(stateprov, null);
336
            unitsGatheringArea.setAreaNames(namedAreas, state2.getConfig(), importer.getTermService(), importer.getVocabularyService());
337
        }
338

    
339
        areaCountry =  unitsGatheringArea.getCountry();
340

    
341
        //                         // other areas
342
        //                         unitsGatheringArea = new UnitsGatheringArea(namedAreaList,dataHolder.getTermService());
343
        //                         ArrayList<DefinedTermBase> nas = unitsGatheringArea.getAreas();
344
        //                         for (DefinedTermBase namedArea : nas) {
345
        //                             unitsGatheringEvent.addArea(namedArea);
346
        //                         }
347

    
348
        // copy gathering event to facade
349
        GatheringEvent gatheringEvent = unitsGatheringEvent.getGatheringEvent();
350
        derivedUnitFacade.setGatheringEvent(gatheringEvent);
351
        derivedUnitFacade.setLocality(gatheringEvent.getLocality());
352
        derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
353
        derivedUnitFacade.setCollector(gatheringEvent.getCollector());
354
        derivedUnitFacade.setCountry((NamedArea)areaCountry);
355

    
356
        for(DefinedTermBase<?> area:unitsGatheringArea.getAreas()){
357
            derivedUnitFacade.addCollectingArea((NamedArea) area);
358
        }
359
        //                         derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
360

    
361
        // add fieldNumber
362
        if (fieldNumber != null) {
363
            derivedUnitFacade.setFieldNumber(fieldNumber);
364
        }
365
        specimenOrObservation.setDerivedUnitBase(derivedUnitBase);
366
        //        }
367
        return specimenOrObservation;
368
    }
369

    
370
    private SpecimenTypeDesignationStatus getSpecimenTypeDesignationStatusByKey(
371
            String key) {
372
        if (key == null) {
373
            return null;
374
        } else if (key.matches("(?i)(T|Type)")) {
375
            return SpecimenTypeDesignationStatus.TYPE();
376
        } else if (key.matches("(?i)(HT|Holotype)")) {
377
            return SpecimenTypeDesignationStatus.HOLOTYPE();
378
        } else if (key.matches("(?i)(LT|Lectotype)")) {
379
            return SpecimenTypeDesignationStatus.LECTOTYPE();
380
        } else if (key.matches("(?i)(NT|Neotype)")) {
381
            return SpecimenTypeDesignationStatus.NEOTYPE();
382
        } else if (key.matches("(?i)(ST|Syntype)")) {
383
            return SpecimenTypeDesignationStatus.SYNTYPE();
384
        } else if (key.matches("(?i)(ET|Epitype)")) {
385
            return SpecimenTypeDesignationStatus.EPITYPE();
386
        } else if (key.matches("(?i)(IT|Isotype)")) {
387
            return SpecimenTypeDesignationStatus.ISOTYPE();
388
        } else if (key.matches("(?i)(ILT|Isolectotype)")) {
389
            return SpecimenTypeDesignationStatus.ISOLECTOTYPE();
390
        } else if (key.matches("(?i)(INT|Isoneotype)")) {
391
            return SpecimenTypeDesignationStatus.ISONEOTYPE();
392
        } else if (key.matches("(?i)(IET|Isoepitype)")) {
393
            return SpecimenTypeDesignationStatus.ISOEPITYPE();
394
        } else if (key.matches("(?i)(PT|Paratype)")) {
395
            return SpecimenTypeDesignationStatus.PARATYPE();
396
        } else if (key.matches("(?i)(PLT|Paralectotype)")) {
397
            return SpecimenTypeDesignationStatus.PARALECTOTYPE();
398
        } else if (key.matches("(?i)(PNT|Paraneotype)")) {
399
            return SpecimenTypeDesignationStatus.PARANEOTYPE();
400
        } else if (key.matches("(?i)(unsp.|Unspecified)")) {
401
            return SpecimenTypeDesignationStatus.UNSPECIFIC();
402
        } else if (key.matches("(?i)(2LT|Second Step Lectotype)")) {
403
            return SpecimenTypeDesignationStatus.SECOND_STEP_LECTOTYPE();
404
        } else if (key.matches("(?i)(2NT|Second Step Neotype)")) {
405
            return SpecimenTypeDesignationStatus.SECOND_STEP_NEOTYPE();
406
        } else if (key.matches("(?i)(OM|Original Material)")) {
407
            return SpecimenTypeDesignationStatus.ORIGINAL_MATERIAL();
408
        } else if (key.matches("(?i)(IcT|Iconotype)")) {
409
            return SpecimenTypeDesignationStatus.ICONOTYPE();
410
        } else if (key.matches("(?i)(PT|Phototype)")) {
411
            return SpecimenTypeDesignationStatus.PHOTOTYPE();
412
        } else if (key.matches("(?i)(IST|Isosyntype)")) {
413
            return SpecimenTypeDesignationStatus.ISOSYNTYPE();
414
        } else {
415
            return null;
416
        }
417
    }
418
    protected DerivedUnitFacade getFacade(String recordBasis, SpecimenOrObservationType defaultAssoc) {
419
        // System.out.println("getFacade() for "+recordBasis+", defaultassociation: "+defaultAssoc);
420
        SpecimenOrObservationType type = null;
421

    
422
        // create specimen
423
        if (recordBasis != null) {
424
            String recordBasisL = recordBasis.toLowerCase();
425
            if (recordBasisL.startsWith("specimen") || recordBasisL.contains("specimen") || recordBasisL.contains("type")) {// specimen
426
                type = SpecimenOrObservationType.PreservedSpecimen;
427
            }
428
            if (recordBasisL.startsWith("observation")) {
429
                type = SpecimenOrObservationType.Observation;
430
            }
431
            if (recordBasisL.contains("fossil")) {
432
                type = SpecimenOrObservationType.Fossil;
433
            }
434

    
435
            if (recordBasisL.startsWith("living")) {
436
                type = SpecimenOrObservationType.LivingSpecimen;
437
            }
438
            if (type == null) {
439
                logger.info("The basis of record does not seem to be known: *" + recordBasisL+"*");
440
                type = defaultAssoc;
441
            }
442
            // TODO fossils?
443
        } else {
444
            logger.info("The basis of record is null");
445
            type = defaultAssoc;
446
        }
447
        DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(type);
448
        return derivedUnitFacade;
449
    }
450

    
451
    @SuppressWarnings("rawtypes")
452
    protected Feature makeFeature(SpecimenOrObservationBase unit) {
453
        if (unit == null){
454
            return null;
455
        }
456
        SpecimenOrObservationType type = unit.getRecordBasis();
457

    
458
        if (type.isFeatureObservation()){
459
            return Feature.OBSERVATION();
460
        }else if (type.isPreservedSpecimen() ||
461
                type == SpecimenOrObservationType.LivingSpecimen ||
462
                type == SpecimenOrObservationType.OtherSpecimen
463
                ){
464
            return Feature.SPECIMEN();
465
        }else if (type == SpecimenOrObservationType.Unknown ||
466
                type == SpecimenOrObservationType.DerivedUnit
467
                ) {
468
            return Feature.INDIVIDUALS_ASSOCIATION();
469
        }
470
        logger.warn("No feature defined for derived unit class: "
471
                + unit.getClass().getSimpleName());
472
        return null;
473
    }
474

    
475
    protected final static String SPLITTER = ",";
476

    
477
    protected  int askQuestion(String question){
478
        Scanner scan = new Scanner(System.in);
479
        logger.info(question);
480
        int index = scan.nextInt();
481
        return index;
482
    }
483

    
484
    protected Reference getReferenceWithType(int reftype) {
485
        Reference ref = null;
486
        switch (reftype) {
487
        case 1:
488
            ref = ReferenceFactory.newGeneric();
489
            break;
490
        case 2:
491
            IBook tmp= ReferenceFactory.newBook();
492
            ref = (Reference)tmp;
493
            break;
494
        case 3:
495
            ref = ReferenceFactory.newArticle();
496
            break;
497
        case 4:
498
            IBookSection tmp2 = ReferenceFactory.newBookSection();
499
            ref = (Reference)tmp2;
500
            break;
501
        case 5:
502
            ref = ReferenceFactory.newJournal();
503
            break;
504
        case 6:
505
            ref = ReferenceFactory.newPrintSeries();
506
            break;
507
        case 7:
508
            ref = ReferenceFactory.newThesis();
509
            break;
510
        default:
511
            break;
512
        }
513
        return ref;
514
    }
515

    
516
    protected void prepareCollectors(TaxonXImportState state,IAgentService agentService) {
517
        //        logger.info("PREPARE COLLECTORS");
518
        List<String> collectors = new ArrayList<>();
519
        String tmp;
520
        List<String> collectorsU = new ArrayList<>(new HashSet<>(collectors));
521
        Set<UUID> uuids = new HashSet<UUID>();
522

    
523
        //existing persons in DB
524
        List<UuidAndTitleCache<Person>> hiberPersons = agentService.getUuidAndTitleCache(Person.class, null, null);
525
        Map<String,Person> titleCachePerson = new HashMap<>();
526
        uuids = new HashSet<UUID>();
527
        for (UuidAndTitleCache<Person> hibernateP:hiberPersons){
528
            uuids.add(hibernateP.getUuid());
529
        }
530

    
531
        if (!uuids.isEmpty()){
532
            List<AgentBase> existingPersons = agentService.find(uuids);
533
            for (AgentBase existingP:existingPersons){
534
                titleCachePerson.put(existingP.getTitleCache(),CdmBase.deproxy(existingP, Person.class));
535
            }
536
        }
537

    
538
        Map<String,UUID> personMap = new HashMap<>();
539
        for (UuidAndTitleCache<Person> person:hiberPersons){
540
            personMap.put(person.getTitleCache(), person.getUuid());
541
        }
542

    
543
        java.util.Collection<AgentBase> personToadd = new ArrayList<>();
544

    
545
        for (String collector:collectorsU){
546
            Person p = Person.NewInstance();
547
            p.setTitleCache(collector,true);
548
            if (!personMap.containsKey(p.getTitleCache())){
549
                personToadd.add(p);
550
            }
551
        }
552

    
553
        if(!personToadd.isEmpty()){
554
            Map<UUID, AgentBase> uuuidPerson = agentService.save(personToadd);
555
            for (UUID u:uuuidPerson.keySet()){
556
                titleCachePerson.put(uuuidPerson.get(u).getTitleCache(), CdmBase.deproxy(uuuidPerson.get(u), Person.class));
557
            }
558
        }
559

    
560
        state.getConfig().setPersons(titleCachePerson);
561
    }
562

    
563
    protected String getFullReference(String name, List<ParserProblem> problems) {
564
        //        logger.info("getFullReference for "+ name);
565
        JTextArea textArea = new JTextArea("Complete the reference or the name '"+name+"'.\nThe current problem is "+StringUtils.join(problems,"--"));
566
        JScrollPane scrollPane = new JScrollPane(textArea);
567
        textArea.setLineWrap(true);
568
        textArea.setWrapStyleWord(true);
569
        scrollPane.setPreferredSize( new Dimension( 700, 70 ) );
570

    
571
        //        JFrame frame = new JFrame("I have a question");
572
        //        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
573
        String s = (String)JOptionPane.showInputDialog(
574
                null,
575
                scrollPane,
576
                "Get full reference or name",
577
                JOptionPane.PLAIN_MESSAGE,
578
                null,
579
                null,
580
                name);
581
        return s;
582
    }
583

    
584

    
585

    
586
    /**
587
     * @param name
588
     * @return
589
     * @throws TransformerException
590
     * @throws TransformerFactoryConfigurationError
591
     */
592
    protected String askWhichScientificName(String fullname,String atomised,String classificationName, Node fullParagraph) throws TransformerFactoryConfigurationError, TransformerException {
593
        //        logger.info("getScientificName for "+ fullname);
594
        //        JFrame frame = new JFrame("I have a question");
595
        //        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
596
        String k = fullname+"_"+atomised;
597

    
598
        String defaultN = "";
599
        if (atomised.length()>fullname.length()) {
600
            defaultN=atomised;
601
        } else {
602
            defaultN=fullname;
603
        }
604

    
605
        if (namesAsked.containsKey(k)){
606
            return namesAsked.get(k);
607
        }
608
        else{
609
            //activate it for ants because a lot of markup is incomplete
610
            if (classificationName.indexOf("Ants")>-1) {
611
                return defaultN;
612
            }
613

    
614
            JTextArea textArea = new JTextArea("The names in the free text and in the xml tags do not match : "+fullname+
615
                    ", or "+atomised+"\n"+formatNode(fullParagraph));
616
            JScrollPane scrollPane = new JScrollPane(textArea);
617
            textArea.setLineWrap(true);
618
            textArea.setWrapStyleWord(true);
619
            scrollPane.setPreferredSize( new Dimension( 700, 200 ) );
620
            String s = (String)JOptionPane.showInputDialog(
621
                    null,
622
                    scrollPane,
623
                    "Which name do I have to use? The current classification is "+classificationName,
624
                    JOptionPane.PLAIN_MESSAGE,
625
                    null,
626
                    null,
627
                    defaultN);
628
            namesAsked.put(k, s);
629
            return s;
630
        }
631
    }
632

    
633

    
634
    protected int askAddParent(String s){
635
        //        boolean hack=true;
636
        //        if (hack) {
637
        //            return 1;
638
        //        }
639
        JTextArea textArea = new JTextArea("If you want to add a parent taxa for "+s+", click \"Yes\"." +
640
                " If it is a root for this classification, click \"No\" or \"Cancel\".");
641
        JScrollPane scrollPane = new JScrollPane(textArea);
642
        textArea.setLineWrap(true);
643
        textArea.setWrapStyleWord(true);
644
        scrollPane.setPreferredSize( new Dimension( 600, 70 ) );
645

    
646
        Object[] options = { UIManager.getString("OptionPane.yesButtonText"),
647
                UIManager.getString("OptionPane.noButtonText")};
648

    
649

    
650
        int addTaxon = JOptionPane.showOptionDialog(null,
651
                scrollPane,
652
                "",
653
                JOptionPane.YES_NO_OPTION,
654
                0,
655
                null,
656
                options,
657
                options[1]);
658
        return addTaxon;
659
    }
660

    
661
    protected String askSetParent(String s){
662
        JTextArea textArea =  new JTextArea("What is the first taxon parent for "+s+"?\n"+
663
                "The rank will be asked later. ");
664
        JScrollPane scrollPane = new JScrollPane(textArea);
665
        textArea.setLineWrap(true);
666
        textArea.setWrapStyleWord(true);
667
        scrollPane.setPreferredSize( new Dimension( 700, 200 ) );
668

    
669
        String s2 = (String)JOptionPane.showInputDialog(
670
                null,
671
                scrollPane,
672
                "",
673
                JOptionPane.PLAIN_MESSAGE,
674
                null,
675
                null,
676
                s);
677
        return s2;
678
    }
679

    
680
    protected String askRank(String s, List<String> rankListStr){
681
        JTextArea  textArea = new JTextArea("What is the rank for "+s+"?");
682
        JScrollPane scrollPane = new JScrollPane(textArea);
683
        textArea.setLineWrap(true);
684
        textArea.setWrapStyleWord(true);
685
        scrollPane.setPreferredSize( new Dimension( 700, 200 ) );
686

    
687
        String r = (String)JOptionPane.showInputDialog(
688
                null,
689
                scrollPane,
690
                "",
691
                JOptionPane.PLAIN_MESSAGE,
692
                null,
693
                rankListStr.toArray(),
694
                null);
695
        return r;
696
    }
697

    
698
    /**
699
     * @param name
700
     * @return
701
     * @throws TransformerException
702
     * @throws TransformerFactoryConfigurationError
703
     */
704
    protected String askFeatureName(String paragraph){
705
        //        logger.info("getScientificName for "+ fullname);
706
        //        JFrame frame = new JFrame("I have a question");
707
        //        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
708
        JTextArea textArea = new JTextArea("How should the feature be named? \n"+paragraph);
709
        JScrollPane scrollPane = new JScrollPane(textArea);
710
        textArea.setLineWrap(true);
711
        textArea.setWrapStyleWord(true);
712
        scrollPane.setPreferredSize( new Dimension( 700, 200 ) );
713
        String s = (String)JOptionPane.showInputDialog(
714
                null,
715
                scrollPane,
716
                "",
717
                JOptionPane.PLAIN_MESSAGE,
718
                null,
719
                null,
720
                "Other");
721
        return s;
722
    }
723

    
724
    /**
725
     * @param taxonname2
726
     * @param bestMatchingTaxon
727
     * @param refMods
728
     * @param similarityAuthor
729
     * @return
730
     */
731
    protected boolean askIfReuseBestMatchingTaxon(INonViralName taxonname2, Taxon bestMatchingTaxon, Reference refMods, double similarityScore, double similarityAuthor) {
732
        Object[] options = { UIManager.getString("OptionPane.yesButtonText"),
733
                UIManager.getString("OptionPane.noButtonText")};
734

    
735
        if (similarityScore<0.66 &&  similarityAuthor<0.5) {
736
            return false;
737
            //            System.out.println("should say NO");
738
        }
739

    
740
        boolean sameSource=false;
741
        boolean noRef=false;
742

    
743
        String sec = refMods.getTitleCache();
744
        String secBest = "";
745
        try{
746
            secBest=bestMatchingTaxon.getSec().getTitleCache();
747
        }
748
        catch(NullPointerException e){
749
            logger.warn("no sec - ignore");
750
        }
751

    
752
        if (secBest.isEmpty()) {
753
            noRef=true;
754
        }
755

    
756
        Object defaultOption=options[1];
757
        if(sec.equalsIgnoreCase(secBest)
758
                //                ||                taxonname2.getTitleCache().split("sec.")[0].trim().equalsIgnoreCase(bestMatchingTaxon.getTitleCache().split("sec.")[0].trim())
759
                ) {
760
            //System.out.println(sec+" and "+secBest);
761
            sameSource=true;
762
            //-1 <=> no author
763
            if (similarityScore>0.65 && (similarityAuthor==-1 || similarityAuthor>0.8)) {
764
                defaultOption=options[0];
765
            } else {
766
                defaultOption=options[1];
767
            }
768
        } else {
769
            if (similarityScore>0.65 && similarityAuthor>0.8) {
770
                if(similarityScore==1 ) {
771
                    return true;
772
                }
773
                defaultOption=options[0];
774
            } else {
775
                defaultOption=options[1];
776
            }
777
        }
778

    
779
        String sourcesStr="";
780

    
781
        Set<IdentifiableSource> sources = bestMatchingTaxon.getSources();
782
        for (IdentifiableSource src:sources){
783
            try{
784
                String srcSec=src.getCitation().getTitleCache();
785
                if(!srcSec.isEmpty()){
786
                    sourcesStr+="\n "+srcSec;
787
                    if (srcSec.equalsIgnoreCase(sec)){
788
                        sameSource=true;
789
                        if (similarityScore>0.65 && similarityAuthor>0.8) {
790
                            defaultOption=options[0];
791
                        } else {
792
                            defaultOption=options[1];
793
                        }
794
                    }
795
                }
796
            }catch(Exception e){
797
                logger.warn("the source reference is maybe null, just ignore it.");
798
            }
799
        }
800

    
801
        if (sameSource && similarityScore>0.9999 && (similarityAuthor==-1 || similarityAuthor>0.8)) {
802
            return true;
803
        }
804
        if(similarityScore<0.66) {
805
            defaultOption=options[1];
806
        }
807

    
808
        //        //only activate it if you know the data you are importing (ok for Chenopodium)
809
        if(defaultOption==options[1]) {
810
            return false;
811
        }
812

    
813
        JTextArea textArea =null;
814
        if (!sourcesStr.isEmpty()) {
815
            textArea = new JTextArea("Does "+taxonname2.toString()+" correspond to "
816
                    + bestMatchingTaxon.toString()+" ?\n Click \"Yes\". if it does, click \"No\" if it does not."
817
                    + "\n The current sources are:"+ sourcesStr);
818
        } else {
819
            textArea = new JTextArea("Does "+taxonname2.toString()+" correspond to "
820
                    + bestMatchingTaxon.toString()+" ?\n Click \"Yes\". if it does, click \"No\" if it does not.");
821
        }
822
        JScrollPane scrollPane = new JScrollPane(textArea);
823
        textArea.setLineWrap(true);
824
        textArea.setWrapStyleWord(true);
825
        scrollPane.setPreferredSize( new Dimension( 600, 70 ) );
826

    
827
        int addTaxon = JOptionPane.showOptionDialog(null,
828
                scrollPane,
829
                refMods.toString(),
830
                JOptionPane.YES_NO_OPTION,
831
                0,
832
                null,
833
                options,
834
                defaultOption);
835
        if(addTaxon==1) {
836
            return false;
837
        } else {
838
            return true;
839
        }
840
    }
841

    
842
    /**
843
     * @param fullLineRefName
844
     * @return
845
     */
846
    protected int askIfNameContained(String fullLineRefName) {
847

    
848
        JTextArea textArea = new JTextArea("Is a scientific name contained in this sentence ? Type 0 if contains a name, 1 if it's only a reference. Press 2 if it's to be ignored \n"+fullLineRefName);
849
        JScrollPane scrollPane = new JScrollPane(textArea);
850
        textArea.setLineWrap(true);
851
        textArea.setWrapStyleWord(true);
852
        scrollPane.setPreferredSize( new Dimension( 600, 400 ) );
853

    
854
        String s = (String)JOptionPane.showInputDialog(
855
                null,
856
                scrollPane,
857
                "",
858
                JOptionPane.PLAIN_MESSAGE,
859
                null,
860
                null,
861
                "0");
862
        return Integer.valueOf(s);
863
    }
864

    
865

    
866
    /**
867
     * @param name
868
     * @return
869
     */
870
    protected Rank askForRank(String fullname,Rank rank, NomenclaturalCode nomenclaturalCode) {
871
        //        logger.info("askForRank for "+ fullname+ ", "+rank);
872
        //        JFrame frame = new JFrame("I have a question");
873
        //        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
874

    
875
        if (ranksAsked.containsKey(fullname)){
876
            return ranksAsked.get(fullname);
877
        }
878
        else{
879
            boolean np=false;
880
            int npi=0;
881
            Rank cR = null;
882

    
883
            while (!np && npi<2)
884
            {
885

    
886

    
887
                JTextArea textArea = new JTextArea("What is the correct rank for "+fullname+"?");
888
                JScrollPane scrollPane = new JScrollPane(textArea);
889
                textArea.setLineWrap(true);
890
                textArea.setWrapStyleWord(true);
891
                scrollPane.setPreferredSize( new Dimension( 600, 50 ) );
892

    
893
                List<Rank> rankList = new ArrayList<Rank>();
894
                rankList = importer.getTermService().list(Rank.class, null, null, null, null);
895

    
896
                List<String> rankListStr = new ArrayList<String>();
897
                for (Rank r:rankList) {
898
                    rankListStr.add(r.toString());
899
                }
900
                String s = (String)JOptionPane.showInputDialog(
901
                        null,
902
                        scrollPane,
903
                        "The rank extracted from the TaxonX file is "+rank.toString(),
904
                        JOptionPane.PLAIN_MESSAGE,
905
                        null,
906
                        rankListStr.toArray(),
907
                        rank.toString());
908

    
909

    
910
                try {
911
                    npi++;
912
                    cR = Rank.getRankByEnglishName(s,nomenclaturalCode,true);
913
                    np=true;
914
                } catch (UnknownCdmTypeException e) {
915
                    logger.warn("Unknown rank ?!"+s);
916
                    logger.warn(e);
917
                }
918
            }
919
            ranksAsked.put(fullname,cR);
920
            return cR;
921

    
922
        }
923
    }
924

    
925
    /**
926
     * ask user to specify what kind of paragraph the current "multiple" section is
927
     * default possibilities are "synonyms","material examined","distribution","image caption","other"
928
     * could make sense to replace this list with the CDM-Feature list
929
     * if "other" is selected, a second pop-up will be prompted to ask user to specify a new Feature name.
930
     * @param fullParagraph : the current Node
931
     * @return the section name
932
     * */
933
    protected String askMultiple(Node fullParagraph){
934
        String fp = "";
935
        try {
936
            fp = formatNode(fullParagraph);
937
        } catch (TransformerFactoryConfigurationError e1) {
938
            // TODO Auto-generated catch block
939
            e1.printStackTrace();
940
        } catch (TransformerException e1) {
941
            // TODO Auto-generated catch block
942
            e1.printStackTrace();
943
        }
944
        JTextArea textArea = new JTextArea("What category is it for this paragraph \n"+fp);
945
        JScrollPane scrollPane = new JScrollPane(textArea);
946
        textArea.setLineWrap(true);
947
        textArea.setWrapStyleWord(true);
948
        scrollPane.setPreferredSize( new Dimension( 600, 400 ) );
949

    
950
        String[] possiblities = {"synonyms","material examined","distribution","image caption","Other","vernacular name","type status","new category"};
951

    
952

    
953
        String s = (String)JOptionPane.showInputDialog(
954
                null,
955
                scrollPane,
956
                "",
957
                JOptionPane.PLAIN_MESSAGE,
958
                null,
959
                possiblities,
960
                "Other");
961

    
962
        if (s.equalsIgnoreCase("new category")) {
963
            try {
964
                s=askFeatureName(formatNode(fullParagraph));
965
            } catch (TransformerFactoryConfigurationError e) {
966
                logger.warn(e);
967
            } catch (TransformerException e) {
968
                logger.warn(e);
969
            }
970
        }
971
        return s;
972

    
973
    }
974

    
975

    
976

    
977
    /**
978
     * asks for the hierarchical parent, based on the current classification
979
     * @param taxon
980
     * @param classification
981
     * @return Taxon, the parent Taxon
982
     */
983
    protected Taxon askParent(Taxon taxon,Classification classification ) {
984
        // System.out.println("ASK PARENT "+classification);
985
        //        logger.info("ask Parent "+taxon.getTitleCache());
986
        Set<TaxonNode> allNodes = classification.getAllNodes();
987
        Map<String,Taxon> nodesMap = new HashMap<String, Taxon>();
988

    
989
        for (TaxonNode tn:allNodes){
990
            Taxon t = tn.getTaxon();
991
            nodesMap.put(t.getTitleCache(), t);
992
        }
993
        List<String> nodeList = new ArrayList<String>();
994
        for (String nl : nodesMap.keySet()) {
995
            nodeList.add(nl+" - "+nodesMap.get(nl).getName().getRank());
996
        }
997
        Collections.sort(nodeList);
998
        nodeList.add(0, "Not here!");
999

    
1000
        JFrame frame = new JFrame("I have a question");
1001
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1002
        String s = (String)JOptionPane.showInputDialog(
1003
                frame,
1004
                "What is the taxon parent for "+taxon.getTitleCache()+"?",
1005
                "The current classification is "+classification.getTitleCache(),
1006
                JOptionPane.PLAIN_MESSAGE,
1007
                null,
1008
                nodeList.toArray(),
1009
                "Not here!");
1010

    
1011
        Taxon returnTaxon = nodesMap.get(s.split(" - ")[0]);
1012
        //        logger.info("ask Parent returns "+s);
1013
        return returnTaxon;
1014
    }
1015

    
1016

    
1017
    /**
1018
     *
1019
     * @param r: the rank as string (with dwc tags)
1020
     * @return Rank : the Rank object corresponding to the current string
1021
     *
1022
     */
1023
    protected Rank getRank(String r){
1024
        if (r==null) {
1025
            r=Rank.UNKNOWN_RANK().toString();
1026
        }
1027
        r=r.replace("dwcranks:", "");
1028
        r =r.replace("dwc:","");
1029

    
1030
        Rank rank = Rank.UNKNOWN_RANK();
1031
        if (r.equalsIgnoreCase("Superfamily")) {
1032
            rank=Rank.SUPERFAMILY();
1033
        }
1034
        else if (r.equalsIgnoreCase("Family")) {
1035
            rank=Rank.FAMILY();
1036
        }
1037
        else if (r.equalsIgnoreCase("Subfamily")) {
1038
            rank=Rank.SUBFAMILY();
1039
        }
1040
        else if (r.equalsIgnoreCase("Tribe")) {
1041
            rank=Rank.TRIBE();
1042
        }
1043
        else if (r.equalsIgnoreCase("Subtribe")) {
1044
            rank=Rank.SUBTRIBE();
1045
        }
1046
        else if (r.equalsIgnoreCase("Genus")) {
1047
            rank=Rank.GENUS();
1048
        }
1049
        else if (r.equalsIgnoreCase("Subgenus")) {
1050
            rank=Rank.SUBGENUS();
1051
        }
1052
        else if (r.equalsIgnoreCase("Section")) {
1053
            rank=Rank.SECTION_BOTANY();
1054
        }
1055
        else if (r.equalsIgnoreCase("Subsection")) {
1056
            rank=Rank.SUBSECTION_BOTANY();
1057
        }
1058
        else if (r.equalsIgnoreCase("Series")) {
1059
            rank=Rank.SERIES();
1060
        }
1061
        else if (r.equalsIgnoreCase("Subseries")) {
1062
            rank=Rank.SUBSERIES();
1063
        }
1064
        else if (r.equalsIgnoreCase("Species")) {
1065
            rank=Rank.SPECIES();
1066
        }
1067
        else if (r.equalsIgnoreCase("Subspecies")) {
1068
            rank=Rank.SUBSPECIES();
1069
        }
1070
        else if (r.equalsIgnoreCase("Variety") || r.equalsIgnoreCase("varietyEpithet")) {
1071
            rank=Rank.VARIETY();
1072
        }
1073
        else if (r.equalsIgnoreCase("Subvariety")) {
1074
            rank=Rank.SUBVARIETY();
1075
        }
1076
        else if (r.equalsIgnoreCase("Form")) {
1077
            rank=Rank.FORM();
1078
        }
1079
        else if (r.equalsIgnoreCase("Subform")) {
1080
            rank=Rank.SUBFORM();
1081
        }else if (r.equalsIgnoreCase("higher")) {
1082
//            rank=Rank.SUPRAGENERICTAXON();
1083
        	logger.warn("handling of 'higher' rank still unclear");
1084
        }
1085

    
1086
        return rank;
1087
    }
1088

    
1089

    
1090
    /**
1091
     * @param ato: atomised taxon name data
1092
     * @return rank present in the xmldata fields
1093
     */
1094
    protected Rank getRank(Map<String, String> ato) {
1095
        Rank rank=Rank.UNKNOWN_RANK();
1096

    
1097
        if (ato == null) {
1098
            return rank;
1099
        }
1100
        if (ato.containsKey("dwc:family")){
1101
            rank=Rank.FAMILY();
1102
        }
1103
        if (ato.containsKey("dwc:tribe") || ato.containsKey("dwcranks:tribe")){
1104
            rank=Rank.TRIBE();
1105
        }
1106
        if (ato.containsKey("dwc:genus")) {
1107
            rank= Rank.GENUS();
1108
        }
1109
        if (ato.containsKey("dwc:subgenus")) {
1110
            rank= Rank.SUBGENUS();
1111
        }
1112
        if (ato.containsKey("dwc:specificepithet") || ato.containsKey("dwc:species")) {
1113
            rank= Rank.SPECIES();
1114
        }
1115
        if (ato.containsKey("dwc:infraspecificepithet")) {
1116
            rank= Rank.INFRASPECIES();
1117
        }
1118
        if (ato.containsKey("dwcranks:varietyepithet")) {
1119
            rank=Rank.VARIETY();
1120
        }
1121
        //popUp(rank.getTitleCache());
1122
        return rank;
1123
    }
1124

    
1125
    /**
1126
     * Format a XML node for a clean (screen) output with tags
1127
     * @param Node : the node to format
1128
     * @return String : the XML section formated for a screen output
1129
     * */
1130

    
1131
    protected String formatNode(Node node) throws TransformerFactoryConfigurationError, TransformerException{
1132
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
1133
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
1134
        //initialize StreamResult with File object to save to file
1135
        StreamResult result = new StreamResult(new StringWriter());
1136
        DOMSource source = new DOMSource(node);
1137
        transformer.transform(source, result);
1138
        String xmlString = result.getWriter().toString();
1139
        return xmlString;
1140
    }
1141

    
1142
    protected boolean containsDistinctLetters(String word){
1143
        Set<Character> dl = new HashSet<>();
1144
        for (char a: word.toCharArray()) {
1145
            dl.add(a);
1146
        }
1147
        if(dl.size()>1 && word.indexOf("no description text")==-1) {
1148
            return true;
1149
        } else {
1150
            return false;
1151
        }
1152
    }
1153

    
1154
    /**
1155
     * Tries to match the status string against any new name status
1156
     * and returns the status if it matches. Returns <code>null</code> otherwise.
1157
     * @param status
1158
     * @return
1159
     */
1160
    protected String newNameStatus(String status){
1161
    	String pattern = "(" + "((sp|spec|gen|comb|)\\.\\s*nov.)" +
1162
    				"|(new\\s*(species|combination))" +
1163
    				"|(n\\.\\s*sp\\.)" +
1164
    				"|(sp\\.\\s*n\\.)" +
1165
    				")";
1166
    	if (status.trim().matches(pattern)){
1167
    		//FIXME
1168
    		return null;
1169
//    		return status;
1170
    	}else{
1171
    		return null;
1172
    	}
1173
    }
1174

    
1175
    /** Creates an cdm-NomenclaturalCode by the tcs NomenclaturalCode
1176
     */
1177
    protected NomenclaturalStatusType nomStatusString2NomStatus (String nomStatus) throws UnknownCdmTypeException{
1178

    
1179
        if (nomStatus == null){ return null;
1180
        }else if ("Valid".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.VALID();
1181

    
1182
        }else if ("Alternative".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ALTERNATIVE();
1183
        }else if ("nom. altern.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ALTERNATIVE();
1184

    
1185
        }else if ("Ambiguous".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.AMBIGUOUS();
1186

    
1187
        }else if ("Doubtful".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.DOUBTFUL();
1188

    
1189
        }else if ("Confusum".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.CONFUSUM();
1190

    
1191
        }else if ("Illegitimate".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ILLEGITIMATE();
1192
        }else if ("nom. illeg.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ILLEGITIMATE();
1193

    
1194
        }else if ("Superfluous".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.SUPERFLUOUS();
1195
        }else if ("nom. superfl.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.SUPERFLUOUS();
1196

    
1197
        }else if ("Rejected".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.REJECTED();
1198
        }else if ("nom. rej.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.REJECTED();
1199

    
1200
        }else if ("Utique Rejected".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.UTIQUE_REJECTED();
1201

    
1202
        }else if ("Conserved Prop".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.CONSERVED_PROP();
1203

    
1204
        }else if ("Orthography Conserved Prop".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ORTHOGRAPHY_CONSERVED_PROP();
1205

    
1206
        }else if ("Legitimate".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.LEGITIMATE();
1207

    
1208
        }else if ("Novum".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.NOVUM();
1209
        }else if ("nom. nov.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.NOVUM();
1210

    
1211
        }else if ("Utique Rejected Prop".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.UTIQUE_REJECTED_PROP();
1212

    
1213
        }else if ("Orthography Conserved".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ORTHOGRAPHY_CONSERVED();
1214

    
1215
        }else if ("Rejected Prop".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.REJECTED_PROP();
1216

    
1217
        }else if ("Conserved".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.CONSERVED();
1218
        }else if ("nom. cons.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.CONSERVED();
1219

    
1220
        }else if ("Sanctioned".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.SANCTIONED();
1221

    
1222
        }else if ("Invalid".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.INVALID();
1223
        }else if ("nom. inval.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.INVALID();
1224

    
1225
        }else if ("Nudum".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.NUDUM();
1226
        }else if ("nom. nud.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.NUDUM();
1227

    
1228
        }else if ("Combination Invalid".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.COMBINATION_INVALID();
1229

    
1230
        }else if ("Provisional".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.PROVISIONAL();
1231
        }else if ("nom. provis.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.PROVISIONAL();
1232
        }
1233
        else {
1234
            throw new UnknownCdmTypeException("Unknown Nomenclatural status type " + nomStatus);
1235
        }
1236
    }
1237

    
1238
    //TypeDesignation
1239
    protected  SpecimenTypeDesignationStatus typeStatusId2TypeStatus (int typeStatusId)  throws UnknownCdmTypeException{
1240
        switch (typeStatusId){
1241
        case 0: return null;
1242
        case 1: return SpecimenTypeDesignationStatus.HOLOTYPE();
1243
        case 2: return SpecimenTypeDesignationStatus.LECTOTYPE();
1244
        case 3: return SpecimenTypeDesignationStatus.NEOTYPE();
1245
        case 4: return SpecimenTypeDesignationStatus.EPITYPE();
1246
        case 5: return SpecimenTypeDesignationStatus.ISOLECTOTYPE();
1247
        case 6: return SpecimenTypeDesignationStatus.ISONEOTYPE();
1248
        case 7: return SpecimenTypeDesignationStatus.ISOTYPE();
1249
        case 8: return SpecimenTypeDesignationStatus.PARANEOTYPE();
1250
        case 9: return SpecimenTypeDesignationStatus.PARATYPE();
1251
        case 10: return SpecimenTypeDesignationStatus.SECOND_STEP_LECTOTYPE();
1252
        case 11: return SpecimenTypeDesignationStatus.SECOND_STEP_NEOTYPE();
1253
        case 12: return SpecimenTypeDesignationStatus.SYNTYPE();
1254
        case 21: return SpecimenTypeDesignationStatus.ICONOTYPE();
1255
        case 22: return SpecimenTypeDesignationStatus.PHOTOTYPE();
1256
        default: {
1257
            throw new UnknownCdmTypeException("Unknown TypeDesignationStatus (id=" + Integer.valueOf(typeStatusId).toString() + ")");
1258
        }
1259
        }
1260
    }
1261
}
(3-3/9)