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          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          // Always cache the class name. This table exists to improve
79          // performance. Create the class name if one does not exist.
80  
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 }