1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
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 }