Add remoting checks for merge, Move to merge call which returns updated transient...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / DerivateViewEditorInput.java
index bed369ad394f34c34fda4a049260540aaa1d16c1..f0d3afe2dfaab718431fceff60fec1ca0dc56a84 100644 (file)
 */
 package eu.etaxonomy.taxeditor.editor.view.derivate;
 
-import java.util.Collection;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
 
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IPersistableElement;
 
+import eu.etaxonomy.cdm.api.application.CdmApplicationState;
+import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
+import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
+import eu.etaxonomy.taxeditor.editor.EditorUtil;
+import eu.etaxonomy.taxeditor.editor.Messages;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
 
 /**
  * Editor input for the {@link DerivateView} which holds the currently selected derivate for which
  * the derivate hierarchy should be shown in the DerivateView.<br>
- * It also holds a {@link FieldUnit} which is the root of the hierarchy. (both may be the same object)
+ * It also holds a {@link SpecimenOrObservationBase} which is the root of the hierarchy. (both may be the same object)
  * @author pplitzner
  * @date 25.11.2013
  *
  */
-public class DerivateViewEditorInput implements IEditorInput {
+public class DerivateViewEditorInput extends CdmEntitySessionInput implements IEditorInput {
 
     /**
-     * The selected derivate
+     * The selected derivate {@link UUID}s
      */
-    private final SpecimenOrObservationBase<?> derivate;
+    private final Set<SpecimenOrObservationBase<?>> derivateEntities;
     /**
-     * The root of the hierarchy (may be the same object as the derivate)
+     * List of the {@link UUID}s of the root elements of the hierarchy (may be the same objects as the derivates)
      */
-    private FieldUnit fieldUnit;
+    private Set<SpecimenOrObservationBase<?>> rootEntities;
+    private Set<UUID> rootUUIDs;
+
+    private final ConversationHolder conversationHolder;
+
+    private static final List<String> SPECIMEN_INIT_STRATEGY = Arrays.asList(new String[] {
+            "descriptions",
+            "annotations",
+            "markers",
+            "credits",
+            "extensions",
+            "rights",
+            "sources",
+            "derivationEvents.derivatives.annotations",
+            "derivationEvents.derivatives.markers",
+            "derivationEvents.derivatives.credits",
+            "derivationEvents.derivatives.extensions",
+            "derivationEvents.derivatives.rights",
+            "derivationEvents.derivatives.sources"
+    });
 
     /**
-     * Creates an editor input for the {@link DerivateView} with the currently selected derivate and the
-     * corresponding {@link FieldUnit} (both may be the same object).
-     * @param derivate the derivate for which the derivate hierarchy should be shown
-     * @param fieldUnit the root of the hierarchy
+     * Creates an editor input for the {@link DerivateView} with the currently selected derivates and the
+     * corresponding {@link FieldUnit}s (both may be the same object).
+     * @param derivateUuids the {@link UUID}s of the derivates for which the derivate hierarchy should be shown
+     * @param rootUUIDs the root of the hierarchy
      */
-    public DerivateViewEditorInput(SpecimenOrObservationBase<?> derivate) {
-        super();
-        this.derivate = derivate;
-        if(derivate instanceof FieldUnit){
-            this.fieldUnit = (FieldUnit) derivate;
-        }
-        else if(derivate instanceof DerivedUnit){
-            Collection<FieldUnit> fieldUnits = CdmStore.getCurrentApplicationConfiguration().getOccurrenceService().getFieldUnits((DerivedUnit) derivate);
-            if(!fieldUnits.isEmpty()){
-                // TODO think about handling multiple parent FieldUnits
-                this.fieldUnit = fieldUnits.iterator().next();
+    public DerivateViewEditorInput(Set<UUID> derivateUuids) {
+        super(false);
+        rootUUIDs = derivateUuids;
+        //FIXME:Remoting temporary hack for making the sessions work
+        //This should ideally be changed to initializing the
+        //super class with a collection of (id) objects which can
+        //then be used for the hashCode, equals methods
+        initSession();
+        this.conversationHolder = CdmStore.createConversation();
+        this.derivateEntities = new HashSet<SpecimenOrObservationBase<?>>();
+        this.rootEntities = new HashSet<SpecimenOrObservationBase<?>>();
+        for (UUID uuid : derivateUuids) {
+            SpecimenOrObservationBase<?> derivate = CdmStore.getService(IOccurrenceService.class).load(uuid, SPECIMEN_INIT_STRATEGY);
+            derivateEntities.add(derivate);
+            if(derivate instanceof FieldUnit){
+                rootEntities.add(derivate);
             }
+            else if(derivate instanceof DerivedUnit){
+                SpecimenOrObservationBase<?> topMostDerivate = EditorUtil.getTopMostDerivate(derivate);
+                if(topMostDerivate!=null){
+                    rootEntities.add(topMostDerivate);
+                }
+            }
+        }
+        if(rootEntities.isEmpty()){
+            rootEntities = derivateEntities;
         }
-        if(fieldUnit==null){
-            AbstractUtility.errorDialog("Failed initializing editor", DerivateViewEditorInput.class, "No FieldUnit found!");
+        if(rootEntities.isEmpty()){
+            MessagingUtils.messageDialog(Messages.DerivateViewEditorInput_FAIL_INIT, DerivateViewEditorInput.class, Messages.DerivateViewEditorInput_NO_ROOT);
         }
+
+
     }
 
     /* (non-Javadoc)
@@ -95,7 +144,7 @@ public class DerivateViewEditorInput implements IEditorInput {
      */
     @Override
     public String getName() {
-        return derivate.toString();
+        return getEditorName();
     }
 
     /* (non-Javadoc)
@@ -111,22 +160,40 @@ public class DerivateViewEditorInput implements IEditorInput {
      */
     @Override
     public String getToolTipText() {
-        return derivate.toString();
+        return getEditorName();
     }
 
+    private String getEditorName() {
+        String name = null;
+        for( SpecimenOrObservationBase<?> specimen : rootEntities){
+            if(specimen!=null){
+                if(name==null){
+                    name = DerivateLabelProvider.getDerivateText(specimen, conversationHolder);
+                }
+                else{
+                    name += " + "+DerivateLabelProvider.getDerivateText(specimen, conversationHolder); //$NON-NLS-1$
+                }
+            }
+        }
+        return name;
+    }
 
-    /**
-     * @return the specimen
-     */
-    public SpecimenOrObservationBase<?> getDerivate() {
-        return derivate;
+    @Override
+    public Set<SpecimenOrObservationBase<?>> getRootEntities() {
+        return rootEntities;
     }
 
-    /**
-     * @return the fieldUnit
-     */
-    public FieldUnit getFieldUnit() {
-        return fieldUnit;
+    public Set<SpecimenOrObservationBase<?>> getDerivateEntities() {
+        return derivateEntities;
+    }
+
+    public void addRootEntity(SpecimenOrObservationBase<?> root){
+        rootEntities.add(root);
+    }
+
+
+    public ConversationHolder getConversationHolder() {
+        return conversationHolder;
     }
 
     /* (non-Javadoc)
@@ -136,7 +203,7 @@ public class DerivateViewEditorInput implements IEditorInput {
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((fieldUnit == null) ? 0 : fieldUnit.hashCode());
+        result = prime * result + ((rootUUIDs == null) ? 0 : rootUUIDs.hashCode());
         return result;
     }
 
@@ -155,14 +222,47 @@ public class DerivateViewEditorInput implements IEditorInput {
             return false;
         }
         DerivateViewEditorInput other = (DerivateViewEditorInput) obj;
-        if (fieldUnit == null) {
-            if (other.fieldUnit != null) {
+        if (rootUUIDs == null) {
+            if (other.rootUUIDs != null) {
                 return false;
             }
-        } else if (!fieldUnit.equals(other.fieldUnit)) {
+        } else if (!rootUUIDs.equals(other.rootUUIDs)) {
             return false;
         }
         return true;
     }
 
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#merge()
+     */
+    @Override
+    public void merge() {
+        if(CdmStore.getCurrentSessionManager().isRemoting()) {
+            List<SpecimenOrObservationBase> mergedEntities = CdmApplicationState.getCurrentAppConfig().getOccurrenceService().merge(new ArrayList(getRootEntities()), true);
+        }
+
+    }
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
+     */
+    @Override
+    public Map<Object, List<String>> getPropertyPathsMap() {
+        List<String> specimenPropertyPaths = Arrays.asList(new String[] {
+                "descriptions",
+                "derivationEvents.derivates",
+                "annotations",
+                "markers",
+                "credits",
+                "extensions",
+                "rights",
+                "sources"
+        });
+        Map<Object, List<String>> specimenPropertyPathMap =
+                new HashMap<Object, List<String>>();
+        specimenPropertyPathMap.put(SpecimenOrObservationBase.class,specimenPropertyPaths);
+        return specimenPropertyPathMap;
+    }
+
+
 }