ea88f5fe07eca8f2663d986ea467cb50d27e5b8d
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / main / java / eu / etaxonomy / cdm / api / cache / CachedCommonServiceImpl.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.api.cache;
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.TreeMap;
19 import java.util.TreeSet;
20
21 import org.hibernate.collection.internal.PersistentList;
22 import org.hibernate.collection.internal.PersistentMap;
23 import org.hibernate.collection.internal.PersistentSet;
24 import org.hibernate.collection.internal.PersistentSortedMap;
25 import org.hibernate.collection.internal.PersistentSortedSet;
26 import org.hibernate.collection.spi.PersistentCollection;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Component;
29
30 import eu.etaxonomy.cdm.api.cache.CdmEntityCachingUtils.CollectionType;
31 import eu.etaxonomy.cdm.api.service.ICommonService;
32 import eu.etaxonomy.cdm.model.common.CdmBase;
33 import eu.etaxonomy.cdm.model.common.PersistentMultiLanguageText;
34 import eu.etaxonomy.taxeditor.remoting.session.CdmEntitySessionManager;
35
36 /**
37 * @author cmathew
38 * @date 14 Oct 2014
39 *
40 */
41 @Component
42 public class CachedCommonServiceImpl implements ICachedCommonService {
43
44
45 @Autowired
46 private ICommonService commonService;
47
48 private static boolean cacheEnabled = true;
49
50 @Autowired
51 private CdmEntitySessionManager cdmEntitySessionManager;
52
53 @Autowired
54 private CdmEntityCachingUtils cdmEntityCachingUtils;
55
56
57 public static boolean isCacheEnabled() {
58 return cacheEnabled;
59 }
60
61 public static void setCacheEnabled(boolean cacheEnabled) {
62 CachedCommonServiceImpl.cacheEnabled = cacheEnabled;
63 }
64
65 /* (non-Javadoc)
66 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#find(java.lang.Class, int)
67 */
68 @Override
69 public CdmBase find(Class<? extends CdmBase> clazz, int id) {
70 if(cacheEnabled) {
71 CdmBase cdmEntity = cdmEntitySessionManager.getActiveCdmTransientEntityCacher().getFromCache(clazz, id);
72 if(cdmEntity != null) {
73 return cdmEntity;
74 } else {
75 cdmEntity = CdmBase.deproxy(commonService.find(clazz, id),clazz);
76 cdmEntitySessionManager.getActiveCdmTransientEntityCacher().put(cdmEntity);
77 return cdmEntity;
78 }
79 } else {
80 return CdmBase.deproxy(commonService.find(clazz, id),clazz);
81 }
82 }
83
84 /* (non-Javadoc)
85 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#initializeCollection(org.hibernate.collection.spi.PersistentCollection)
86 */
87 @Override
88 public PersistentCollection initializeCollection(PersistentCollection col) {
89 PersistentCollection pc = commonService.initializeCollection(col);
90 return pc;
91 }
92
93 @Override
94 public void updatePersistentCollection(CollectionField colf) {
95 if(cacheEnabled) {
96 switch(colf.getType()) {
97 case MAP:
98 cdmEntityCachingUtils.cachify((Map<Object,Object>)colf.getCollection());
99 case SET:
100 case LIST:
101 default:
102 }
103 }
104 }
105
106 /* (non-Javadoc)
107 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#isEmpty(org.hibernate.collection.spi.PersistentCollection)
108 */
109 @Override
110 public boolean isEmpty(PersistentCollection col) {
111 return commonService.isEmpty(col);
112
113 }
114
115
116 /* (non-Javadoc)
117 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#size(org.hibernate.collection.spi.PersistentCollection)
118 */
119 @Override
120 public int size(PersistentCollection col) {
121 return commonService.size(col);
122 }
123
124
125 /* (non-Javadoc)
126 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#get(org.hibernate.collection.spi.PersistentCollection, int)
127 */
128 @Override
129 public Object get(PersistentCollection col, int index) {
130 return commonService.get(col, index);
131 }
132
133
134 /* (non-Javadoc)
135 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#contains(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
136 */
137 @Override
138 public boolean contains(PersistentCollection col, Object element) {
139 return commonService.contains(col, element);
140 }
141
142
143 /* (non-Javadoc)
144 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsKey(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
145 */
146 @Override
147 public boolean containsKey(PersistentCollection col, Object key) {
148 return commonService.containsKey(col, key);
149 }
150
151
152 /* (non-Javadoc)
153 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsValue(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
154 */
155 @Override
156 public boolean containsValue(PersistentCollection col, Object element) {
157 return commonService.containsValue(col, element);
158 }
159
160 @SuppressWarnings("rawtypes")
161 @Override
162 public CollectionField getCollectionField(PersistentCollection pc) {
163 if(pc != null) {
164 if(pc instanceof PersistentSet) {
165 return new CollectionField(new HashSet((Set)pc), CollectionType.SET);
166 }
167 if(pc instanceof PersistentSortedSet) {
168 return new CollectionField(new TreeSet((Set)pc), CollectionType.SET);
169 }
170 if(pc instanceof PersistentList) {
171 return new CollectionField(new ArrayList((List)pc), CollectionType.LIST);
172 }
173 if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
174 return new CollectionField(new HashMap((Map)pc), CollectionType.MAP);
175 }
176 if(pc instanceof PersistentSortedMap) {
177 return new CollectionField(new TreeMap((Map)pc), CollectionType.MAP);
178 }
179 }
180 return null;
181 }
182
183 public class CollectionField {
184 private final Object col;
185 private final CollectionType type;
186 public CollectionField(Object col, CollectionType type) {
187 this.col = col;
188 this.type = type;
189 }
190
191 public Object getCollection() {
192 return this.col;
193 }
194
195 public CollectionType getType() {
196 return this.type;
197 }
198 }
199
200
201
202 }