3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.cache
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.TreeMap
;
19 import java
.util
.TreeSet
;
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
;
30 import eu
.etaxonomy
.cdm
.api
.cache
.LazyLoadingCachingUtils
.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
;
41 public class CachedCommonServiceImpl
implements ICachedCommonService
{
45 private ICommonService commonService
;
47 private static boolean cacheEnabled
= true;
50 private CdmEntityCacheManager cdmEntityCacheManager
;
53 private LazyLoadingCachingUtils lazyLoadingCachingUtils
;
56 public static boolean isCacheEnabled() {
60 public static void setCacheEnabled(boolean cacheEnabled
) {
61 CachedCommonServiceImpl
.cacheEnabled
= cacheEnabled
;
65 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#find(java.lang.Class, int)
68 public CdmBase
find(Class
<?
extends CdmBase
> clazz
, int id
){
70 CdmBase cdmEntity
= cdmEntityCacheManager
.getCdmTransientEntityCacher().getFromCache(clazz
, id
);
71 if(cdmEntity
!= null) {
74 cdmEntity
= commonService
.find(clazz
, id
);
75 cdmEntity
= cdmEntityCacheManager
.getCdmTransientEntityCacher().put(clazz
, id
, cdmEntity
);
79 return commonService
.find(clazz
, id
);
85 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#initializeCollection(org.hibernate.collection.spi.PersistentCollection)
88 public PersistentCollection
initializeCollection(PersistentCollection col
) {
89 PersistentCollection pc
= commonService
.initializeCollection(col
);
95 public void updatePersistentCollection(CollectionField colf
) {
96 switch(colf
.getType()) {
98 lazyLoadingCachingUtils
.updatePersistentCollection((Map
)colf
.getCollection());
106 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#isEmpty(org.hibernate.collection.spi.PersistentCollection)
109 public boolean isEmpty(PersistentCollection col
) {
110 return commonService
.isEmpty(col
);
116 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#size(org.hibernate.collection.spi.PersistentCollection)
119 public int size(PersistentCollection col
) {
120 return commonService
.size(col
);
125 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#get(org.hibernate.collection.spi.PersistentCollection, int)
128 public Object
get(PersistentCollection col
, int index
) {
129 return commonService
.get(col
, index
);
134 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#contains(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
137 public boolean contains(PersistentCollection col
, Object element
) {
138 return commonService
.contains(col
, element
);
143 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsKey(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
146 public boolean containsKey(PersistentCollection col
, Object key
) {
147 return commonService
.containsKey(col
, key
);
152 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsValue(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
155 public boolean containsValue(PersistentCollection col
, Object element
) {
156 return commonService
.containsValue(col
, element
);
160 public CollectionField
getCollectionField(PersistentCollection pc
) {
162 if(pc
instanceof PersistentSet
) {
163 return new CollectionField(new HashSet((Set
)pc
), CollectionType
.SET
);
165 if(pc
instanceof PersistentSortedSet
) {
166 return new CollectionField(new TreeSet((Set
)pc
), CollectionType
.SET
);
168 if(pc
instanceof PersistentList
) {
169 return new CollectionField(new ArrayList((List
)pc
), CollectionType
.LIST
);
171 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
172 return new CollectionField(new HashMap((Map
)pc
), CollectionType
.MAP
);
174 if(pc
instanceof PersistentSortedMap
) {
175 return new CollectionField(new TreeMap((Map
)pc
), CollectionType
.MAP
);
181 public class CollectionField
{
182 private final Object col
;
183 private final CollectionType type
;
184 public CollectionField(Object col
, CollectionType type
) {
189 public Object
getCollection() {
193 public CollectionType
getType() {