Project

General

Profile

« Previous | Next » 

Revision 113008ea

Added by Andreas Müller almost 6 years ago

cleanup

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/gbif/in/GbifImport.java
290 290
    }
291 291

  
292 292

  
293
@Override
294
protected void handleSingleUnit(SpecimenImportStateBase<SpecimenImportConfiguratorBase, SpecimenImportStateBase> state,
295
        Object itemObject){
296
    GbifResponse item;
297
    if (itemObject instanceof GbifResponse){
298
        item = (GbifResponse) itemObject;
299
    } else{
300
        logger.error("For Gbif Import the item has to be of type GbifResponse.");
301
        return;
302
    }
303
    if (logger.isDebugEnabled()) {
304
        logger.info("handleSingleUnit "+state.getRef());
305
    }
306

  
307
        ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
308
        if(cdmAppController==null){
309
            cdmAppController = this;
293
    @Override
294
    protected void handleSingleUnit(SpecimenImportStateBase<SpecimenImportConfiguratorBase, SpecimenImportStateBase> state,
295
            Object itemObject){
296
        GbifResponse item;
297
        if (itemObject instanceof GbifResponse){
298
            item = (GbifResponse) itemObject;
299
        } else{
300
            logger.error("For Gbif Import the item has to be of type GbifResponse.");
301
            return;
310 302
        }
311
        //check if unit already exists
312
        DerivedUnitFacade derivedUnitFacade;
313
        derivedUnitFacade = item.getDerivedUnitFacade();
314
        state.setDerivedUnitBase(derivedUnitFacade.innerDerivedUnit());
315
        TaxonName bestMatchingName =  findBestMatchingNames(item, state);
316
        if (bestMatchingName == null){
317
            bestMatchingName = item.getScientificName();
303
        if (logger.isDebugEnabled()) {
304
            logger.info("handleSingleUnit "+state.getRef());
318 305
        }
319
        if (bestMatchingName != null){
320
            Taxon taxon = getOrCreateTaxonForName(bestMatchingName, state);
321
            if (state.getConfig().isAddIndividualsAssociationsSuchAsSpecimenAndObservations()) {
322
                //do not add IndividualsAssociation to non-preferred taxa
323
                if(logger.isDebugEnabled()){
324
                    logger.info("isDoCreateIndividualsAssociations");
325
                }
326
                for (DeterminationEvent determinationEvent:derivedUnitFacade.getDeterminations()){
327
                    makeIndividualsAssociation(state, taxon, determinationEvent);
328
                }
329 306

  
330
                save(state.getDerivedUnitBase(), state);
307
            ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
308
            if(cdmAppController==null){
309
                cdmAppController = this;
310
            }
311
            //check if unit already exists
312
            DerivedUnitFacade derivedUnitFacade;
313
            derivedUnitFacade = item.getDerivedUnitFacade();
314
            state.setDerivedUnitBase(derivedUnitFacade.innerDerivedUnit());
315
            TaxonName bestMatchingName =  findBestMatchingNames(item, state);
316
            if (bestMatchingName == null){
317
                bestMatchingName = item.getScientificName();
318
            }
319
            if (bestMatchingName != null){
320
                Taxon taxon = getOrCreateTaxonForName(bestMatchingName, state);
321
                if (state.getConfig().isAddIndividualsAssociationsSuchAsSpecimenAndObservations()) {
322
                    //do not add IndividualsAssociation to non-preferred taxa
323
                    if(logger.isDebugEnabled()){
324
                        logger.info("isDoCreateIndividualsAssociations");
325
                    }
326
                    for (DeterminationEvent determinationEvent:derivedUnitFacade.getDeterminations()){
327
                        makeIndividualsAssociation(state, taxon, determinationEvent);
328
                    }
329

  
330
                    save(state.getDerivedUnitBase(), state);
331
                }
331 332
            }
332
        }
333 333

  
334 334

  
335 335

  
336
        // handle collection data
337
        handleCollectionData(state, derivedUnitFacade);
338
        save(item.getDerivedUnitFacade().baseUnit(), state);
339
        save(item.getDerivedUnitFacade().getFieldUnit(false), state);
340
        importAssociatedUnits(state, item, derivedUnitFacade);
341
        /*
342
        if(state.getConfig().isIgnoreImportOfExistingSpecimens()){
343
            String[] tripleId = item.getTripleID();
344
            SpecimenOrObservationBase<?> existingSpecimen = findExistingSpecimen(tripleId[0], state);
345
            DerivedUnitFacade derivedUnitFacade;
346
            if(existingSpecimen!=null && existingSpecimen.isInstanceOf(DerivedUnit.class)){
347
                DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(existingSpecimen, DerivedUnit.class);
348
                state.setDerivedUnitBase(derivedUnit);
349
                derivedUnitFacade = item.getDerivedUnitFacade();
350
                List<NonViralName> names = findExistingNames(item.getScientificName().getNameCache(), state);
351
                if (!names.isEmpty()){
352
                    findBestMatchingName(names, item);
336
            // handle collection data
337
            handleCollectionData(state, derivedUnitFacade);
338
            save(item.getDerivedUnitFacade().baseUnit(), state);
339
            save(item.getDerivedUnitFacade().getFieldUnit(false), state);
340
            importAssociatedUnits(state, item, derivedUnitFacade);
341
            /*
342
            if(state.getConfig().isIgnoreImportOfExistingSpecimens()){
343
                String[] tripleId = item.getTripleID();
344
                SpecimenOrObservationBase<?> existingSpecimen = findExistingSpecimen(tripleId[0], state);
345
                DerivedUnitFacade derivedUnitFacade;
346
                if(existingSpecimen!=null && existingSpecimen.isInstanceOf(DerivedUnit.class)){
347
                    DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(existingSpecimen, DerivedUnit.class);
348
                    state.setDerivedUnitBase(derivedUnit);
349
                    derivedUnitFacade = item.getDerivedUnitFacade();
350
                    List<NonViralName> names = findExistingNames(item.getScientificName().getNameCache(), state);
351
                    if (!names.isEmpty()){
352
                        findBestMatchingName(names, item);
353
                    }
354
                    save(item.getDerivedUnitFacade().baseUnit(), state);
355
                    importAssociatedUnits(state, item, derivedUnitFacade);
356
                    state.getReport().addAlreadyExistingSpecimen(SpecimenImportUtility.getUnitID(derivedUnit, state.getConfig()), derivedUnit);
357
                    return;
353 358
                }
354
                save(item.getDerivedUnitFacade().baseUnit(), state);
355
                importAssociatedUnits(state, item, derivedUnitFacade);
356
                state.getReport().addAlreadyExistingSpecimen(SpecimenImportUtility.getUnitID(derivedUnit, state.getConfig()), derivedUnit);
357
                return;
358 359
            }
359
        }
360 360

  
361
        //import new specimen
362

  
363
        // import DNA unit
364
        //TODO!!!!
365
        if(state.getDataHolder().getKindOfUnit()!=null && state.getDataHolder().getKindOfUnit().equalsIgnoreCase("dna")){
366
            GbifDnaParser dnaParser = new GbifDnaParser(state.getPrefix(), state.getReport(), state.getCdmRepository());
367
            DnaSample dnaSample = dnaParser.parse(item, state);
368
            save(dnaSample, state);
369
            //set dna as derived unit to avoid creating an extra specimen for this dna sample (instead just the field unit will be created)
370
            state.setDerivedUnitBase(dnaSample);
371
            derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
372
        }
373
        else{
374
            // create facade
375
            derivedUnitFacade = getFacade(state);
376
            state.setDerivedUnitBase(derivedUnitFacade.innerDerivedUnit());
377
     //   }
378

  
379
        /**
380
         * GATHERING EVENT
381

  
382
        // gathering event
383
        UnitsGatheringEvent<GbifImportConfigurator> unitsGatheringEvent =
384
               /* new UnitsGatheringEvent<GbifImportConfigurator>(cdmAppController.getTermService(),
385
                state.getDataHolder().locality, null, state.getDataHolder().decimalLongitude,
386
                state.getDataHolder().decimalLatitude, state.getDataHolder().getGatheringElevationText(),
387
                state.getDataHolder().getGatheringElevationMin(), state.getDataHolder().getGatheringElevationMax(),
388
                state.getDataHolder().getGatheringElevationUnit(), state.getDataHolder().getGatheringDateText(),
389
                state.getDataHolder().getGatheringNotes(), state.getTransformer().getReferenceSystemByKey(
390
                        state.getDataHolder().getGatheringSpatialDatum()), state.getDataHolder().gatheringAgentList,
391
                state.getDataHolder().gatheringTeamList, state.getConfig());
392

  
393
        // country
394
        UnitsGatheringArea unitsGatheringArea = new UnitsGatheringArea();
395
        //  unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(), getTermService());
396
        unitsGatheringArea.setParams(state.getDataHolder().countryCode, state.getDataHolder().country, state.getConfig(), cdmAppController.getTermService(), cdmAppController.getOccurrenceService());
397

  
398
        DefinedTermBase<?> areaCountry =  unitsGatheringArea.getCountry();
399

  
400
        // other areas
401
        unitsGatheringArea = new UnitsGatheringArea();
402
        //            unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),getTermService());
403
        unitsGatheringArea.setAreas(state.getDataHolder().getNamedAreaList(),state.getConfig(), cdmAppController.getTermService(), cdmAppController.getVocabularyService());
404
        ArrayList<DefinedTermBase> nas = unitsGatheringArea.getAreas();
405
        for (DefinedTermBase namedArea : nas) {
406
            unitsGatheringEvent.addArea(namedArea);
407
        }
361
            //import new specimen
362

  
363
            // import DNA unit
364
            //TODO!!!!
365
            if(state.getDataHolder().getKindOfUnit()!=null && state.getDataHolder().getKindOfUnit().equalsIgnoreCase("dna")){
366
                GbifDnaParser dnaParser = new GbifDnaParser(state.getPrefix(), state.getReport(), state.getCdmRepository());
367
                DnaSample dnaSample = dnaParser.parse(item, state);
368
                save(dnaSample, state);
369
                //set dna as derived unit to avoid creating an extra specimen for this dna sample (instead just the field unit will be created)
370
                state.setDerivedUnitBase(dnaSample);
371
                derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
372
            }
373
            else{
374
                // create facade
375
                derivedUnitFacade = getFacade(state);
376
                state.setDerivedUnitBase(derivedUnitFacade.innerDerivedUnit());
377
         //   }
378

  
379
            /**
380
             * GATHERING EVENT
381

  
382
            // gathering event
383
            UnitsGatheringEvent<GbifImportConfigurator> unitsGatheringEvent =
384
                   /* new UnitsGatheringEvent<GbifImportConfigurator>(cdmAppController.getTermService(),
385
                    state.getDataHolder().locality, null, state.getDataHolder().decimalLongitude,
386
                    state.getDataHolder().decimalLatitude, state.getDataHolder().getGatheringElevationText(),
387
                    state.getDataHolder().getGatheringElevationMin(), state.getDataHolder().getGatheringElevationMax(),
388
                    state.getDataHolder().getGatheringElevationUnit(), state.getDataHolder().getGatheringDateText(),
389
                    state.getDataHolder().getGatheringNotes(), state.getTransformer().getReferenceSystemByKey(
390
                            state.getDataHolder().getGatheringSpatialDatum()), state.getDataHolder().gatheringAgentList,
391
                    state.getDataHolder().gatheringTeamList, state.getConfig());
392

  
393
            // country
394
            UnitsGatheringArea unitsGatheringArea = new UnitsGatheringArea();
395
            //  unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(), getTermService());
396
            unitsGatheringArea.setParams(state.getDataHolder().countryCode, state.getDataHolder().country, state.getConfig(), cdmAppController.getTermService(), cdmAppController.getOccurrenceService());
397

  
398
            DefinedTermBase<?> areaCountry =  unitsGatheringArea.getCountry();
399

  
400
            // other areas
401
            unitsGatheringArea = new UnitsGatheringArea();
402
            //            unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),getTermService());
403
            unitsGatheringArea.setAreas(state.getDataHolder().getNamedAreaList(),state.getConfig(), cdmAppController.getTermService(), cdmAppController.getVocabularyService());
404
            ArrayList<DefinedTermBase> nas = unitsGatheringArea.getAreas();
405
            for (DefinedTermBase namedArea : nas) {
406
                unitsGatheringEvent.addArea(namedArea);
407
            }
408 408

  
409
        // copy gathering event to facade
410
        GatheringEvent gatheringEvent = unitsGatheringEvent.getGatheringEvent();
411
        derivedUnitFacade.setLocality(gatheringEvent.getLocality());
412
        derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
413
        derivedUnitFacade.setCollector(gatheringEvent.getCollector());
414
        derivedUnitFacade.setCountry((NamedArea)areaCountry);
415
        derivedUnitFacade.setAbsoluteElevationText(gatheringEvent.getAbsoluteElevationText());
416
        derivedUnitFacade.setAbsoluteElevation(gatheringEvent.getAbsoluteElevation());
417
        derivedUnitFacade.setAbsoluteElevationMax(gatheringEvent.getAbsoluteElevationMax());
418
        derivedUnitFacade.setGatheringPeriod(gatheringEvent.getTimeperiod());
419

  
420
        for(DefinedTermBase<?> area:unitsGatheringArea.getAreas()){
421
            derivedUnitFacade.addCollectingArea((NamedArea) area);
422
        }
423
        //            derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
424
        // TODO exsiccatum
425

  
426
        // add fieldNumber
427
        derivedUnitFacade.setFieldNumber(NB(state.getDataHolder().getFieldNumber()));
428

  
429
        // add unitNotes
430
        derivedUnitFacade.addAnnotation(Annotation.NewDefaultLanguageInstance(NB(state.getDataHolder().getUnitNotes())));
431

  
432
        // //add Multimedia URLs
433
        if (state.getDataHolder().getMultimediaObjects().size() != -1) {
434
            for (String multimediaObject : state.getDataHolder().getMultimediaObjects()) {
435
                Media media;
436
                try {
437
                    media = getImageMedia(multimediaObject, READ_MEDIA_DATA);
438
                    derivedUnitFacade.addDerivedUnitMedia(media);
439
                    if(state.getConfig().isAddMediaAsMediaSpecimen()){
440
                        //add media also as specimen scan
441
                        MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(SpecimenOrObservationType.Media);
442
                        mediaSpecimen.setMediaSpecimen(media);
443
                        DefinedTermBase specimenScanTerm = getTermService().load(SPECIMEN_SCAN_TERM);
444
                        if(specimenScanTerm instanceof DefinedTerm){
445
                            mediaSpecimen.setKindOfUnit((DefinedTerm) specimenScanTerm);
409
            // copy gathering event to facade
410
            GatheringEvent gatheringEvent = unitsGatheringEvent.getGatheringEvent();
411
            derivedUnitFacade.setLocality(gatheringEvent.getLocality());
412
            derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
413
            derivedUnitFacade.setCollector(gatheringEvent.getCollector());
414
            derivedUnitFacade.setCountry((NamedArea)areaCountry);
415
            derivedUnitFacade.setAbsoluteElevationText(gatheringEvent.getAbsoluteElevationText());
416
            derivedUnitFacade.setAbsoluteElevation(gatheringEvent.getAbsoluteElevation());
417
            derivedUnitFacade.setAbsoluteElevationMax(gatheringEvent.getAbsoluteElevationMax());
418
            derivedUnitFacade.setGatheringPeriod(gatheringEvent.getTimeperiod());
419

  
420
            for(DefinedTermBase<?> area:unitsGatheringArea.getAreas()){
421
                derivedUnitFacade.addCollectingArea((NamedArea) area);
422
            }
423
            //            derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
424
            // TODO exsiccatum
425

  
426
            // add fieldNumber
427
            derivedUnitFacade.setFieldNumber(NB(state.getDataHolder().getFieldNumber()));
428

  
429
            // add unitNotes
430
            derivedUnitFacade.addAnnotation(Annotation.NewDefaultLanguageInstance(NB(state.getDataHolder().getUnitNotes())));
431

  
432
            // //add Multimedia URLs
433
            if (state.getDataHolder().getMultimediaObjects().size() != -1) {
434
                for (String multimediaObject : state.getDataHolder().getMultimediaObjects()) {
435
                    Media media;
436
                    try {
437
                        media = getImageMedia(multimediaObject, READ_MEDIA_DATA);
438
                        derivedUnitFacade.addDerivedUnitMedia(media);
439
                        if(state.getConfig().isAddMediaAsMediaSpecimen()){
440
                            //add media also as specimen scan
441
                            MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(SpecimenOrObservationType.Media);
442
                            mediaSpecimen.setMediaSpecimen(media);
443
                            DefinedTermBase specimenScanTerm = getTermService().load(SPECIMEN_SCAN_TERM);
444
                            if(specimenScanTerm instanceof DefinedTerm){
445
                                mediaSpecimen.setKindOfUnit((DefinedTerm) specimenScanTerm);
446
                            }
447
                            DerivationEvent derivationEvent = DerivationEvent.NewInstance(DerivationEventType.PREPARATION());
448
                            derivationEvent.addDerivative(mediaSpecimen);
449
                            derivedUnitFacade.innerDerivedUnit().addDerivationEvent(derivationEvent);
446 450
                        }
447
                        DerivationEvent derivationEvent = DerivationEvent.NewInstance(DerivationEventType.PREPARATION());
448
                        derivationEvent.addDerivative(mediaSpecimen);
449
                        derivedUnitFacade.innerDerivedUnit().addDerivationEvent(derivationEvent);
451

  
452
                    } catch (MalformedURLException e) {
453
                        // TODO Auto-generated catch block
454
                        e.printStackTrace();
450 455
                    }
451 456

  
452
                } catch (MalformedURLException e) {
453
                    // TODO Auto-generated catch block
454
                    e.printStackTrace();
455 457
                }
456

  
457 458
            }
458
        }
459 459

  
460
        //          /*
461
        //           * merge AND STORE DATA
462
        //
463
        //          getTermService().saveOrUpdate(areaCountry);// TODO save area sooner
464
        //
465
        //          for (NamedArea area : otherAreas) {
466
        //              getTermService().saveOrUpdate(area);// merge it sooner (foreach area)
467
        //          }
460
            //          /*
461
            //           * merge AND STORE DATA
462
            //
463
            //          getTermService().saveOrUpdate(areaCountry);// TODO save area sooner
464
            //
465
            //          for (NamedArea area : otherAreas) {
466
            //              getTermService().saveOrUpdate(area);// merge it sooner (foreach area)
467
            //          }
468 468

  
469
        save(unitsGatheringEvent.getLocality(), state);
469
            save(unitsGatheringEvent.getLocality(), state);
470 470

  
471
        // handle collection data
472
        setCollectionData(state, derivedUnitFacade);
471
            // handle collection data
472
            setCollectionData(state, derivedUnitFacade);
473 473

  
474
        //Reference stuff
475
       // SpecimenUserInteraction sui = state.getConfig().getSpecimenUserInteraction();
476
        Map<String,OriginalSourceBase<?>> sourceMap = new HashMap<String, OriginalSourceBase<?>>();
474
            //Reference stuff
475
           // SpecimenUserInteraction sui = state.getConfig().getSpecimenUserInteraction();
476
            Map<String,OriginalSourceBase<?>> sourceMap = new HashMap<String, OriginalSourceBase<?>>();
477 477

  
478
        state.getDataHolder().docSources = new ArrayList<String>();
479
        for (String[] fullReference : state.getDataHolder().referenceList) {
480
            String strReference=fullReference[0];
481
            String citationDetail = fullReference[1];
482
            String citationURL = fullReference[2];
478
            state.getDataHolder().docSources = new ArrayList<String>();
479
            for (String[] fullReference : state.getDataHolder().referenceList) {
480
                String strReference=fullReference[0];
481
                String citationDetail = fullReference[1];
482
                String citationURL = fullReference[2];
483 483

  
484
            if (!citationURL.isEmpty()) {
485
                citationDetail+=", "+citationURL;
486
            }
484
                if (!citationURL.isEmpty()) {
485
                    citationDetail+=", "+citationURL;
486
                }
487 487

  
488
            Reference reference;
489
            if(strReference.equals(state.getRef().getTitleCache())){
490
                reference = state.getRef();
491
            }
492
            else{
493
                reference = ReferenceFactory.newGeneric();
494
                reference.setTitle(strReference);
495
            }
488
                Reference reference;
489
                if(strReference.equals(state.getRef().getTitleCache())){
490
                    reference = state.getRef();
491
                }
492
                else{
493
                    reference = ReferenceFactory.newGeneric();
494
                    reference.setTitle(strReference);
495
                }
496 496

  
497
            IdentifiableSource sour = getIdentifiableSource(reference,citationDetail);
497
                IdentifiableSource sour = getIdentifiableSource(reference,citationDetail);
498 498

  
499
            try{
500
                if (sour.getCitation() != null){
501
                    if(StringUtils.isNotBlank(sour.getCitationMicroReference())) {
502
                        state.getDataHolder().docSources.add(sour.getCitation().getTitleCache()+ "---"+sour.getCitationMicroReference());
503
                    } else {
504
                        state.getDataHolder().docSources.add(sour.getCitation().getTitleCache());
499
                try{
500
                    if (sour.getCitation() != null){
501
                        if(StringUtils.isNotBlank(sour.getCitationMicroReference())) {
502
                            state.getDataHolder().docSources.add(sour.getCitation().getTitleCache()+ "---"+sour.getCitationMicroReference());
503
                        } else {
504
                            state.getDataHolder().docSources.add(sour.getCitation().getTitleCache());
505
                        }
505 506
                    }
507
                }catch(Exception e){
508
                    logger.warn("oups");
506 509
                }
507
            }catch(Exception e){
508
                logger.warn("oups");
510
                reference.addSource(sour);
511
                save(reference, state);
509 512
            }
510
            reference.addSource(sour);
511
            save(reference, state);
512
        }
513
        List<IdentifiableSource> issTmp = new ArrayList<IdentifiableSource>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
514
        List<DescriptionElementSource> issTmp2 = new ArrayList<DescriptionElementSource>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
513
            List<IdentifiableSource> issTmp = new ArrayList<IdentifiableSource>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
514
            List<DescriptionElementSource> issTmp2 = new ArrayList<DescriptionElementSource>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
515 515

  
516
        Set<OriginalSourceBase> osbSet = new HashSet<OriginalSourceBase>();
517
        if(issTmp2!=null) {
518
            osbSet.addAll(issTmp2);
519
        }
520
        if(issTmp!=null) {
521
            osbSet.addAll(issTmp);
522
        }
523

  
524
        addToSourceMap(sourceMap, osbSet);
525

  
526
        if( state.getConfig().isInteractWithUser()){
527
            List<OriginalSourceBase<?>>sources=null;
528
            if(!state.isDerivedUnitSourcesSet()){
529
                sources= sui.askForSource(sourceMap, "the unit itself","",getReferenceService(), state.getDataHolder().docSources);
530
                state.setDerivedUnitSources(sources);
531
                state.setDerivedUnitSourcesSet(true);
516
            Set<OriginalSourceBase> osbSet = new HashSet<OriginalSourceBase>();
517
            if(issTmp2!=null) {
518
                osbSet.addAll(issTmp2);
532 519
            }
533
            else{
534
                sources=state.getDerivedUnitSources();
520
            if(issTmp!=null) {
521
                osbSet.addAll(issTmp);
535 522
            }
536
//          System.out.println("nb sources: "+sources.size());
537
//          System.out.println("derivedunitfacade : "+derivedUnitFacade.getTitleCache());
538
            for (OriginalSourceBase<?> sour:sources){
539
                if(sour.isInstanceOf(IdentifiableSource.class)){
540
                    if(sourceNotLinkedToElement(derivedUnitFacade,sour)) {
541
//                      System.out.println("add source to derivedunitfacade1 "+derivedUnitFacade.getTitleCache());
542
                        derivedUnitFacade.addSource((IdentifiableSource)sour.clone());
543
                    }
544
                }else{
545
                    if(sourceNotLinkedToElement(derivedUnitFacade,sour)) {
546
//                      System.out.println("add source to derivedunitfacade2 "+derivedUnitFacade.getTitleCache());
547
                        derivedUnitFacade.addSource(OriginalSourceType.Import,sour.getCitation(),sour.getCitationMicroReference(), ioName);
548
                    }
523

  
524
            addToSourceMap(sourceMap, osbSet);
525

  
526
            if( state.getConfig().isInteractWithUser()){
527
                List<OriginalSourceBase<?>>sources=null;
528
                if(!state.isDerivedUnitSourcesSet()){
529
                    sources= sui.askForSource(sourceMap, "the unit itself","",getReferenceService(), state.getDataHolder().docSources);
530
                    state.setDerivedUnitSources(sources);
531
                    state.setDerivedUnitSourcesSet(true);
549 532
                }
550
            }
551
        }else{
552
            for (OriginalSourceBase<?> sr : sourceMap.values()){
553
                if(sr.isInstanceOf(IdentifiableSource.class)){
554
                    if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
555
//                      System.out.println("add source to derivedunitfacade3 "+derivedUnitFacade.getTitleCache());
556
                        derivedUnitFacade.addSource((IdentifiableSource)sr.clone());
533
                else{
534
                    sources=state.getDerivedUnitSources();
535
                }
536
    //          System.out.println("nb sources: "+sources.size());
537
    //          System.out.println("derivedunitfacade : "+derivedUnitFacade.getTitleCache());
538
                for (OriginalSourceBase<?> sour:sources){
539
                    if(sour.isInstanceOf(IdentifiableSource.class)){
540
                        if(sourceNotLinkedToElement(derivedUnitFacade,sour)) {
541
    //                      System.out.println("add source to derivedunitfacade1 "+derivedUnitFacade.getTitleCache());
542
                            derivedUnitFacade.addSource((IdentifiableSource)sour.clone());
543
                        }
544
                    }else{
545
                        if(sourceNotLinkedToElement(derivedUnitFacade,sour)) {
546
    //                      System.out.println("add source to derivedunitfacade2 "+derivedUnitFacade.getTitleCache());
547
                            derivedUnitFacade.addSource(OriginalSourceType.Import,sour.getCitation(),sour.getCitationMicroReference(), ioName);
548
                        }
557 549
                    }
558
                }else{
559
                    if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
560
//                      System.out.println("add source to derivedunitfacade4 "+derivedUnitFacade.getTitleCache());
561
                        derivedUnitFacade.addSource(OriginalSourceType.Import,sr.getCitation(),sr.getCitationMicroReference(), ioName);
550
                }
551
            }else{
552
                for (OriginalSourceBase<?> sr : sourceMap.values()){
553
                    if(sr.isInstanceOf(IdentifiableSource.class)){
554
                        if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
555
    //                      System.out.println("add source to derivedunitfacade3 "+derivedUnitFacade.getTitleCache());
556
                            derivedUnitFacade.addSource((IdentifiableSource)sr.clone());
557
                        }
558
                    }else{
559
                        if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
560
    //                      System.out.println("add source to derivedunitfacade4 "+derivedUnitFacade.getTitleCache());
561
                            derivedUnitFacade.addSource(OriginalSourceType.Import,sr.getCitation(),sr.getCitationMicroReference(), ioName);
562
                        }
562 563
                    }
563 564
                }
564 565
            }
565
        }
566 566

  
567
        save(item, state);
567
            save(item, state);
568 568

  
569
        if(DEBUG) {
570
            logger.info("saved ABCD specimen ...");
571
        }
569
            if(DEBUG) {
570
                logger.info("saved ABCD specimen ...");
571
            }
572 572

  
573
        // handle identifications
574
        handleIdentifications(state, derivedUnitFacade);
573
            // handle identifications
574
            handleIdentifications(state, derivedUnitFacade);
575 575

  
576
        //associatedUnits
577
        importAssociatedUnits(state, item, derivedUnitFacade);
576
            //associatedUnits
577
            importAssociatedUnits(state, item, derivedUnitFacade);
578 578

  
579 579

  
580 580

  
581
    } catch (Exception e) {
582
        String message = "Error when reading record!";
583
        logger.warn(message);
584
        state.getReport().addException(message, e);
585
        e.printStackTrace();
586
        state.setUnsuccessfull();
581
        } catch (Exception e) {
582
            String message = "Error when reading record!";
583
            logger.warn(message);
584
            state.getReport().addException(message, e);
585
            e.printStackTrace();
586
            state.setUnsuccessfull();
587
        }
588
    */
589
        return;
587 590
    }
588
*/
589
    return;
590
}
591

  
592

  
593
/**
594
 * @param state
595
 * @param derivedUnitFacade
596
 */
597
private void handleCollectionData(
598
        SpecimenImportStateBase<SpecimenImportConfiguratorBase, SpecimenImportStateBase> state,
599
        DerivedUnitFacade derivedUnitFacade) {
600
   eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnitFacade.getCollection();
601
   if (collection != null) {
602
       Institution institution = getInstitution(collection.getInstitute().getCode(), state);
603

  
604
       collection = getCollection(institution, collection.getCode(), state);
605
   }
606

  
607
}
608 591

  
609 592

  
593
    /**
594
     * @param state
595
     * @param derivedUnitFacade
596
     */
597
    private void handleCollectionData(
598
            SpecimenImportStateBase<SpecimenImportConfiguratorBase, SpecimenImportStateBase> state,
599
            DerivedUnitFacade derivedUnitFacade) {
600
       eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnitFacade.getCollection();
601
       if (collection != null) {
602
           Institution institution = getInstitution(collection.getInstitute().getCode(), state);
610 603

  
604
           collection = getCollection(institution, collection.getCode(), state);
605
       }
611 606

  
607
    }
612 608

  
613 609

  
614
/**
615
 * @param state
616
 * @param derivedUnitFacade
617
 */
618
private void handleDeterminations(
619
        SpecimenImportStateBase<SpecimenImportConfiguratorBase, SpecimenImportStateBase> state,
620
        DerivedUnitFacade derivedUnitFacade) {
621
    SpecimenImportConfiguratorBase config = state.getConfig();
610
    /**
611
     * @param state
612
     * @param derivedUnitFacade
613
     */
614
    private void handleDeterminations(
615
            SpecimenImportStateBase<SpecimenImportConfiguratorBase, SpecimenImportStateBase> state,
616
            DerivedUnitFacade derivedUnitFacade) {
617
        SpecimenImportConfiguratorBase config = state.getConfig();
622 618

  
623 619

  
624
    String scientificName = "";
625
    boolean preferredFlag = false;
620
        String scientificName = "";
621
        boolean preferredFlag = false;
626 622

  
627
    if (state.getDataHolder().getNomenclatureCode() == ""){
628
        state.getDataHolder().setNomenclatureCode(config.getNomenclaturalCode().toString());
629
    }
630
    Set<DeterminationEvent> determinations =  derivedUnitFacade.getDeterminations();
631
    Iterator<DeterminationEvent> determinationIterator = determinations.iterator();
632
    DeterminationEvent event;
633
    Taxon taxon;
634
    TaxonName name ;
635
    while (determinationIterator.hasNext()) {
636
        event = determinationIterator.next();
637
        taxon = (Taxon)event.getTaxon();
638
        if (taxon == null){
639
            name = event.getTaxonName();
640
            if (!name.getTaxa().isEmpty()){
641
                taxon = (Taxon)name.getTaxa().iterator().next();
642
            }
623
        if (state.getDataHolder().getNomenclatureCode() == ""){
624
            state.getDataHolder().setNomenclatureCode(config.getNomenclaturalCode().toString());
643 625
        }
644
        if (taxon != null){
645
            addTaxonNode(taxon, state,preferredFlag);
646
            linkDeterminationEvent(state, taxon, preferredFlag, derivedUnitFacade, null, null);
626
        Set<DeterminationEvent> determinations =  derivedUnitFacade.getDeterminations();
627
        Iterator<DeterminationEvent> determinationIterator = determinations.iterator();
628
        DeterminationEvent event;
629
        Taxon taxon;
630
        TaxonName name ;
631
        while (determinationIterator.hasNext()) {
632
            event = determinationIterator.next();
633
            taxon = (Taxon)event.getTaxon();
634
            if (taxon == null){
635
                name = event.getTaxonName();
636
                if (!name.getTaxa().isEmpty()){
637
                    taxon = name.getTaxa().iterator().next();
638
                }
639
            }
640
            if (taxon != null){
641
                addTaxonNode(taxon, state,preferredFlag);
642
                linkDeterminationEvent(state, taxon, preferredFlag, derivedUnitFacade, null, null);
643
            }
647 644
        }
648
    }
649 645

  
650
}
646
    }
651 647

  
652
/**
653
 * @param names
654
 * @param item
655
 */
656
private TaxonName findBestMatchingNames(GbifResponse item, SpecimenImportStateBase state) {
657
   //TODO
658
    if (item.getScientificName() != null){
659

  
660
       List<TaxonName> names = findExistingNames(item.getScientificName().getNameCache(), state);
661
       if (!names.isEmpty()){
662
           TaxonName result = names.get(0);
663
           Set<DeterminationEvent> detEvents = item.getDerivedUnitFacade().baseUnit().getDeterminations();
664
           for (DeterminationEvent event:detEvents){
665
               if(event.getTaxonName().getNameCache().equals(result.getNameCache()) ){
666
                  event.setTaxonName(result);
667
               } else{
668
                   names = findExistingNames(event.getTaxonName().getNameCache(), state);
669
                   if (!names.isEmpty()){
670
                       event.setTaxonName(names.get(0));
648
    /**
649
     * @param names
650
     * @param item
651
     */
652
    private TaxonName findBestMatchingNames(GbifResponse item, SpecimenImportStateBase state) {
653
       //TODO
654
        if (item.getScientificName() != null){
655

  
656
           List<TaxonName> names = findExistingNames(item.getScientificName().getNameCache(), state);
657
           if (!names.isEmpty()){
658
               TaxonName result = names.get(0);
659
               Set<DeterminationEvent> detEvents = item.getDerivedUnitFacade().baseUnit().getDeterminations();
660
               for (DeterminationEvent event:detEvents){
661
                   if(event.getTaxonName().getNameCache().equals(result.getNameCache()) ){
662
                      event.setTaxonName(result);
663
                   } else{
664
                       names = findExistingNames(event.getTaxonName().getNameCache(), state);
665
                       if (!names.isEmpty()){
666
                           event.setTaxonName(names.get(0));
667
                       }
671 668
                   }
672 669
               }
670
               return result;
673 671
           }
674
           return result;
675
       }
676
    }
677
   return null;
678

  
679
}
680

  
681

  
682

  
683
/**
684
 * @param titleCache
685
 * @param state
686
 * @return
687
 */
688
private List<TaxonName> findExistingNames(String nameCache, SpecimenImportStateBase state) {
689
    return getNameService().findNamesByNameCache(nameCache, MatchMode.LIKE, null);
690
}
691

  
692

  
693

  
672
        }
673
       return null;
694 674

  
675
    }
695 676

  
696 677

  
697
/* (non-Javadoc)
698
 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
699
 */
700
@Override
701
protected boolean isIgnore(SpecimenImportStateBase<SpecimenImportConfiguratorBase, SpecimenImportStateBase> state) {
702 678

  
703
    return false;
704
}
679
    /**
680
     * @param titleCache
681
     * @param state
682
     * @return
683
     */
684
    private List<TaxonName> findExistingNames(String nameCache, SpecimenImportStateBase state) {
685
        return getNameService().findNamesByNameCache(nameCache, MatchMode.LIKE, null);
686
    }
705 687

  
706 688

  
707 689

  
690
    @Override
691
    protected boolean isIgnore(SpecimenImportStateBase<SpecimenImportConfiguratorBase, SpecimenImportStateBase> state) {
692
        return false;
693
    }
708 694

  
709 695

  
710 696

  

Also available in: Unified diff