Merge branch 'release/5.26.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / newWizard / NewTaxonWizard.java
index 1947c452fc8605984f61a867df5e05cdc26d2c1e..a3aa31281a13aaccd197d5e0641e48db09065f87 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
 * European Distributed Institute of Taxonomy
@@ -10,8 +9,14 @@
 
 package eu.etaxonomy.taxeditor.newWizard;
 
+import java.util.HashSet;
+import java.util.Set;
+
+import eu.etaxonomy.cdm.api.service.INameService;
 import eu.etaxonomy.cdm.api.service.ITaxonService;
-import eu.etaxonomy.cdm.model.name.NonViralName;
+import eu.etaxonomy.cdm.model.name.HybridRelationship;
+import eu.etaxonomy.cdm.model.name.TaxonName;
+import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.ui.section.name.NonViralNameWizardPage;
@@ -25,26 +30,54 @@ import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonWizardPage;
  */
 public class NewTaxonWizard extends AbstractNewEntityWizard<Taxon> {
 
-       private TaxonWizardPage taxonWizardPage;
-
        /** {@inheritDoc} */
        @Override
        public void addPages() {
-           addPage(new NonViralNameWizardPage(formFactory, getConversationHolder(), (NonViralName) getEntity().getName()));
-               taxonWizardPage = new TaxonWizardPage(formFactory, getConversationHolder(), getEntity());
-               addPage(taxonWizardPage);
+           addPage(new TaxonWizardPage(formFactory, getConversationHolder(), getEntity()));
+           addPage(new NonViralNameWizardPage(formFactory, getConversationHolder(), getEntity().getName()));
        }
 
        /** {@inheritDoc} */
        @Override
        protected Taxon createNewEntity() {
-               return Taxon.NewInstance(NonViralName.NewInstance(null), null);
+               return Taxon.NewInstance(TaxonNameFactory.NewNonViralInstance(null), null);
        }
 
        /** {@inheritDoc} */
        @Override
        protected void saveEntity() {
-           CdmStore.getService(ITaxonService.class).save(getEntity());
+// TODO: if new entity is an hybrid, the hybrid names should be saved first.. see newTaxonnodeWizard
+           Taxon taxon = getEntity();
+           if (!taxon.getName().getHybridChildRelations().isEmpty() || !taxon.getName().getHybridParentRelations().isEmpty()){
+            Set<TaxonName> namesToSave = new HashSet<>();
+            for (HybridRelationship hybridRelationship: taxon.getName().getHybridChildRelations()){
+                if (hybridRelationship.getParentName() != taxon.getName()){
+                    if (!hybridRelationship.getParentName().isPersited()){
+                        namesToSave.add(hybridRelationship.getParentName());
+                    }
+               }else if (hybridRelationship.getHybridName() != taxon.getName()){
+                   if (!hybridRelationship.getHybridName().isPersited()){
+                       namesToSave.add(hybridRelationship.getHybridName());
+                   }
+                }
+
+            }
+
+            for (HybridRelationship hybridRelationship: taxon.getName().getHybridParentRelations()){
+                if (hybridRelationship.getParentName() != taxon.getName()){
+                    if (!hybridRelationship.getParentName().isPersited()){
+                        namesToSave.add(hybridRelationship.getParentName());
+                    }
+               }else if (hybridRelationship.getHybridName() != taxon.getName()){
+                   if (!hybridRelationship.getHybridName().isPersited()){
+                       namesToSave.add(hybridRelationship.getHybridName());
+                   }
+                }
+
+            }
+            CdmStore.getService(INameService.class).save(namesToSave);
+        }
+           CdmStore.getService(ITaxonService.class).save(taxon);
        }
 
        /** {@inheritDoc} */