ref #7648 generalizing bean instantiation in cdm presenters
authorAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Wed, 26 Sep 2018 10:17:29 +0000 (12:17 +0200)
committerAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Wed, 26 Sep 2018 10:17:29 +0000 (12:17 +0200)
12 files changed:
src/main/java/eu/etaxonomy/cdm/vaadin/view/common/InstitutionEditorPresenter.java
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/NameTypeDesignationPresenter.java
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/TaxonNameEditorPresenter.java
src/main/java/eu/etaxonomy/cdm/vaadin/view/occurrence/CollectionEditorPresenter.java
src/main/java/eu/etaxonomy/cdm/vaadin/view/reference/ReferenceEditorPresenter.java
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/RegistrationEditorPresenter.java
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/StartRegistrationPresenter.java
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmDTOEditorPresenter.java
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmPopupEditor.java
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractEditorPresenter.java
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractPopupEditor.java
src/main/java/eu/etaxonomy/vaadin/mvp/CdmEditorPresenterBase.java

index b839594a319ae1997c039f8c6f05f9ae374ff069..419c92b2cc41f87f364d5dd2080c6212ed0c3687 100644 (file)
@@ -43,7 +43,6 @@ public class InstitutionEditorPresenter extends AbstractCdmDTOEditorPresenter<In
 
     private static final long serialVersionUID = -1996365248431425021L;
 
-
     /**
      * {@inheritDoc}
      */
@@ -62,7 +61,7 @@ public class InstitutionEditorPresenter extends AbstractCdmDTOEditorPresenter<In
         if(identifier != null){
             bean = (Institution) getRepo().getAgentService().load(identifier, initStrategy);
         } else {
-            bean = Institution.NewInstance();
+            bean = cdmEntityInstantiator.createNewBean();
         }
         return bean;
     }
index 3890fa82686172d377977611f407e10046397e91..ac59f06768ba18430e93eb9674e86efff2632f5a 100644 (file)
@@ -46,6 +46,7 @@ import eu.etaxonomy.cdm.vaadin.ui.config.TaxonNamePopupEditorConfig;
 import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
 import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
 import eu.etaxonomy.vaadin.mvp.AbstractView;
+import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
 import eu.etaxonomy.vaadin.mvp.BoundField;
 import eu.etaxonomy.vaadin.ui.view.PopupView;
 
@@ -66,6 +67,19 @@ public class NameTypeDesignationPresenter
 
     private TaxonName typifiedNameInContext;
 
+    protected static BeanInstantiator<NameTypeDesignation> defaultBeanInstantiator = new BeanInstantiator<NameTypeDesignation>() {
+
+        @Override
+        public NameTypeDesignation createNewBean() {
+            return NameTypeDesignation.NewInstance();
+        }
+    };
+
+
+    @Override
+    protected BeanInstantiator<NameTypeDesignation> defaultBeanInstantiator(){
+       return defaultBeanInstantiator;
+    }
 
     /**
      * {@inheritDoc}
@@ -113,11 +127,7 @@ public class NameTypeDesignationPresenter
         if(uuid != null){
             typeDesignation = (NameTypeDesignation) getRepo().getNameService().loadTypeDesignation(uuid, initStrategy);
         } else {
-            if(beanInstantiator != null){
-                typeDesignation = beanInstantiator.createNewBean();
-            } else {
-                typeDesignation = NameTypeDesignation.NewInstance();
-            }
+            typeDesignation = createNewBean();
         }
 
         typifiedNamesAsLoaded = new HashSet<>(typeDesignation.getTypifiedNames());
index 0c9106600023a1cc8eb5e6008ebae805ebec2675..28ae239989c138dbaf82b46ed179e994ee5ebefb 100644 (file)
@@ -217,7 +217,11 @@ public class TaxonNameEditorPresenter extends AbstractCdmDTOEditorPresenter<Taxo
         if(identifier != null){
             taxonName = getRepo().getNameService().load(identifier, initStrategy);
         } else {
-            taxonName = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
+            if(cdmEntityInstantiator != null) {
+                taxonName = cdmEntityInstantiator.createNewBean();
+            } else {
+                taxonName = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
+            }
         }
 
         if(getView().isModeEnabled(TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY)){
index 6bb4a693ac4abf760dc35946f78c6d286a47fc32..0d337adedcf97cb545ff19a8de0ee2989486d994 100644 (file)
@@ -33,6 +33,7 @@ import eu.etaxonomy.cdm.vaadin.event.InstitutionEditorAction;
 import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
 import eu.etaxonomy.cdm.vaadin.view.common.InstitutionPopupEditor;
 import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
+import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
 import eu.etaxonomy.vaadin.mvp.BoundField;
 import eu.etaxonomy.vaadin.ui.view.PopupView;
 
@@ -47,6 +48,19 @@ public class CollectionEditorPresenter extends AbstractCdmEditorPresenter<Collec
 
     private static final long serialVersionUID = -1996365248431425021L;
 
+    protected static BeanInstantiator<Collection> defaultBeanInstantiator = new BeanInstantiator<Collection>() {
+
+        @Override
+        public Collection createNewBean() {
+            return Collection.NewInstance();
+        }
+    };
+
+
+    @Override
+    protected BeanInstantiator<Collection> defaultBeanInstantiator(){
+       return defaultBeanInstantiator;
+    }
 
     /**
      * {@inheritDoc}
@@ -66,7 +80,7 @@ public class CollectionEditorPresenter extends AbstractCdmEditorPresenter<Collec
         if(identifier != null){
             bean = getRepo().getCollectionService().load(identifier, initStrategy);
         } else {
-            bean = Collection.NewInstance();
+            bean = createNewBean();
         }
         return bean;
     }
index 8e95cc6fc65507f16ad9cc9e7e00f1f1b0ceb100..f8a370b3183159613896adce3650ba8e0dca97ab 100644 (file)
@@ -37,6 +37,7 @@ import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
 import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
 import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
 import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
+import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
 
 /**
  * @author a.kohlbecker
@@ -89,6 +90,22 @@ public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Referen
                 AnnotationType.EDITORIAL().getUuid(), AnnotationType.TECHNICAL().getUuid()));
     }
 
+
+    protected static BeanInstantiator<Reference> defaultBeanInstantiator = new BeanInstantiator<Reference>() {
+
+        @Override
+        public Reference createNewBean() {
+            return ReferenceFactory.newGeneric();
+        }
+    };
+
+
+    @Override
+    protected BeanInstantiator<Reference> defaultBeanInstantiator(){
+       return defaultBeanInstantiator;
+    }
+
+
     /**
      * {@inheritDoc}
      */
@@ -106,22 +123,11 @@ public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Referen
         if(identifier != null){
             reference = getRepo().getReferenceService().load(identifier, initStrategy);
         } else {
-            reference = createNewReference();
+            reference = createNewBean();
         }
         return reference;
     }
 
-    /**
-     * TODO this should better go into {@link AbstractCdmEditorPresenter}
-     *
-     * @return
-     */
-    protected Reference createNewReference() {
-        if(this.beanInstantiator != null){
-            return beanInstantiator.createNewBean();
-        }
-        return ReferenceFactory.newGeneric();
-    }
 
     /**
      * {@inheritDoc}
index 3467cdd3f8047b7ac71e6f1b20e15e7bb9ac83b7..2291582c4c03dde719279406ceef7f3c0b8a9057 100644 (file)
@@ -23,6 +23,7 @@ import eu.etaxonomy.cdm.model.name.Registration;
 import eu.etaxonomy.cdm.service.UserHelperAccess;
 import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
 import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
+import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
 
 /**
  * @author a.kohlbecker
@@ -43,6 +44,21 @@ public class RegistrationEditorPresenter extends AbstractCdmEditorPresenter<Regi
         return getRepo().getRegistrationService();
     }
 
+
+    protected static BeanInstantiator<Registration> defaultBeanInstantiator = new BeanInstantiator<Registration>() {
+
+        @Override
+        public Registration createNewBean() {
+            return Registration.NewInstance();
+        }
+    };
+
+
+    @Override
+    protected BeanInstantiator<Registration> defaultBeanInstantiator(){
+       return defaultBeanInstantiator;
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -54,7 +70,7 @@ public class RegistrationEditorPresenter extends AbstractCdmEditorPresenter<Regi
             List<String> initStrategy = Arrays.asList(new String[] {"$", "typeDesignations"});
             reg = getRepo().getRegistrationService().load(identifier, initStrategy );
         } else {
-            reg = Registration.NewInstance();
+            reg = createNewBean();
         }
         return reg;
     }
index af76bfb8536be5934812dd8227d24ec9409c440a..e86c690b4aa5ba09e95583dcd9c642c216e206bf 100644 (file)
@@ -199,8 +199,7 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
      */
     @Override
     protected void saveBean(RegistrationDTO bean) {
-        // TODO Auto-generated method stub
-
+        // not needed //
     }
 
     /**
@@ -208,8 +207,7 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
      */
     @Override
     protected void deleteBean(RegistrationDTO bean) {
-        // TODO Auto-generated method stub
-
+        // not needed //
     }
 
 }
index 7abe8155905ea07e7f4aebfcbdcf66a9a23d1532..a1217ead84108b50b65e4f4452f379e3cb06a603 100644 (file)
@@ -23,6 +23,14 @@ public abstract class AbstractCdmDTOEditorPresenter<DTO extends CdmEntityAdapter
 
     private static final long serialVersionUID = -6315824180341694825L;
 
+    protected BeanInstantiator<CDM> cdmEntityInstantiator = null;
+
+    @Override
+    protected BeanInstantiator<DTO> defaultBeanInstantiator(){
+        // not needed in the AbstractCdmDTOEditorPresenter since replaced by cdmEntityInstantiator
+       return null;
+    }
+
 
     @Override
     protected CDM cdmEntity(DTO dto) {
@@ -30,5 +38,13 @@ public abstract class AbstractCdmDTOEditorPresenter<DTO extends CdmEntityAdapter
     }
 
 
+    /**
+     * @param cdmEntityInstantiator the cdmEntityInstantiator to set
+     */
+    public void setCdmEntityInstantiator(BeanInstantiator<CDM> cdmEntityInstantiator) {
+        this.cdmEntityInstantiator = cdmEntityInstantiator;
+    }
+
+
 
 }
\ No newline at end of file
index 4c882e68c9b93ba43cb1b53bef31d6dd69d34ecd..98d8388abdb01da83cd5d757a805899080d72671 100644 (file)
@@ -39,6 +39,4 @@ public abstract class AbstractCdmPopupEditor<CDM extends CdmBase, P extends CdmE
         getPresenter().setGrantsForCurrentUser(crud);
     }
 
-
-
 }
index 7043de06552c5f1eb5cf19b1bf4011d00232ccef..ea2f0c2fdabde399276d94777a38ebca9017c5c3 100644 (file)
@@ -42,8 +42,6 @@ public abstract class AbstractEditorPresenter<DTO extends Object, V extends Appl
 
     FlushMode previousPreSaveEvenFlushMode = null;
 
-    protected BeanInstantiator<DTO> beanInstantiator = null;
-
     /**
      * Load the bean to be edited in the editor freshly from the persistent storage.
      * Ore create an new empty instance in case the supplied <code>identifier</code> is <code>null</code>.
@@ -63,12 +61,6 @@ public abstract class AbstractEditorPresenter<DTO extends Object, V extends Appl
 
     }
 
-    /**
-     * @param beanInstantiator the beanInstantiator to set
-     */
-    public void setBeanInstantiator(BeanInstantiator<DTO> beanInstantiator) {
-        this.beanInstantiator = beanInstantiator;
-    }
 
     @EventBusListenerMethod
     public void onEditorPreSaveEvent(EditorPreSaveEvent<DTO> preSaveEvent){
index 347d95d4e75363c9d07566b49f0f0229957ee801..9166ea01679e63e497dcaeaa27fbc68a89ab3971 100644 (file)
@@ -742,7 +742,11 @@ public abstract class AbstractPopupEditor<DTO extends Object, P extends Abstract
      * @param beanInstantiator
      */
     public final void setBeanInstantiator(BeanInstantiator<DTO> beanInstantiator) {
-        getPresenter().setBeanInstantiator(beanInstantiator);
+        if(AbstractCdmEditorPresenter.class.isAssignableFrom(getPresenter().getClass())){
+            ((CdmEditorPresenterBase)getPresenter()).setBeanInstantiator(beanInstantiator);
+        } else {
+            throw new RuntimeException("BeanInstantiator can only be set for popup editors with a peresenter of the type CdmEditorPresenterBase");
+        }
     }
 
     /**
index 609ad44c00be9e7f2eb0c03e7d5eec040919745a..4592a15a885112d246d4b6fa727e9b5b29a8c734 100644 (file)
@@ -48,6 +48,29 @@ public abstract class CdmEditorPresenterBase<DTO, CDM extends CdmBase, V extends
 
     private static final Logger logger = Logger.getLogger(CdmEditorPresenterBase.class);
 
+    protected BeanInstantiator<DTO> beanInstantiator = null;
+
+
+    /**
+     * @param beanInstantiator the beanInstantiator to set
+     */
+    public void setBeanInstantiator(BeanInstantiator<DTO> beanInstantiator) {
+        this.beanInstantiator = beanInstantiator;
+    }
+
+
+    protected DTO createNewBean() {
+        if(this.beanInstantiator != null){
+            return beanInstantiator.createNewBean();
+        }
+        return defaultBeanInstantiator().createNewBean();
+    }
+
+    /**
+     * @return
+     */
+    protected abstract BeanInstantiator<DTO> defaultBeanInstantiator();
+
     /**
      * if not null, this CRUD set is to be used to create a CdmAuthoritiy for the base entity which will be
      * granted to the current use as long this grant is not assigned yet.