ref #5971: new main menu admin with cdm preference nomenclatural code and insert...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / TaxonEditorInput.java
index ff10a6f5b6c9418d5d5f6405af5e5f343fa3784e..782cd3da554dda7a52435064945b114108542814 100644 (file)
@@ -9,9 +9,11 @@
 
 package eu.etaxonomy.taxeditor.editor;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
@@ -48,7 +50,7 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  * @created 19.03.2009
  * @version 1.0
  */
-public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorInput, IConversationEnabled, IPersistableElement {
+public class TaxonEditorInput  extends CdmEntitySessionInput implements IEditorInput, IConversationEnabled, IPersistableElement {
 
     private final ConversationHolder conversation;
 
@@ -58,8 +60,6 @@ public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorIn
 
     private TaxonBase initiallySelectedTaxonBase;
 
-    private final ITaxonNodeService service;
-
     private enum CdmType {
         TAXON_NODE,
         TAXON_BASE,
@@ -67,8 +67,8 @@ public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorIn
     }
 
     private TaxonEditorInput(UUID uuid, CdmType type) {
+        super(true);
         this.conversation = CdmStore.createConversation();
-        service = CdmStore.getService(ITaxonNodeService.class);
         switch(type) {
         case PARENT_TAXON_NODE:
             initForParentTaxonNode(uuid);
@@ -97,23 +97,17 @@ public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorIn
     private void initForTaxonNode(UUID taxonNodeUuid) {
 
 
-        TaxonNode taxonNode = getCdmEntitySession().remoteLoad(service,taxonNodeUuid, null);
+        TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
 
         if(taxonNode == null){
             MessagingUtils.warningDialog("Not yet implemented", TaxonEditorInput.class, "Selected element is not type TaxonBase.");
         }
         init(taxonNode);
 
-
-               if(taxonNode == null){
-                       MessagingUtils.warningDialog("Not yet implemented", TaxonEditorInput.class, "Selected element is not of type TaxonNode but [null].");
-                       return null;
-               }
     }
 
     private void initForTaxonBase(UUID taxonBaseUuid) {
-
-        TaxonBase taxonBase = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonService.class),taxonBaseUuid);
+        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
         if (taxonBase != null){
             if(taxonBase.isInstanceOf(Taxon.class)){
                 Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
@@ -137,7 +131,11 @@ public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorIn
             }else if(taxonBase instanceof Synonym){
                 Synonym synonym = (Synonym) taxonBase;
 
-                Set<Taxon> taxa = synonym.getAcceptedTaxa();
+                Set<Taxon> taxa = new HashSet<>();
+                Taxon taxon = synonym.getAcceptedTaxon();
+                if (taxon != null){
+                       taxa.add(taxon);
+                }
                 setInputForMultipleTaxa(conversation, taxa);
             }
         }
@@ -160,51 +158,12 @@ public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorIn
         TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
 
         // add the new taxon to the editors persistence context
-        UUID newTaxonNodeUuid = getCdmEntitySession().remoteSave(CdmStore.getService(ITaxonNodeService.class),newTaxonNode);
+        UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
 
         initForTaxonNode(newTaxonNodeUuid);
     }
 
-    public static TaxonEditorInput NewInstanceFromTaxonBase(UUID taxonBaseUuid){
-       ConversationHolder conversation = CdmStore.createConversation();
-
-       TaxonEditorInput input = null;
-
-       TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
-       if (taxonBase != null){
-               if(taxonBase.isInstanceOf(Taxon.class)){
-                       Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
-
-                       if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
-                               // TODO get accepted taxon
-                               MessagingUtils.info("trying to open misapplied Name ");
-
-                               Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
-                               Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
-                               for(TaxonRelationship relation : relations){
-                                       if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
-                                               acceptedTaxa.add(relation.getToTaxon());
-                                       }
-                               }
-                               input =  getInputForMultipleTaxa(conversation, acceptedTaxa);
-
-                       }else{
-                               input = getInputForMultipleNodes(conversation, taxon.getTaxonNodes());
-                       }
-               }else if(taxonBase instanceof Synonym){
-                       Synonym synonym = (Synonym) taxonBase;
-
-                       Set<Taxon> taxa = synonym.getAcceptedTaxa();
-                       input = getInputForMultipleTaxa(conversation, taxa);
-               }
-               if (input != null){
-                       input.setInitiallySelectedTaxonBase(taxonBase);
-               }
-       }
-
-
-       return input;
-    }
+
 
 
     private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
@@ -216,9 +175,11 @@ public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorIn
             if(taxonNode != null){
                 init(taxonNode);
             }
-        }else if(taxonNodes.size() == 0){
+        } else if (taxonNodes.size() == 0) {
             // this is an undesired state
-            MessagingUtils.warningDialog("Incorrect state", TaxonEditorInput.class, "The accepted taxon is not part of any classification. This should not have happened.");
+            MessagingUtils.warningDialog("Incorrect state",TaxonEditorInput.class,"The accepted "
+                    + "taxon is not part of any classification. Editing with the "
+                    + "name editor is currently not implemented. Try to edit the taxon with the bulk editor.");
         }
     }
 
@@ -263,8 +224,6 @@ public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorIn
     }
 
 
-
-
     /**
      * <p>NewEmptyInstance</p>
      *
@@ -383,8 +342,8 @@ public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorIn
     public boolean equals(Object obj) {
         if (TaxonEditorInput.class.equals(obj.getClass())
                 && getTaxon() != null
-                && getTaxon().equals(((TaxonEditorInput) obj).getTaxon())){
-            if(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase() != null){
+                && getTaxon().equals(((TaxonEditorInput) obj).getTaxon())) {
+            if (((TaxonEditorInput) obj).getInitiallySelectedTaxonBase() != null){
                 setInitiallySelectedTaxonBase(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase());
             }
             return true;
@@ -480,34 +439,78 @@ public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorIn
         return initiallySelectedTaxonBase;
     }
 
-    @Override
-    public String toString() {
-        return String.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
-    }
+//    @Override
+//    public String toString() {
+//        return String.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
+//    }
+
 
     /* (non-Javadoc)
-     * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#getService()
+     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
      */
     @Override
-    public ITaxonNodeService getService() {
-        return service;
+    public List<TaxonNode> getRootEntities() {
+        return Arrays.asList(taxonNode);
     }
 
-
     /* (non-Javadoc)
-     * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#getRootEntities()
+     * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#merge()
      */
     @Override
-    public  List<TaxonNode> getRootEntities() {
-        return Arrays.asList(taxonNode);
-    }
+    public void merge() {
+       CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
 
+    }
 
-    /* (non-Javadoc)
-     * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#update()
-     */
     @Override
-    public <T extends CdmBase> void update() {
-        taxonNode = getCdmEntitySession().remoteUpdate(service, taxonNode);
+    public Map<Object, List<String>> getPropertyPathsMap() {
+        return null;
     }
+
+    private List<String> getTaxonNodePropertyPaths() {
+        List<String> taxonNodePropertyPaths = new ArrayList<String>();
+        for(String propertyPath : getTaxonBasePropertyPaths()) {
+            taxonNodePropertyPaths.add("taxon." + propertyPath);
+        }
+        return taxonNodePropertyPaths;
+    }
+
+    private List<String> getTaxonBasePropertyPaths() {
+        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
+                "sec",
+                "createdBy",
+                "updatedBy",
+                "annotations",
+                "markers",
+                "credits",
+                "extensions",
+                "rights",
+                "sources",
+                "descriptions",
+                "relationsToThisTaxon",
+                "relationsFromThisTaxon",
+                "taxonNodes",
+                "descriptions.descriptionElements.feature",
+                "descriptions.descriptionElements.area",
+                "descriptions.descriptionElements.status",
+                "descriptions.markers",
+                "name.descriptions",
+                "name.typeDesignations",
+                "name.status",
+                "name.nomenclaturalReference.inReference",
+                "name.taxonBases.taxonNodes",
+                "name.relationsFromThisName",
+                "name.relationsToThisName",
+                "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status",
+                "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName",
+                "synonymRelations.synonym.name.status.type",
+                "synonymRelations.synonym.name.relationsToThisName.fromName",
+                "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship",
+                "synonymRelations.synonym.name.nomenclaturalReference.authorship",
+                "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations"
+        });
+
+        return taxonBasePropertyPaths;
+    }
+
 }