Project

General

Profile

Download (8.42 KB) Statistics
| Branch: | Tag: | Revision:
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
 */
39
public class ProxyUtils {
40

    
41

    
42

    
43
    public static enum CollectionType {
44
        SET,
45
        LIST,
46
        MAP,
47
        BAG;
48

    
49
        @Override
50
        public String toString() {
51
            return this.name().toLowerCase();
52
        }
53
    }
54

    
55
    public static Object getCollectionType(Object obj, Class<?> clazz) {
56
        if(obj != null) {
57
            if(obj instanceof List) {
58
            	//the field in PersistentBag is called "bag" although it is an ArrayList -> #
59
            	if(clazz.equals(PersistentBag.class)){
60
            		return CollectionType.BAG;
61
            	}
62
                return CollectionType.LIST;
63
            }
64
            if(obj instanceof Set) {
65
                return CollectionType.SET;
66
            }
67
            if(obj instanceof Map) {
68
                return CollectionType.MAP;
69
            }
70
            throw new ProxyUtilsException("Cannot get Collection Type for " + obj.getClass().getName());
71
        }
72
        return null;
73
    }
74

    
75
    public static Object getObject(PersistentCollection pc) {
76
        if(pc != null) {
77
            if(pc instanceof PersistentSet) {
78
                return new HashSet<>((Set<?>)pc);
79
            }
80
            if(pc instanceof PersistentSortedSet) {
81
                return new TreeSet<>((Set<?>)pc);
82
            }
83
            if(pc instanceof PersistentList || pc instanceof PersistentBag) {
84
                return new ArrayList<>((List<?>)pc);
85
            }
86
            if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
87
                return new HashMap<>((Map<?,?>)pc);
88
            }
89
            if(pc instanceof PersistentSortedMap) {
90
                return new TreeMap<>((Map<?,?>)pc);
91
            }
92
            throw new ProxyUtilsException("Cannot get Collection field for type " + pc.getClass().getName());
93
        }
94
        return null;
95
    }
96

    
97
    public static CollectionField getCollectionField(PersistentCollection pc) {
98
        if(pc != null) {
99
            if(pc instanceof PersistentSet) {
100
                return new CollectionField(new HashSet<>((Set<?>)pc), CollectionType.SET);
101
            }
102
            if(pc instanceof PersistentSortedSet) {
103
                return new CollectionField(new TreeSet<>((Set<?>)pc), CollectionType.SET);
104
            }
105
            if(pc instanceof PersistentList) {
106
                return new CollectionField(new ArrayList<>((List<?>)pc), CollectionType.LIST);
107
            }
108
            if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
109
                return new CollectionField(new HashMap<>((Map<?,?>)pc), CollectionType.MAP);
110
            }
111
            if(pc instanceof PersistentSortedMap) {
112
                return new CollectionField(new TreeMap<>((Map<?,?>)pc), CollectionType.MAP);
113
            }
114
            throw new ProxyUtilsException("Cannot get Collection field for type " + pc.getClass().getName());
115
        }
116
        return null;
117
    }
118

    
119
    public static class CollectionField {
120
        private final Object col;
121
        private final CollectionType type;
122
        public CollectionField(Object col, CollectionType type) {
123
            this.col = col;
124
            this.type = type;
125
        }
126

    
127
        public Object getCollection() {
128
            return this.col;
129
        }
130

    
131
        public CollectionType getType() {
132
            return this.type;
133
        }
134
    }
135

    
136
    /**
137
     * de-proxies the passed object <code>o</code> if it is an initialized proxy object,
138
     * otherwise <code>o</code> is returned.
139
     */
140
    public static Object deproxy(Object o) {
141
        if(o != null && o instanceof HibernateProxy) {
142
            LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
143
            if(!hli.isUninitialized()) {
144
                return hli.getImplementation();
145

    
146
            }
147
        }
148

    
149
        if(o != null && o instanceof PersistentCollection) {
150
            PersistentCollection pc = ((PersistentCollection)o);
151
            if(pc.wasInitialized()) {
152
                return  ProxyUtils.getObject(pc);
153

    
154
            }
155
        }
156
        return o;
157
    }
158

    
159
    /**
160
     * de-proxies the passed object <code>o</code> if it is an initialized proxy object,
161
     * otherwise <code>null</code> is returned.
162
     */
163
    public static Object deproxyOrNull(Object o) {
164
        if(o != null && o instanceof HibernateProxy) {
165
            LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
166
            if(!hli.isUninitialized()) {
167
                return hli.getImplementation();
168
            } else {
169
                return null;
170
            }
171
        }
172

    
173
        if(o != null && o instanceof PersistentCollection) {
174
            PersistentCollection pc = ((PersistentCollection)o);
175
            if(pc.wasInitialized()) {
176
                return  ProxyUtils.getObject(pc);
177
            } else {
178
                return null;
179
            }
180
        }
181
        return o;
182
    }
183

    
184
    public static boolean isUninitializedProxy(Object o) {
185
        if(o != null && o instanceof HibernateProxy) {
186
            LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
187
            if(hli.isUninitialized()) {
188
                return true;
189
            }
190
        }
191

    
192
        if(o != null && o instanceof PersistentCollection) {
193
            PersistentCollection pc = ((PersistentCollection)o);
194
            if(!pc.wasInitialized()) {
195
                return true;
196
            }
197
        }
198

    
199
        return false;
200
    }
201

    
202
    /*
203
     * ########################################### makes no sense without remoting ###########################################
204
    @SuppressWarnings("unchecked")
205
	public static Object remoteLoadPersistentCollectionIfProxy(Object o, UUID ownerUuid, String fieldName) throws ClassNotFoundException {
206
        if(o != null && o instanceof HibernateProxy) {
207
            LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
208
            if(hli.isUninitialized()) {
209
                return CdmApplicationState.getCachedCommonService().find((Class<CdmBase>)Class.forName(hli.getEntityName()),
210
                        ((Integer)hli.getIdentifier()).intValue());
211
            }
212
        }
213

    
214
        if(o != null && o instanceof PersistentCollection) {
215
            PersistentCollection pc = ((PersistentCollection)o);
216
            if(!pc.wasInitialized()) {
217
                return CdmApplicationState.getCachedCommonService().initializeCollection(ownerUuid, fieldName);
218
            }
219
        }
220

    
221
        return o;
222
    }
223
    ########################################### ########################################### */
224

    
225

    
226

    
227

    
228
    public static void setRoleValueInOwner(Object owner, String role, Object value) {
229
        if(role == null || role.isEmpty()) {
230
            throw new ProxyUtilsException("Role cannot be null or an empty string");
231
        }
232

    
233
        String fieldName = role.substring(role.lastIndexOf(".") + 1);
234

    
235
        Field field = ReflectionUtils.findField(owner.getClass(), fieldName);
236

    
237
        if(field == null) {
238
            throw new ProxyUtilsException("Field '" + fieldName
239
                    + "' not found when searching in class '" + owner.getClass() + "' and its supercalsses");
240
        }
241

    
242
        field.setAccessible(true);
243

    
244
        try {
245
            field.set(owner, value);
246
        } catch (IllegalArgumentException e) {
247
            throw new ProxyUtilsException(e);
248
        } catch (IllegalAccessException e) {
249
            throw new ProxyUtilsException(e);
250
        }
251
    }
252

    
253
}
(10-10/11)