001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.cache.CacheRegistryItem;
018 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019 import com.liferay.portal.kernel.spring.aop.Skip;
020 import com.liferay.portal.kernel.transaction.Propagation;
021 import com.liferay.portal.kernel.transaction.Transactional;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.model.ClassName;
024 import com.liferay.portal.model.ModelHintsUtil;
025 import com.liferay.portal.model.impl.ClassNameImpl;
026 import com.liferay.portal.service.base.ClassNameLocalServiceBaseImpl;
027
028 import java.util.List;
029 import java.util.Map;
030 import java.util.concurrent.ConcurrentHashMap;
031
032
035 public class ClassNameLocalServiceImpl
036 extends ClassNameLocalServiceBaseImpl implements CacheRegistryItem {
037
038 @Override
039 public ClassName addClassName(String value) {
040 ClassName className = classNamePersistence.fetchByValue(value);
041
042 if (className == null) {
043 long classNameId = counterLocalService.increment();
044
045 className = classNamePersistence.create(classNameId);
046
047 className.setValue(value);
048
049 classNamePersistence.update(className);
050 }
051
052 return className;
053 }
054
055 @Override
056 public void afterPropertiesSet() {
057 super.afterPropertiesSet();
058
059 CacheRegistryUtil.register(this);
060 }
061
062 @Override
063 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
064 public void checkClassNames() {
065 List<ClassName> classNames = classNamePersistence.findAll();
066
067 for (ClassName className : classNames) {
068 _classNames.put(className.getValue(), className);
069 }
070
071 List<String> models = ModelHintsUtil.getModels();
072
073 for (String model : models) {
074 getClassName(model);
075 }
076 }
077
078 @Override
079 @Skip
080 public ClassName fetchClassName(String value) {
081 if (Validator.isNull(value)) {
082 return _nullClassName;
083 }
084
085 ClassName className = _classNames.get(value);
086
087 if (className == null) {
088 className = classNamePersistence.fetchByValue(value);
089
090 if (className == null) {
091 return _nullClassName;
092 }
093
094 _classNames.put(value, className);
095 }
096
097 return className;
098 }
099
100 @Override
101 @Skip
102 public long fetchClassNameId(Class<?> clazz) {
103 return fetchClassNameId(clazz.getName());
104 }
105
106 @Override
107 @Skip
108 public long fetchClassNameId(String value) {
109 try {
110 ClassName className = fetchClassName(value);
111
112 return className.getClassNameId();
113 }
114 catch (Exception e) {
115 throw new RuntimeException(
116 "Unable to get class name from value " + value, e);
117 }
118 }
119
120 @Override
121 @Skip
122 public ClassName getClassName(String value) {
123 if (Validator.isNull(value)) {
124 return _nullClassName;
125 }
126
127
128
129
130 ClassName className = _classNames.get(value);
131
132 if (className == null) {
133 className = classNameLocalService.addClassName(value);
134
135 _classNames.put(value, className);
136 }
137
138 return className;
139 }
140
141 @Override
142 @Skip
143 public long getClassNameId(Class<?> clazz) {
144 return getClassNameId(clazz.getName());
145 }
146
147 @Override
148 @Skip
149 public long getClassNameId(String value) {
150 try {
151 ClassName className = getClassName(value);
152
153 return className.getClassNameId();
154 }
155 catch (Exception e) {
156 throw new RuntimeException(
157 "Unable to get class name from value " + value, e);
158 }
159 }
160
161 @Override
162 public String getRegistryName() {
163 return ClassNameLocalServiceImpl.class.getName();
164 }
165
166 @Override
167 public void invalidate() {
168 _classNames.clear();
169 }
170
171 private static final Map<String, ClassName> _classNames =
172 new ConcurrentHashMap<String, ClassName>();
173 private static final ClassName _nullClassName = new ClassNameImpl();
174
175 }