1
14
15 package com.liferay.portal.service.impl;
16
17 import com.liferay.portal.kernel.annotation.Propagation;
18 import com.liferay.portal.kernel.annotation.Transactional;
19 import com.liferay.portal.kernel.exception.PortalException;
20 import com.liferay.portal.kernel.exception.SystemException;
21 import com.liferay.portal.kernel.util.Validator;
22 import com.liferay.portal.model.ClassName;
23 import com.liferay.portal.model.ModelHintsUtil;
24 import com.liferay.portal.model.impl.ClassNameImpl;
25 import com.liferay.portal.service.base.ClassNameLocalServiceBaseImpl;
26
27 import java.util.List;
28 import java.util.Map;
29 import java.util.concurrent.ConcurrentHashMap;
30
31
36 public class ClassNameLocalServiceImpl extends ClassNameLocalServiceBaseImpl {
37
38 public ClassName addClassName(String value) throws SystemException {
39 long classNameId = counterLocalService.increment();
40
41 ClassName className = classNamePersistence.create(classNameId);
42
43 className.setValue(value);
44
45 classNamePersistence.update(className, false);
46
47 return className;
48 }
49
50 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
51 public void checkClassNames() throws SystemException {
52 if (_classNames.isEmpty()) {
53 List<ClassName> classNames = classNamePersistence.findAll();
54
55 for (ClassName className : classNames) {
56 _classNames.put(className.getValue(), className);
57 }
58 }
59
60 List<String> models = ModelHintsUtil.getModels();
61
62 for (String model : models) {
63 getClassName(model);
64 }
65 }
66
67 public ClassName getClassName(long classNameId)
68 throws PortalException, SystemException {
69
70 return classNamePersistence.findByPrimaryKey(classNameId);
71 }
72
73 public ClassName getClassName(String value) throws SystemException {
74 if (Validator.isNull(value)) {
75 return _nullClassName;
76 }
77
78
81 ClassName className = _classNames.get(value);
82
83 if (className == null) {
84 className = classNamePersistence.fetchByValue(value);
85
86 if (className == null) {
87 className = classNameLocalService.addClassName(value);
88 }
89
90 _classNames.put(value, className);
91 }
92
93 return className;
94 }
95
96 public long getClassNameId(Class<?> classObj) {
97 return getClassNameId(classObj.getName());
98 }
99
100 public long getClassNameId(String value) {
101 try {
102 ClassName className = getClassName(value);
103
104 return className.getClassNameId();
105 }
106 catch (Exception e) {
107 throw new RuntimeException(
108 "Unable to get class name from value " + value, e);
109 }
110 }
111
112 private static ClassName _nullClassName = new ClassNameImpl();
113 private static Map<String, ClassName> _classNames =
114 new ConcurrentHashMap<String, ClassName>();
115
116 }