2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.cache
;
11 import java
.lang
.reflect
.Field
;
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
.PersistentBag
;
22 import org
.hibernate
.collection
.internal
.PersistentList
;
23 import org
.hibernate
.collection
.internal
.PersistentMap
;
24 import org
.hibernate
.collection
.internal
.PersistentSet
;
25 import org
.hibernate
.collection
.internal
.PersistentSortedMap
;
26 import org
.hibernate
.collection
.internal
.PersistentSortedSet
;
27 import org
.hibernate
.collection
.spi
.PersistentCollection
;
28 import org
.hibernate
.proxy
.HibernateProxy
;
29 import org
.hibernate
.proxy
.LazyInitializer
;
30 import org
.springframework
.util
.ReflectionUtils
;
32 import eu
.etaxonomy
.cdm
.model
.common
.PersistentMultiLanguageText
;
38 public class ProxyUtils
{
40 public static enum CollectionType
{
47 public String
toString() {
48 return this.name().toLowerCase();
52 public static Object
getCollectionType(Object obj
, Class
<?
> clazz
) {
54 if(obj
instanceof List
) {
55 //the field in PersistentBag is called "bag" although it is an ArrayList -> #
56 if(clazz
.equals(PersistentBag
.class)){
57 return CollectionType
.BAG
;
59 return CollectionType
.LIST
;
61 if(obj
instanceof Set
) {
62 return CollectionType
.SET
;
64 if(obj
instanceof Map
) {
65 return CollectionType
.MAP
;
67 throw new ProxyUtilsException("Cannot get Collection Type for " + obj
.getClass().getName());
72 public static Object
getObject(PersistentCollection pc
) {
74 if(pc
instanceof PersistentSet
) {
75 return new HashSet
<>((Set
<?
>)pc
);
77 if(pc
instanceof PersistentSortedSet
) {
78 return new TreeSet
<>((Set
<?
>)pc
);
80 if(pc
instanceof PersistentList
|| pc
instanceof PersistentBag
) {
81 return new ArrayList
<>((List
<?
>)pc
);
83 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
84 return new HashMap
<>((Map
<?
,?
>)pc
);
86 if(pc
instanceof PersistentSortedMap
) {
87 return new TreeMap
<>((Map
<?
,?
>)pc
);
89 throw new ProxyUtilsException("Cannot get Collection field for type " + pc
.getClass().getName());
94 public static CollectionField
getCollectionField(PersistentCollection pc
) {
96 if(pc
instanceof PersistentSet
) {
97 return new CollectionField(new HashSet
<>((Set
<?
>)pc
), CollectionType
.SET
);
99 if(pc
instanceof PersistentSortedSet
) {
100 return new CollectionField(new TreeSet
<>((Set
<?
>)pc
), CollectionType
.SET
);
102 if(pc
instanceof PersistentList
) {
103 return new CollectionField(new ArrayList
<>((List
<?
>)pc
), CollectionType
.LIST
);
105 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
106 return new CollectionField(new HashMap
<>((Map
<?
,?
>)pc
), CollectionType
.MAP
);
108 if(pc
instanceof PersistentSortedMap
) {
109 return new CollectionField(new TreeMap
<>((Map
<?
,?
>)pc
), CollectionType
.MAP
);
111 throw new ProxyUtilsException("Cannot get Collection field for type " + pc
.getClass().getName());
116 public static class CollectionField
{
117 private final Object col
;
118 private final CollectionType type
;
119 public CollectionField(Object col
, CollectionType type
) {
124 public Object
getCollection() {
128 public CollectionType
getType() {
134 * de-proxies the passed object <code>o</code> if it is an initialized proxy object,
135 * otherwise <code>o</code> is returned.
137 public static Object
deproxy(Object o
) {
138 if(o
!= null && o
instanceof HibernateProxy
) {
139 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
140 if(!hli
.isUninitialized()) {
141 return hli
.getImplementation();
145 if(o
!= null && o
instanceof PersistentCollection
) {
146 PersistentCollection pc
= ((PersistentCollection
)o
);
147 if(pc
.wasInitialized()) {
148 return ProxyUtils
.getObject(pc
);
155 * de-proxies the passed object <code>o</code> if it is an initialized proxy object,
156 * otherwise <code>null</code> is returned.
158 public static Object
deproxyOrNull(Object o
) {
159 if(o
!= null && o
instanceof HibernateProxy
) {
160 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
161 if(!hli
.isUninitialized()) {
162 return hli
.getImplementation();
168 if(o
!= null && o
instanceof PersistentCollection
) {
169 PersistentCollection pc
= ((PersistentCollection
)o
);
170 if(pc
.wasInitialized()) {
171 return ProxyUtils
.getObject(pc
);
179 public static boolean isUninitializedProxy(Object o
) {
180 if(o
!= null && o
instanceof HibernateProxy
) {
181 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
182 if(hli
.isUninitialized()) {
187 if(o
!= null && o
instanceof PersistentCollection
) {
188 PersistentCollection pc
= ((PersistentCollection
)o
);
189 if(!pc
.wasInitialized()) {
198 * ########################################### makes no sense without remoting ###########################################
199 @SuppressWarnings("unchecked")
200 public static Object remoteLoadPersistentCollectionIfProxy(Object o, UUID ownerUuid, String fieldName) throws ClassNotFoundException {
201 if(o != null && o instanceof HibernateProxy) {
202 LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
203 if(hli.isUninitialized()) {
204 return CdmApplicationState.getCachedCommonService().find((Class<CdmBase>)Class.forName(hli.getEntityName()),
205 ((Integer)hli.getIdentifier()).intValue());
209 if(o != null && o instanceof PersistentCollection) {
210 PersistentCollection pc = ((PersistentCollection)o);
211 if(!pc.wasInitialized()) {
212 return CdmApplicationState.getCachedCommonService().initializeCollection(ownerUuid, fieldName);
218 ########################################### ########################################### */
220 public static void setRoleValueInOwner(Object owner
, String role
, Object value
) {
221 if(role
== null || role
.isEmpty()) {
222 throw new ProxyUtilsException("Role cannot be null or an empty string");
225 String fieldName
= role
.substring(role
.lastIndexOf(".") + 1);
227 Field field
= ReflectionUtils
.findField(owner
.getClass(), fieldName
);
230 throw new ProxyUtilsException("Field '" + fieldName
231 + "' not found when searching in class '" + owner
.getClass() + "' and its supercalsses");
234 field
.setAccessible(true);
237 field
.set(owner
, value
);
238 } catch (IllegalArgumentException e
) {
239 throw new ProxyUtilsException(e
);
240 } catch (IllegalAccessException e
) {
241 throw new ProxyUtilsException(e
);