fix delete polytomouskeynode and improve merge in bulk editor
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / MergeGroupHandler.java
index 5c0e8676ed02da8bbc536308dd3309393683d872..4a8ae8a3ce9df24256ef97a5e99e4312e99a7e11 100644 (file)
@@ -22,12 +22,17 @@ import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.handlers.HandlerUtil;
 import org.eclipse.ui.texteditor.IDocumentProvider;
 
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.strategy.merge.MergeException;
 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityContainer;
 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
 import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * <p>MergeGroupHandler class.</p>
@@ -71,10 +76,41 @@ public class MergeGroupHandler extends AbstractHandler {
                        }                       
                        Object targetEntity = ((IEntityContainer<?>) targetAnnotation).getEntity();
                        
+                       TeamOrPersonBase teamOrPerson = null;
+                       Reference ref = null;
+                       if (targetEntity instanceof TeamOrPersonBase){
+                               teamOrPerson = HibernateProxyHelper.deproxy(targetEntity, TeamOrPersonBase.class);
+                       } else if(targetEntity instanceof Reference){
+                               ref = HibernateProxyHelper.deproxy(targetEntity, Reference.class);
+                       }
                        logger.info("Merging group");
 //                     model.printAnnotations();
                        for (LineAnnotation annotation : candidateAnnotations) {
+                               //first check whether entities are mergeable
+                               
                                
+                       try{
+                               if (ref != null){
+                                       Reference ref2 = HibernateProxyHelper.deproxy(annotation.getEntity(), Reference.class);
+                                       
+                                       if (!CdmStore.getCommonService().isMergeable(ref, ref2, null)){
+                                               MessageDialog.openWarning(HandlerUtil.getActiveShell(event), 
+                                                               "No merge possible", "A merge of " + ref.getTitleCache() + " and " + ref2.getTitleCache() + " is not possible.");
+                                               return null;
+                                       }
+                               }
+                               if (teamOrPerson != null){
+                                       TeamOrPersonBase teamOrPerson2 = HibernateProxyHelper.deproxy(annotation.getEntity(), TeamOrPersonBase.class);
+                                       
+                                       if (!CdmStore.getCommonService().isMergeable(teamOrPerson, teamOrPerson2, null)){
+                                               MessageDialog.openWarning(HandlerUtil.getActiveShell(event), 
+                                                               "No merge possible", "A merge of " + teamOrPerson.getTitleCache() + " and " + teamOrPerson2.getTitleCache() + " is not possible.");
+                                               return null;
+                                       }
+                               }
+                       }catch(MergeException e){
+                                       
+                       }
                                ((BulkEditor) editor).removeAnnotatedLine(annotation);
                                
                                // Mark entity container for merging with target entity