Project

General

Profile

Download (6.29 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.service;
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
            if(cdmEntity == null) {
71
                throw new NullPointerException("CDM Entity of type " + clazz.getName() + " with id " + id  + " is null.");
72
            }
73
            return cdmEntitySessionManager.load(cdmEntity);
74
        } else {
75
            return CdmBase.deproxy(commonService.find(clazz, id),clazz);
76
        }
77
    }
78

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

    
88
    @Override
89
    public void updatePersistentCollection(CollectionField colf) {
90
    	if(cacheEnabled) {
91
    		cdmEntitySessionManager.load(colf.getCollection());
92
    	}
93
    }
94

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

    
102
    }
103

    
104

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

    
113

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

    
122

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

    
131

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

    
140

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

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

    
173
    public class CollectionField {
174
        private final Object col;
175
        private final CollectionType type;
176
        public CollectionField(Object col, CollectionType type) {
177
            this.col = col;
178
            this.type = type;
179
        }
180

    
181
        public Object getCollection() {
182
            return this.col;
183
        }
184

    
185
        public CollectionType getType() {
186
            return this.type;
187
        }
188
    }
189

    
190

    
191

    
192
}
(1-1/6)