refreshing RegistrationWorkingsetPresenter on popup editor close
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / registration / RegistrationWorkingsetPresenter.java
index 97f8035f9535d6cbc05b99dc8561f5b943ca6371..c5f524701b9e95f890441bde17dcfc4ba0fb2639 100644 (file)
@@ -15,6 +15,7 @@ import java.util.List;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.event.EventListener;
 import org.springframework.security.core.Authentication;
+import org.springframework.transaction.TransactionStatus;
 
 import com.vaadin.spring.annotation.SpringComponent;
 import com.vaadin.spring.annotation.ViewScope;
@@ -25,6 +26,8 @@ import com.vaadin.ui.Window;
 
 import eu.etaxonomy.cdm.api.service.INameService;
 import eu.etaxonomy.cdm.api.service.IRegistrationService;
+import eu.etaxonomy.cdm.api.service.idminter.IdentifierMinter.Identifier;
+import eu.etaxonomy.cdm.api.service.idminter.RegistrationIdentifierMinter;
 import eu.etaxonomy.cdm.model.common.User;
 import eu.etaxonomy.cdm.model.name.Rank;
 import eu.etaxonomy.cdm.model.name.Registration;
@@ -48,10 +51,12 @@ import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkingsetAction;
 import eu.etaxonomy.cdm.vaadin.model.EntityReference;
 import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
 import eu.etaxonomy.cdm.vaadin.security.UserHelper;
+import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
 import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
 import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
 import eu.etaxonomy.cdm.vaadin.view.name.SpecimenTypeDesignationWorkingsetPopupEditor;
 import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditor;
+import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditorMode;
 import eu.etaxonomy.cdm.vaadin.view.name.TypeDesignationWorkingsetEditorIdSet;
 import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
 import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
@@ -72,11 +77,13 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
     @Autowired
     private IRegistrationWorkingSetService workingSetService;
 
+    @Autowired
+    private RegistrationIdentifierMinter minter;
+
     /**
      * @return the workingSetService
      */
     public IRegistrationWorkingSetService getWorkingSetService() {
-        ensureBoundConversation();
         return workingSetService;
     }
 
@@ -120,27 +127,37 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
     protected Registration createNewRegistrationForName(Integer taxonNameId) {
         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         // move into RegistrationWorkflowStateMachine
+        TransactionStatus txStatus = getRepo().startTransaction();
         long identifier = System.currentTimeMillis();
+
+        Identifier<String> identifiers = minter.mint();
+        if(identifiers.getIdentifier() == null){
+            throw new RuntimeException("RegistrationIdentifierMinter configuration incomplete.");
+        }
         Registration reg = Registration.NewInstance(
-                "http://phycobank.org/" + identifier,
-                "" + identifier,
+                identifiers.getIdentifier(),
+                identifiers.getLocalId(),
                 taxonNameId != null ? getRepo().getNameService().find(taxonNameId) : null,
                 null);
         Authentication authentication = currentSecurityContext().getAuthentication();
         reg.setSubmitter((User)authentication.getPrincipal());
         EntityChangeEvent event = getRegistrationStore().saveBean(reg);
         UserHelper.fromSession().createAuthorityForCurrentUser(Registration.class, event.getEntityId(), Operation.UPDATE, RegistrationStatus.PREPARATION.name());
+        getRepo().commitTransaction(txStatus);
         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         return getRepo().getRegistrationService().find(event.getEntityId());
     }
 
 
     /**
+     * @param doReload TODO
      *
      */
-    protected void refreshView() {
-        getConversationHolder().getSession().clear();
-        presentWorkingSet(workingset.getCitationId());
+    protected void refreshView(boolean doReload) {
+        if(doReload){
+            loadWorkingSet(workingset.getCitationId());
+        }
+        getView().setWorkingset(workingset);
     }
 
 
@@ -152,21 +169,21 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
 
         super.handleViewEntered();
 
-        presentWorkingSet(getView().getCitationID());
+        loadWorkingSet(getView().getCitationID());
+        getView().setWorkingset(workingset);
 
-        CdmFilterablePagingProvider<TaxonName> pagingProvider = new CdmFilterablePagingProvider<TaxonName>(
-                getRepo().getNameService(), this);
+        CdmFilterablePagingProvider<TaxonName, TaxonName> pagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(
+                getRepo().getNameService());
         CdmTitleCacheCaptionGenerator<TaxonName> titleCacheGenrator = new CdmTitleCacheCaptionGenerator<TaxonName>();
         getView().getAddExistingNameCombobox().setCaptionGenerator(titleCacheGenrator);
         getView().getAddExistingNameCombobox().loadFrom(pagingProvider, pagingProvider, pagingProvider.getPageSize());
     }
 
+
     /**
-     * Loads the WorkingSet from the data base and passes it to the view.
-     *
-     * @param registrationID
+     * @param referenceID
      */
-    private void presentWorkingSet(Integer referenceID) {
+    protected void loadWorkingSet(Integer referenceID) {
         try {
             workingset = getWorkingSetService().loadWorkingSetByReferenceID(referenceID);
         } catch (RegistrationValidationException error) {
@@ -183,54 +200,107 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
             Reference citation = getRepo().getReferenceService().find(referenceID);
             workingset = new RegistrationWorkingSet(citation);
         }
-        // getView().setHeaderText("Registrations for " + workingset.getCitation());
-        getView().setWorkingset(workingset);
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD && #event.sourceComponent == null")
+    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).ADD")
     public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
+
+        if(!checkFromOwnView(event)){
+            return;
+        }
+
         ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
+        popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
         popup.loadInEditor(null);
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
+    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).EDIT")
     public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
+
+        if(!checkFromOwnView(event)){
+            return;
+        }
         ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
+        popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
         popup.withDeleteButton(true);
         popup.loadInEditor(event.getEntityId());
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
+    @EventListener
+    public void onDoneWithReferencePopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
+        if(event.getPopup() instanceof ReferencePopupEditor){
+            if(event.getReason().equals(Reason.SAVE)){
+                refreshView(true);
+            }
+        }
+    }
+
+    @EventListener
+    public void onDoneWithSpecimenTypeDesignationWorkingsetPopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
+        if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
+            if(event.getReason().equals(Reason.SAVE)){
+                refreshView(true);
+            }
+        }
+    }
+
+    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).EDIT")
     public void onRegistrationEditorAction(RegistrationEditorAction event) {
+
+        if(!checkFromOwnView(event)){
+            return;
+        }
+
         RegistrationPopupEditor popup = getNavigationManager().showInPopup(RegistrationPopupEditor.class);
         popup.loadInEditor(event.getEntityId());
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
+    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).EDIT")
     public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
 
+        if(!checkFromOwnView(event)){
+            return;
+        }
+
         TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
         popup.withDeleteButton(true);
-        // disable NomReferenceCombobox:
-        // the in the registration application inReferences should only edited centrally
-        popup.getNomReferenceCombobox().setEnabled(false);
+        configureTaxonNameEditor(popup);
         popup.loadInEditor(event.getEntityId());
+        if(event.getSourceComponent() != null){
+            popup.setReadOnly(event.getSourceComponent().isReadOnly());
+        }
+
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD")
+
+    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).ADD")
     public void onTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
 
-        newTaxonNameForRegistration = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
+        if(!checkFromOwnView(event)){
+            return;
+        }
+
+        newTaxonNameForRegistration = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
         newTaxonNameForRegistration.setNomenclaturalReference(getRepo().getReferenceService().find(workingset.getCitationId()));
         EntityChangeEvent nameSaveEvent = getTaxonNameStore().saveBean(newTaxonNameForRegistration);
-        UserHelper.fromSession().createAuthorityForCurrentUser(TaxonName.class, nameSaveEvent.getEntityId(), EnumSet.of(CRUD.UPDATE,CRUD.DELETE), null);
         newTaxonNameForRegistration = getRepo().getNameService().find(nameSaveEvent.getEntityId());
         TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
+        popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE,CRUD.DELETE));
         popup.withDeleteButton(true);
+        configureTaxonNameEditor(popup);
         popup.loadInEditor(newTaxonNameForRegistration.getId());
-        // disable NomReferenceCombobox:
-        // the in the registration application inReferences should only edited centrally
-        popup.getNomReferenceCombobox().setEnabled(false);
+    }
+
+    /**
+     * TODO consider putting this into a Configurer Bean per UIScope.
+     * In the configurator bean this methods popup papamerter should be of the type
+     * AbstractPopupEditor
+     *
+     * @param popup
+     */
+    protected void configureTaxonNameEditor(TaxonNamePopupEditor popup) {
+        popup.enableMode(TaxonNamePopupEditorMode.suppressReplacementAuthorshipData);
+        popup.enableMode(TaxonNamePopupEditorMode.nomenclaturalReferenceSectionEditingOnly);
     }
 
     /**
@@ -247,18 +317,24 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
     @EventListener
     public void onDoneWithTaxonnameEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
         if(event.getPopup() instanceof TaxonNamePopupEditor){
+            TransactionStatus txStatus = getRepo().startTransaction();
             if(event.getReason().equals(Reason.SAVE)){
                 if(newTaxonNameForRegistration != null){
                     int taxonNameId = newTaxonNameForRegistration.getId();
                     getRepo().getSession().refresh(newTaxonNameForRegistration);
                     Registration reg = createNewRegistrationForName(taxonNameId);
+                    // reload workingset into current session
+                    loadWorkingSet(workingset.getCitationId());
                     workingset.add(reg);
                 }
-                refreshView();
+                refreshView(true);
             } else if(event.getReason().equals(Reason.CANCEL)){
-                // clean up
-                getTaxonNameStore().deleteBean(newTaxonNameForRegistration);
+                if(newTaxonNameForRegistration != null){
+                    // clean up
+                    getTaxonNameStore().deleteBean(newTaxonNameForRegistration);
+                }
             }
+            getRepo().commitTransaction(txStatus);
             newTaxonNameForRegistration = null;
         }
     }
@@ -290,19 +366,27 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
 
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
+    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).EDIT")
     public void onTypeDesignationsEditorActionEdit(TypeDesignationWorkingsetEditorAction event) {
 
-            if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET ){
-                SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
-                popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getEntityId()));
-            } else {
-                // TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET
-                // FIXME implement NameTypeDesignationWorkingsetPopupEditor
+        if(!checkFromOwnView(event)){
+            return;
+        }
+
+        if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET ){
+            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
+            popup.withDeleteButton(true);
+            popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getEntityId()));
+            if(event.getSourceComponent() != null){
+                popup.setReadOnly(event.getSourceComponent().isReadOnly());
             }
+        } else {
+            // TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET
+            // FIXME implement NameTypeDesignationWorkingsetPopupEditor
+        }
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD && #event.sourceComponent == null")
+    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).ADD && #event.sourceComponent == null")
     public void onAddNewTypeDesignationWorkingset(TypeDesignationWorkingsetEditorAction event) {
 
         if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET){
@@ -310,16 +394,16 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
             TypeDesignationWorkingsetEditorIdSet identifierSet;
             Integer typifiedNameId;
             if(newRegistrationDTOWithExistingName != null){
-                typifiedNameId = newRegistrationDTOWithExistingName.getTypifiedName().getId();
+                typifiedNameId = newRegistrationDTOWithExistingName.getTypifiedNameRef().getId();
             } else {
                 RegistrationDTO registrationDTO = workingset.getRegistrationDTO(event.getRegistrationId()).get();
-                EntityReference typifiedNameRef = registrationDTO.getTypifiedName();
+                EntityReference typifiedNameRef = registrationDTO.getTypifiedNameRef();
                 if(typifiedNameRef != null){
                     // case for registrations without name, in which case the typifiedName is only defined via the typedesignations
                     typifiedNameId = typifiedNameRef.getId();
                 } else {
                     // case of registrations with a name in the nomenclatural act.
-                    typifiedNameId = registrationDTO.getName().getId();
+                    typifiedNameId = registrationDTO.getNameRef().getId();
                 }
             }
             identifierSet = new TypeDesignationWorkingsetEditorIdSet(
@@ -327,8 +411,8 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
                     getView().getCitationID(),
                     typifiedNameId
                     );
-            popup.loadInEditor(identifierSet
-                 );
+            popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
+            popup.loadInEditor(identifierSet);
             popup.withDeleteButton(true);
         } else {
             // TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET
@@ -353,7 +437,7 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
     public void onDoneWithTypeDesignationEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
         if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
             if(event.getReason().equals(Reason.SAVE)){
-                 refreshView();
+                refreshView(true);
             } else if(event.getReason().equals(Reason.CANCEL)){
                 // clean up
                 if(newRegistrationDTOWithExistingName != null){
@@ -394,19 +478,19 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
         }
         if(Reference.class.isAssignableFrom(event.getEntityType())){
             if(workingset.getCitationId().equals(event.getEntityId())){
-                refreshView();
+                refreshView(true);
             }
         } else
         if(Registration.class.isAssignableFrom(event.getEntityType())){
             if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
-                refreshView();
+                refreshView(true);
             }
         } else
         if(TaxonName.class.isAssignableFrom(event.getEntityType())){
             if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
-                reg.getTypifiedName() != null
-                && reg.getTypifiedName().getId() == event.getEntityId())){
-                    refreshView();
+                reg.getTypifiedNameRef() != null
+                && reg.getTypifiedNameRef().getId() == event.getEntityId())){
+                    refreshView(true);
             }
         } else
         if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
@@ -416,7 +500,7 @@ public class RegistrationWorkingsetPresenter extends AbstractPresenter<Registrat
                             )
                         )
                     ){
-                refreshView();
+                refreshView(true);
             }
         }