Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / service / CachedCommonServiceImpl.java
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.UUID;
13
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Component;
16
17 import eu.etaxonomy.cdm.api.service.ICommonService;
18 import eu.etaxonomy.cdm.model.common.CdmBase;
19 import eu.etaxonomy.taxeditor.remoting.cache.ProxyUtils.CollectionField;
20 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
21
22 /**
23 * @author cmathew
24 * @date 14 Oct 2014
25 *
26 */
27 @Component
28 public class CachedCommonServiceImpl implements ICachedCommonService {
29
30
31 @Autowired
32 private ICommonService commonService;
33
34 private static boolean cacheEnabled = true;
35
36 @Autowired
37 private ICdmEntitySessionManager cdmEntitySessionManager;
38
39
40 public static boolean isCacheEnabled() {
41 return cacheEnabled;
42 }
43
44 public static void setCacheEnabled(boolean cacheEnabled) {
45 CachedCommonServiceImpl.cacheEnabled = cacheEnabled;
46 }
47
48 /* (non-Javadoc)
49 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#find(java.lang.Class, int)
50 */
51 @Override
52 public CdmBase find(Class<? extends CdmBase> clazz, int id) {
53 if(cacheEnabled) {
54 CdmBase cdmEntity = CdmBase.deproxy(commonService.find(clazz, id),clazz);
55 if(cdmEntity == null) {
56 throw new NullPointerException("CDM Entity of type " + clazz.getName() + " with id " + id + " is null.");
57 }
58 return cdmEntitySessionManager.load(cdmEntity, false);
59 } else {
60 return CdmBase.deproxy(commonService.find(clazz, id),clazz);
61 }
62 }
63
64
65 @Override
66 public void updatePersistentCollection(CollectionField colf) {
67 if(cacheEnabled) {
68 cdmEntitySessionManager.load(colf.getCollection(), false);
69 }
70 }
71
72 /* (non-Javadoc)
73 * @see eu.etaxonomy.taxeditor.service.ICachedCommonService#initializeCollection(java.util.UUID, java.lang.String)
74 */
75 @Override
76 public Object initializeCollection(UUID ownerUuid, String fieldName) {
77 return commonService.initializeCollection(ownerUuid, fieldName);
78 }
79
80
81 /* (non-Javadoc)
82 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#isEmpty(org.hibernate.collection.spi.PersistentCollection)
83 */
84 @Override
85 public boolean isEmpty(UUID ownerUuid, String fieldName) {
86 return commonService.isEmpty(ownerUuid, fieldName);
87
88 }
89
90
91 /* (non-Javadoc)
92 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#size(org.hibernate.collection.spi.PersistentCollection)
93 */
94 @Override
95 public int size(UUID ownerUuid, String fieldName) {
96 return commonService.size(ownerUuid, fieldName);
97 }
98
99
100
101 /* (non-Javadoc)
102 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#contains(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
103 */
104 @Override
105 public boolean contains(UUID ownerUuid, String fieldName, Object element) {
106 return commonService.contains(ownerUuid, fieldName, element);
107 }
108
109
110 /* (non-Javadoc)
111 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsKey(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
112 */
113 @Override
114 public boolean containsKey(UUID ownerUuid, String fieldName, Object key) {
115 return commonService.containsKey(ownerUuid, fieldName, key);
116 }
117
118
119 /* (non-Javadoc)
120 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsValue(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
121 */
122 @Override
123 public boolean containsValue(UUID ownerUuid, String fieldName, Object element) {
124 return commonService.containsValue(ownerUuid, fieldName, element);
125 }
126
127
128
129
130
131
132 }