Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / cache / ProxyUtils.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.taxeditor.remoting.cache;
11
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;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.TreeMap;
20 import java.util.TreeSet;
21
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;
32
33 import eu.etaxonomy.cdm.model.common.PersistentMultiLanguageText;
34 import eu.etaxonomy.taxeditor.remoting.CdmRemotingException;
35
36 /**
37 * @author cmathew
38 * @date 17 Feb 2015
39 *
40 */
41 public class ProxyUtils {
42
43
44
45 public static enum CollectionType {
46 SET,
47 LIST,
48 MAP;
49
50 @Override
51 public String toString() {
52 return this.name().toLowerCase();
53 }
54 }
55
56 public static Object getCollectionType(Object obj) {
57 if(obj != null) {
58 if(obj instanceof List) {
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 CdmRemotingException("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 CdmRemotingException("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 CdmRemotingException("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 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();
139
140 }
141 }
142
143 if(o != null && o instanceof PersistentCollection) {
144 PersistentCollection pc = ((PersistentCollection)o);
145 if(pc.wasInitialized()) {
146 return ProxyUtils.getObject(pc);
147
148 }
149 }
150 return o;
151 }
152
153 public static boolean isProxy(Object o) {
154 if(o != null && o instanceof HibernateProxy) {
155 LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
156 if(hli.isUninitialized()) {
157 return true;
158 }
159 }
160
161 if(o != null && o instanceof PersistentCollection) {
162 PersistentCollection pc = ((PersistentCollection)o);
163 if(!pc.wasInitialized()) {
164 return true;
165 }
166 }
167
168 return false;
169 }
170
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");
174 }
175
176 String fieldName = role.substring(role.lastIndexOf(".") + 1);
177
178 Field field = ReflectionUtils.findField(owner.getClass(), fieldName);
179
180 if(field == null) {
181 throw new CdmRemotingException("Field '" + fieldName
182 + "' not found when searching in class '" + owner.getClass() + "' and its supercalsses");
183 }
184
185 field.setAccessible(true);
186
187 try {
188 field.set(owner, value);
189 } catch (IllegalArgumentException e) {
190 throw new CdmRemotingException(e);
191 } catch (IllegalAccessException e) {
192 throw new CdmRemotingException(e);
193 }
194 }
195
196 }