Create taxon/parse name when wizard is closed and not with every key
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / newWizard / NewTaxonNodeWizard.java
index 1d67fd636bb92f1b349127943a0b8e19aebd408d..e8dfc554796b2e2cdc33fdde7584a4a94cecef6c 100644 (file)
@@ -14,7 +14,9 @@ import eu.etaxonomy.cdm.api.application.CdmChangeEvent;
 import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
 import eu.etaxonomy.cdm.api.service.IClassificationService;
 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
+import eu.etaxonomy.cdm.api.service.UpdateResult;
 import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
@@ -39,38 +41,64 @@ public class NewTaxonNodeWizard extends AbstractNewEntityWizard<ITaxonTreeNode>{
 
     @Override
     public void addPages() {
-        taxonNodePage = new TaxonNodeWizardPage(formFactory, getConversationHolder(), getEntity(), wizardPageListener);
+        taxonNodePage = new TaxonNodeWizardPage(formFactory, getConversationHolder(), getEntity(), wizardPageListener, true);
+
         addPage(taxonNodePage);
     }
 
     @Override
     protected void saveEntity() {
-        if(taxonNodePage.getTaxon() == null || StringUtils.isEmpty(taxonNodePage.getTaxon().getName().getFullTitleCache())){
+        Taxon taxon = taxonNodePage.createTaxon();
+        if(taxon == null || StringUtils.isEmpty(taxon.getName().getFullTitleCache())){
             openEmptyEditor = true;
         }else{
             getConversationHolder().bind();
             ITaxonTreeNode parent = getParentTreeNode();
-            Taxon taxon = taxonNodePage.getTaxon();
+            Reference sec = taxon.getSec();
+            taxon.setSec(null);
             try{
-                TaxonNode taxonNode = parent.addChildTaxon(taxon, parent.getReference(), parent.getMicroReference());
-                taxonNode = CdmStore.getService(ITaxonNodeService.class).merge(taxonNode);
-                generatedTaxonNodeUuid = taxonNode.getUuid();
-                Set<CdmBase> affectedObjects = new HashSet<CdmBase>();
-                //FIXME:Remoting Hack for now - need to generalise this
-                // by making the affected objects a set of ICdmBase
-                // Also, this needs to be moved to the new operations
-                // architecture.
-                if(parent instanceof Classification) {
-                    affectedObjects.add((Classification)parent);
-                }
-
-                if(parent instanceof TaxonNode) {
-                    affectedObjects.add((TaxonNode)parent);
+               UUID parentNodeUuid;
+               if(parent instanceof Classification){
+                   parentNodeUuid = ((Classification) parent).getRootNode().getUuid();
+               }
+               else{
+                   parentNodeUuid = parent.getUuid();
+               }
+                UpdateResult result = CdmStore.getService(ITaxonNodeService.class).createNewTaxonNode(parentNodeUuid, taxon, parent.getReference(), parent.getMicroReference());
+
+                //TaxonNode taxonNode = parent.addChildTaxon(taxon, parent.getReference(), parent.getMicroReference());
+
+              /*  if(CdmStore.getCurrentSessionManager().isRemoting()) {
+                    taxonNode = CdmStore.getService(ITaxonNodeService.class).merge(taxonNode);
+                } else {
+                    taxonNode = CdmStore.getService(ITaxonNodeService.class).save(taxonNode);
+                }*/
+                if (result.isOk()){
+                       generatedTaxonNodeUuid = result.getCdmEntity().getUuid();
+                       TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
+                       taxonNode.getTaxon().setSec(sec);
+                       CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
+                    Set<CdmBase> affectedObjects = new HashSet<CdmBase>();
+                    if (result.getUpdatedObjects().iterator().hasNext()){
+                       TaxonNode parentNode = (TaxonNode)result.getUpdatedObjects().iterator().next();
+                       if(parentNode.getParent() == null) {
+                            affectedObjects.add(taxonNode.getClassification());
+                        }
+                       affectedObjects.add(parentNode);
+                    }
+                    if(CdmStore.getCurrentSessionManager().isRemoting()) {
+                        CdmApplicationState.getCurrentDataChangeService()
+                            .fireChangeEvent(new CdmChangeEvent(Action.Create, affectedObjects, NewTaxonNodeWizard.class), true);
+                    }
+                }else{
+                    if (!result.isOk()){
+                        if (!result.getExceptions().isEmpty()){
+                            MessagingUtils.error(getClass(), "Taxon could not be created", result.getExceptions().iterator().next());
+                        }else{
+                            MessagingUtils.warn(getClass(), "Taxon could not be created");
+                        }
+                    }
                 }
-
-                CdmApplicationState.getCurrentDataChangeService()
-                .fireChangeEvent(new CdmChangeEvent(Action.Create, affectedObjects, NewTaxonNodeWizard.class), true);
-
             }catch(IllegalArgumentException e){
                 MessagingUtils.warningDialog("Taxon already exists in classfication", getClass(), e.getMessage());
             }