Merge branch 'develop' into taxonDescription
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / e4 / TaxonEditorInputE4.java
index f0627bdc6bc5da81d6da279fab52ddcda654e7b5..ff9634d2f83998e66ca3eb91f4955bc19e1fe54d 100644 (file)
@@ -13,6 +13,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -42,6 +43,7 @@ import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
 import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
+import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleAcceptedTaxaDialog;
 import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
@@ -67,19 +69,19 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
 
     private TaxonNode taxonNode;
 
-    private Map<TaxonBase, TaxonBaseDeletionConfigurator> toDelete = new HashMap<>();
-    private Set<Synonym> toSaveNewSynonym = new HashSet<>();
-    private Set<TaxonBase> toSaveNewConcept = new HashSet<>();
-    private Set<TaxonName> toSaveNewName = new HashSet<>();
+    private Map<TaxonBase, TaxonBaseDeletionConfigurator> toDeletes = new HashMap<>();
+    private Set<Synonym> toSaveNewSynonyms = new HashSet<>();
+    private List<TaxonBase> toSaveNewConcepts = new ArrayList<>();
+    private List<TaxonName> toSaveNewNames = new ArrayList<>();
 
 
     private Set<AbstractPostTaxonOperation> operations = new HashSet<>();
 
     private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
 
-    private TaxonBase initiallySelectedTaxonBase;
+    private TaxonBase<?> initiallySelectedTaxonBase;
 
-    UISynchronize sync;
+    private UISynchronize sync;
 
 
     public void setSync(UISynchronize sync) {
@@ -134,7 +136,7 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
                 Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
                 setInitiallySelectedTaxonBase(taxon);
 
-                if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
+                if ( (taxon.isMisapplication() || taxon.isProparteSynonym())&& (taxon.getTaxonNodes().size() >0 || taxon.getRelationsFromThisTaxon().size() > 1)){
                     // TODO get accepted taxon
                     MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
 
@@ -145,7 +147,10 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
                             acceptedTaxa.add(relation.getToTaxon());
                         }
                     }
-                    setInputForMultipleTaxa(conversation, acceptedTaxa);
+                    if (taxon.getTaxonNodes().size() > 0){
+                        acceptedTaxa.add(taxon);
+                    }
+                    setInputForRelatedTaxa(conversation, acceptedTaxa);
 
                 }else{
                     setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
@@ -185,8 +190,11 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
             taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNode.getUuid(), getTaxonNodePropertyPaths());
             init(taxonNode);
         }else if(taxonNodes.size() > 1){
+
             TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
-            taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
+            if(taxonNode != null){
+                taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
+            }
             if(taxonNode != null){
                 init(taxonNode);
             }
@@ -213,6 +221,32 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
         }
     }
 
+    private void setInputForRelatedTaxa(ConversationHolder conversation, Set<Taxon> taxa){
+        if(taxa.size() == 1){
+            Taxon taxon = taxa.iterator().next();
+            Set<TaxonNode> nodes = taxon.getTaxonNodes();
+            TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(nodes);
+            init(taxonNode);
+        }else if(taxa.size() > 1){
+            Iterator<Taxon> taxonIterator = taxa.iterator();
+            Set<TaxonNode> nodes = new HashSet<>();
+            while (taxonIterator.hasNext()){
+
+                nodes.addAll(taxonIterator.next().getTaxonNodes());
+            }
+            TaxonNode taxonNode = ChooseFromMultipleAcceptedTaxaDialog.choose(nodes);
+            if(taxonNode != null){
+                taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
+            }
+            if(taxonNode != null){
+                init(taxonNode);
+            }
+        } else if (taxa.size() == 0) {
+            // this is an undesired state
+            MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
+        }
+    }
+
     public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) {
         return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
 
@@ -307,10 +341,11 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
     }
 
     public void addToSaveNewSynonym(Synonym toSaveNew) {
-        this.toSaveNewSynonym.add(toSaveNew);
+        this.toSaveNewSynonyms.add(toSaveNew);
     }
     public void addToSaveNewConcept(Taxon toSaveNew) {
-        this.toSaveNewConcept.add(toSaveNew);
+       this.toSaveNewConcepts.add(toSaveNew);
+
     }
 
     @Override
@@ -323,43 +358,43 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
         if (!this.getCdmEntitySession().isActive()){
             this.getCdmEntitySession().bind();
         }
-        for(Entry<TaxonBase, TaxonBaseDeletionConfigurator> entry:toDelete.entrySet()){
+        for(Entry<TaxonBase, TaxonBaseDeletionConfigurator> entry:toDeletes.entrySet()){
             delete(entry.getKey(), entry.getValue());
         }
-        toDelete.clear();
+        toDeletes.clear();
         for(AbstractPostOperation entry:operations){
             EditorUtil.executeOperation(entry,sync);
         }
         operations.clear();
 
-        for (Synonym syn: toSaveNewSynonym){
+        for (Synonym syn: toSaveNewSynonyms){
             for (HybridRelationship rel : syn.getName().getHybridChildRelations()){
-                if (!rel.getParentName().isPersited()) {
-                    toSaveNewName.add(rel.getParentName());
-                }
-                    if (!rel.getHybridName().isPersited()) {
-                   toSaveNewName.add(rel.getHybridName());
-                }
+//                if (!rel.getParentName().isPersited()) {
+                    toSaveNewNames.add(rel.getParentName());
+//                }
+//                    if (!rel.getHybridName().isPersited()) {
+                   toSaveNewNames.add(rel.getHybridName());
+//                }
 
             }
         }
 
-        for (TaxonBase concept: toSaveNewConcept){
+        for (TaxonBase concept: toSaveNewConcepts){
             for (HybridRelationship rel : concept.getName().getHybridChildRelations()){
-                if (!rel.getParentName().isPersited()) {
-                    toSaveNewName.add(rel.getParentName());
-                }
-                if (!rel.getHybridName().isPersited()) {
-                    toSaveNewName.add(rel.getHybridName());
-                }
+//                if (!rel.getParentName().isPersited()) {
+                    toSaveNewNames.add(rel.getParentName());
+//                }
+//                if (!rel.getHybridName().isPersited()) {
+                    toSaveNewNames.add(rel.getHybridName());
+//                }
 
             }
         }
-        CdmStore.getService(INameService.class).saveOrUpdate(toSaveNewName);
-        CdmStore.getService(ITaxonService.class).saveOrUpdate(toSaveNewConcept);
+        CdmStore.getService(INameService.class).merge(toSaveNewNames, true);
+        CdmStore.getService(ITaxonService.class).merge(toSaveNewConcepts, true);
 
-        toSaveNewName.clear();
-        toSaveNewConcept.clear();
+        toSaveNewNames.clear();
+        toSaveNewConcepts.clear();
 
         CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
 
@@ -404,16 +439,6 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
                 "sources", //$NON-NLS-1$
                 "identifiers",
                 "descriptions", //$NON-NLS-1$
-//                "relationsToThisTaxon.relatedFrom.name.taxonBases.taxonNodes", //$NON-NLS-1$
-//                "relationsToThisTaxon.relatedFrom.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
-//                "relationsToThisTaxon.relatedFrom.name.taxonBases.relationsToThisTaxon", //$NON-NLS-1$
-                "relationsToThisTaxon.relatedFrom.name.typeDesignations.typeSpecimen.derivedFrom.originals",//$NON-NLS-1$
-                "relationsToThisTaxon.relatedFrom.name.typeDesignations.typeName",//$NON-NLS-1$
-//                "relationsFromThisTaxon.relatedTo.name.taxonBases.taxonNodes", //$NON-NLS-1$
-//                "relationsFromThisTaxon.relatedTo.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
-//                "relationsFromThisTaxon.relatedTo.name.taxonBases.relationsToThisTaxon",
-                "relationsFromThisTaxon.relatedTo.name.typeDesignations.typeSpecimen.derivedFrom.originals",//$NON-NLS-1$
-                "relationsFromThisTaxon.relatedTo.name.typeDesignations.typeName",//$NON-NLS-1$
                 "taxonNodes", //$NON-NLS-1$
                 "descriptions.descriptionElements.feature", //$NON-NLS-1$
                 "descriptions.descriptionElements.area", //$NON-NLS-1$
@@ -432,8 +457,6 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
                 "synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
                 "synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
                 "synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
-//                "synonyms.name.homotypicalGroup", //$NON-NLS-1$
-//                "synonyms.synonym.name.homotypicalGroup.typifiedNames", //$NON-NLS-1$
                 "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
         });
 
@@ -445,7 +468,7 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
      * @param deleteConfig
      */
     public void addTaxonBaseToDelete(TaxonBase selectedElement, TaxonBaseDeletionConfigurator deleteConfig) {
-        this.toDelete.put(selectedElement, deleteConfig);
+        this.toDeletes.put(selectedElement, deleteConfig);
 
     }
 
@@ -453,7 +476,7 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
      * @param newName
      */
     public void addToSaveNewName(TaxonName newName) {
-        this.toSaveNewName.add(newName);
+        this.toSaveNewNames.add(newName);
     }
 
 }