Project

General

Profile

Download (7.13 KB) Statistics
| Branch: | Tag: | Revision:
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
import java.util.UUID;
22

    
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;
33

    
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;
38

    
39
/**
40
 * @author cmathew
41
 * @date 17 Feb 2015
42
 *
43
 */
44
public class ProxyUtils {
45

    
46

    
47

    
48
    public static enum CollectionType {
49
        SET,
50
        LIST,
51
        MAP;
52

    
53
        @Override
54
        public String toString() {
55
            return this.name().toLowerCase();
56
        }
57
    }
58

    
59
    public static Object getCollectionType(Object obj) {
60
        if(obj != null) {
61
            if(obj instanceof List) {
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 CdmRemotingException("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 CdmRemotingException("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 CdmRemotingException("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
    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

    
146
        if(o != null && o instanceof PersistentCollection) {
147
            PersistentCollection pc = ((PersistentCollection)o);
148
            if(pc.wasInitialized()) {
149
                return  ProxyUtils.getObject(pc);
150

    
151
            }
152
        }
153
        return o;
154
    }
155

    
156
    public static boolean isProxy(Object o) {
157
        if(o != null && o instanceof HibernateProxy) {
158
            LazyInitializer hli = ((HibernateProxy)o).getHibernateLazyInitializer();
159
            if(hli.isUninitialized()) {
160
                return true;
161
            }
162
        }
163

    
164
        if(o != null && o instanceof PersistentCollection) {
165
            PersistentCollection pc = ((PersistentCollection)o);
166
            if(!pc.wasInitialized()) {
167
                return true;
168
            }
169
        }
170

    
171
        return false;
172
    }
173

    
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());
180
            }
181
        }
182

    
183
        if(o != null && o instanceof PersistentCollection) {
184
            PersistentCollection pc = ((PersistentCollection)o);
185
            if(!pc.wasInitialized()) {
186
                return CdmApplicationState.getCachedCommonService().initializeCollection(ownerUuid, fieldName);
187
            }
188
        }
189

    
190
        return o;
191
    }
192

    
193

    
194

    
195

    
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");
199
        }
200

    
201
        String fieldName = role.substring(role.lastIndexOf(".") + 1);
202

    
203
        Field field = ReflectionUtils.findField(owner.getClass(), fieldName);
204

    
205
        if(field == null) {
206
            throw new CdmRemotingException("Field '" + fieldName
207
                    + "' not found when searching in class '" + owner.getClass() + "' and its supercalsses");
208
        }
209

    
210
        field.setAccessible(true);
211

    
212
        try {
213
            field.set(owner, value);
214
        } catch (IllegalArgumentException e) {
215
            throw new CdmRemotingException(e);
216
        } catch (IllegalAccessException e) {
217
            throw new CdmRemotingException(e);
218
        }
219
    }
220

    
221
}
(9-9/9)