added recursive loading for collection and maps and also correspoding tests.
authorCherian Mathew <c.mathew@bgbm.org>
Wed, 12 Nov 2014 16:30:12 +0000 (16:30 +0000)
committerCherian Mathew <c.mathew@bgbm.org>
Wed, 12 Nov 2014 16:30:12 +0000 (16:30 +0000)
cleanup of code.

13 files changed:
.gitattributes
eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationRemoteController.java
eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CachedCommonServiceImpl.java
eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmEntityCachingUtils.java [deleted file]
eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmModelCacher.java
eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmRemoteCacheManager.java
eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmServiceCacher.java
eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmTransientEntityCacher.java
eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/taxeditor/remoting/session/CdmEntitySessionManager.java
eu.etaxonomy.taxeditor.remoting/src/test/java/eu/etaxonomy/cdm/api/cache/CdmClientCachingTest.java
eu.etaxonomy.taxeditor.remoting/src/test/java/eu/etaxonomy/cdm/polytomouskey/PolytomousKeyTest.java
eu.etaxonomy.taxeditor.remoting/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/BaseRemotingTest.java
eu.etaxonomy.taxeditor.remoting/src/test/resources/eu/etaxonomy/cdm/api/cache/CdmClientCachingTest.xml

index 0361e5956d45bedee638dc6614e58354d81c2137..dc55ce09b913620d98ccfa909fc87a6f3d820a14 100644 (file)
@@ -1018,7 +1018,6 @@ eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/application/C
 eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CachedCommonServiceImpl.java -text
 eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmClientCacheException.java -text
 eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmEntityCacheKey.java -text
-eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmEntityCachingUtils.java -text
 eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmModelCacher.java -text
 eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmModelFieldPropertyFromClass.java -text
 eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmRemoteCacheManager.java -text
index 9253104bdde7c902cc59233ba90cf406f07b8fae..ced61c636f8a5373cc2583e0ebe458b59411f437 100644 (file)
@@ -25,6 +25,7 @@ import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 
+import eu.etaxonomy.cdm.api.cache.CdmModelCacher;
 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
 import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
 import eu.etaxonomy.cdm.common.monitor.SubProgressMonitor;
@@ -192,6 +193,9 @@ public class CdmApplicationRemoteController  extends CdmApplicationController {
         configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationRemoteConfiguration");
                AbstractLazyInitializer.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
                AbstractPersistentCollection.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
+               
+               CdmModelCacher cmdmc = new CdmModelCacher();
+               cmdmc.cacheGetterFields();
 
     }
 
index 0a10de1070395fb5b7663bece983b95574acea6e..5ff07c97d2bbc9263225e6ea3cd575734f389db4 100644 (file)
@@ -27,12 +27,12 @@ import org.hibernate.collection.spi.PersistentCollection;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import eu.etaxonomy.cdm.api.cache.CdmEntityCachingUtils.CollectionType;
 import eu.etaxonomy.cdm.api.service.ICommonService;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.PersistentMultiLanguageText;
 import eu.etaxonomy.taxeditor.remoting.session.CdmEntitySessionManager;
 import eu.etaxonomy.taxeditor.remoting.CdmRemotingException;
+import eu.etaxonomy.cdm.api.cache.CdmTransientEntityCacher.CollectionType;;
 
 /**
  * @author cmathew
@@ -51,9 +51,6 @@ public class CachedCommonServiceImpl implements ICachedCommonService {
     @Autowired
     private CdmEntitySessionManager cdmEntitySessionManager;
 
-    @Autowired
-    private CdmEntityCachingUtils cdmEntityCachingUtils;
-
 
     public static boolean isCacheEnabled() {
         return cacheEnabled;
diff --git a/eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmEntityCachingUtils.java b/eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/cache/CdmEntityCachingUtils.java
deleted file mode 100644 (file)
index 1a3b99c..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-// $Id$
-/**
-* Copyright (C) 2014 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.cdm.api.cache;
-
-import java.util.Iterator;
-import java.util.Map;
-
-import net.sf.ehcache.Cache;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.taxeditor.remoting.session.CdmEntitySessionManager;
-
-/**
- * @author cmathew
- * @date 15 Oct 2014
- *
- */
-@Component
-public class CdmEntityCachingUtils {
-
-    @Autowired
-    private CdmEntitySessionManager cdmEntityCacheManager;
-    
-   
-
-    public static enum CollectionType {
-        SET,
-        LIST,
-        MAP;
-
-        @Override
-        public String toString() {
-            return this.name().toLowerCase();
-        }
-    }
-
-    public void cachify(Map<Object,Object> map) {
-        if(map == null || map.isEmpty()) {
-            return;
-        }
-
-        int originalMapSize = map.size();
-        Object[] result = new Object[ map.size() * 2 ];
-        Iterator<Map.Entry<Object,Object>> iter = map.entrySet().iterator();
-        int i=0;
-        while ( iter.hasNext() ) {
-            Map.Entry<Object,Object> e = (Map.Entry<Object,Object>) iter.next();
-            result[i++] = e.getKey();
-            result[i++] = e.getValue();
-        }
-
-        for(i=0; i<result.length;i++) {
-            if(result[i] instanceof CdmBase) {
-                CdmBase cdmBase = (CdmBase)result[i];
-                CdmBase cachedCdmBase = cdmEntityCacheManager.getActiveCdmTransientEntityCacher().load(cdmBase, true);
-                result[i] = cachedCdmBase;
-            }
-        }
-        map.clear();
-        for(i = 0; i < originalMapSize; i+=2 ) {
-            map.put(
-                    result[i],
-                    result[i+1]
-                );
-        }
-
-    }
-    
-    public void cachify(CdmBase cdmBase, boolean recursive) {
-       
-       
-    }
-
-
-
-}
index ae20c6a9533aa0503641bfa63b6539a4de178f68..baba3fb474e8d11be9179710488e7c7ef4f3662a 100644 (file)
@@ -28,7 +28,7 @@ public class CdmModelCacher {
 
                Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
                configuration.buildMappings();
-               Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();                      
+               Iterator<PersistentClass> classMappingIterator = configuration.getClassMappings();                        
 
                Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
                cache.removeAll();
index 330a64c5c5f4e018182bffdee0bb05877a703a93..d49390a46d7358f1738aca591b751db95f63898b 100644 (file)
@@ -14,7 +14,6 @@ import org.springframework.stereotype.Component;
 public class CdmRemoteCacheManager {   
        
        private CacheManager cdmlibModelCacheManager;
-       private CacheManager defaultCacheManager;
        
        private static CdmRemoteCacheManager cdmRemoteCacheManager = null;
        
@@ -36,6 +35,9 @@ public class CdmRemoteCacheManager {
     private CdmRemoteCacheManager() {
        checkCacheProperties();
        try {
+               // NOTE:Programmatically creating the cache manager may solve the problem of 
+               //      recreating data written to disk on startup
+               //      see https://stackoverflow.com/questions/1729605/ehcache-persist-to-disk-issues
                String cacheFilePath = CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE.getFile().getAbsolutePath();
                        cdmlibModelCacheManager = new CacheManager(cacheFilePath);
                        
index c7d6bac8f314578b7a96295c8665bba03f1aa1ca..db7d0ed79b6a31ec66edcca30b0e755738ab38a4 100644 (file)
@@ -25,7 +25,12 @@ public class CdmServiceCacher extends CdmCacher {
 
        @Autowired
        ITermService termService;
-
+       
+       @Override
+       protected void setup() {
+               CdmTransientEntityCacher.setDefaultCacher(this);                
+       }
+       
        @Override
        protected CdmBase findByUuid(UUID uuid) {
                CdmBase term = termService.findWithoutFlush(uuid);
index 2387c23275b36684f0468bdaa332786f9ae51837..8f933c398889b194e3181f427b7b66d42cb9545e 100644 (file)
@@ -9,10 +9,12 @@
  */
 package eu.etaxonomy.cdm.api.cache;
 
+import java.io.Serializable;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -50,12 +52,12 @@ import eu.etaxonomy.cdm.model.common.CdmBase;
  *
  */
 
-public class CdmTransientEntityCacher extends CdmServiceCacher {
+public class CdmTransientEntityCacher  {
 
        private static final Logger logger = Logger.getLogger(CdmTransientEntityCacher.class);
 
-       @Autowired
-       private CdmEntityCachingUtils cdmEntityCachingUtils;
+       
+       private static CdmServiceCacher cdmServiceCacher;
 
     @Autowired
     private ICommonService commonService;
@@ -68,12 +70,26 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
 
        private static boolean isRecursiveEnabled = true;
 
+    public static enum CollectionType {
+        SET,
+        LIST,
+        MAP;
 
+        @Override
+        public String toString() {
+            return this.name().toLowerCase();
+        }
+    }
+    
+       private CdmTransientEntityCacher() {
+               
+       }
+       
        public CdmTransientEntityCacher(String cacheId) {
                this.cacheId = cacheId;
 
                cache = new Cache(getEntityCacheConfiguration(cacheId));                
-               getDefaultCacheManager().addCache(cache);
+               cdmServiceCacher.getDefaultCacheManager().addCache(cache);
 
                cdmlibModelCache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
 
@@ -100,7 +116,10 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
 
        }
 
-
+       public static void setDefaultCacher(CdmServiceCacher css) {
+               cdmServiceCacher = css;
+       }
+       
        /**
         * Returns the cache corresponding to the cache id
         *
@@ -108,15 +127,18 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
         * @return
         */
        private Cache getCache() {
-               return getDefaultCacheManager().getCache(cacheId);
+               return cdmServiceCacher.getDefaultCacheManager().getCache(cacheId);
        }
 
+       @SuppressWarnings("unchecked")
        public <T extends Object> T load(T obj, boolean recursive) {
                if(obj instanceof CdmBase) {
                        return load(obj, recursive);
                } else if (obj instanceof Map) {
-                       return load(obj, recursive);
-               }
+                       return (T) load((Map<T,T>)obj, recursive);
+               } else if (obj instanceof Collection) {
+                       return (T) load((Collection<T>)obj, recursive);
+               } 
                
                return obj;
        }
@@ -124,10 +146,13 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
        @SuppressWarnings("unchecked")
        private <T extends Object> T loadRecursive(T obj, Set<CdmBase> alreadyVisitedEntities) {
                if(obj instanceof CdmBase) {
-                       return loadRecursive(obj, alreadyVisitedEntities);
+                       return (T) loadRecursive((CdmBase)obj, alreadyVisitedEntities);
                } else if (obj instanceof Map) {
                        return (T) load((Map<T,T>)obj, alreadyVisitedEntities);
-               }
+               } else if (obj instanceof Collection) {
+                       return (T) load((Collection<T>)obj, alreadyVisitedEntities);
+               } 
+               
                logger.info("No caching yet for type " + obj.getClass().getName());
                
                return obj;
@@ -135,7 +160,7 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
        
        public <T extends Object> Map<T,T> load(Map<T,T> map, boolean recursive){
                if(isRecursiveEnabled && recursive) {
-                       logger.info("---- starting recursive load for map");
+                       logger.info("---- starting recursive load for cdm entity map");
                        Set<CdmBase> alreadyVisitedEntities = new HashSet<CdmBase>();
                        Map<T,T> cachedMap = load(map, alreadyVisitedEntities);
                        alreadyVisitedEntities.clear();
@@ -145,6 +170,7 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
                        return load(map, null);
                }
        }
+       
 
        private <T extends Object> Map<T,T> load(Map<T,T> map, Set<CdmBase> alreadyVisitedEntities){
         if(map == null || map.isEmpty()) {
@@ -177,6 +203,45 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
         }
         return map;
        }
+       
+       public <T extends Object> Collection<T> load(Collection<T> collection, boolean recursive){
+               if(isRecursiveEnabled && recursive) {
+                       logger.info("---- starting recursive load for cdm entity collection");
+                       Set<CdmBase> alreadyVisitedEntities = new HashSet<CdmBase>();
+                       Collection<T> cachedList = load(collection, alreadyVisitedEntities);
+                       alreadyVisitedEntities.clear();
+                       logger.info("---- ending recursive load for cdm entity collection \n");
+                       return cachedList;
+               } else {
+                       return load(collection, null);
+               }
+       }
+       
+       @SuppressWarnings("unchecked")
+       private <T extends Object> Collection<T> load(Collection<T> collection, Set<CdmBase> alreadyVisitedEntities){
+               int length = collection.size();
+               Object[] result = new Object[length];
+               Iterator<T> collectionItr = collection.iterator();
+               int count = 0;
+               while(collectionItr.hasNext()) {
+                       if(alreadyVisitedEntities == null) {
+                       result[count] = load(collectionItr.next(), false);
+               } else {                              
+                       result[count] = loadRecursive(collectionItr.next(), alreadyVisitedEntities);
+               }       
+                       count++;
+               }
+               
+               collection.clear();
+                                               
+               for ( int i = 0; i < length; i++ ) {
+                       collection.add((T)result[i]);
+               }
+               
+               return collection;
+       }
+
+       
        /**
         * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
         * in the cache corresponding to the given cache id
@@ -198,15 +263,6 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
                        return load(cdmEntity);
                }
                
-               // split into two methods 
-               // - load(CdmBase cdmBase)
-               // - recursiveLoad(CdmBase cdmbase, List<CdmBase> alreadyVisitedEntities) 
-               //   : the entity to be visted should added before the recursive call
-               //     and deleted after the call to end finally with a list
-               //     of size zero at the end
-               //   : entity to recurse should be the original entity and
-               //     not the cached one
-               
        }
 
        
@@ -336,7 +392,7 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
                Element cachedCdmEntityElement = getCacheElement(id);
 
                if(cachedCdmEntityElement == null) {
-                       cachedCdmEntityElement = getCacheElement(cdmEntity.getUuid());
+                       cachedCdmEntityElement = cdmServiceCacher.getCacheElement(cdmEntity.getUuid());
                        if(cachedCdmEntityElement != null) {
                                logger.info("Cdm Entity with id : " + cdmEntity.getId() + " already exists in permanent cache. Ignoring put.");
                                return;
@@ -377,12 +433,11 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
        public CdmBase getFromCache(CdmBase cdmBase) {
                
                CdmEntityCacheKey cacheId = generateKey(cdmBase);
-
                CdmBase cachedCdmEntity = getFromCache(cacheId);
 
                if(cachedCdmEntity == null) {
                        // ... then try the permanent (uuid-based) cache
-                       cachedCdmEntity = getFromCache(cdmBase.getUuid());              
+                       cachedCdmEntity = cdmServiceCacher.getFromCache(cdmBase.getUuid());             
                }
 
                return cachedCdmEntity;
@@ -390,15 +445,14 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
        
        public CdmBase getFromCache(CdmBase cdmBase, Class<? extends CdmBase> clazz) {
                
-               cdmBase = CdmBase.deproxy(cdmBase, clazz);
-               
+               cdmBase = CdmBase.deproxy(cdmBase, clazz);              
                CdmEntityCacheKey cacheId = generateKey(cdmBase);
 
                CdmBase cachedCdmEntity = getFromCache(cacheId);
 
                if(cachedCdmEntity == null) {
                        // ... then try the permanent (uuid-based) cache
-                       cachedCdmEntity = getFromCache(cdmBase.getUuid());              
+                       cachedCdmEntity = cdmServiceCacher.getFromCache(cdmBase.getUuid());             
                }
 
                return cachedCdmEntity;
@@ -413,30 +467,10 @@ public class CdmTransientEntityCacher extends CdmServiceCacher {
                return entities;
        }
 
-
-
-
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
-        */
-       @Override
-       public boolean exists(UUID uuid) {
-               return (getCacheElement(uuid) != null || super.getCacheElement(uuid) != null);
-       }
-
        public boolean exists(CdmEntityCacheKey key) {
                return (getCacheElement(key) != null);
        }
 
-
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
-        */
-       @Override
-       public boolean existsAndIsNotNull(UUID uuid) {
-               return getFromCache(uuid) != null;
-       }
-
        public boolean existsAndIsNotNull(CdmEntityCacheKey id) {
                return getFromCache(id) != null;
        }
index 817e430be2fc9952a812961e824813c991a557cd..b8c69e524808e5ab9cea04e9c6815cbd37b94f84 100644 (file)
@@ -47,6 +47,9 @@ public class CdmEntitySessionManager {
        }
        
        public CdmTransientEntityCacher getActiveCdmTransientEntityCacher() {
+               if(activeSession == null) {
+                       throw new CdmClientSessionException("No active session has been bound");
+               }
         return activeSession.getCdmTransientEntityCacher();
     }
        
index 03bb6762a6326e26f6522020010695ddb87520ca..637e7ab38fa14628c24c5a41369ab369e0d9245f 100644 (file)
 package eu.etaxonomy.cdm.api.cache;
 
 import java.util.Arrays;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.UUID;
 
 import org.apache.log4j.Level;
@@ -24,6 +26,7 @@ import org.junit.Test;
 import org.unitils.dbunit.annotation.DataSet;
 
 import eu.etaxonomy.cdm.api.service.ICommonService;
+import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
 import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
 import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.model.common.CdmBase;
@@ -48,19 +51,22 @@ public class CdmClientCachingTest extends BaseRemotingTest {
 
     private static final Logger logger = Logger.getLogger(CdmClientCachingTest.class);
 
-    UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
-    UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
+    private UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
+    private UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
+    private UUID polytomousKeyNodeUuid1 = UUID.fromString("75e4c924-ff58-4ee7-a59d-fd9173517d08");
+    private UUID polytomousKeyNodeUuid2 = UUID.fromString("b775c027-13c0-4b87-8aa9-712faeaafbdc");
 
 
-    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
-    ICommonService commonService = getRemoteApplicationController().getCommonService();
-    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
+    private IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
+    private IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
+    private ICommonService commonService = getRemoteApplicationController().getCommonService();
+    private ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
 
-    CdmEntitySessionManager cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
+    private CdmEntitySessionManager cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
     
-       CdmTransientEntityCacher cacher;
-       
-    Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
+    
+       private CdmTransientEntityCacher cacher;
+    private ISessionEventListener sessionOwner;
        
     private static final List<String> PKEY_DEPTH1_INIT_STRATEGY = Arrays.asList(new String[] {
                        });
@@ -71,16 +77,12 @@ public class CdmClientCachingTest extends BaseRemotingTest {
     private static final List<String> PKEY_DEPTH3_INIT_STRATEGY = Arrays.asList(new String[] {                 
                "root.statement"});
     
-    private ISessionEventListener sessionOwner;
+
     
     @BeforeClass
     public static void initializePolytomousKeyTest() {
         logger.setLevel(Level.INFO);
-        CDMServer.getInstance().setKeepServerRunning(true);    
-
-               CdmModelCacher cmdmc = new CdmModelCacher();
-               cmdmc.cacheGetterFields();
-               
+        CDMServer.getInstance().setKeepServerRunning(true);            
         //initializeController("default", "127.0.0.1", 8080, "", NomenclaturalCode.ICNAFP);
     }
 
@@ -291,69 +293,132 @@ public class CdmClientCachingTest extends BaseRemotingTest {
     }
     
 
-
+    /**
+     * when : loading objects from a collection 
+     * then : the object stored in the cache should be the same
+     */
     @Test
-    public void savePolytomousKeyNodeDataWithSameSubKey() {
-        //CachedCommonServiceImpl.setCacheEnabled(false);
-       ISessionEventListener sessionOwner = new MockSessionOwner();
-       cdmEntitySessionManager.bind(sessionOwner);
-        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
-        PolytomousKeyNode pkeynode = pkey.getRoot();
-
-        PolytomousKey subkey1 = pkeynode.getChildAt(0).getSubkey();
-        String subkey1title = subkey1.getTitleCache();
-        subkey1.setTitleCache(subkey1title + "test", true);
-
-
-        PolytomousKey subkey2 = pkeynode.getChildAt(1).getChildAt(0).getSubkey();
-        String subkey2title = subkey2.getTitleCache();
-        subkey2.setTitleCache(subkey2title + "test", true);
-
-        Assert.assertSame(subkey1, subkey2);
-
-        polytomousKeyService.merge(pkey);
+    public void subGraphCollectionLoadTest() {
+       
+       // this call will load into the session cache the graph and update the objects in the sub-graph for a
+       // polytomous key  
+       //      |- root : polytomous key node 
+       //              |- question : KeyStatement
+       //              |- statement : KeyStatement     
+        
+       PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
+       pkey = cdmEntitySessionManager.add(sessionOwner, pkey);
+       
+       PolytomousKeyNode childOfRootPKNode = pkey.getRoot().getChildAt(1);
+       
+       Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
+       Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
+       
     }
-
+    
+    /**
+     * when : loading objects from a map
+     * then : the object stored in the cache should be the same
+     */
     @Test
-    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
-        CachedCommonServiceImpl.setCacheEnabled(false);
-        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
-        PolytomousKeyNode pkeynode = pkey.getRoot();
-
-        PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
-        PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(children, 0);
-        PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
-        String subkey1title = subkey1.getTitleCache();
-        subkey1.setTitleCache(subkey1title + "test", true);
-
-        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(children, 1);
-        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
-        String subkey2title = subkey2.getTitleCache();
-        subkey2.setTitleCache(subkey2title + "test", true);
+    public void subGraphMapLoadTest() {
+       
+       Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
+        Language hindi = Language.getLanguageFromUuid(UUID.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
+        
+        
+       // this call will load into the session cache the graph and update the objects in the sub-graph for a
+       // polytomous key  
+       //      |- root : polytomous key node 
+       //              |- question : KeyStatement
+       //              |- statement : KeyStatement     
+        
+       PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
+       pkey = cdmEntitySessionManager.add(sessionOwner, pkey);
+       
+       Map<Language, LanguageString> labelMap = pkey.getRoot().getStatement().getLabel();
+       Set<Language> languages = labelMap.keySet();
+       Iterator<Language> languagesItr = languages.iterator();
+       while(languagesItr.hasNext()) {
+               Language lang = languagesItr.next();
+               if(lang.equals(english)) {
+                       Assert.assertSame(lang, english);                       
+               }
+               if(lang.equals(hindi)) {
+                       Assert.assertSame(lang, hindi);
+               }
+               Assert.assertSame(lang, cacher.getFromCache(lang));
+       }       
+    }
+    
+    /**
+     * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
+     * then : the object should be the same
+     */
+    @Test
+    public void subGraphObjectLoadTest1() {
 
-        Assert.assertNotSame(subkey1, subkey2);
+       // this call will load into the session cache a polytomous key node object              
+       PolytomousKeyNode rootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid1),PolytomousKeyNode.class);
+       rootPKNode = cdmEntitySessionManager.add(sessionOwner, rootPKNode);
 
-        polytomousKeyService.merge(pkey);
+       Assert.assertNotNull(rootPKNode);
+       Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
+       
+       PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
+       childOfRootPKNode = cdmEntitySessionManager.add(sessionOwner, childOfRootPKNode);
+       
+       Assert.assertNotNull(childOfRootPKNode);
+       Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
+       
+       // this call will load into the session cache the graph and update the objects in the sub-graph for a
+       // polytomous key  
+       //      |- root : polytomous key node 
+       //              |- question : KeyStatement
+       //              |- statement : KeyStatement     
+        
+       PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
+       pkey = cdmEntitySessionManager.add(sessionOwner, pkey);
+       
+       Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
+       Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
     }
 
-
+    /**
+     * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
+     * then : the object should be the same
+     */
     @Test
-    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
-        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
-        PolytomousKeyNode pkeynode = pkey.getRoot();
+    public void subGraphObjectLoadTest2() {
 
-        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
-        label1.size();
-        //Language lang1 = label1.get(english).getLanguage();
-        //String title1 = lang1.getTitleCache();
+       // this call will load into the session cache the graph and update the objects in the sub-graph for a
+       // polytomous key  
+       //      |- root : polytomous key node 
+       //              |- question : KeyStatement
+       //              |- statement : KeyStatement     
+        
+       PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
+       pkey = cdmEntitySessionManager.add(sessionOwner, pkey);
+       
+       // this call will load into the session cache a polytomous key node object              
+       PolytomousKeyNode rootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid1),PolytomousKeyNode.class);
+       rootPKNode = cdmEntitySessionManager.add(sessionOwner, rootPKNode);
 
-        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
-        label2.size();
-        //Language lang2 = label2.get(english).getLanguage();
-        //String title2 = lang2.getTitleCache();
+       Assert.assertNotNull(rootPKNode);
+       Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
+       
+       PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
+       childOfRootPKNode = cdmEntitySessionManager.add(sessionOwner, childOfRootPKNode);
+       
+       Assert.assertNotNull(childOfRootPKNode);
+       Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
+       
 
-        polytomousKeyService.merge(pkey);
+       
+       Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
+       Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
     }
 
 
+
 }
index 6483826213ccfd13db334b7162c566f2846d9e67..c79f0c2a39bb0cd2d5c74b0dd740b197e8a7aa5d 100644 (file)
@@ -224,18 +224,20 @@ public class PolytomousKeyTest extends BaseRemotingTest {
 
     @Test
     public void savePolytomousKeyNodeDataWithSameSubKey() {
-        //CachedCommonServiceImpl.setCacheEnabled(false);
+        
        ISessionEventListener sessionOwner = new MockSessionOwner();
        cdmEntitySessionManager.bind(sessionOwner);
         PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+        pkey = cdmEntitySessionManager.add(sessionOwner, pkey);
+        
         PolytomousKeyNode pkeynode = pkey.getRoot();
 
-        PolytomousKey subkey1 = pkeynode.getChildAt(0).getSubkey();
+        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
         String subkey1title = subkey1.getTitleCache();
         subkey1.setTitleCache(subkey1title + "test", true);
 
 
-        PolytomousKey subkey2 = pkeynode.getChildAt(1).getChildAt(0).getSubkey();
+        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
         String subkey2title = subkey2.getTitleCache();
         subkey2.setTitleCache(subkey2title + "test", true);
 
@@ -246,8 +248,12 @@ public class PolytomousKeyTest extends BaseRemotingTest {
 
     @Test
     public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
-        CachedCommonServiceImpl.setCacheEnabled(false);
+       ISessionEventListener sessionOwner = new MockSessionOwner();
+       cdmEntitySessionManager.bind(sessionOwner);
+       
         PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+        pkey = cdmEntitySessionManager.add(sessionOwner, pkey);
+        
         PolytomousKeyNode pkeynode = pkey.getRoot();
 
         PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
@@ -261,7 +267,9 @@ public class PolytomousKeyTest extends BaseRemotingTest {
         String subkey2title = subkey2.getTitleCache();
         subkey2.setTitleCache(subkey2title + "test", true);
 
-        Assert.assertNotSame(subkey1, subkey2);
+        Assert.assertNotSame(childNode0, childNode1);
+        
+        Assert.assertSame(subkey1, subkey2);
 
         polytomousKeyService.merge(pkey);
     }
index f446ab7f7eb25b888589c10c29dac9b196b21d35..39172c60917731e1cee6ce77c249983d187f7082 100644 (file)
@@ -64,7 +64,8 @@ public class BaseRemotingTest extends UnitilsJUnit4 {
     
     @BeforeClass
     public static void initializeBaseRemotingTest() {
-        //Logger.getRootLogger().setLevel(Level.INFO);
+        //NOTE: Run this the cdmTest H2 DB whenever it needs to be
+       //      recreated e.g. after a model change
        //DatabaseUnitils.disableConstraints();
 
                try {
index c068d69c20aec48f6a50ce59f56f847852f9297a..32d33316f0a50fa226651a244d250c80a1a2513d 100644 (file)
@@ -41,7 +41,7 @@
   <PolytomousKeyNode_LanguageString PolytomousKeyNode_id="2759" modifyingtext_id="13793" modifyingtext_mapkey_id="124"/>
 
   <LanguageString id="13780" created="2014-03-10 10:03:09.0" uuid="cceaa60b-61f4-4c8a-bb0e-d45f0fa3e2dc" text="Modifying Text 1a" language_id="124"/>
-  <LanguageString id="13781" created="2014-03-10 10:03:17.0" uuid="01a0ace4-de49-4725-9909-e881fb13035d" text="" language_id="124"/>
+  <LanguageString id="13781" created="2014-03-10 10:03:17.0" uuid="01a0ace4-de49-4725-9909-e881fb13035d" text="Modifying Text 1b" language_id="124"/>
   <LanguageString id="13782" created="2014-03-10 10:03:48.0" uuid="ec0fea40-69dd-4c3e-ab7a-34948037a069" updated="2014-03-10 10:06:32.0" text="Capitula with ligulate ray-florets; leaves pinnatisect" createdby_id="30" updatedby_id="30" language_id="124"/>
   <LanguageString id="13783" created="2014-03-10 10:04:03.0" uuid="82f8a3a0-567f-4d89-ba10-3f3699111d55" updated="2014-03-10 10:06:32.0" text="Ray-florets yellow" createdby_id="30" updatedby_id="30" language_id="124"/>
   <LanguageString id="13784" created="2014-03-10 10:04:21.0" uuid="4270501f-1efc-45c2-9e8f-c65f120e8f4c" updated="2014-03-10 10:06:32.0" text="Stem thinly pilose; leaves 10-15 mm wide, with spreading linear-subulate segments; perennial herb" createdby_id="30" updatedby_id="30" language_id="124"/>
@@ -59,6 +59,7 @@
   <LanguageString id="13796" created="2014-03-10 10:03:25.0" uuid="69cffb28-8a85-478f-bcae-533e20938ccd" updated="2014-03-10 10:06:32.0" text="Capitula without ligulate ray-florets; leaves entire or subentire" createdby_id="30" updatedby_id="30" language_id="124"/>
   <LanguageString id="14570" created="2014-10-09 15:42:33.0" uuid="84967760-6dbe-4af4-8e0c-309f1af6ce20" updated="2014-10-09 15:42:44.0" text="Question 1" createdby_id="10" updatedby_id="10" language_id="124"/>
   <LanguageString id="14571" created="2014-10-09 15:42:55.0" uuid="d82f37b1-f51d-42c1-a291-0c7820db155e" updated="2014-10-09 15:43:03.0" text="Question 2" createdby_id="10" updatedby_id="10" language_id="124"/>
+  <LanguageString id="14572" created="2014-10-09 15:42:55.0" uuid="d82f37b1-f51d-42c1-a291-0c7820db155f" updated="2014-10-09 15:43:03.0" text="Koi Sach Ya Sawal" createdby_id="30" updatedby_id="30" language_id="125"/>
 
   <TaxonBase DTYPE="Taxon" id="32" created="2011-03-01 20:11:26.0" uuid="8217ef77-2ab1-4318-bd67-ccd0cdef07c4" protectedtitlecache="false" titleCache="Achillea arabica Kotschy sec. Cyprus" doubtful="false" usenamecache="false" excluded="false" taxonstatusunknown="false" taxonomicchildrencount="0" unplaced="false" createdby_id="11"  sec_id="10" publish="true"/>
   <TaxonBase DTYPE="Taxon" id="35" created="2010-12-21 15:09:43.0" uuid="fade44fd-34b2-455f-861a-d1372b866f06" protectedtitlecache="false" titleCache="Achillea sec. Cyprus" doubtful="false" usenamecache="false" excluded="false" taxonstatusunknown="false" taxonomicchildrencount="0" unplaced="false" createdby_id="11"  sec_id="10" publish="true"/>
@@ -98,6 +99,7 @@
   <AgentBase DTYPE="Person" id="4809" created="2010-12-21 15:09:59.0" uuid="945d08f2-eb92-45b6-9252-6275ea6d338b" protectedtitlecache="false" titleCache="R. A. Graham" nomenclaturaltitle="R. A. Graham" createdby_id="11"/>
 
   <DefinedTermBase DTYPE="Language" id="124" created="2010-12-21 15:09:09.0" uuid="e9f8cdb7-6819-44e8-95d3-e2d0690c3523" protectedtitlecache="false" titleCache="English" uri=""  vocabulary_id="10" termType="LA" idInVocabulary="eng"/>
+  <DefinedTermBase DTYPE="Language" id="125" created="2010-12-21 15:09:09.0" uuid="0a1d9d1d-135d-4575-b172-669b51673c39" protectedtitlecache="false" titleCache="Hindi" uri=""  vocabulary_id="10" termType="LA" idInVocabulary="hin"/>
 
   <Extension id="10" created="2014-10-09 15:45:41.0" uuid="0d6f35d7-7e76-446a-a017-7e8355ee1b4c" updated="2014-10-09 15:48:01.0" extendedObj_type="eu.etaxonomy.cdm.model.description.PolytomousKey" extendedObj_id="1550" value="http://test.com" createdby_id="10" updatedby_id="10" type_id="2075"/>
 
   <KeyStatement_LanguageString KeyStatement_id="2467" label_id="13796" label_mapkey_id="124"/>
   <KeyStatement_LanguageString KeyStatement_id="2760" label_id="14570" label_mapkey_id="124"/>
   <KeyStatement_LanguageString KeyStatement_id="2761" label_id="14571" label_mapkey_id="124"/>
+  <KeyStatement_LanguageString KeyStatement_id="2460" label_id="14572" label_mapkey_id="125"/>  
 
   <PolytomousKey id="751" created="2013-03-24 11:17:08.0" uuid="9d8bf4f6-a70a-4b80-8556-2ccfb436ff01" protectedtitlecache="true" titleCache="Asphodeline" createdby_id="30"  startNumber="1"/>
   <PolytomousKey id="1550" created="2014-03-10 10:02:35.0" uuid="0d53ba20-7de4-4baa-bd8a-401048447d66" updated="2014-03-10 10:02:42.0" protectedtitlecache="true" titleCache="Achillea" createdby_id="30" updatedby_id="30" root_id="2750" startNumber="1"/>