3b302659c707ad45ef973ae6442197a95bd1a35a
[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.List;
13 import java.util.UUID;
14
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Component;
17
18 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
19 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
20 import eu.etaxonomy.cdm.api.service.ICommonService;
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22 import eu.etaxonomy.taxeditor.remoting.cache.ProxyUtils.CollectionField;
23 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
24 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
25
26 /**
27 * @author cmathew
28 * @date 14 Oct 2014
29 *
30 */
31 @Component
32 public class CachedCommonServiceImpl implements ICachedCommonService {
33
34
35 @Autowired
36 private ICommonService commonService;
37
38 private static boolean cacheEnabled = true;
39
40 @Autowired
41 private ICdmEntitySessionManager cdmEntitySessionManager;
42
43
44 public static boolean isCacheEnabled() {
45 return cacheEnabled;
46 }
47
48 public static void setCacheEnabled(boolean cacheEnabled) {
49 CachedCommonServiceImpl.cacheEnabled = cacheEnabled;
50 }
51
52 /* (non-Javadoc)
53 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#find(java.lang.Class, int)
54 */
55 @Override
56 public CdmBase find(Class<? extends CdmBase> clazz, int id) {
57 if(cacheEnabled) {
58 CdmBase cdmEntity = CdmBase.deproxy(commonService.find(clazz, id, getPropertyPaths(clazz)),clazz);
59 if(cdmEntity == null) {
60 throw new NullPointerException("CDM Entity of type " + clazz.getName() + " with id " + id + " is null.");
61 }
62 return cdmEntitySessionManager.load(cdmEntity, false);
63 } else {
64 return CdmBase.deproxy(commonService.find(clazz, id),clazz);
65 }
66 }
67
68
69 private List<String> getPropertyPaths(Object obj) {
70 List<String> propertyPaths = null;
71 ICdmEntitySession cdmEntitySession =
72 ((CdmApplicationRemoteController)CdmApplicationState.getCurrentAppConfig()).getCdmEntitySessionManager().getActiveSession();
73 if(cdmEntitySession != null) {
74 propertyPaths = cdmEntitySession.getPropertyPaths(obj);
75 }
76 return propertyPaths;
77 }
78
79 @Override
80 public void updatePersistentCollection(CollectionField colf) {
81 if(cacheEnabled) {
82 cdmEntitySessionManager.load(colf.getCollection(), false);
83 }
84 }
85
86 /* (non-Javadoc)
87 * @see eu.etaxonomy.taxeditor.service.ICachedCommonService#initializeCollection(java.util.UUID, java.lang.String)
88 */
89 @Override
90 public Object initializeCollection(UUID ownerUuid, String fieldName) {
91 return commonService.initializeCollection(ownerUuid, fieldName, getPropertyPaths(fieldName));
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(UUID ownerUuid, String fieldName) {
100 return commonService.isEmpty(ownerUuid, fieldName);
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(UUID ownerUuid, String fieldName) {
110 return commonService.size(ownerUuid, fieldName);
111 }
112
113
114
115 /* (non-Javadoc)
116 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#contains(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
117 */
118 @Override
119 public boolean contains(UUID ownerUuid, String fieldName, Object element) {
120 return commonService.contains(ownerUuid, fieldName, element);
121 }
122
123
124 /* (non-Javadoc)
125 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsKey(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
126 */
127 @Override
128 public boolean containsKey(UUID ownerUuid, String fieldName, Object key) {
129 return commonService.containsKey(ownerUuid, fieldName, key);
130 }
131
132
133 /* (non-Javadoc)
134 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsValue(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
135 */
136 @Override
137 public boolean containsValue(UUID ownerUuid, String fieldName, Object element) {
138 return commonService.containsValue(ownerUuid, fieldName, element);
139 }
140
141
142
143
144
145
146 }