Merge branch 'develop' into remoting-4.0
[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.beans.factory.annotation.Autowired;
9 import org.springframework.stereotype.Component;
10
11 import eu.etaxonomy.cdm.api.service.ITermService;
12 import eu.etaxonomy.cdm.model.common.CdmBase;
13 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
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 @Autowired
38 ITermService termService;
39
40
41 private ICdmEntitySessionManager cdmEntitySessionManager;
42
43 private CdmTransientEntityCacher cdmTransientEntityCacher;
44
45 private CacheLoader cacheLoader;
46
47 @Override
48 protected void setup() {
49 DefinedTermBase.setCacher(this);
50 CdmTransientEntityCacher.setDefaultCacher(this);
51 TermServiceRequestExecutor.setDefaultCacher(this);
52 cacheLoader = new CacheLoader(this);
53 }
54
55
56 @Override
57 protected CacheConfiguration getDefaultCacheConfiguration() {
58 // For a better understanding on how to size caches, refer to
59 // http://ehcache.org/documentation/configuration/cache-size
60
61 SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
62 sizeOfConfig.setMaxDepth(100);
63 sizeOfConfig.setMaxDepthExceededBehavior("abort");
64
65 return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
66 .eternal(true)
67 .statistics(true)
68 .sizeOfPolicy(sizeOfConfig)
69 .overflowToOffHeap(false);
70
71 }
72
73 @Override
74 protected CdmBase findByUuid(UUID uuid) {
75 CdmBase term = termService.findWithoutFlush(uuid);
76 return load(term);
77 }
78
79 /* (non-Javadoc)
80 * @see eu.etaxonomy.cdm.model.ICdmCacher#isCachable(eu.etaxonomy.cdm.model.common.CdmBase)
81 */
82 @Override
83 public boolean isCachable(CdmBase cdmEntity) {
84 if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
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 }