Project

General

Profile

Download (6.27 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.cdm.api.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.cache.LazyLoadingCachingUtils.CollectionType;
31
import eu.etaxonomy.cdm.api.service.ICommonService;
32
import eu.etaxonomy.cdm.model.common.CdmBase;
33
import eu.etaxonomy.cdm.model.common.PersistentMultiLanguageText;
34

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

    
43

    
44
    @Autowired
45
    private ICommonService commonService;
46

    
47
    private static boolean cacheEnabled = true;
48

    
49
    @Autowired
50
    private CdmEntityCacheManager cdmEntityCacheManager;
51

    
52
    @Autowired
53
    private LazyLoadingCachingUtils lazyLoadingCachingUtils;
54

    
55

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

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

    
64
    /* (non-Javadoc)
65
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#find(java.lang.Class, int)
66
     */
67
    @Override
68
    public CdmBase find(Class<? extends CdmBase> clazz, int id){
69
        if(cacheEnabled) {
70
            CdmBase cdmEntity = cdmEntityCacheManager.getCdmTransientEntityCacher().getFromCache(clazz, id);
71
            if(cdmEntity != null) {
72
                return cdmEntity;
73
            } else {
74
                cdmEntity = commonService.find(clazz, id);
75
                cdmEntity = cdmEntityCacheManager.getCdmTransientEntityCacher().put(clazz, id, cdmEntity);
76
                return cdmEntity;
77
            }
78
        } else {
79
            return commonService.find(clazz, id);
80
        }
81
    }
82

    
83

    
84
    /* (non-Javadoc)
85
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#initializeCollection(org.hibernate.collection.spi.PersistentCollection)
86
     */
87
    @Override
88
    public PersistentCollection initializeCollection(PersistentCollection col) {
89
            PersistentCollection pc = commonService.initializeCollection(col);
90

    
91
            return pc;
92
    }
93

    
94
    @Override
95
    public void updatePersistentCollection(CollectionField colf) {
96
        switch(colf.getType()) {
97
        case MAP:
98
            lazyLoadingCachingUtils.updatePersistentCollection((Map)colf.getCollection());
99
        case SET:
100
        case LIST:
101
        default:
102
        }
103
    }
104

    
105
    /* (non-Javadoc)
106
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#isEmpty(org.hibernate.collection.spi.PersistentCollection)
107
     */
108
    @Override
109
    public boolean isEmpty(PersistentCollection col) {
110
            return commonService.isEmpty(col);
111

    
112
    }
113

    
114

    
115
    /* (non-Javadoc)
116
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#size(org.hibernate.collection.spi.PersistentCollection)
117
     */
118
    @Override
119
    public int size(PersistentCollection col) {
120
        return commonService.size(col);
121
    }
122

    
123

    
124
    /* (non-Javadoc)
125
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#get(org.hibernate.collection.spi.PersistentCollection, int)
126
     */
127
    @Override
128
    public Object get(PersistentCollection col, int index) {
129
        return commonService.get(col, index);
130
    }
131

    
132

    
133
    /* (non-Javadoc)
134
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#contains(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
135
     */
136
    @Override
137
    public boolean contains(PersistentCollection col, Object element) {
138
        return commonService.contains(col, element);
139
    }
140

    
141

    
142
    /* (non-Javadoc)
143
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsKey(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
144
     */
145
    @Override
146
    public boolean containsKey(PersistentCollection col, Object key) {
147
        return commonService.containsKey(col, key);
148
    }
149

    
150

    
151
    /* (non-Javadoc)
152
     * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsValue(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
153
     */
154
    @Override
155
    public boolean containsValue(PersistentCollection col, Object element) {
156
        return commonService.containsValue(col, element);
157
    }
158

    
159
    @Override
160
    public CollectionField getCollectionField(PersistentCollection pc) {
161
        if(pc != null) {
162
            if(pc instanceof PersistentSet) {
163
                return new CollectionField(new HashSet((Set)pc), CollectionType.SET);
164
            }
165
            if(pc instanceof PersistentSortedSet) {
166
                return new CollectionField(new TreeSet((Set)pc), CollectionType.SET);
167
            }
168
            if(pc instanceof PersistentList) {
169
                return new CollectionField(new ArrayList((List)pc), CollectionType.LIST);
170
            }
171
            if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
172
                return new CollectionField(new HashMap((Map)pc), CollectionType.MAP);
173
            }
174
            if(pc instanceof PersistentSortedMap) {
175
                return new CollectionField(new TreeMap((Map)pc), CollectionType.MAP);
176
            }
177
        }
178
        return null;
179
    }
180

    
181
    public class CollectionField {
182
        private final Object col;
183
        private final CollectionType type;
184
        public CollectionField(Object col, CollectionType type) {
185
            this.col = col;
186
            this.type = type;
187
        }
188

    
189
        public Object getCollection() {
190
            return this.col;
191
        }
192

    
193
        public CollectionType getType() {
194
            return this.type;
195
        }
196
    }
197

    
198
}
(1-1/6)