Project

General

Profile

Download (6.13 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2014 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.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.PersistentList;
22
import org.hibernate.collection.internal.PersistentMap;
23
import org.hibernate.collection.internal.PersistentSet;
24
import org.hibernate.collection.internal.PersistentSortedMap;
25
import org.hibernate.collection.internal.PersistentSortedSet;
26
import org.hibernate.collection.spi.PersistentCollection;
27
import org.springframework.beans.factory.annotation.Autowired;
28
import org.springframework.stereotype.Component;
29

    
30
import eu.etaxonomy.cdm.api.service.ICommonService;
31
import eu.etaxonomy.cdm.model.common.CdmBase;
32
import eu.etaxonomy.cdm.model.common.PersistentMultiLanguageText;
33
import eu.etaxonomy.taxeditor.remoting.CdmRemotingException;
34
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher.CollectionType;
35
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
36

    
37
/**
38
 * @author cmathew
39
 * @date 14 Oct 2014
40
 *
41
 */
42
@Component
43
public class CachedCommonServiceImpl implements ICachedCommonService {
44

    
45

    
46
    @Autowired
47
    private ICommonService commonService;
48

    
49
    private static boolean cacheEnabled = true;
50

    
51
    @Autowired
52
    private ICdmEntitySessionManager cdmEntitySessionManager;
53

    
54

    
55
    public static boolean isCacheEnabled() {
56
        return cacheEnabled;
57
    }
58

    
59
    public static void setCacheEnabled(boolean cacheEnabled) {
60
        CachedCommonServiceImpl.cacheEnabled = cacheEnabled;
61
    }
62

    
63
    /* (non-Javadoc)
64
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#find(java.lang.Class, int)
65
     */
66
    @Override
67
    public CdmBase find(Class<? extends CdmBase> clazz, int id) {
68
        if(cacheEnabled) {
69
            CdmBase cdmEntity = CdmBase.deproxy(commonService.find(clazz, id),clazz);
70
            return cdmEntitySessionManager.load(cdmEntity);
71
        } else {
72
            return CdmBase.deproxy(commonService.find(clazz, id),clazz);
73
        }
74
    }
75

    
76
    /* (non-Javadoc)
77
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#initializeCollection(org.hibernate.collection.spi.PersistentCollection)
78
     */
79
    @Override
80
    public PersistentCollection initializeCollection(PersistentCollection col) {
81
            PersistentCollection pc = commonService.initializeCollection(col);
82
            return pc;
83
    }
84

    
85
    @Override
86
    public void updatePersistentCollection(CollectionField colf) {
87
    	if(cacheEnabled) {
88
    		cdmEntitySessionManager.load(colf.getCollection());
89
    	}
90
    }
91

    
92
    /* (non-Javadoc)
93
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#isEmpty(org.hibernate.collection.spi.PersistentCollection)
94
     */
95
    @Override
96
    public boolean isEmpty(PersistentCollection col) {
97
            return commonService.isEmpty(col);
98

    
99
    }
100

    
101

    
102
    /* (non-Javadoc)
103
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#size(org.hibernate.collection.spi.PersistentCollection)
104
     */
105
    @Override
106
    public int size(PersistentCollection col) {
107
        return commonService.size(col);
108
    }
109

    
110

    
111
    /* (non-Javadoc)
112
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#get(org.hibernate.collection.spi.PersistentCollection, int)
113
     */
114
    @Override
115
    public Object get(PersistentCollection col, int index) {
116
        return commonService.get(col, index);
117
    }
118

    
119

    
120
    /* (non-Javadoc)
121
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#contains(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
122
     */
123
    @Override
124
    public boolean contains(PersistentCollection col, Object element) {
125
        return commonService.contains(col, element);
126
    }
127

    
128

    
129
    /* (non-Javadoc)
130
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsKey(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
131
     */
132
    @Override
133
    public boolean containsKey(PersistentCollection col, Object key) {
134
        return commonService.containsKey(col, key);
135
    }
136

    
137

    
138
    /* (non-Javadoc)
139
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsValue(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
140
     */
141
    @Override
142
    public boolean containsValue(PersistentCollection col, Object element) {
143
        return commonService.containsValue(col, element);
144
    }
145

    
146
    @SuppressWarnings("rawtypes")
147
	@Override
148
    public CollectionField getCollectionField(PersistentCollection pc) {
149
        if(pc != null) {
150
            if(pc instanceof PersistentSet) {
151
                return new CollectionField(new HashSet((Set)pc), CollectionType.SET);
152
            }
153
            if(pc instanceof PersistentSortedSet) {
154
                return new CollectionField(new TreeSet((Set)pc), CollectionType.SET);
155
            }
156
            if(pc instanceof PersistentList) {
157
                return new CollectionField(new ArrayList((List)pc), CollectionType.LIST);
158
            }
159
            if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
160
                return new CollectionField(new HashMap((Map)pc), CollectionType.MAP);
161
            }
162
            if(pc instanceof PersistentSortedMap) {
163
                return new CollectionField(new TreeMap((Map)pc), CollectionType.MAP);
164
            }
165
            throw new CdmRemotingException("Cannot get Collection field for type " + pc.getClass().getName());
166
        }
167
        return null;
168
    }
169

    
170
    public class CollectionField {
171
        private final Object col;
172
        private final CollectionType type;
173
        public CollectionField(Object col, CollectionType type) {
174
            this.col = col;
175
            this.type = type;
176
        }
177

    
178
        public Object getCollection() {
179
            return this.col;
180
        }
181

    
182
        public CollectionType getType() {
183
            return this.type;
184
        }
185
    }
186

    
187

    
188

    
189
}
(1-1/8)