adapt master to develop
[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.CdmApplicationState;
17 import eu.etaxonomy.cdm.model.common.CdmBase;
18 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
19
20 /**
21 * @author cmathew
22 * @date 14 Oct 2014
23 */
24 @Component
25 public class CachedCommonServiceImpl implements ICachedCommonService {
26
27 private static boolean cacheEnabled = true;
28
29 public static boolean isCacheEnabled() {
30 return cacheEnabled;
31 }
32
33 public static void setCacheEnabled(boolean cacheEnabled) {
34 CachedCommonServiceImpl.cacheEnabled = cacheEnabled;
35 }
36
37 @Override
38 public CdmBase find(Class<? extends CdmBase> clazz, int id) {
39 CdmBase cdmEntity = CdmApplicationState.getCurrentAppConfig().getCommonService().find(clazz, id, getPropertyPaths(clazz));
40 if(cdmEntity == null) {
41 throw new NullPointerException("CDM Entity of type " + clazz.getName() + " with id " + id + " is null.");
42 }
43 return cdmEntity;
44 }
45
46 private List<String> getPropertyPaths(Object obj) {
47 List<String> propertyPaths = null;
48 ICdmEntitySession cdmEntitySession =
49 CdmApplicationState.getCurrentAppConfig().getCdmEntitySessionManager().getActiveSession();
50 if(cdmEntitySession != null) {
51 propertyPaths = cdmEntitySession.getPropertyPaths(obj);
52 }
53 return propertyPaths;
54 }
55
56 @Override
57 public Object initializeCollection(UUID ownerUuid, String fieldName) {
58 return CdmApplicationState.getCurrentAppConfig().getCommonService().initializeCollection(ownerUuid, fieldName, getPropertyPaths(fieldName));
59 }
60
61 @Override
62 public boolean isEmpty(UUID ownerUuid, String fieldName) {
63 return CdmApplicationState.getCurrentAppConfig().getCommonService().isEmpty(ownerUuid, fieldName);
64 }
65
66 @Override
67 public int size(UUID ownerUuid, String fieldName) {
68 return CdmApplicationState.getCurrentAppConfig().getCommonService().size(ownerUuid, fieldName);
69 }
70
71 @Override
72 public boolean contains(UUID ownerUuid, String fieldName, Object element) {
73 return CdmApplicationState.getCurrentAppConfig().getCommonService().contains(ownerUuid, fieldName, element);
74 }
75
76 @Override
77 public boolean containsKey(UUID ownerUuid, String fieldName, Object key) {
78 return CdmApplicationState.getCurrentAppConfig().getCommonService().containsKey(ownerUuid, fieldName, key);
79 }
80
81 @Override
82 public boolean containsValue(UUID ownerUuid, String fieldName, Object element) {
83 return CdmApplicationState.getCurrentAppConfig().getCommonService().containsValue(ownerUuid, fieldName, element);
84 }
85 }