#5299 Handle session caching of new entities
authorCherian Mathew <c.mathew@bgbm.org>
Thu, 1 Oct 2015 13:14:24 +0000 (15:14 +0200)
committerCherian Mathew <c.mathew@bgbm.org>
Thu, 1 Oct 2015 13:14:24 +0000 (15:14 +0200)
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationState.java
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CdmTransientEntityCacher.java
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/CdmEntitySession.java
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/DefaultNewEntityListener.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/ICdmEntitySession.java
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/NullSession.java
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/mock/MockCdmEntitySession.java
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/operation/TaxonNameEditorTest.java
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/session/CdmEntitySessionAwareTest.java
eu.etaxonomy.taxeditor.test/src/test/resources/eu/etaxonomy/taxeditor/operation/TaxonNameEditorTest.xml

index 2c90c55b7fac2e52dd5cfe3000d8ccf23e795b91..1251d3325c3edd00e8a72c9de0dc9b36e54ce826 100644 (file)
@@ -20,7 +20,9 @@ import eu.etaxonomy.cdm.api.service.ICommonService;
 import eu.etaxonomy.cdm.api.service.IService;
 import eu.etaxonomy.cdm.api.service.ITestService;
 import eu.etaxonomy.cdm.io.service.IIOService;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.taxeditor.service.ICachedCommonService;
+import eu.etaxonomy.taxeditor.session.DefaultNewEntityListener;
 
 /**
  * @author cmathew
@@ -43,6 +45,7 @@ public class CdmApplicationState {
     public static CdmApplicationState getInstance() {
         if(cdmApplicationState == null) {
             cdmApplicationState = new CdmApplicationState();
+            CdmBase.setNewEntityListener(new DefaultNewEntityListener());
         }
 
         return cdmApplicationState;
index d92bdad5577d0abf554fe0730bbf2c54577dc058..a811d28db45dc21a1557a1342d218236cfe6c6dd 100644 (file)
@@ -11,10 +11,12 @@ package eu.etaxonomy.taxeditor.remoting.cache;
 
 import java.util.ArrayList;
 import java.util.Collection;
+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 net.sf.ehcache.Cache;
 import net.sf.ehcache.CacheManager;
@@ -63,7 +65,7 @@ public class CdmTransientEntityCacher implements ICdmCacher {
 
     private final CacheLoader cacheLoader;
 
-
+    private final Map<UUID, CdmBase> newEntitiesMap = new HashMap<UUID, CdmBase>();
 
     public CdmTransientEntityCacher(String cacheId, ICdmEntitySessionManager cdmEntitySessionManager) {
         this.cacheId = cacheId;
@@ -75,6 +77,7 @@ public class CdmTransientEntityCacher implements ICdmCacher {
         this.cdmEntitySessionManager = cdmEntitySessionManager;
 
         cacheLoader = new CacheLoader(this);
+
     }
 
     public CdmTransientEntityCacher(Object sessionOwner, ICdmEntitySessionManager cdmEntitySessionManager) {
@@ -186,7 +189,11 @@ public class CdmTransientEntityCacher implements ICdmCacher {
     }
 
 
-
+    public void addNewEntity(CdmBase newEntity) {
+        if(newEntity != null && newEntity.getId() == 0 && newEntity.getUuid() != null) {
+            newEntitiesMap.put(newEntity.getUuid(), newEntity);
+        }
+    }
 
     @Override
     public void put(CdmBase cdmEntity) {
@@ -200,9 +207,16 @@ public class CdmTransientEntityCacher implements ICdmCacher {
 
         cachedCdmEntity = getFromCache(id);
         if(cachedCdmEntity == null) {
-            getCache().put(new Element(id, cdmEntity));
-            cdmEntity.initListener();
-            logger.info(" - object of type " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId() + " put in cache");
+            CdmBase cdmEntityToCache = cdmEntity;
+            CdmBase newEntity = newEntitiesMap.get(cdmEntity.getUuid());
+            if(newEntity != null) {
+                newEntity.setId(cdmEntity.getId());
+                cdmEntityToCache = newEntity;
+            }
+            getCache().put(new Element(id, cdmEntityToCache));
+            cdmEntityToCache.initListener();
+            newEntitiesMap.remove(cdmEntity.getUuid());
+            logger.info(" - object of type " + cdmEntityToCache.getClass().getName() + " with id " + cdmEntityToCache.getId() + " put in cache");
             return;
         }
         logger.info(" - object of type " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId() + " already exists");
@@ -274,6 +288,7 @@ public class CdmTransientEntityCacher implements ICdmCacher {
     public void dispose() {
         CacheManager.create().removeCache(cache.getName());
         cache.dispose();
+        newEntitiesMap.clear();
 
     }
 
index a8cc4ecd9c6ea218040a1a87bf80ec8e7e9a8375..4bb1c3d58c500ddcd90073e74ba4111fef2be9a8 100644 (file)
@@ -64,6 +64,7 @@ public class CdmEntitySession implements ICdmEntitySession  {
 
 
 
+
     /* (non-Javadoc)
      * @see eu.etaxonomy.taxeditor.session.ICdmEntitySession#load(java.lang.Object, boolean)
      */
@@ -299,4 +300,12 @@ public class CdmEntitySession implements ICdmEntitySession  {
         return propertyPathsMap.get(obj);
     }
 
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySession#addNewCdmEntity(eu.etaxonomy.cdm.model.common.CdmBase)
+     */
+    @Override
+    public void addNewCdmEntity(CdmBase newEntity) {
+        cdmTransientEntityCacher.addNewEntity(newEntity);
+    }
+
 }
diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/DefaultNewEntityListener.java b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/DefaultNewEntityListener.java
new file mode 100644 (file)
index 0000000..c265577
--- /dev/null
@@ -0,0 +1,37 @@
+// $Id$
+/**
+* Copyright (C) 2015 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.session;
+
+import org.apache.log4j.Logger;
+
+import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
+import eu.etaxonomy.cdm.api.application.CdmApplicationState;
+import eu.etaxonomy.cdm.model.NewEntityListener;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+
+/**
+ * @author cmathew
+ * @date 30 Sep 2015
+ *
+ */
+public class DefaultNewEntityListener implements NewEntityListener {
+
+    private static final Logger logger = Logger.getLogger(DefaultNewEntityListener.class);
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.cdm.model.NewEntityListener#onCreate(eu.etaxonomy.cdm.model.common.CdmBase)
+     */
+    @Override
+    public void onCreate(CdmBase cdmBase) {
+        logger.warn("New Entity created : " + cdmBase);
+        ((CdmApplicationRemoteController)CdmApplicationState.getCurrentAppConfig()).getCdmEntitySessionManager().getActiveSession().addNewCdmEntity(cdmBase);
+    }
+
+}
index c43e88d43ea75b06633584a860b92d8da075c4a1..22388ed7e5b87827be47628f9c26375c4af6b586 100644 (file)
@@ -88,4 +88,6 @@ public interface ICdmEntitySession {
      */
     public List<String> getPropertyPaths(Object obj);
 
+    public void addNewCdmEntity(CdmBase newEntity);
+
 }
\ No newline at end of file
index eed1b494db784b984f47ab223988eaeba8fcef0c..fcb6c69b6fdab0f38dbb13b0b41953c8c83d76b6 100644 (file)
@@ -209,4 +209,13 @@ public class NullSession implements ICdmEntitySession {
         return propertyPathsMap.get(obj);
     }
 
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySession#addNewCdmEntity(eu.etaxonomy.cdm.model.common.CdmBase)
+     */
+    @Override
+    public void addNewCdmEntity(CdmBase newEntity) {
+        // TODO Auto-generated method stub
+
+    }
+
 }
index 5db78a9f77f7dedbab8065107db25d89cd372a1a..ef72bfa021c366c021aeb87b850aba79d5122646 100644 (file)
@@ -198,5 +198,14 @@ public class MockCdmEntitySession implements ICdmEntitySession  {
         return null;
     }
 
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySession#addNewCdmEntity(eu.etaxonomy.cdm.model.common.CdmBase)
+     */
+    @Override
+    public void addNewCdmEntity(CdmBase newEntity) {
+        // TODO Auto-generated method stub
+
+    }
+
 
 }
index 76e9242bf77ccdb23d1dacd3459220acafa8b8bf..57aa4c41019b5c591529a779c59c5b7ce835652a 100644 (file)
@@ -18,6 +18,7 @@ import java.util.UUID;
 import org.apache.log4j.Logger;
 import org.eclipse.core.commands.ExecutionException;
 import org.junit.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.unitils.dbunit.annotation.DataSet;
 
@@ -25,6 +26,11 @@ import eu.etaxonomy.cdm.api.service.IClassificationService;
 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
 import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.common.OriginalSourceType;
+import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
+import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.description.TaxonDescription;
+import eu.etaxonomy.cdm.model.description.TextData;
 import eu.etaxonomy.cdm.model.name.BotanicalName;
 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
 import eu.etaxonomy.cdm.model.name.NonViralName;
@@ -33,6 +39,7 @@ import eu.etaxonomy.cdm.model.taxon.Synonym;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.taxeditor.editor.name.operation.CreateSynonymInExistingHomotypicalGroupOperation;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * @author cmathew
@@ -151,5 +158,48 @@ public class TaxonNameEditorTest extends BaseOperationTest {
         taxonService.merge(taxon);
     }
 
+    @Ignore
+    @Test
+    public void addDescription() {
+        UUID taxonNodeUuid = UUID.fromString("ce54c396-3694-47f2-abb0-1d7b7e057985");
+
+        TaxonNode taxonNode = taxonNodeService.load(taxonNodeUuid);
+        Taxon taxon = taxonNode.getTaxon();
+        TaxonDescription description = TaxonDescription.NewInstance(taxon);
+
+
+        TextData textData = TextData.NewInstance();
+
+        textData.setFeature(Feature.ECOLOGY());
+        description.addElement(textData);
+
+        DescriptionElementSource descriptionElementSource = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
+        textData.addSource(descriptionElementSource);
+
+
+        CdmStore.getService(ITaxonNodeService.class).merge(taxonNode);
+
+        description.getId();
+    }
+
+
+    @Test
+    public void addTaxonNodeCascadeProblem() {
+        UUID taxonNodeUuid = UUID.fromString("ce54c396-3694-47f2-abb0-1d7b7e057985");
+
+        TaxonNode taxonNode = taxonNodeService.load(taxonNodeUuid);
+        Taxon childTaxon = Taxon.NewInstance(null, null);
+        TaxonNode childTaxonNode = taxonNode.addChildTaxon(childTaxon, null, null);
+        Taxon grandChildTaxon = Taxon.NewInstance(null, null);
+        TaxonNode grandChildTaxonNode = childTaxonNode.addChildTaxon(grandChildTaxon, null, null);
+
+        CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
+
+        Assert.assertEquals(taxonNode.getChildNodes().get(0).getId(), childTaxonNode.getId());
+        Assert.assertEquals(taxonNode.getChildNodes().get(0).getTaxon().getId(), childTaxon.getId());
+        Assert.assertEquals(taxonNode.getChildNodes().get(0).getChildNodes().get(0).getId(), grandChildTaxonNode.getId());
+        Assert.assertEquals(taxonNode.getChildNodes().get(0).getChildNodes().get(0).getTaxon().getId(), grandChildTaxon.getId());
+    }
+
 }
 
index c89201247332d6c9b26833c7380d92810f056520..82b9e94d13020b096b20972adfc1a2e33b1cf565 100644 (file)
@@ -178,12 +178,12 @@ public class CdmEntitySessionAwareTest extends RemotingSessionAwareTest {
         pKey = polytomousKeyService.merge(pKey, true);
         grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
         Assert.assertTrue(0 != grandChildNode.getId());
-
-        grandChildNode.setStatement(KeyStatement.NewInstance("test"));
+        KeyStatement ks = KeyStatement.NewInstance("test");
+        grandChildNode.setStatement(ks);
         pKey = polytomousKeyService.merge(pKey, true);
 
-        grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
-        KeyStatement ks = grandChildNode.getStatement();
+        //grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
+        //KeyStatement ks = grandChildNode.getStatement();
         Assert.assertTrue(0 != ks.getId());
 
     }
@@ -197,10 +197,8 @@ public class CdmEntitySessionAwareTest extends RemotingSessionAwareTest {
         rootChildNode.addChild(grandChildNode);
 
 
-        grandChildNode = polytomousKeyNodeService.merge(grandChildNode);
+        polytomousKeyNodeService.merge(grandChildNode);
 
-        PolytomousKeyNode greatGrandChildNode = PolytomousKeyNode.NewInstance();
-        grandChildNode.addChild(greatGrandChildNode);
 
         Assert.assertFalse(pKey.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
     }
index fb244293ad12b3c3196178fc290360a26b702543..5d91d4033ef92d0c6cdbafaf60310850d887f442 100644 (file)
@@ -35,4 +35,6 @@
   <SynonymRelationship id="50" created="2010-12-21 15:09:45.0" uuid="60d1466f-5823-4a69-a071-1887d0c1e72b" doubtful="false" partial="false" proparte="false"  relatedfrom_id="388" relatedto_id="387" />
 
   <LanguageString id="10"/>
+  <DescriptionBase/>
+  <DescriptionElementBase/>
 </dataset>