adapt master to develop
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / operation / SwapSynonymAndAcceptedOperation.java
index 4d868010a107385f582ee147868398d9f9344ee1..f486d35d8865a8c08c4c45f7f39eee1cdc27ddb3 100644 (file)
@@ -6,11 +6,12 @@
 * 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.name.operation;
 
-import java.util.ArrayList;
-import java.util.List;
+//=======
+//import java.util.UUID;
+//>>>>>>> Stashed changes
+import java.util.UUID;
 
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.operations.IUndoContext;
@@ -20,16 +21,18 @@ import org.eclipse.core.runtime.IStatus;
 
 import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.api.service.UpdateResult;
+import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingSwapEnum;
+//=======
+//import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
+//>>>>>>> Stashed changes
 import eu.etaxonomy.cdm.model.taxon.Synonym;
-import eu.etaxonomy.cdm.model.taxon.SynonymType;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
-import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.taxeditor.editor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
 import eu.etaxonomy.taxeditor.store.CdmStore;
-import src.main.java.eu.etaxonomy.taxeditor.editor.l10n.Messages;
 
 /**
  * <p>SwapSynonymAndAcceptedOperation class.</p>
@@ -40,13 +43,13 @@ import src.main.java.eu.etaxonomy.taxeditor.editor.l10n.Messages;
 public class SwapSynonymAndAcceptedOperation extends AbstractPostTaxonOperation {
 
        private final Synonym synonym;
-       // TODO store the old relationship for undo reasons
-       private SynonymType synonymType;
-
-       private List<TaxonNode> nodes;
 
+       private SecReferenceHandlingSwapEnum secHandling;
        private boolean setNameInSource;
 
+       private UUID newSecAcc = null;
+       private UUID newSecSyn = null;
+
        /**
         * <p>Constructor for SwapSynonymAndAcceptedOperation.</p>
         *
@@ -58,41 +61,87 @@ public class SwapSynonymAndAcceptedOperation extends AbstractPostTaxonOperation
         */
        public SwapSynonymAndAcceptedOperation(String label, IUndoContext undoContext,
                        Taxon taxon, Synonym synonym, IPostOperationEnabled postOperationEnabled,
-                       ICdmEntitySessionEnabled cdmEntitySessionEnabled, boolean setNameInSource) {
+                       ICdmEntitySessionEnabled cdmEntitySessionEnabled, boolean setNameInSource, SecReferenceHandlingSwapEnum secHandling, UUID newSecAcc, UUID newSecSyn) {
                super(Messages.SwapSynonymAndAcceptedOperation_SWAP_SYN_ACC_TAXON, undoContext, taxon, postOperationEnabled, cdmEntitySessionEnabled);
                this.setNameInSource = setNameInSource;
+               this.secHandling = secHandling;
                this.synonym = synonym;
-               this.nodes = new ArrayList(taxon.getTaxonNodes());
-
-
+               this.newSecAcc = newSecAcc;
+               this.newSecSyn = newSecSyn;
        }
 
-       /** {@inheritDoc} */
        @Override
        public IStatus execute(IProgressMonitor monitor, IAdaptable info)
                        throws ExecutionException {
 
                monitor.worked(20);
-
-               UpdateResult result = CdmStore.getService(ITaxonService.class).swapSynonymAndAcceptedTaxon(synonym.getUuid(), element.getUuid(), setNameInSource);//TODO
-               element = (Taxon)result.getCdmEntity();
-               List<String> propertyPath = new ArrayList<>();
-               propertyPath.add("synonyms");
+/*<<<<<<< Updated upstream
+//             UpdateResult result = CdmStore.getService(ITaxonService.class).swapSynonymAndAcceptedTaxon(synonym.getUuid(), element.getUuid(), setNameInSource, false);//TODO
+               UpdateResult result = CdmStore.getService(ITaxonService.class).swapSynonymAndAcceptedTaxon(synonym.getUuid(), element.getUuid(), setNameInSource, false, secHandling, newSecAcc, newSecSyn);//TODO
+               //note: without reload a multiple representations exceptions appears, not yet tested why
+               Taxon newTaxon = (Taxon)CdmStore.getService(ITaxonService.class).load(result.getCdmEntity().getUuid());
+               Synonym newSyn = null;
+               for (CdmBase cdmBase : result.getUpdatedObjects()){
+                   if (cdmBase instanceof Synonym){
+                       newSyn = (Synonym)cdmBase;
+                   }
+               }
+
+               //for some reason simple cloning of the secundum source does not work if external links exist (throws exception because an OriginalSourceBase_id in an AuditTable (probably OriginalSourceBase_ExternalLink) becomes null)
+
+               //remember oldAcc secSource info
+               Reference oldAccRef = newTaxon.getSec();
+               String oldAccMicroRef = newTaxon.getSecMicroReference();
+               Set<ExternalLink> oldAccLinks = new HashSet<>();
+               if (newTaxon.getSecSource()!= null){
+                   for (ExternalLink link: newTaxon.getSecSource().getLinks()){
+                       oldAccLinks.add(link.clone());
+                   }
+               }
+
+               if (newSyn != null ){
+                   //adapt new taxon secSource info
+                   newTaxon.setSec(newSyn.getSec());
+                   newTaxon.setSecMicroReference(newSyn.getSecMicroReference());
+               if (newTaxon.getSecSource() != null){
+                   newTaxon.getSecSource().getLinks().clear();
+                   if (newSyn.getSecSource() != null){
+                       for (ExternalLink link: newSyn.getSecSource().getLinks()){
+                           newTaxon.getSecSource().addLink(link.clone());
+                       }
+                   }
+               }
+
+               //adapt new synonym secSource info
+               newSyn.setSec(oldAccRef);
+               newSyn.setSecMicroReference(oldAccMicroRef);
+               if (newSyn.getSecSource()!= null){
+                   newSyn.getSecSource().getLinks().clear();
+                   for (ExternalLink link: oldAccLinks){
+                       newSyn.getSecSource().addLink(link);
+                   }
+               }
+               }
+
+               List<TaxonBase> mergeList = new ArrayList<>();
+               mergeList.add(newTaxon);
+               mergeList.add(newSyn);
+               */
+//=======
+               UpdateResult result = CdmStore.getService(ITaxonService.class).swapSynonymAndAcceptedTaxon(synonym.getUuid(), element.getUuid(), setNameInSource, false, secHandling, newSecAcc, newSecSyn);//TODO
+//>>>>>>> Stashed changes
 
                monitor.worked(40);
 
                return postExecute(element);
        }
 
-       /** {@inheritDoc} */
        @Override
        public IStatus redo(IProgressMonitor monitor, IAdaptable info)
                        throws ExecutionException {
-               // TODO Auto-generated method stub
                return execute(monitor, info);
        }
 
-       /** {@inheritDoc} */
        @Override
        public IStatus undo(IProgressMonitor monitor, IAdaptable info)
                        throws ExecutionException {