cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / e4 / AbstractCdmEditorPartE4.java
index 770f20109641b359dac18ab3938a88d2c56c270d..de6e851c89c971f162340653f453ca9c186a6afe 100644 (file)
@@ -8,6 +8,7 @@
 */
 package eu.etaxonomy.taxeditor.view.e4;
 
+import java.util.Collection;
 import java.util.Set;
 
 import javax.annotation.PreDestroy;
@@ -28,23 +29,30 @@ import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.swt.SWTException;
+import org.springframework.security.core.GrantedAuthority;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
+import eu.etaxonomy.cdm.api.service.ITermNodeService;
 import eu.etaxonomy.cdm.api.service.ITermService;
 import eu.etaxonomy.cdm.api.service.IVocabularyService;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.name.TaxonName;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
 import eu.etaxonomy.cdm.persistence.dto.TermDto;
+import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
 import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
+import eu.etaxonomy.taxeditor.editor.IBulkEditor;
 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.security.RequiredPermissions;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
 import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
@@ -54,19 +62,19 @@ import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
 /**
  * @author pplitzner
  * @since Aug 10, 2017
- *
  */
 public abstract class AbstractCdmEditorPartE4
         implements IConversationEnabled, IDirtyMarkable, ISelectionElementEditingPart, IPostOperationEnabled{
 
-    private DelaySelection delaySelection = null;
+       private static final Logger logger = Logger.getLogger(AbstractCdmEditorPartE4.class);
+
+       private DelaySelection delaySelection = null;
     /**
      * This is the monitor for the DelaySelection runnable.
      * If it is <code>true</code> then it is currently delaying a selection.
      */
     private boolean isInDelay;
-    private boolean isEnabled;
-    private static final Logger logger = Logger.getLogger(AbstractCdmEditorPartE4.class);
+    private boolean isEnabled = true;
 
     /**
      * This class invokes internal_selectionChanged() in a separate thread.
@@ -105,7 +113,6 @@ public abstract class AbstractCdmEditorPartE4
         public synchronized void setThisPart(MPart thisPart) {
             this.thisPart = thisPart;
         }
-
     }
 
     protected Viewer viewer;
@@ -130,6 +137,10 @@ public abstract class AbstractCdmEditorPartE4
 
     protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
 
+    protected boolean showEmptyIfNoActiveEditor(){
+        return true;
+    }
+
     @Inject
     public void selectionChanged(
             @Optional@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
@@ -145,32 +156,33 @@ public abstract class AbstractCdmEditorPartE4
             return;
         }
         // no active editor found
-        if(activePart==thisPart && WorkbenchUtility.getActiveEditorPart(partService)==null){
+        if(activePart==thisPart && WorkbenchUtility.getActiveEditorPart(partService)==null && showEmptyIfNoActiveEditor()){
             showEmptyPage();
             return;
         }
         if (viewer != null && viewer.getControl()!= null && viewer.getInput() != null && !viewer.getControl().isDisposed()){
            try{
-               viewer.getControl().setEnabled(true);
+               viewer.getControl().setEnabled(isEnabled);
            }catch(SWTException e){
               logger.debug("Something went wrong for viewer.getControl().setEnabled(true) in " + this.getClass().getSimpleName(), e);
            }
-
         }
 
-        if((previousSelection!=null && selection!=null) && (activePart != null &&  selectionProvidingPart != null && activePart.equals(selectionProvidingPart)) &&
-                (previousSelection==selection
-                || previousSelection.equals(selection)
-                || new StructuredSelection(selection).equals(previousSelection))
+        if((previousSelection!=null && selection!=null)
+               && (activePart != null &&  selectionProvidingPart != null && activePart.equals(selectionProvidingPart))
+               && (previousSelection==selection
+                    || previousSelection.equals(selection)
+                    || new StructuredSelection(selection).equals(previousSelection))
                 ) {
             return;
         }
         if(delaySelection==null){
             delaySelection = new DelaySelection(selection, activePart, thisPart);
+        }else{
+            delaySelection.setSelection(selection);
+            delaySelection.setActivePart(activePart);
+            delaySelection.setThisPart(thisPart);
         }
-        delaySelection.setSelection(selection);
-        delaySelection.setActivePart(activePart);
-        delaySelection.setThisPart(thisPart);
         if(!isInDelay){
             isInDelay = true;
             sync.asyncExec(delaySelection);
@@ -178,7 +190,6 @@ public abstract class AbstractCdmEditorPartE4
         }
     }
 
-    /** {@inheritDoc} */
     @Override
     public void changed(Object object) {
         if(selectionProvidingPart!=null){
@@ -186,6 +197,9 @@ public abstract class AbstractCdmEditorPartE4
             if(part instanceof IDirtyMarkable){
                 ((IDirtyMarkable) part).changed(object);
             }
+            if (part instanceof IBulkEditor){
+               ((IBulkEditor)part).setSelection(createSelection(object));
+            }
         }
     }
 
@@ -193,33 +207,71 @@ public abstract class AbstractCdmEditorPartE4
         return viewer;
     }
 
+    public boolean isEnabled() {
+        return isEnabled;
+    }
+
+    public void setEnabled(boolean isEnabled) {
+        this.isEnabled = isEnabled;
+    }
+
     protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
+        if(CdmStore.getCurrentSessionManager().getActiveSession()==null){
+            return;
+        }
         if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
             Object element = selection.getFirstElement();
             Object part = createPartObject(activePart);
+            if (viewer.getControl().isDisposed()){
+                return;
+            }
             viewer.getControl().setEnabled(true);
-            if(selection.getFirstElement()!=null){
+            if(element != null){
                 if (element instanceof Taxon){
+
                     Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
-                    if (taxon.isMisapplication() || taxon.isProparteSynonym() || taxon.isInvalidDesignation()){
+                    if (part instanceof ITaxonEditor){
+                        TaxonNode node = ((ITaxonEditor) part).getTaxonNode();
+                        if (node != null){
+                            boolean doEnable = CdmStore.currentAuthentiationHasPermission(node,
+                                    RequiredPermissions.TAXON_EDIT);
+                            if (!doEnable){
+                                 //check whether there are explicit TaxonNode rights
+                                 boolean taxonnodePermissionExists = false;
+                                 Collection<? extends GrantedAuthority> authorities = CdmStore.getCurrentAuthentiation().getAuthorities();
+                                 for (GrantedAuthority grantedAuthority: authorities){
+                                     if (grantedAuthority.getAuthority().startsWith("TAXONNODE")){
+                                         taxonnodePermissionExists = true;
+                                     }
+                                 }
+                                 if (!taxonnodePermissionExists){
+                                     doEnable = true;
+                                 }
+                            }
+
+                            //TODO: differ between the views
+                            this.isEnabled = doEnable;
+                        }
+                    }
+                    if (taxon.isMisapplication() || taxon.isProparteSynonym() ){
 
                         if(part instanceof ITaxonEditor){
                             Taxon accepted = ((ITaxonEditor) part).getTaxon();
+
                             Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
 
                             if (rels != null && rels.iterator().hasNext() && !taxon.equals(accepted)){
                                 TaxonRelationship rel = rels.iterator().next();
-                                if ((rel.getType().isMisappliedNameOrInvalidDesignation() || rel.getType().isAnySynonym()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
+                                if ((rel.getType().isMisappliedName() || rel.getType().isAnySynonym()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
                                     viewer.setInput(rel);
                                     selectionProvidingPart = activePart;
                                     return;
                                 }
                             }
                         }
-
-
                     }
                 }
+
                 //unwrap term DTOs
                 if(element instanceof TermDto){
                     element = CdmStore.getService(ITermService.class).load(((TermDto) element).getUuid());
@@ -228,23 +280,33 @@ public abstract class AbstractCdmEditorPartE4
                     element = CdmStore.getService(IVocabularyService.class).load(((TermVocabularyDto) element).getUuid());
                 }
 
+
                 selectionProvidingPart = activePart;
                 if (viewer instanceof DetailsViewerE4){
-                    ((DetailsViewerE4)viewer).setInput(element, part);
-                    if (!selectionProvidingPart.getElementId().equals("eu.etaxonomy.taxeditor.editor.view.checklist.e4.DistributionEditorPart")){
-                        ((DetailsViewerE4)viewer).setDetailsEnabled(true);
-                    }
 
+                    if (selectionProvidingPart.getElementId().equals("eu.etaxonomy.taxeditor.editor.view.checklist.e4.DistributionEditorPart")){
+                        ((DetailsViewerE4)viewer).setDetailsEnabled(false);
+                    }else{
+                        ((DetailsViewerE4)viewer).setDetailsEnabled(isEnabled);
+                    }
+                    ((DetailsViewerE4)viewer).setInput(element, part);
+                   
                 }
 
                 else{
+                    if(element instanceof TermNodeDto){
+                        element = CdmStore.getService(ITermNodeService.class).load(((TermNodeDto) element).getUuid());
+                    }
                     if (activePart.getObject() instanceof DetailsPartE4 && element instanceof TaxonName){
                         selectionProvidingPart = ((DetailsPartE4)activePart.getObject()).getSelectionProvidingPart();
                     }
+
                     viewer.setInput(element);
+                    viewer.getControl().setEnabled(isEnabled);
                 }
             }
         }
+        
     }
 
     protected Object createPartObject(MPart activePart) {
@@ -271,11 +333,10 @@ public abstract class AbstractCdmEditorPartE4
                             null,
                             e,
                             true);
-
                 }
             }
-
         }
+
         reset();
         if(thisPart!=null){
             thisPart.setLabel(getViewName());
@@ -296,9 +357,6 @@ public abstract class AbstractCdmEditorPartE4
         return structuredSelection;
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public ConversationHolder getConversationHolder() {
         if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
@@ -307,27 +365,22 @@ public abstract class AbstractCdmEditorPartE4
         return null;
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public boolean postOperation(Object objectAffectedByOperation) {
         changed(objectAffectedByOperation);
+//        Object part = selectionProvidingPart.getObject();
+//        if (part instanceof IBulkEditor){
+//             ((IBulkEditor)part).setSelection(createSelection(objectAffectedByOperation));
+//        }
         return true;
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public boolean onComplete() {
         viewer.refresh();
         return true;
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public MPart getSelectionProvidingPart() {
         return selectionProvidingPart;
@@ -349,16 +402,10 @@ public abstract class AbstractCdmEditorPartE4
 
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void update(CdmDataChangeMap arg0) {
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void forceDirty() {
     }