Project

General

Profile

« Previous | Next » 

Revision 1594c653

Added by Andreas Müller over 2 years ago

ref #9890 move importDeduplicationHelper to import state and include state in helper (cont.)

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/utils/ImportDeduplicationHelper.java
52 52
 * @author a.mueller
53 53
 * @since 11.02.2017
54 54
 */
55
public class ImportDeduplicationHelper<STATE extends ImportStateBase> {
55
public class ImportDeduplicationHelper {
56

  
56 57
    private static final Logger logger = Logger.getLogger(ImportDeduplicationHelper.class);
57 58

  
58 59
    private ICdmRepository repository;
59 60

  
60
    private STATE state;
61
    private ImportStateBase<?,?> state;
61 62

  
62 63
    boolean referenceMapIsInitialized = false;
63 64
    boolean nameMapIsInitialized = false;
......
99 100

  
100 101
     /**
101 102
      * @param repository
102
      * @param state not used, only for correct casting of generics
103
      * @param state
103 104
      * @return
104 105
      */
105
     public static <STATE extends ImportStateBase<?,?>> ImportDeduplicationHelper<STATE> NewInstance(ICdmRepository repository, STATE state){
106
         return new ImportDeduplicationHelper<>(repository, state);
106
     public static <STATE extends ImportStateBase<?,?>> ImportDeduplicationHelper NewInstance(ICdmRepository repository, STATE state){
107
         return new ImportDeduplicationHelper(repository, state);
107 108
     }
108 109

  
109 110
 // ************************ CONSTRUCTOR *****************************/
110 111

  
111
    public ImportDeduplicationHelper(ICdmRepository repository, STATE state) {
112
    private ImportDeduplicationHelper(ICdmRepository repository, ImportStateBase<?,?> state) {
112 113
         this.repository = repository;
113 114
         if (repository == null){
114 115
             logger.warn("Repository is null. Deduplication does not work against database.");
......
384 385
     * @param state the import state
385 386
     * @param name the name with authors and references to replace
386 387
     */
387
    public void replaceAuthorNamesAndNomRef(STATE state,
388
            INonViralName name) {
388
    public void replaceAuthorNamesAndNomRef(INonViralName name) {
389

  
389 390
        TeamOrPersonBase<?> combAuthor = name.getCombinationAuthorship();
390
        name.setCombinationAuthorship(getExistingAuthor(state, combAuthor));
391
        name.setCombinationAuthorship(getExistingAuthor(combAuthor));
391 392

  
392 393
        TeamOrPersonBase<?> exAuthor = name.getExCombinationAuthorship();
393
        name.setExCombinationAuthorship(getExistingAuthor(state, exAuthor));
394
        name.setExCombinationAuthorship(getExistingAuthor(exAuthor));
394 395

  
395 396
        TeamOrPersonBase<?> basioAuthor = name.getBasionymAuthorship();
396
        name.setBasionymAuthorship(getExistingAuthor(state, basioAuthor));
397
        name.setBasionymAuthorship(getExistingAuthor(basioAuthor));
397 398

  
398 399
        TeamOrPersonBase<?> exBasioAuthor = name.getExBasionymAuthorship();
399
        name.setExBasionymAuthorship(getExistingAuthor(state, exBasioAuthor));
400
        name.setExBasionymAuthorship(getExistingAuthor(exBasioAuthor));
400 401

  
401 402
        INomenclaturalReference nomRef = name.getNomenclaturalReference();
402 403
        if (nomRef != null){
403 404
            TeamOrPersonBase<?> refAuthor = nomRef.getAuthorship();
404
            nomRef.setAuthorship(getExistingAuthor(state, refAuthor));
405
            nomRef.setAuthorship(getExistingAuthor(refAuthor));
405 406

  
406
            Reference existingRef = getExistingReference(state, (Reference)nomRef);
407
            Reference existingRef = getExistingReference((Reference)nomRef);
407 408
            if (existingRef != null){
408 409
                name.setNomenclaturalReference(existingRef);
409 410
            }
410 411
        }
411 412
    }
412 413

  
413
    public <T extends TeamOrPersonBase<?>> T getExistingAuthor(STATE state,
414
            T author) {
414
    public <T extends TeamOrPersonBase<?>> T getExistingAuthor(T author) {
415 415
        if (author == null){
416 416
            return null;
417 417
        }else{
418
            initAgentMap(state);
418
            initAgentMap();
419 419
            initAuthorTitleCaches(author);
420 420
            T result = getTeamOrPerson(author);
421 421
            if (result == null){
422 422
                putAgentBase(author.getTitleCache(), author);
423 423
                if (author.isInstanceOf(Team.class)){
424
                    handleTeam(state, CdmBase.deproxy(author, Team.class));
424
                    handleTeam(CdmBase.deproxy(author, Team.class));
425 425
                }
426 426
                result = author;
427 427
            }
......
454 454
        ref.getTitleCache();
455 455
   }
456 456

  
457
    public AgentBase<?> getExistingAgent(STATE state,
458
            AgentBase<?> agent) {
457
    public AgentBase<?> getExistingAgent(AgentBase<?> agent) {
459 458
        if (agent == null){
460 459
            return null;
461 460
        } else if (agent.isInstanceOf(TeamOrPersonBase.class)){
462
            return getExistingAuthor(state, CdmBase.deproxy(agent, TeamOrPersonBase.class));
461
            return getExistingAuthor(CdmBase.deproxy(agent, TeamOrPersonBase.class));
463 462
        }else{
464
            initAgentMap(state);
463
            initAgentMap();
465 464
            Institution result = institutionMap.get(agent.getTitleCache());
466 465
            if (result == null){
467 466
                putAgentBase(agent.getTitleCache(), agent);
......
472 471
    }
473 472

  
474 473
    @SuppressWarnings("rawtypes")
475
    private void initAgentMap(STATE state) {
474
    private void initAgentMap() {
476 475
        if (!agentMapIsInitialized && repository != null){
477 476
            List<String> propertyPaths = Arrays.asList("");
478 477
            List<AgentBase> existingAgents = repository.getAgentService().list(null, null, null, null, propertyPaths);
......
483 482
        }
484 483
    }
485 484

  
486
    private void handleTeam(STATE state, Team team) {
485
    private void handleTeam(Team team) {
487 486
        List<Person> members = team.getTeamMembers();
488 487
        for (int i =0; i< members.size(); i++){
489 488
            Person person = CdmBase.deproxy(members.get(i));
......
496 495
        }
497 496
    }
498 497

  
499
    public Collection getExistingCollection(STATE state, Collection collection) {
498
    public Collection getExistingCollection(Collection collection) {
500 499
        if (collection == null){
501 500
            return null;
502 501
        }else{
503
            initCollectionMap(state);
502
            initCollectionMap();
504 503
            Collection result = getMatchingCollections(collection).orElse(null);
505 504
            if (result == null){
506 505
                result = collection;
......
514 513
        }
515 514
    }
516 515

  
517
    private void initCollectionMap(STATE state) {
516
    private void initCollectionMap() {
518 517
        if (!collectionMapIsInitialized && repository != null){
519 518
            List<String> propertyPaths = Arrays.asList("");
520 519
            List<Collection> existingCollections = repository.getCollectionService().list(null, null, null, null, propertyPaths);
......
525 524
        }
526 525
    }
527 526

  
528
   public Reference getExistingReference(STATE state, Reference ref) {
527
   public Reference getExistingReference(Reference ref) {
529 528
       if (ref == null){
530 529
           return null;
531 530
       }else{
532
           initRerenceMap(state);
531
           initRerenceMap();
533 532
           initReferenceCaches(ref);
534 533
           Reference result = getMatchingReference(ref).orElse(null);
535 534
           if (result == null){
536 535
               result = ref;
537 536
               Reference inRef = result.getInReference();
538 537
               if (inRef != null){
539
                   result.setInReference(getExistingReference(state, result.getInReference()));
538
                   result.setInReference(getExistingReference(result.getInReference()));
540 539
               }
541 540
               putReference(result.getTitleCache(), result);
542 541
           }else{
......
548 547
       }
549 548
   }
550 549

  
551
   private void initRerenceMap(STATE state) {
550
   private void initRerenceMap() {
552 551
       if (!referenceMapIsInitialized && repository != null){
553 552
           List<String> propertyPaths = Arrays.asList("");
554 553
           List<Reference> existingReferences = repository.getReferenceService().list(null, null, null, null, propertyPaths);
......
559 558
       }
560 559
   }
561 560

  
562
   public <NAME extends INonViralName> NAME getExistingName(STATE state, NAME name) {
561
   public <NAME extends INonViralName> NAME getExistingName(NAME name) {
563 562
       if (name == null){
564 563
           return null;
565 564
       }else{
566
           initNameMap(state);
565
           initNameMap();
567 566
           @SuppressWarnings("unchecked")
568 567
           NAME result = (NAME)getMatchingName(name).orElse(null);
569 568
           if (result == null){
......
572 571
               for (HybridRelationship rel : parentRelations){
573 572
                   INonViralName parent = rel.getParentName();
574 573
                   if (parent != null){
575
                       rel.setParentName(getExistingName(state, parent));
574
                       rel.setParentName(getExistingName(parent));
576 575
                   }
577 576
               }
578 577
               putName(result.getTitleCache(), result);
......
585 584
       }
586 585
   }
587 586

  
588
   private void initNameMap(STATE state) {
587
   private void initNameMap() {
589 588
       if (!nameMapIsInitialized && repository != null){
590 589
           List<String> propertyPaths = Arrays.asList("");
591 590
           List<TaxonName> existingNames = repository.getNameService().list(null, null, null, null, propertyPaths);
......
596 595
       }
597 596
   }
598 597

  
599
   public Rights getExistingCopyright(STATE state,
600
           Rights right) {
598
   public Rights getExistingCopyright(Rights right) {
601 599
       if (right == null || !RightsType.COPYRIGHT().equals(right.getType())){
602 600
           return null;
603 601
       }else{
604
           initCopyrightMap(state);
602
           initCopyrightMap();
605 603
           String key = makeCopyrightKey(right);
606 604
           Set<Rights> set = copyrightMap.get(key);
607 605
           if (set == null || set.isEmpty()){
......
615 613
       }
616 614
   }
617 615

  
618
    private void initCopyrightMap(STATE state) {
616
    private void initCopyrightMap() {
619 617
        if (!copyrightMapIsInitialized && repository != null){
620 618
            List<String> propertyPaths = Arrays.asList("");
621 619
            List<Rights> existingRights = repository.getRightsService().list(null, null, null, null, propertyPaths);

Also available in: Unified diff