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
.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
.model
.common
.PersistentMultiLanguageText
;
34 import eu
.etaxonomy
.taxeditor
.remoting
.CdmRemotingException
;
41 public class ProxyUtils
{
45 public static enum CollectionType
{
51 public String
toString() {
52 return this.name().toLowerCase();
56 public static Object
getCollectionType(Object obj
) {
58 if(obj
instanceof List
) {
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 CdmRemotingException("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 CdmRemotingException("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 CdmRemotingException("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 public static Object
deproxy(Object o
) {
135 if(o
!= null && o
instanceof HibernateProxy
) {
136 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
137 if(!hli
.isUninitialized()) {
138 return hli
.getImplementation();
143 if(o
!= null && o
instanceof PersistentCollection
) {
144 PersistentCollection pc
= ((PersistentCollection
)o
);
145 if(pc
.wasInitialized()) {
146 return ProxyUtils
.getObject(pc
);
153 public static boolean isProxy(Object o
) {
154 if(o
!= null && o
instanceof HibernateProxy
) {
155 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
156 if(hli
.isUninitialized()) {
161 if(o
!= null && o
instanceof PersistentCollection
) {
162 PersistentCollection pc
= ((PersistentCollection
)o
);
163 if(!pc
.wasInitialized()) {
171 public static void setRoleValueInOwner(Object owner
, String role
, Object value
) {
172 if(role
== null || role
.isEmpty()) {
173 throw new CdmRemotingException("Role cannot be null or an empty string");
176 String fieldName
= role
.substring(role
.lastIndexOf(".") + 1);
178 Field field
= ReflectionUtils
.findField(owner
.getClass(), fieldName
);
181 throw new CdmRemotingException("Field '" + fieldName
182 + "' not found when searching in class '" + owner
.getClass() + "' and its supercalsses");
185 field
.setAccessible(true);
188 field
.set(owner
, value
);
189 } catch (IllegalArgumentException e
) {
190 throw new CdmRemotingException(e
);
191 } catch (IllegalAccessException e
) {
192 throw new CdmRemotingException(e
);