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
{
54 public String
toString() {
55 return this.name().toLowerCase();
59 public static Object
getCollectionType(Object obj
) {
61 if(obj
instanceof List
) {
62 return CollectionType
.LIST
;
64 if(obj
instanceof Set
) {
65 return CollectionType
.SET
;
67 if(obj
instanceof Map
) {
68 return CollectionType
.MAP
;
70 throw new CdmRemotingException("Cannot get Collection Type for " + obj
.getClass().getName());
75 public static Object
getObject(PersistentCollection pc
) {
77 if(pc
instanceof PersistentSet
) {
78 return new HashSet((Set
)pc
);
80 if(pc
instanceof PersistentSortedSet
) {
81 return new TreeSet((Set
)pc
);
83 if(pc
instanceof PersistentList
|| pc
instanceof PersistentBag
) {
84 return new ArrayList((List
)pc
);
86 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
87 return new HashMap((Map
)pc
);
89 if(pc
instanceof PersistentSortedMap
) {
90 return new TreeMap((Map
)pc
);
92 throw new CdmRemotingException("Cannot get Collection field for type " + pc
.getClass().getName());
97 public static CollectionField
getCollectionField(PersistentCollection pc
) {
99 if(pc
instanceof PersistentSet
) {
100 return new CollectionField(new HashSet((Set
)pc
), CollectionType
.SET
);
102 if(pc
instanceof PersistentSortedSet
) {
103 return new CollectionField(new TreeSet((Set
)pc
), CollectionType
.SET
);
105 if(pc
instanceof PersistentList
) {
106 return new CollectionField(new ArrayList((List
)pc
), CollectionType
.LIST
);
108 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
109 return new CollectionField(new HashMap((Map
)pc
), CollectionType
.MAP
);
111 if(pc
instanceof PersistentSortedMap
) {
112 return new CollectionField(new TreeMap((Map
)pc
), CollectionType
.MAP
);
114 throw new CdmRemotingException("Cannot get Collection field for type " + pc
.getClass().getName());
119 public static class CollectionField
{
120 private final Object col
;
121 private final CollectionType type
;
122 public CollectionField(Object col
, CollectionType type
) {
127 public Object
getCollection() {
131 public CollectionType
getType() {
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();
146 if(o
!= null && o
instanceof PersistentCollection
) {
147 PersistentCollection pc
= ((PersistentCollection
)o
);
148 if(pc
.wasInitialized()) {
149 return ProxyUtils
.getObject(pc
);
156 public static boolean isProxy(Object o
) {
157 if(o
!= null && o
instanceof HibernateProxy
) {
158 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
159 if(hli
.isUninitialized()) {
164 if(o
!= null && o
instanceof PersistentCollection
) {
165 PersistentCollection pc
= ((PersistentCollection
)o
);
166 if(!pc
.wasInitialized()) {
174 public static Object
remoteLoadPersistentCollectionIfProxy(Object o
, UUID ownerUuid
, String fieldName
) throws ClassNotFoundException
{
175 if(o
!= null && o
instanceof HibernateProxy
) {
176 LazyInitializer hli
= ((HibernateProxy
)o
).getHibernateLazyInitializer();
177 if(hli
.isUninitialized()) {
178 return CdmApplicationState
.getCachedCommonService().find((Class
<CdmBase
>)Class
.forName(hli
.getEntityName()),
179 ((Integer
)hli
.getIdentifier()).intValue());
183 if(o
!= null && o
instanceof PersistentCollection
) {
184 PersistentCollection pc
= ((PersistentCollection
)o
);
185 if(!pc
.wasInitialized()) {
186 return CdmApplicationState
.getCachedCommonService().initializeCollection(ownerUuid
, fieldName
);
196 public static void setRoleValueInOwner(Object owner
, String role
, Object value
) {
197 if(role
== null || role
.isEmpty()) {
198 throw new CdmRemotingException("Role cannot be null or an empty string");
201 String fieldName
= role
.substring(role
.lastIndexOf(".") + 1);
203 Field field
= ReflectionUtils
.findField(owner
.getClass(), fieldName
);
206 throw new CdmRemotingException("Field '" + fieldName
207 + "' not found when searching in class '" + owner
.getClass() + "' and its supercalsses");
210 field
.setAccessible(true);
213 field
.set(owner
, value
);
214 } catch (IllegalArgumentException e
) {
215 throw new CdmRemotingException(e
);
216 } catch (IllegalAccessException e
) {
217 throw new CdmRemotingException(e
);