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
.taxeditor
.remoting
.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
.service
.ICommonService
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.PersistentMultiLanguageText
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.CdmRemotingException
;
34 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
.CollectionType
;
35 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
43 public class CachedCommonServiceImpl
implements ICachedCommonService
{
47 private ICommonService commonService
;
49 private static boolean cacheEnabled
= true;
52 private ICdmEntitySessionManager cdmEntitySessionManager
;
55 public static boolean isCacheEnabled() {
59 public static void setCacheEnabled(boolean cacheEnabled
) {
60 CachedCommonServiceImpl
.cacheEnabled
= cacheEnabled
;
64 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#find(java.lang.Class, int)
67 public CdmBase
find(Class
<?
extends CdmBase
> clazz
, int id
) {
69 CdmBase cdmEntity
= CdmBase
.deproxy(commonService
.find(clazz
, id
),clazz
);
70 if(cdmEntity
== null) {
71 throw new NullPointerException("CDM Entity of type " + clazz
.getName() + " with id " + id
+ " is null.");
73 return cdmEntitySessionManager
.load(cdmEntity
);
75 return CdmBase
.deproxy(commonService
.find(clazz
, id
),clazz
);
80 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#initializeCollection(org.hibernate.collection.spi.PersistentCollection)
83 public PersistentCollection
initializeCollection(PersistentCollection col
) {
84 PersistentCollection pc
= commonService
.initializeCollection(col
);
89 public void updatePersistentCollection(CollectionField colf
) {
91 cdmEntitySessionManager
.load(colf
.getCollection());
96 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#isEmpty(org.hibernate.collection.spi.PersistentCollection)
99 public boolean isEmpty(PersistentCollection col
) {
100 return commonService
.isEmpty(col
);
106 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#size(org.hibernate.collection.spi.PersistentCollection)
109 public int size(PersistentCollection col
) {
110 return commonService
.size(col
);
115 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#get(org.hibernate.collection.spi.PersistentCollection, int)
118 public Object
get(PersistentCollection col
, int index
) {
119 return commonService
.get(col
, index
);
124 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#contains(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
127 public boolean contains(PersistentCollection col
, Object element
) {
128 return commonService
.contains(col
, element
);
133 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsKey(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
136 public boolean containsKey(PersistentCollection col
, Object key
) {
137 return commonService
.containsKey(col
, key
);
142 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsValue(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
145 public boolean containsValue(PersistentCollection col
, Object element
) {
146 return commonService
.containsValue(col
, element
);
149 @SuppressWarnings("rawtypes")
151 public CollectionField
getCollectionField(PersistentCollection pc
) {
153 if(pc
instanceof PersistentSet
) {
154 return new CollectionField(new HashSet((Set
)pc
), CollectionType
.SET
);
156 if(pc
instanceof PersistentSortedSet
) {
157 return new CollectionField(new TreeSet((Set
)pc
), CollectionType
.SET
);
159 if(pc
instanceof PersistentList
) {
160 return new CollectionField(new ArrayList((List
)pc
), CollectionType
.LIST
);
162 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
163 return new CollectionField(new HashMap((Map
)pc
), CollectionType
.MAP
);
165 if(pc
instanceof PersistentSortedMap
) {
166 return new CollectionField(new TreeMap((Map
)pc
), CollectionType
.MAP
);
168 throw new CdmRemotingException("Cannot get Collection field for type " + pc
.getClass().getName());
173 public class CollectionField
{
174 private final Object col
;
175 private final CollectionType type
;
176 public CollectionField(Object col
, CollectionType type
) {
181 public Object
getCollection() {
185 public CollectionType
getType() {