Updated version in pom / project files to taxeditor version : 3.12.4 and cdmlib versi...
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / cache / CdmServiceCacher.java
1 package eu.etaxonomy.cdm.api.cache;
2
3 import java.util.UUID;
4
5 import net.sf.ehcache.config.CacheConfiguration;
6 import net.sf.ehcache.config.SizeOfPolicyConfiguration;
7
8 import org.springframework.stereotype.Component;
9
10 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
11 import eu.etaxonomy.cdm.model.common.CdmBase;
12 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
13 import eu.etaxonomy.cdm.model.common.TermBase;
14 import eu.etaxonomy.taxeditor.remoting.cache.CacheLoader;
15 import eu.etaxonomy.taxeditor.remoting.cache.CdmEntityCacheKey;
16 import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
17 import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
18 import eu.etaxonomy.taxeditor.session.CdmEntitySession;
19 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
20 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
21 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
22
23 /**
24 * Class which uses CDM services to cache cdm entities
25 *
26 * FIXME: Currently only handles terms entities. It would be
27 * interesting to have a generic method which finds the
28 * correct service to load / cache the entity.
29 *
30 * @author cmathew
31 *
32 * @param <T>
33 */
34 @Component
35 public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionManagerObserver {
36
37
38
39 private ICdmEntitySessionManager cdmEntitySessionManager;
40
41 private CdmTransientEntityCacher cdmTransientEntityCacher;
42
43 private CacheLoader cacheLoader;
44
45 @Override
46 protected void setup() {
47 DefinedTermBase.setCacher(this);
48 CdmTransientEntityCacher.setDefaultCacher(this);
49 TermServiceRequestExecutor.setDefaultCacher(this);
50
51 cacheLoader = new CacheLoader(this);
52 }
53
54
55 @Override
56 protected CacheConfiguration getDefaultCacheConfiguration() {
57 // For a better understanding on how to size caches, refer to
58 // http://ehcache.org/documentation/configuration/cache-size
59
60 SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
61 sizeOfConfig.setMaxDepth(100);
62 sizeOfConfig.setMaxDepthExceededBehavior("abort");
63
64 return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
65 .eternal(true)
66 .statistics(true)
67 .sizeOfPolicy(sizeOfConfig)
68 .overflowToOffHeap(false);
69
70 }
71
72 @Override
73 protected CdmBase findByUuid(UUID uuid) {
74 CdmBase term = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
75 return load(term);
76 }
77
78 /* (non-Javadoc)
79 * @see eu.etaxonomy.cdm.model.ICdmCacher#isCachable(eu.etaxonomy.cdm.model.common.CdmBase)
80 */
81 @Override
82 public boolean isCachable(CdmBase cdmEntity) {
83 // if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
84 if(cdmEntity != null && cdmEntity instanceof TermBase) {
85 return true;
86 }
87 return false;
88 }
89
90
91 public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
92 this.cdmEntitySessionManager = cdmEntitySessionManager;
93 if(cdmEntitySessionManager != null) {
94 cdmEntitySessionManager.addSessionObserver(this);
95 }
96 }
97
98
99 public CdmTransientEntityCacher getCurrentCacher() {
100 ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
101 if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
102 return ((CdmEntitySession) cdmEntitySession).getCacher();
103 }
104 return null;
105 }
106
107 @Override
108 public CdmBase getFromCache(CdmBase cdmBase) {
109 CdmBase cachedCdmEntity = null;
110 // first we check in the active session cache if the
111 // entity has been loaded there
112 // FIXME:Remoting do we really need the cdmTransientEntityCacher
113 // here. Is it not guarenteed that all every entity which 'isCachable'
114 // by this cacher is cached only in this cacher ?
115 if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
116 CdmEntityCacheKey key = CdmTransientEntityCacher.generateKey(cdmBase);
117 cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
118
119 }
120 if(cachedCdmEntity == null) {
121 cachedCdmEntity = super.getFromCache(cdmBase);
122 }
123 return cachedCdmEntity;
124 }
125
126
127 /* (non-Javadoc)
128 * @see eu.etaxonomy.cdm.api.cache.CdmCacher#load(eu.etaxonomy.cdm.model.common.CdmBase)
129 */
130 @Override
131 public CdmBase load(CdmBase cdmEntity) {
132 CdmBase cachedCdmEntity = getFromCache(cdmEntity.getUuid());
133
134 if(isCachable(cdmEntity) && cachedCdmEntity == null) {
135 cachedCdmEntity = cacheLoader.load(cdmEntity, false, true);
136 }
137 return cachedCdmEntity;
138 }
139
140 /* (non-Javadoc)
141 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver#changed()
142 */
143 @Override
144 public void changed() {
145 ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
146 if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
147 this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
148 } else {
149 this.cdmTransientEntityCacher = null;
150 }
151 }
152 }