Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / service / CachedCommonServiceImpl.java
1 /**
2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.service;
10
11 import java.util.List;
12 import java.util.UUID;
13
14 import org.springframework.stereotype.Component;
15
16 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
17 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
18 import eu.etaxonomy.cdm.model.common.CdmBase;
19 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
20
21 /**
22 * @author cmathew
23 * @date 14 Oct 2014
24 *
25 */
26 @Component
27 public class CachedCommonServiceImpl implements ICachedCommonService {
28
29
30 private static boolean cacheEnabled = true;
31
32 public static boolean isCacheEnabled() {
33 return cacheEnabled;
34 }
35
36 public static void setCacheEnabled(boolean cacheEnabled) {
37 CachedCommonServiceImpl.cacheEnabled = cacheEnabled;
38 }
39
40 /* (non-Javadoc)
41 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#find(java.lang.Class, int)
42 */
43 @Override
44 public CdmBase find(Class<? extends CdmBase> clazz, int id) {
45 CdmBase cdmEntity = CdmApplicationState.getCurrentAppConfig().getCommonService().find(clazz, id, getPropertyPaths(clazz));
46 if(cdmEntity == null) {
47 throw new NullPointerException("CDM Entity of type " + clazz.getName() + " with id " + id + " is null.");
48 }
49 return cdmEntity;
50
51 }
52
53
54 private List<String> getPropertyPaths(Object obj) {
55 List<String> propertyPaths = null;
56 ICdmEntitySession cdmEntitySession =
57 ((CdmApplicationRemoteController)CdmApplicationState.getCurrentAppConfig()).getCdmEntitySessionManager().getActiveSession();
58 if(cdmEntitySession != null) {
59 propertyPaths = cdmEntitySession.getPropertyPaths(obj);
60 }
61 return propertyPaths;
62 }
63
64 /* (non-Javadoc)
65 * @see eu.etaxonomy.taxeditor.service.ICachedCommonService#initializeCollection(java.util.UUID, java.lang.String)
66 */
67 @Override
68 public Object initializeCollection(UUID ownerUuid, String fieldName) {
69 return CdmApplicationState.getCurrentAppConfig().getCommonService().initializeCollection(ownerUuid, fieldName, getPropertyPaths(fieldName));
70 }
71
72
73 /* (non-Javadoc)
74 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#isEmpty(org.hibernate.collection.spi.PersistentCollection)
75 */
76 @Override
77 public boolean isEmpty(UUID ownerUuid, String fieldName) {
78 return CdmApplicationState.getCurrentAppConfig().getCommonService().isEmpty(ownerUuid, fieldName);
79
80 }
81
82
83 /* (non-Javadoc)
84 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#size(org.hibernate.collection.spi.PersistentCollection)
85 */
86 @Override
87 public int size(UUID ownerUuid, String fieldName) {
88 return CdmApplicationState.getCurrentAppConfig().getCommonService().size(ownerUuid, fieldName);
89 }
90
91
92
93 /* (non-Javadoc)
94 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#contains(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
95 */
96 @Override
97 public boolean contains(UUID ownerUuid, String fieldName, Object element) {
98 return CdmApplicationState.getCurrentAppConfig().getCommonService().contains(ownerUuid, fieldName, element);
99 }
100
101
102 /* (non-Javadoc)
103 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsKey(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
104 */
105 @Override
106 public boolean containsKey(UUID ownerUuid, String fieldName, Object key) {
107 return CdmApplicationState.getCurrentAppConfig().getCommonService().containsKey(ownerUuid, fieldName, key);
108 }
109
110
111 /* (non-Javadoc)
112 * @see eu.etaxonomy.taxeditor.remoting.service.ICachedCommonService#containsValue(org.hibernate.collection.spi.PersistentCollection, java.lang.Object)
113 */
114 @Override
115 public boolean containsValue(UUID ownerUuid, String fieldName, Object element) {
116 return CdmApplicationState.getCurrentAppConfig().getCommonService().containsValue(ownerUuid, fieldName, element);
117 }
118
119
120
121
122
123
124 }