Fix NPE when connecting to local cdm server with dev settings
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerChooser.java
index 09f3784042fbbc3cd5f4476c1f55c12aefc5968f..e0031327fbe6eadc717671d4ac3a3c5877f2145b 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2015 EDIT
  * European Distributed Institute of Taxonomy
@@ -32,11 +31,17 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.commands.ICommandService;
 import org.eclipse.ui.handlers.IHandlerService;
 
+import eu.etaxonomy.cdm.api.service.IOccurrenceService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.model.common.ICdmBase;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.taxon.Synonym;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * This class opens a popup dialog and provides the possibility to choose from a
@@ -74,7 +79,7 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
         //if only one editor is available then open it
         if(nameViewerMap.size()==1){
             Command command = nameViewerMap.keySet().iterator().next();
-            executeCommand(command.getId(), input);
+            executeCommand(command, input);
         }
         else{
             if(nameViewerMap.isEmpty()){
@@ -84,27 +89,41 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
         }
     }
 
-    private void executeCommand(String commandId, Object input) {
-        ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
-        //get the command from plugin.xml
-        Command command = commandService.getCommand(commandId);
-
+    private void executeCommand(Command command, Object input) {
+        //for generic UuidAndTitleCache objects try to load the object
+        if (input instanceof UuidAndTitleCache){
+            UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache)input;
+            Class type = uuidAndTitleCache.getType();
+            if(type == Taxon.class || type == Synonym.class){
+               input = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
+            }
+            else if(SpecimenOrObservationBase.class.isAssignableFrom(type)){
+               input = CdmStore.getService(IOccurrenceService.class).load(uuidAndTitleCache.getUuid());
+            }
+        }
         //set uuid parameter
         if(input instanceof ICdmBase){
             Map<String, UUID> params = new HashMap<String, UUID>();
-            params.put(commandId+".uuid", ((ICdmBase) input).getUuid());
+            String commandId = command.getId();
+                       params.put(commandId+".uuid", ((ICdmBase) input).getUuid());
+
+                       if(command.isEnabled()) {
 
-            //build the parameterized command
-            ParameterizedCommand pc = ParameterizedCommand.generateCommand(command, params);
+                           //build the parameterized command
+                           ParameterizedCommand pc = ParameterizedCommand.generateCommand(command, params);
 
-            if(command.isEnabled()) {
                 IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
                 try {
-                    handlerService.executeCommand(pc, null);
+                    if(pc!=null){
+                        handlerService.executeCommand(pc, null);
+                    }
+                    else{
+                        handlerService.executeCommand(commandId, null);
+                    }
                 } catch (NotDefinedException nde) {
                     throw new RuntimeException("Could not find open command: " + commandId);
                 } catch (Exception exception) {
-                    MessagingUtils.error(getClass(), "An exception occured while trying execute "+commandId, exception);
+                    MessagingUtils.error(getClass(), "An exception occurred while trying execute "+commandId, exception);
                 }
             }
         }
@@ -125,8 +144,8 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
         ISelection selection = event.getSelection();
         if(selection instanceof IStructuredSelection){
             Object firstElement = ((IStructuredSelection) selection).getFirstElement();
-            if(firstElement instanceof String && nameViewerMap.containsKey(firstElement)){
-                executeCommand((String) firstElement, this.input);
+            if(firstElement instanceof Command && nameViewerMap.containsKey(firstElement)){
+                executeCommand((Command) firstElement, this.input);
                 this.close();
             }
         }