3 * Copyright (C) 2015 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
.lang
.reflect
.Field
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
19 import java
.util
.TreeMap
;
20 import java
.util
.TreeSet
;
21 import java
.util
.UUID
;
23 import org
.hibernate
.collection
.internal
.PersistentBag
;
24 import org
.hibernate
.collection
.internal
.PersistentList
;
25 import org
.hibernate
.collection
.internal
.PersistentMap
;
26 import org
.hibernate
.collection
.internal
.PersistentSet
;
27 import org
.hibernate
.collection
.internal
.PersistentSortedMap
;
28 import org
.hibernate
.collection
.internal
.PersistentSortedSet
;
29 import org
.hibernate
.collection
.spi
.PersistentCollection
;
30 import org
.hibernate
.proxy
.HibernateProxy
;
31 import org
.hibernate
.proxy
.LazyInitializer
;
32 import org
.springframework
.util
.ReflectionUtils
;
34 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
35 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
36 import eu
.etaxonomy
.cdm
.model
.common
.PersistentMultiLanguageText
;
37 import eu
.etaxonomy
.taxeditor
.remoting
.CdmRemotingException
;
44 public class ProxyUtils
{
48 public static enum CollectionType
{
55 public String
toString() {
56 return this.name().toLowerCase();
60 public static Object
getCollectionType(Object obj
, Class
<?
> clazz
) {
62 if(obj
instanceof List
) {
63 //the field in PersistentBag is called "bag" although it is an ArrayList -> #
64 if(clazz
.equals(PersistentBag
.class)){
65 return CollectionType
.BAG
;
67 return CollectionType
.LIST
;
69 if(obj
instanceof Set
) {
70 return CollectionType
.SET
;
72 if(obj
instanceof Map
) {
73 return CollectionType
.MAP
;
75 throw new CdmRemotingException("Cannot get Collection Type for " + obj
.getClass().getName());
80 public static Object
getObject(PersistentCollection pc
) {
82 if(pc
instanceof PersistentSet
) {
83 return new HashSet
<>((Set
<?
>)pc
);
85 if(pc
instanceof PersistentSortedSet
) {
86 return new TreeSet
<>((Set
<?
>)pc
);
88 if(pc
instanceof PersistentList
|| pc
instanceof PersistentBag
) {
89 return new ArrayList
<>((List
<?
>)pc
);
91 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
92 return new HashMap
<>((Map
<?
,?
>)pc
);
94 if(pc
instanceof PersistentSortedMap
) {
95 return new TreeMap
<>((Map
<?
,?
>)pc
);
97 throw new CdmRemotingException("Cannot get Collection field for type " + pc
.getClass().getName());
102 public static CollectionField
getCollectionField(PersistentCollection pc
) {
104 if(pc
instanceof PersistentSet
) {
105 return new CollectionField(new HashSet
<>((Set
<?
>)pc
), CollectionType
.SET
);
107 if(pc
instanceof PersistentSortedSet
) {
108 return new CollectionField(new TreeSet
<>((Set
<?
>)pc
), CollectionType
.SET
);
110 if(pc
instanceof PersistentList
) {
111 return new CollectionField(new ArrayList
<>((List
<?
>)pc
), CollectionType
.LIST
);
113 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
114 return new CollectionField(new HashMap
<>((Map
<?
,?
>)pc
), CollectionType
.MAP
);
116 if(pc
instanceof PersistentSortedMap
) {
117 return new CollectionField(new TreeMap
<>((Map
<?
,?
>)pc
), CollectionType
.MAP
);
119 throw new CdmRemotingException("Cannot get Collection field for type " + pc
.getClass().getName());
124 public static class CollectionField
{
125 private final Object col
;
126 private final CollectionType type
;
127 public CollectionField(Object col
, CollectionType type
) {
132 public Object
getCollection() {
136 public CollectionType
getType() {
142 public static Object
deproxy(Object o
) {
143 if(o
!= null && o
instanceof HibernateProxy
) {
144 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
145 if(!hli
.isUninitialized()) {
146 return hli
.getImplementation();
151 if(o
!= null && o
instanceof PersistentCollection
) {
152 PersistentCollection pc
= ((PersistentCollection
)o
);
153 if(pc
.wasInitialized()) {
154 return ProxyUtils
.getObject(pc
);
161 public static boolean isProxy(Object o
) {
162 if(o
!= null && o
instanceof HibernateProxy
) {
163 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
164 if(hli
.isUninitialized()) {
169 if(o
!= null && o
instanceof PersistentCollection
) {
170 PersistentCollection pc
= ((PersistentCollection
)o
);
171 if(!pc
.wasInitialized()) {
179 @SuppressWarnings("unchecked")
180 public static Object
remoteLoadPersistentCollectionIfProxy(Object o
, UUID ownerUuid
, String fieldName
) throws ClassNotFoundException
{
181 if(o
!= null && o
instanceof HibernateProxy
) {
182 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
183 if(hli
.isUninitialized()) {
184 return CdmApplicationState
.getCachedCommonService().find((Class
<CdmBase
>)Class
.forName(hli
.getEntityName()),
185 ((Integer
)hli
.getIdentifier()).intValue());
189 if(o
!= null && o
instanceof PersistentCollection
) {
190 PersistentCollection pc
= ((PersistentCollection
)o
);
191 if(!pc
.wasInitialized()) {
192 return CdmApplicationState
.getCachedCommonService().initializeCollection(ownerUuid
, fieldName
);
202 public static void setRoleValueInOwner(Object owner
, String role
, Object value
) {
203 if(role
== null || role
.isEmpty()) {
204 throw new CdmRemotingException("Role cannot be null or an empty string");
207 String fieldName
= role
.substring(role
.lastIndexOf(".") + 1);
209 Field field
= ReflectionUtils
.findField(owner
.getClass(), fieldName
);
212 throw new CdmRemotingException("Field '" + fieldName
213 + "' not found when searching in class '" + owner
.getClass() + "' and its supercalsses");
216 field
.setAccessible(true);
219 field
.set(owner
, value
);
220 } catch (IllegalArgumentException e
) {
221 throw new CdmRemotingException(e
);
222 } catch (IllegalAccessException e
) {
223 throw new CdmRemotingException(e
);