Project

General

Profile

« Previous | Next » 

Revision eee96dac

Added by Andreas Kohlbecker about 5 years ago

ref #8050 moving transactional code from RegistrationWorkingsetPresenter to new service class

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/RegistrationWorkingsetPresenter.java
24 24

  
25 25
import org.apache.log4j.Logger;
26 26
import org.springframework.beans.factory.annotation.Autowired;
27
import org.springframework.transaction.TransactionStatus;
28 27
import org.vaadin.spring.events.EventScope;
29 28
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
30 29

  
......
55 54
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
56 55
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
57 56
import eu.etaxonomy.cdm.model.reference.Reference;
58
import eu.etaxonomy.cdm.model.reference.ReferenceType;
59 57
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
60 58
import eu.etaxonomy.cdm.ref.EntityReference;
61 59
import eu.etaxonomy.cdm.ref.TypedEntityReference;
......
63 61
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
64 62
import eu.etaxonomy.cdm.service.CdmFilterablePagingProviderFactory;
65 63
import eu.etaxonomy.cdm.service.CdmStore;
64
import eu.etaxonomy.cdm.service.IRegistrationWorkflowService;
66 65
import eu.etaxonomy.cdm.service.UserHelperAccess;
67 66
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItem;
68 67
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStatusFieldInstantiator;
......
112 111
    @Autowired
113 112
    private IRegistrationWorkingSetService regWorkingSetService;
114 113

  
114
    @Autowired
115
    private IRegistrationWorkflowService registrationWorkflowService;
116

  
115 117
    @Autowired
116 118
    private CdmFilterablePagingProviderFactory pagingProviderFactory;
117 119

  
......
159 161
    }
160 162

  
161 163
    /**
162
     * Checks
163
     * <ol>
164
     * <li>if there is NOT any registration for this name created in the current registration system</li>
165
     * <li>Checks if the name belongs to the current workingset</li>
166
     * </ol>
167
     * If both checks are successful the method returns <code>true</code>.
168
     */
169
    public boolean canCreateNameRegistrationFor(TaxonName name) {
170
        return !getRepo().getRegistrationService().checkRegistrationExistsFor(name) && checkWokingsetContainsProtologe(name);
171
    }
172 164

  
173
    /**
174
     * @param name
175
     * @return
176
     */
177
    public boolean checkWokingsetContainsProtologe(TaxonName name) {
178
        Reference nomRef = name.getNomenclaturalReference();
179
        UUID citationUuid = workingset.getCitationUuid();
180
        // @formatter:off
181
        return nomRef != null && (
182
                // nomref matches
183
                nomRef.getUuid().equals(citationUuid) ||
184
                // nomref.inreference matches
185
                (nomRef.getType() != null && nomRef.getType() == ReferenceType.Section && nomRef.getInReference() != null && nomRef.getInReference().getUuid().equals(citationUuid))
186
                );
187
        // @formatter:on
188
    }
189 165

  
190 166
    /**
191 167
     * @param doReload reload the workingset from the persistent storage.
......
451 427
        if(event.getPopup() instanceof TaxonNamePopupEditor){
452 428
            if(newNameForRegistrationPopupEditor != null && event.getPopup().equals(newNameForRegistrationPopupEditor)){
453 429
                if(event.getReason().equals(Reason.SAVE)){
454

  
455 430
                    try {
456
                        // TODO move into a service class --------------
457
                        TransactionStatus txStatus = getRepo().startTransaction();
458
                        try {
459
                            UUID taxonNameUuid = newNameForRegistrationPopupEditor.getBean().getUuid();
460
                            if(newNameForRegistrationPopupEditor.getBean().cdmEntity().isPersited()){
461
                                getRepo().getSession().refresh(newNameForRegistrationPopupEditor.getBean().cdmEntity());
462
                            }
463
                            Registration reg = getRepo().getRegistrationService().createRegistrationForName(taxonNameUuid);
464
                            if(!newNameBlockingRegistrations.isEmpty()){
465
                                for(Registration blockingReg : newNameBlockingRegistrations){
466
                                    blockingReg = getRepo().getRegistrationService().load(blockingReg.getUuid());
467
                                    reg.getBlockedBy().add(blockingReg);
468
                                }
469
                                getRepo().getRegistrationService().saveOrUpdate(reg);
470
                                newNameBlockingRegistrations.clear();
471
                            }
472
                           getRepo().commitTransaction(txStatus);
473
                        } catch (Exception e){
474
                            getRepo().getTransactionManager().rollback(txStatus);
475
                            throw e;
476
                        }
477
                        // --------------------------------------------------
478
                        // reload workingset into current session
431
                        TaxonName taxonName = newNameForRegistrationPopupEditor.getBean().cdmEntity();
432
                        registrationWorkflowService.createRegistration(taxonName, newNameBlockingRegistrations);
479 433
                        loadWorkingSet(workingset.getCitationUuid());
434
                        refreshView(true);
435
                        getView().getAddNewNameRegistrationButton().setEnabled(true);
480 436
                    } finally {
481 437
                        clearSession();
482 438
                        refreshView(true);
483 439
                        getView().getAddNewNameRegistrationButton().setEnabled(true);
484 440
                    }
485 441
                }
486

  
487 442
                // nullify and clear the memory on this popup editor in any case (SAVE, CANCEL, DELETE)
488 443
                newNameForRegistrationPopupEditor = null;
489 444
                newNameBlockingRegistrations.clear();
......
495 450
    }
496 451

  
497 452

  
453

  
454

  
498 455
    /**
499 456
     * Creates a new Registration for an exiting (previously published) name.
500 457
     *
......
513 470
        if(typifiedName != null){
514 471
            boolean doReloadWorkingSet = false;
515 472
            try {
516
                // TODO move into a service class --------------
517
                TransactionStatus txStatus = getRepo().startTransaction(true);
518
                try {
519
                    Reference citation = getRepo().getReferenceService().load(workingset.getCitationUuid(), Arrays.asList("authorship.$", "inReference.authorship.$"));
520
                    // here we completely ignore the ExistingNameRegistrationType since the user should not have the choice
521
                    // to create a typification only registration in the working (publication) set which contains
522
                    // the protologe. This is known from the nomenclatural reference.
523
                    if(canCreateNameRegistrationFor(typifiedName)){
524
                        // the citation which is the base for workingset contains the protologe of the name and the name has not
525
                        // been registered before:
526
                        // create a registration for the name and the first typifications
527
                        Registration newRegistrationWithExistingName = getRepo().getRegistrationService().createRegistrationForName(typifiedName.getUuid());
528
                        workingset.add(new RegistrationDTO(newRegistrationWithExistingName, typifiedName, citation));
529
                        doReloadWorkingSet = true;
530
                    } else {
531
                        if(!checkWokingsetContainsProtologe(typifiedName)){
532
                            // create a typification only registration
533
                            Registration typificationOnlyRegistration = getRepo().getRegistrationService().newRegistration();
534
                            if(!getRepo().getRegistrationService().checkRegistrationExistsFor(typifiedName)){
535
                                // oops, yet no registration for this name, so we create it as blocking registration:
536
                                Registration blockingNameRegistration = getRepo().getRegistrationService().createRegistrationForName(typifiedName.getUuid());
537
                                typificationOnlyRegistration.getBlockedBy().add(blockingNameRegistration);
538
                            }
539
                            RegistrationDTO regDTO = new RegistrationDTO(typificationOnlyRegistration, typifiedName, citation);
540
                            workingset.add(regDTO);
541
                        }
542
                    }
543
                    getRepo().commitTransaction(txStatus);
544
                } catch (Exception e){
545
                    getRepo().getTransactionManager().rollback(txStatus);
546
                    throw e;
547
                }
548
                // --------------------------------------------------
549
                // tell the view to update the workingset
473
                doReloadWorkingSet = registrationWorkflowService.createRegistrationforExistingName(workingset, typifiedName);
550 474
            } finally {
551 475
                clearSession();
552 476
                refreshView(doReloadWorkingSet);
......
558 482

  
559 483
    }
560 484

  
561

  
562 485
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
563 486
    public void onTypeDesignationsEditorActionEdit(TypeDesignationWorkingsetEditorAction event) {
564 487

  
......
686 609

  
687 610
                try {
688 611
                    clearSession();
689
                    // TODO move into a service class --------------
690
                    TransactionStatus txStatus = getRepo().startTransaction();
691
                    try {
692
                        UUID regUUID = nameTypeDesignationPopupEditorRegistrationUUIDMap.get(event.getPopup());
693
                        Stack<EditorActionContext>context = ((AbstractPopupEditor)event.getPopup()).getEditorActionContext();
694
                        Registration registration = findRegistrationInContext(context);
695
                        getRepo().getRegistrationService().addTypeDesignation(registration, typeDesignationUuid);
696
                        getRepo().getRegistrationService().saveOrUpdate(registration);
697
                        nameTypeDesignationPopupEditorRegistrationUUIDMap.remove(event.getPopup());
698
                        getRepo().commitTransaction(txStatus);
699
                    } catch (Exception e){
700
                        getRepo().getTransactionManager().rollback(txStatus);
701
                        throw e;
702
                    }
703
                    // TODO move into a service class --------------
612
                    Stack<EditorActionContext>context = ((AbstractPopupEditor)event.getPopup()).getEditorActionContext();
613
                    Registration registration = findRegistrationInContext(context);
614
                    getRepo().getSession().clear();
615
                    registrationWorkflowService.addTypeDesignation(typeDesignationUuid, registration);
616
                    nameTypeDesignationPopupEditorRegistrationUUIDMap.remove(event.getPopup());
704 617
                } finally {
705 618
                    clearSession();
706 619
                    refreshView(true);
......
760 673
                Stack<EditorActionContext>context = ((AbstractPopupEditor)event.getSourceView()).getEditorActionContext();
761 674
                EditorActionContext rootContext = context.get(0);
762 675
                if(rootContext.getParentView().equals(getView()) && event.getSourceView() != newNameForRegistrationPopupEditor){
763

  
764 676
                    try {
765 677
                        clearSession();
766
                        // TODO move into a service class --------------
767
                        TransactionStatus txStatus = getRepo().startTransaction();
768
                        try {
769
                            // create a blocking registration, the new Registration will be persisted
770
                            UUID taxonNameUUID = event.getEntityUuid();
678
                        // create a blocking registration, the new Registration will be persisted
679
                        UUID taxonNameUUID = event.getEntityUuid();
680

  
681
                        if(context.get(1).getParentView() instanceof TaxonNamePopupEditor && !((TaxonNamePopupEditor)context.get(1).getParentView()).getBean().cdmEntity().isPersited()){
682
                            // Oha!! The event came from a popup editor and the
683
                            // first popup in the context is a TaxonNameEditor with un-persisted name
684
                            // This is a name for a new registration which has not yet been created.
685
                            // It is necessary to store blocking registrations in the newNameBlockingRegistrations
771 686
                            Registration blockingRegistration = getRepo().getRegistrationService().createRegistrationForName(taxonNameUUID);
772

  
773
                            if(context.get(1).getParentView() instanceof TaxonNamePopupEditor && !((TaxonNamePopupEditor)context.get(1).getParentView()).getBean().cdmEntity().isPersited()){
774
                                // Oha!! The event came from a popup editor and the
775
                                // first popup in the context is a TaxonNameEditor with un-persisted name
776
                                // This is a name for a new registration which has not yet been created.
777
                                // It is necessary to store blocking registrations in the newNameBlockingRegistrations
778
                                newNameBlockingRegistrations.add(blockingRegistration);
779
                                logger.debug("Blocking registration created and memorized");
780
                            } else {
781
                                // some new name somehow related to an existing registration
782
                                Registration registration = findRegistrationInContext(context);
783
                                registration.getBlockedBy().add(blockingRegistration);
784

  
785
                                if(registration.isPersited()){
786
                                    getRepo().getRegistrationService().saveOrUpdate(registration);
787
                                    logger.debug("Blocking registration created, added to registion and persited");
788
                                }
789
                            }
790
                            getRepo().commitTransaction(txStatus);
791
                        } catch (Exception e){
792
                            getRepo().getTransactionManager().rollback(txStatus);
793
                            throw e;
687
                            newNameBlockingRegistrations.add(blockingRegistration);
688
                            logger.debug("Blocking registration created and memorized");
689
                        } else {
690
                            Registration registration = findRegistrationInContext(context);
691
                            // some new name somehow related to an existing registration
692
                            registrationWorkflowService.addBlockingRegistration(taxonNameUUID, registration);
794 693
                        }
795
                        // TODO move into a service class --------------
796 694
                    } finally {
797 695
                        clearSession();
798 696
                    }
......
822 720
    }
823 721

  
824 722
    /**
723
     * Finds the Registration in the EditorContext stack
724
     *
825 725
     * @param context
826 726
     * @return
827 727
     */
......
834 734
        }
835 735
        Registration registration = registrationDTOOptional.get().registration();
836 736

  
837
        if(registration.isPersited()){
838
             registration = getRepo().getRegistrationService().load(registration.getUuid());
839
             if(registration == null){
840
                 throw new NullPointerException("Registration not found for " + regReference + " which has been hold in the rootContext");
841
             }
842
         } else {
843
             logger.trace("Registration is not yet persisted.");
844
         }
737
        // registration = reloadRegistration(registration);
845 738
        return registration;
846 739
    }
847 740

  
741

  
742

  
848 743
    @EventBusListenerMethod(filter = ShowDetailsEventEntityTypeFilter.RegistrationDTO.class)
849 744
    public void onShowDetailsEventForRegistrationDTO(ShowDetailsEvent<RegistrationDTO, UUID> event) {
850 745

  
......
910 805
        cache.dispose();
911 806
    }
912 807

  
808
    /**
809
     * @param name
810
     * @return
811
     */
812
    public boolean canCreateNameRegistrationFor(TaxonName name) {
813
        return registrationWorkflowService.canCreateNameRegistrationFor(workingset, name);
814
    }
815

  
816
    /**
817
     * @param name
818
     * @return
819
     */
820
    public boolean checkWokingsetContainsProtologe(TaxonName name) {
821
        return registrationWorkflowService.checkWokingsetContainsProtologe(workingset, name);
822
    }
823

  
913 824
}

Also available in: Unified diff