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
;
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
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.CdmRemotingException
;
40 public class ProxyUtils
{
44 public static enum CollectionType
{
50 public String
toString() {
51 return this.name().toLowerCase();
55 public static Object
getCollectionType(Object obj
) {
57 if(obj
instanceof List
) {
58 return CollectionType
.LIST
;
60 if(obj
instanceof Set
) {
61 return CollectionType
.SET
;
63 if(obj
instanceof Map
) {
64 return CollectionType
.MAP
;
66 throw new CdmRemotingException("Cannot get Collection Type for " + obj
.getClass().getName());
71 public static Object
getObject(PersistentCollection pc
) {
73 if(pc
instanceof PersistentSet
) {
74 return new HashSet((Set
)pc
);
76 if(pc
instanceof PersistentSortedSet
) {
77 return new TreeSet((Set
)pc
);
79 if(pc
instanceof PersistentList
) {
80 return new ArrayList((List
)pc
);
82 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
83 return new HashMap((Map
)pc
);
85 if(pc
instanceof PersistentSortedMap
) {
86 return new TreeMap((Map
)pc
);
88 throw new CdmRemotingException("Cannot get Collection field for type " + pc
.getClass().getName());
93 public static CollectionField
getCollectionField(PersistentCollection pc
) {
95 if(pc
instanceof PersistentSet
) {
96 return new CollectionField(new HashSet((Set
)pc
), CollectionType
.SET
);
98 if(pc
instanceof PersistentSortedSet
) {
99 return new CollectionField(new TreeSet((Set
)pc
), CollectionType
.SET
);
101 if(pc
instanceof PersistentList
) {
102 return new CollectionField(new ArrayList((List
)pc
), CollectionType
.LIST
);
104 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
105 return new CollectionField(new HashMap((Map
)pc
), CollectionType
.MAP
);
107 if(pc
instanceof PersistentSortedMap
) {
108 return new CollectionField(new TreeMap((Map
)pc
), CollectionType
.MAP
);
110 throw new CdmRemotingException("Cannot get Collection field for type " + pc
.getClass().getName());
115 public static class CollectionField
{
116 private final Object col
;
117 private final CollectionType type
;
118 public CollectionField(Object col
, CollectionType type
) {
123 public Object
getCollection() {
127 public CollectionType
getType() {
133 public static Object
deproxy(Object o
) {
134 if(o
!= null && o
instanceof HibernateProxy
) {
135 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
136 if(!hli
.isUninitialized()) {
137 return hli
.getImplementation();
142 if(o
!= null && o
instanceof PersistentCollection
) {
143 PersistentCollection pc
= ((PersistentCollection
)o
);
144 if(pc
.wasInitialized()) {
145 return ProxyUtils
.getObject(pc
);
152 public static boolean isProxy(Object o
) {
153 if(o
!= null && o
instanceof HibernateProxy
) {
154 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
155 if(hli
.isUninitialized()) {
160 if(o
!= null && o
instanceof PersistentCollection
) {
161 PersistentCollection pc
= ((PersistentCollection
)o
);
162 if(!pc
.wasInitialized()) {
170 public static void setRoleValueInOwner(Object owner
, String role
, Object value
) {
171 if(role
== null || role
.isEmpty()) {
172 throw new CdmRemotingException("Role cannot be null or an empty string");
175 String fieldName
= role
.substring(role
.lastIndexOf(".") + 1);
177 Field field
= ReflectionUtils
.findField(owner
.getClass(), fieldName
);
180 throw new CdmRemotingException("Field '" + fieldName
181 + "' not found when searching in class '" + owner
.getClass() + "' and its supercalsses");
184 field
.setAccessible(true);
187 field
.set(owner
, value
);
188 } catch (IllegalArgumentException e
) {
189 throw new CdmRemotingException(e
);
190 } catch (IllegalAccessException e
) {
191 throw new CdmRemotingException(e
);