Merge branch 'develop' of wp5.e-taxonomy.eu:/var/git/taxeditor 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.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 import eu.etaxonomy.taxeditor.remoting.CdmRemotingException;
34
35 /**
36 * @author cmathew
37 * @date 17 Feb 2015
38 *
39 */
40 public class ProxyUtils {
41
42
43
44 public static enum CollectionType {
45 SET,
46 LIST,
47 MAP;
48
49 @Override
50 public String toString() {
51 return this.name().toLowerCase();
52 }
53 }
54
55 public static Object getCollectionType(Object obj) {
56 if(obj != null) {
57 if(obj instanceof List) {
58 return CollectionType.LIST;
59 }
60 if(obj instanceof Set) {
61 return CollectionType.SET;
62 }
63 if(obj instanceof Map) {
64 return CollectionType.MAP;
65 }
66 throw new CdmRemotingException("Cannot get Collection Type for " + obj.getClass().getName());
67 }
68 return null;
69 }
70
71 public static Object getObject(PersistentCollection pc) {
72 if(pc != null) {
73 if(pc instanceof PersistentSet) {
74 return new HashSet((Set)pc);
75 }
76 if(pc instanceof PersistentSortedSet) {
77 return new TreeSet((Set)pc);
78 }
79 if(pc instanceof PersistentList) {
80 return new ArrayList((List)pc);
81 }
82 if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
83 return new HashMap((Map)pc);
84 }
85 if(pc instanceof PersistentSortedMap) {
86 return new TreeMap((Map)pc);
87 }
88 throw new CdmRemotingException("Cannot get Collection field for type " + pc.getClass().getName());
89 }
90 return null;
91 }
92
93 public static CollectionField getCollectionField(PersistentCollection pc) {
94 if(pc != null) {
95 if(pc instanceof PersistentSet) {
96 return new CollectionField(new HashSet((Set)pc), CollectionType.SET);
97 }
98 if(pc instanceof PersistentSortedSet) {
99 return new CollectionField(new TreeSet((Set)pc), CollectionType.SET);
100 }
101 if(pc instanceof PersistentList) {
102 return new CollectionField(new ArrayList((List)pc), CollectionType.LIST);
103 }
104 if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
105 return new CollectionField(new HashMap((Map)pc), CollectionType.MAP);
106 }
107 if(pc instanceof PersistentSortedMap) {
108 return new CollectionField(new TreeMap((Map)pc), CollectionType.MAP);
109 }
110 throw new CdmRemotingException("Cannot get Collection field for type " + pc.getClass().getName());
111 }
112 return null;
113 }
114
115 public static class CollectionField {
116 private final Object col;
117 private final CollectionType type;
118 public CollectionField(Object col, CollectionType type) {
119 this.col = col;
120 this.type = type;
121 }
122
123 public Object getCollection() {
124 return this.col;
125 }
126
127 public CollectionType getType() {
128 return this.type;
129 }
130 }
131
132
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();
138
139 }
140 }
141
142 if(o != null && o instanceof PersistentCollection) {
143 PersistentCollection pc = ((PersistentCollection)o);
144 if(pc.wasInitialized()) {
145 return ProxyUtils.getObject(pc);
146
147 }
148 }
149 return o;
150 }
151
152 public static boolean isProxy(Object o) {
153 if(o != null && o instanceof HibernateProxy) {
154 LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
155 if(hli.isUninitialized()) {
156 return true;
157 }
158 }
159
160 if(o != null && o instanceof PersistentCollection) {
161 PersistentCollection pc = ((PersistentCollection)o);
162 if(!pc.wasInitialized()) {
163 return true;
164 }
165 }
166
167 return false;
168 }
169
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");
173 }
174
175 String fieldName = role.substring(role.lastIndexOf(".") + 1);
176
177 Field field = ReflectionUtils.findField(owner.getClass(), fieldName);
178
179 if(field == null) {
180 throw new CdmRemotingException("Field '" + fieldName
181 + "' not found when searching in class '" + owner.getClass() + "' and its supercalsses");
182 }
183
184 field.setAccessible(true);
185
186 try {
187 field.set(owner, value);
188 } catch (IllegalArgumentException e) {
189 throw new CdmRemotingException(e);
190 } catch (IllegalAccessException e) {
191 throw new CdmRemotingException(e);
192 }
193 }
194
195 }