import java.util.UUID;
+import net.sf.ehcache.config.CacheConfiguration;
+import net.sf.ehcache.config.SizeOfPolicyConfiguration;
+
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
-import eu.etaxonomy.cdm.api.cache.CdmCacher;
import eu.etaxonomy.cdm.api.service.ITermService;
import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.DefinedTermBase;
+import eu.etaxonomy.taxeditor.remoting.cache.CacheLoader;
+import eu.etaxonomy.taxeditor.remoting.cache.CdmEntityCacheKey;
+import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
+import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
+import eu.etaxonomy.taxeditor.session.CdmEntitySession;
+import eu.etaxonomy.taxeditor.session.CdmEntitySessionManager;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
/**
* Class which uses CDM services to cache cdm entities
@Component
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);
- return term;
- }
+ @Autowired
+ ITermService termService;
+
+ @Autowired
+ private CdmEntitySessionManager cdmEntitySessionManager;
+
+ private CdmTransientEntityCacher cdmTransientEntityCacher;
+
+ private CacheLoader cacheLoader;
+
+ @Override
+ protected void setup() {
+ DefinedTermBase.setCacher(this);
+ CdmTransientEntityCacher.setDefaultCacher(this);
+ TermServiceRequestExecutor.setDefaultCacher(this);
+ cacheLoader = new CacheLoader(this);
+ }
+
+ @Override
+ protected CacheConfiguration getDefaultCacheConfiguration() {
+ // For a better understanding on how to size caches, refer to
+ // http://ehcache.org/documentation/configuration/cache-size
+
+ SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
+ sizeOfConfig.setMaxDepth(100);
+ sizeOfConfig.setMaxDepthExceededBehavior("abort");
+
+ return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
+ .eternal(true)
+ .statistics(true)
+ .sizeOfPolicy(sizeOfConfig)
+ .overflowToOffHeap(false);
+
+ }
+
+ @Override
+ protected CdmBase findByUuid(UUID uuid) {
+ CdmBase term = termService.findWithoutFlush(uuid);
+ return load(term);
+ }
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.model.ICdmCacher#isCachable(eu.etaxonomy.cdm.model.common.CdmBase)
+ */
+ @Override
+ public boolean isCachable(CdmBase cdmEntity) {
+ if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
+ return true;
+ }
+ return false;
+ }
+
+ public void setActiveSession(ICdmEntitySession cdmEntitySession) {
+ if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
+ this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
+ } else {
+ this.cdmTransientEntityCacher = null;
+ }
+ }
+
+ @Override
+ public CdmBase getFromCache(CdmBase cdmBase) {
+ CdmBase cachedCdmEntity = null;
+ // first we check in the active session cache if the
+ if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
+ CdmEntityCacheKey key = CdmTransientEntityCacher.generateKey(cdmBase);
+ cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
+
+ }
+ if(cachedCdmEntity == null) {
+ cachedCdmEntity = super.getFromCache(cdmBase);
+ }
+ return cachedCdmEntity;
+ }
+
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.api.cache.CdmCacher#load(eu.etaxonomy.cdm.model.common.CdmBase)
+ */
+ @Override
+ public CdmBase load(CdmBase cdmEntity) {
+ CdmBase cachedCdmEntity = getFromCache(cdmEntity.getUuid());
+
+ if(isCachable(cdmEntity)) {
+ cachedCdmEntity = cacheLoader.load(cdmEntity, true, false);
+ }
+ return cachedCdmEntity;
+ }
+
}