cleanup
[cdmlib.git] / cdmlib-cache / src / main / java / eu / etaxonomy / cdm / cache / ProxyUtils.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.cache;
10
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;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.TreeMap;
19 import java.util.TreeSet;
20
21 import org.hibernate.collection.internal.PersistentBag;
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;
31
32 import eu.etaxonomy.cdm.model.common.PersistentMultiLanguageText;
33
34 /**
35 * @author cmathew
36 * @since 17 Feb 2015
37 */
38 public class ProxyUtils {
39
40 public static enum CollectionType {
41 SET,
42 LIST,
43 MAP,
44 BAG;
45
46 @Override
47 public String toString() {
48 return this.name().toLowerCase();
49 }
50 }
51
52 public static Object getCollectionType(Object obj, Class<?> clazz) {
53 if(obj != null) {
54 if(obj instanceof List) {
55 //the field in PersistentBag is called "bag" although it is an ArrayList -> #
56 if(clazz.equals(PersistentBag.class)){
57 return CollectionType.BAG;
58 }
59 return CollectionType.LIST;
60 }
61 if(obj instanceof Set) {
62 return CollectionType.SET;
63 }
64 if(obj instanceof Map) {
65 return CollectionType.MAP;
66 }
67 throw new ProxyUtilsException("Cannot get Collection Type for " + obj.getClass().getName());
68 }
69 return null;
70 }
71
72 public static Object getObject(PersistentCollection pc) {
73 if(pc != null) {
74 if(pc instanceof PersistentSet) {
75 return new HashSet<>((Set<?>)pc);
76 }
77 if(pc instanceof PersistentSortedSet) {
78 return new TreeSet<>((Set<?>)pc);
79 }
80 if(pc instanceof PersistentList || pc instanceof PersistentBag) {
81 return new ArrayList<>((List<?>)pc);
82 }
83 if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
84 return new HashMap<>((Map<?,?>)pc);
85 }
86 if(pc instanceof PersistentSortedMap) {
87 return new TreeMap<>((Map<?,?>)pc);
88 }
89 throw new ProxyUtilsException("Cannot get Collection field for type " + pc.getClass().getName());
90 }
91 return null;
92 }
93
94 public static CollectionField getCollectionField(PersistentCollection pc) {
95 if(pc != null) {
96 if(pc instanceof PersistentSet) {
97 return new CollectionField(new HashSet<>((Set<?>)pc), CollectionType.SET);
98 }
99 if(pc instanceof PersistentSortedSet) {
100 return new CollectionField(new TreeSet<>((Set<?>)pc), CollectionType.SET);
101 }
102 if(pc instanceof PersistentList) {
103 return new CollectionField(new ArrayList<>((List<?>)pc), CollectionType.LIST);
104 }
105 if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
106 return new CollectionField(new HashMap<>((Map<?,?>)pc), CollectionType.MAP);
107 }
108 if(pc instanceof PersistentSortedMap) {
109 return new CollectionField(new TreeMap<>((Map<?,?>)pc), CollectionType.MAP);
110 }
111 throw new ProxyUtilsException("Cannot get Collection field for type " + pc.getClass().getName());
112 }
113 return null;
114 }
115
116 public static class CollectionField {
117 private final Object col;
118 private final CollectionType type;
119 public CollectionField(Object col, CollectionType type) {
120 this.col = col;
121 this.type = type;
122 }
123
124 public Object getCollection() {
125 return this.col;
126 }
127
128 public CollectionType getType() {
129 return this.type;
130 }
131 }
132
133 /**
134 * de-proxies the passed object <code>o</code> if it is an initialized proxy object,
135 * otherwise <code>o</code> is returned.
136 */
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();
142 }
143 }
144
145 if(o != null && o instanceof PersistentCollection) {
146 PersistentCollection pc = ((PersistentCollection)o);
147 if(pc.wasInitialized()) {
148 return ProxyUtils.getObject(pc);
149 }
150 }
151 return o;
152 }
153
154 /**
155 * de-proxies the passed object <code>o</code> if it is an initialized proxy object,
156 * otherwise <code>null</code> is returned.
157 */
158 public static Object deproxyOrNull(Object o) {
159 if(o != null && o instanceof HibernateProxy) {
160 LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
161 if(!hli.isUninitialized()) {
162 return hli.getImplementation();
163 } else {
164 return null;
165 }
166 }
167
168 if(o != null && o instanceof PersistentCollection) {
169 PersistentCollection pc = ((PersistentCollection)o);
170 if(pc.wasInitialized()) {
171 return ProxyUtils.getObject(pc);
172 } else {
173 return null;
174 }
175 }
176 return o;
177 }
178
179 public static boolean isUninitializedProxy(Object o) {
180 if(o != null && o instanceof HibernateProxy) {
181 LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
182 if(hli.isUninitialized()) {
183 return true;
184 }
185 }
186
187 if(o != null && o instanceof PersistentCollection) {
188 PersistentCollection pc = ((PersistentCollection)o);
189 if(!pc.wasInitialized()) {
190 return true;
191 }
192 }
193
194 return false;
195 }
196
197 /*
198 * ########################################### makes no sense without remoting ###########################################
199 @SuppressWarnings("unchecked")
200 public static Object remoteLoadPersistentCollectionIfProxy(Object o, UUID ownerUuid, String fieldName) throws ClassNotFoundException {
201 if(o != null && o instanceof HibernateProxy) {
202 LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
203 if(hli.isUninitialized()) {
204 return CdmApplicationState.getCachedCommonService().find((Class<CdmBase>)Class.forName(hli.getEntityName()),
205 ((Integer)hli.getIdentifier()).intValue());
206 }
207 }
208
209 if(o != null && o instanceof PersistentCollection) {
210 PersistentCollection pc = ((PersistentCollection)o);
211 if(!pc.wasInitialized()) {
212 return CdmApplicationState.getCachedCommonService().initializeCollection(ownerUuid, fieldName);
213 }
214 }
215
216 return o;
217 }
218 ########################################### ########################################### */
219
220 public static void setRoleValueInOwner(Object owner, String role, Object value) {
221 if(role == null || role.isEmpty()) {
222 throw new ProxyUtilsException("Role cannot be null or an empty string");
223 }
224
225 String fieldName = role.substring(role.lastIndexOf(".") + 1);
226
227 Field field = ReflectionUtils.findField(owner.getClass(), fieldName);
228
229 if(field == null) {
230 throw new ProxyUtilsException("Field '" + fieldName
231 + "' not found when searching in class '" + owner.getClass() + "' and its supercalsses");
232 }
233
234 field.setAccessible(true);
235
236 try {
237 field.set(owner, value);
238 } catch (IllegalArgumentException e) {
239 throw new ProxyUtilsException(e);
240 } catch (IllegalAccessException e) {
241 throw new ProxyUtilsException(e);
242 }
243 }
244
245 }