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
.taxeditor
.remoting
.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
;
20 import java
.util
.UUID
;
22 import org
.hibernate
.collection
.internal
.PersistentBag
;
23 import org
.hibernate
.collection
.internal
.PersistentList
;
24 import org
.hibernate
.collection
.internal
.PersistentMap
;
25 import org
.hibernate
.collection
.internal
.PersistentSet
;
26 import org
.hibernate
.collection
.internal
.PersistentSortedMap
;
27 import org
.hibernate
.collection
.internal
.PersistentSortedSet
;
28 import org
.hibernate
.collection
.spi
.PersistentCollection
;
29 import org
.hibernate
.proxy
.HibernateProxy
;
30 import org
.hibernate
.proxy
.LazyInitializer
;
31 import org
.springframework
.util
.ReflectionUtils
;
33 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
34 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.PersistentMultiLanguageText
;
36 import eu
.etaxonomy
.taxeditor
.remoting
.CdmRemotingException
;
43 public class ProxyUtils
{
47 public static enum CollectionType
{
54 public String
toString() {
55 return this.name().toLowerCase();
59 public static Object
getCollectionType(Object obj
, Class
<?
> clazz
) {
61 if(obj
instanceof List
) {
62 //the field in PersistentBag is called "bag" although it is an ArrayList -> #
63 if(clazz
.equals(PersistentBag
.class)){
64 return CollectionType
.BAG
;
66 return CollectionType
.LIST
;
68 if(obj
instanceof Set
) {
69 return CollectionType
.SET
;
71 if(obj
instanceof Map
) {
72 return CollectionType
.MAP
;
74 throw new CdmRemotingException("Cannot get Collection Type for " + obj
.getClass().getName());
79 public static Object
getObject(PersistentCollection pc
) {
81 if(pc
instanceof PersistentSet
) {
82 return new HashSet
<>((Set
<?
>)pc
);
84 if(pc
instanceof PersistentSortedSet
) {
85 return new TreeSet
<>((Set
<?
>)pc
);
87 if(pc
instanceof PersistentList
|| pc
instanceof PersistentBag
) {
88 return new ArrayList
<>((List
<?
>)pc
);
90 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
91 return new HashMap
<>((Map
<?
,?
>)pc
);
93 if(pc
instanceof PersistentSortedMap
) {
94 return new TreeMap
<>((Map
<?
,?
>)pc
);
96 throw new CdmRemotingException("Cannot get Collection field for type " + pc
.getClass().getName());
101 public static CollectionField
getCollectionField(PersistentCollection pc
) {
103 if(pc
instanceof PersistentSet
) {
104 return new CollectionField(new HashSet
<>((Set
<?
>)pc
), CollectionType
.SET
);
106 if(pc
instanceof PersistentSortedSet
) {
107 return new CollectionField(new TreeSet
<>((Set
<?
>)pc
), CollectionType
.SET
);
109 if(pc
instanceof PersistentList
) {
110 return new CollectionField(new ArrayList
<>((List
<?
>)pc
), CollectionType
.LIST
);
112 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
113 return new CollectionField(new HashMap
<>((Map
<?
,?
>)pc
), CollectionType
.MAP
);
115 if(pc
instanceof PersistentSortedMap
) {
116 return new CollectionField(new TreeMap
<>((Map
<?
,?
>)pc
), CollectionType
.MAP
);
118 throw new CdmRemotingException("Cannot get Collection field for type " + pc
.getClass().getName());
123 public static class CollectionField
{
124 private final Object col
;
125 private final CollectionType type
;
126 public CollectionField(Object col
, CollectionType type
) {
131 public Object
getCollection() {
135 public CollectionType
getType() {
141 public static Object
deproxy(Object o
) {
142 if(o
!= null && o
instanceof HibernateProxy
) {
143 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
144 if(!hli
.isUninitialized()) {
145 return hli
.getImplementation();
150 if(o
!= null && o
instanceof PersistentCollection
) {
151 PersistentCollection pc
= ((PersistentCollection
)o
);
152 if(pc
.wasInitialized()) {
153 return ProxyUtils
.getObject(pc
);
160 public static boolean isProxy(Object o
) {
161 if(o
!= null && o
instanceof HibernateProxy
) {
162 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
163 if(hli
.isUninitialized()) {
168 if(o
!= null && o
instanceof PersistentCollection
) {
169 PersistentCollection pc
= ((PersistentCollection
)o
);
170 if(!pc
.wasInitialized()) {
178 @SuppressWarnings("unchecked")
179 public static Object
remoteLoadPersistentCollectionIfProxy(Object o
, UUID ownerUuid
, String fieldName
) throws ClassNotFoundException
{
180 if(o
!= null && o
instanceof HibernateProxy
) {
181 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
182 if(hli
.isUninitialized()) {
183 return CdmApplicationState
.getCachedCommonService().find((Class
<CdmBase
>)Class
.forName(hli
.getEntityName()),
184 ((Integer
)hli
.getIdentifier()).intValue());
188 if(o
!= null && o
instanceof PersistentCollection
) {
189 PersistentCollection pc
= ((PersistentCollection
)o
);
190 if(!pc
.wasInitialized()) {
191 return CdmApplicationState
.getCachedCommonService().initializeCollection(ownerUuid
, fieldName
);
201 public static void setRoleValueInOwner(Object owner
, String role
, Object value
) {
202 if(role
== null || role
.isEmpty()) {
203 throw new CdmRemotingException("Role cannot be null or an empty string");
206 String fieldName
= role
.substring(role
.lastIndexOf(".") + 1);
208 Field field
= ReflectionUtils
.findField(owner
.getClass(), fieldName
);
211 throw new CdmRemotingException("Field '" + fieldName
212 + "' not found when searching in class '" + owner
.getClass() + "' and its supercalsses");
215 field
.setAccessible(true);
218 field
.set(owner
, value
);
219 } catch (IllegalArgumentException e
) {
220 throw new CdmRemotingException(e
);
221 } catch (IllegalAccessException e
) {
222 throw new CdmRemotingException(e
);