1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.annotation.Propagation;
20  import com.liferay.portal.kernel.annotation.Transactional;
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  /**
32   * <a href="ClassNameLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class ClassNameLocalServiceImpl extends ClassNameLocalServiceBaseImpl {
37  
38      public ClassName addClassName(String value) throws SystemException {
39          ClassName className = classNamePersistence.fetchByValue(value);
40  
41          if (className == null) {
42              long classNameId = counterLocalService.increment();
43  
44              className = classNamePersistence.create(classNameId);
45  
46              className.setValue(value);
47  
48              classNamePersistence.update(className, false);
49          }
50  
51          return className;
52      }
53  
54      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
55      public void checkClassNames() throws SystemException {
56          if (_classNames.isEmpty()) {
57              List<ClassName> classNames = classNamePersistence.findAll();
58  
59              for (ClassName className : classNames) {
60                  _classNames.put(className.getValue(), className);
61              }
62          }
63  
64          List<String> models = ModelHintsUtil.getModels();
65  
66          for (String model : models) {
67              getClassName(model);
68          }
69      }
70  
71      public ClassName getClassName(long classNameId)
72          throws PortalException, SystemException {
73  
74          return classNamePersistence.findByPrimaryKey(classNameId);
75      }
76  
77      @Transactional(enabled = false)
78      public ClassName getClassName(String value) throws SystemException {
79          if (Validator.isNull(value)) {
80              return _nullClassName;
81          }
82  
83          // Always cache the class name. This table exists to improve
84          // performance. Create the class name if one does not exist.
85  
86          ClassName className = _classNames.get(value);
87  
88          if (className == null) {
89              className = classNameLocalService.addClassName(value);
90  
91              _classNames.put(value, className);
92          }
93  
94          return className;
95      }
96  
97      @Transactional(enabled = false)
98      public long getClassNameId(Class<?> classObj) {
99          return getClassNameId(classObj.getName());
100     }
101 
102     @Transactional(enabled = false)
103     public long getClassNameId(String value) {
104         try {
105             ClassName className = getClassName(value);
106 
107             return className.getClassNameId();
108         }
109         catch (Exception e) {
110             throw new RuntimeException(
111                 "Unable to get class name from value " + value, e);
112         }
113     }
114 
115     private static ClassName _nullClassName = new ClassNameImpl();
116     private static Map<String, ClassName> _classNames =
117         new ConcurrentHashMap<String, ClassName>();
118 
119 }