Merge branch 'release/5.22.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / e4 / TaxonEditorInputE4.java
index 206f6416393576bc82aadf1fb24f155b12bdec7f..acfd4651813a6590493da3bdbaab6ffdae58a268 100644 (file)
@@ -6,28 +6,39 @@
  * The contents of this file are subject to the Mozilla Public License Version 1.1
  * See LICENSE.TXT at the top of this package for the full license terms.
  */
-
 package eu.etaxonomy.taxeditor.editor.e4;
 
 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;
 import java.util.Set;
 import java.util.UUID;
 
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.operations.IOperationHistory;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.e4.ui.di.UISynchronize;
+import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
+
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
 import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.cdm.api.service.INameService;
 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
 import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
 import eu.etaxonomy.cdm.api.service.config.TaxonBaseDeletionConfigurator;
 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
 import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.name.HybridRelationship;
 import eu.etaxonomy.cdm.model.name.TaxonName;
 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
@@ -35,24 +46,26 @@ import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 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.l10n.Messages;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
+import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
-
+import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
- *
  * @author pplitzner
  * @date Aug 24, 2017
- *
  */
-public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConversationEnabled {
+public class TaxonEditorInputE4  extends CdmEntitySessionInput<TaxonNode>
+               implements IConversationEnabled {
 
     private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
 
@@ -60,11 +73,23 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
 
     private TaxonNode taxonNode;
 
-    private Map<TaxonBase, TaxonBaseDeletionConfigurator> toDelete = new HashMap<>();
+    private Map<TaxonBase, TaxonBaseDeletionConfigurator> toDeletes = new HashMap<>();
+    private Set<Synonym> toSaveNewSynonyms = new HashSet<>();
+    private List<TaxonBase> toSaveConcepts = new ArrayList<>();
+    private List<TaxonName> toSaveNewNames = new ArrayList<>();
+
+    private Set<AbstractPostOperation> operations = new HashSet<>();
 
     private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
 
-    private TaxonBase initiallySelectedTaxonBase;
+    private TaxonBase<?> initiallySelectedTaxonBase;
+
+    private UISynchronize sync;
+
+
+    public void setSync(UISynchronize sync) {
+        this.sync = sync;
+    }
 
     private enum CdmType {
         TAXON_NODE,
@@ -92,10 +117,10 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
        this.taxonNode = taxonNode;
     }
 
-
     private void initForTaxonNode(UUID taxonNodeUuid) {
        this.getCdmEntitySession().bind();
         TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
+//     TaxonNode taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNodeUuid, getTaxonNodePropertyPaths());
         if(taxonNode == null){
             MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
         }
@@ -106,24 +131,28 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
 
     private void initForTaxonBase(UUID taxonBaseUuid) {
        this.getCdmEntitySession().bind();
-        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
+//        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
+       TaxonBase<?> taxonBase = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonService.class), taxonBaseUuid, getTaxonBasePropertyPaths());
         if (taxonBase != null){
             if(taxonBase.isInstanceOf(Taxon.class)){
                 Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
                 setInitiallySelectedTaxonBase(taxon);
 
-                if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
+                if ( taxon.isMisapplication() || taxon.isProparteSynonym()){
                     // TODO get accepted taxon
                     MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
 
                     Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
                     Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
                     for(TaxonRelationship relation : relations){
-                        if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
+                        if(relation.getType().isAnyMisappliedName() || relation.getType().isAnySynonym()){
                             acceptedTaxa.add(relation.getToTaxon());
                         }
                     }
-                    setInputForMultipleTaxa(conversation, acceptedTaxa);
+                    if (taxon.getTaxonNodes().size() > 0){
+                        acceptedTaxa.add(taxon);
+                    }
+                    setInputForRelatedTaxa(conversation, acceptedTaxa);
 
                 }else{
                     setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
@@ -160,11 +189,14 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
     private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
         if(taxonNodes.size() == 1){
             TaxonNode taxonNode = taxonNodes.iterator().next();
-            taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
+            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);
             }
@@ -191,6 +223,39 @@ 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 = null;
+            if (nodes.size()>1){
+                taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(nodes);
+            }else if (nodes.size()==1){
+                taxonNode = nodes.iterator().next();
+            }else{
+                MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
+            }
+            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);
 
@@ -254,7 +319,6 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
         return conversation;
     }
 
-    /** {@inheritDoc} */
     @Override
     public void update(CdmDataChangeMap events) {
         if(dataChangeBehavior == null){
@@ -272,6 +336,17 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
         return initiallySelectedTaxonBase;
     }
 
+    public void addOperation(AbstractPostOperation operation) {
+        this.operations.add(operation);
+    }
+
+    public void addToSaveNewSynonym(Synonym toSaveNew) {
+        this.toSaveNewSynonyms.add(toSaveNew);
+    }
+    public void addToSaveConcept(Taxon toSaveNew) {
+       this.toSaveConcepts.add(toSaveNew);
+    }
+
     @Override
     public List<TaxonNode> getRootEntities() {
         return Arrays.asList(taxonNode);
@@ -282,25 +357,85 @@ 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();
-        CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
+        toDeletes.clear();
+
+        // new concepts needs to be saved separately, the list contains all concepts, the
+        for (TaxonBase<?> concept: toSaveConcepts){
+            if (concept != null){
+                if (concept.getName() != null){
+                    for (HybridRelationship rel : concept.getName().getHybridChildRelations()){
+                        toSaveNewNames.add(rel.getParentName());
+                        toSaveNewNames.add(rel.getHybridName());
+                    }
+                }
+            }
+        }
+
+        //handle cascading for hybrid relationshis
+        //accepted taxa
+        if (taxonNode.getTaxon().getName() != null){
+            for (HybridRelationship rel : taxonNode.getTaxon().getName().getHybridChildRelations()){
+                toSaveNewNames.add(rel.getParentName());
+                toSaveNewNames.add(rel.getHybridName());
+            }
+        }
+        //synonyms
+        for (TaxonName synonymName : taxonNode.getTaxon().getSynonymNames()){
+            if (synonymName != null){
+                for (HybridRelationship rel : synonymName.getHybridChildRelations()){
+                    toSaveNewNames.add(rel.getParentName());
+                    toSaveNewNames.add(rel.getHybridName());
+                }
+            }
+        }
+//        for (TaxonName name:toSaveNewNames){
+//            name.get
+//        }
 
+        if (!toSaveNewNames.isEmpty()){
+            CdmStore.getService(INameService.class).merge(toSaveNewNames, true);
+        }
+        if (!toSaveConcepts.isEmpty()){
+            CdmStore.getService(ITaxonService.class).merge(toSaveConcepts, true);
+        }
+
+        toSaveNewNames.clear();
+        toSaveConcepts.clear();
+
+        for(AbstractPostOperation<?> entry:operations){
+            IStatus status = Status.CANCEL_STATUS;
+            final IAdaptable uiInfoAdapter = WorkspaceUndoUtil
+                    .getUIInfoAdapter(AbstractUtility.getShell());
+            String operationlabel = entry.getLabel();
+            try {
+                entry.addContext(IOperationHistory.GLOBAL_UNDO_CONTEXT);
+                status = entry.execute(new NullProgressMonitor(), uiInfoAdapter);
+            } catch (ExecutionException e) {
+                MessagingUtils.operationDialog(AbstractUtility.class, e, TaxeditorStorePlugin.PLUGIN_ID, operationlabel, null);
+            }
+
+            IPostOperationEnabled postOperationEnabled = entry
+                    .getPostOperationEnabled();
+            if (postOperationEnabled != null) {
+                postOperationEnabled.onComplete();
+            }
+
+            //AbstractUtility.executeOperation(entry,sync);
+        }
+
+        operations.clear();
+        CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
     }
 
-    /**
-     * @param key
-     * @param value
-     */
     private void delete(TaxonBase key, TaxonBaseDeletionConfigurator value) {
         if (key instanceof Synonym){
             CdmStore.getService(ITaxonService.class).deleteSynonym(((Synonym)key).getUuid(), (SynonymDeletionConfigurator) value);
         }else{
             CdmStore.getService(ITaxonService.class).deleteTaxon(((Taxon)key).getUuid(), (TaxonDeletionConfigurator) value, null);
         }
-
     }
 
     @Override
@@ -327,40 +462,77 @@ public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConve
                 "extensions", //$NON-NLS-1$
                 "rights", //$NON-NLS-1$
                 "sources", //$NON-NLS-1$
+                "sources.links", //$NON-NLS-1$
+                "identifiers",
                 "descriptions", //$NON-NLS-1$
-                "relationsToThisTaxon", //$NON-NLS-1$
-                "relationsFromThisTaxon", //$NON-NLS-1$
                 "taxonNodes", //$NON-NLS-1$
                 "descriptions.descriptionElements.feature", //$NON-NLS-1$
                 "descriptions.descriptionElements.area", //$NON-NLS-1$
                 "descriptions.descriptionElements.status", //$NON-NLS-1$
+                "descriptions.descriptionElements.markers", //$NON-NLS-1$
+                "descriptions.descriptionElements.sources", //$NON-NLS-1$
+                "descriptions.descriptionElements.annotations", //$NON-NLS-1$
                 "descriptions.markers", //$NON-NLS-1$
+                "descriptions.descriptionSources", //$NON-NLS-1$
+                "descriptions.sources", //$NON-NLS-1$
+                "descriptions.annotations", //$NON-NLS-1$
+                "descriptions.links", //$NON-NLS-1$
                 "name.descriptions", //$NON-NLS-1$
-                "name.typeDesignations", //$NON-NLS-1$
+                "name.typeDesignations.typeSpecimen.derivedFrom.originals", //$NON-NLS-1$
+                "name.typeDesignations.source", //$NON-NLS-1$
+                "name.typeDesignations.source.nameUsedInSource", //$NON-NLS-1$
+                "name.typeDesignations.source.citation", //$NON-NLS-1$
+                "name.typeDesignations.source.links", //$NON-NLS-1$
+                "name.typeDesignations.text", //$NON-NLS-1$
+                "name.typeDesignations.sources", //$NON-NLS-1$
+                "name.typeDesignations.sources.links", //$NON-NLS-1$
                 "name.status", //$NON-NLS-1$
-                "name.nomenclaturalReference.inReference", //$NON-NLS-1$
+                "name.nomenclaturalSource", //$NON-NLS-1$
+                "name.nomenclaturalSource.nameUsedInSource", //$NON-NLS-1$
+                "name.nomenclaturalSource.links.description", //$NON-NLS-1$
+                "name.nomenclaturalSource.citation", //$NON-NLS-1$
+                "name.nomenclaturalSource.citation.inReference", //$NON-NLS-1$
+                "name.nomenclaturalSource.cdmSource", //$NON-NLS-1$
+                "name.nomenclaturalSource.cdmSource.description", //$NON-NLS-1$
+                "name.status.source", //$NON-NLS-1$
+                "name.status.source.links", //$NON-NLS-1$
                 "name.taxonBases.taxonNodes", //$NON-NLS-1$
                 "name.relationsFromThisName", //$NON-NLS-1$
+                "name.relationsFromThisName.source", //$NON-NLS-1$
+                "name.relationsFromThisName.source.links", //$NON-NLS-1$
                 "name.relationsToThisName", //$NON-NLS-1$
-                "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status", //$NON-NLS-1$
+                "name.relationsToThisName.source", //$NON-NLS-1$
+                "name.relationsToThisName.source.links", //$NON-NLS-1$
+                "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
                 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
-                "synonymRelations.synonym.name.status.type", //$NON-NLS-1$
-                "synonymRelations.synonym.name.relationsToThisName.fromName", //$NON-NLS-1$
-                "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
-                "synonymRelations.synonym.name.nomenclaturalReference.authorship", //$NON-NLS-1$
-                "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations" //$NON-NLS-1$
+                "name.rank.vocabulary", //$NON-NLS-1$
+                "synonyms.name.status.type", //$NON-NLS-1$
+                "synonyms.name.relationsToThisName.relatedFrom", //$NON-NLS-1$
+                "synonyms.name.relationsFromThisName.relatedTo", //$NON-NLS-1$
+                "synonyms.name.nomenclaturalSource", //$NON-NLS-1$
+                "synonyms.name.nomenclaturalSource.nameUsedInSource", //$NON-NLS-1$
+                "synonyms.name.nomenclaturalSource.links", //$NON-NLS-1$
+                "synonyms.name.nomenclaturalSource.citation", //$NON-NLS-1$
+                "synonyms.name.nomenclaturalSource.citation.inReference", //$NON-NLS-1$
+                "synonyms.name.nomenclaturalSource.cdmSource", //$NON-NLS-1$
+                "synonyms.name.nomenclaturalSource.cdmSource.description", //$NON-NLS-1$
+                "synonyms.name.nomenclaturalSource.citation.inReference.authorship", //$NON-NLS-1$
+                "synonyms.name.nomenclaturalSource.citation.authorship", //$NON-NLS-1$
+                "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms", //$NON-NLS-1$
+                "relationsFromThisTaxon",//$NON-NLS-1$
+                "relationsToThisTaxon"//$NON-NLS-1$
+
         });
 
         return taxonBasePropertyPaths;
     }
 
-    /**
-     * @param selectedElement
-     * @param deleteConfig
-     */
     public void addTaxonBaseToDelete(TaxonBase selectedElement, TaxonBaseDeletionConfigurator deleteConfig) {
-        this.toDelete.put(selectedElement, deleteConfig);
+        this.toDeletes.put(selectedElement, deleteConfig);
 
     }
 
-}
+    public void addToSaveNewName(TaxonName newName) {
+        this.toSaveNewNames.add(newName);
+    }
+}
\ No newline at end of file