1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.annotation.Propagation;
25  import com.liferay.portal.kernel.annotation.Transactional;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.ClassName;
28  import com.liferay.portal.model.ModelHintsUtil;
29  import com.liferay.portal.model.impl.ClassNameImpl;
30  import com.liferay.portal.service.base.ClassNameLocalServiceBaseImpl;
31  
32  import java.util.List;
33  import java.util.Map;
34  import java.util.concurrent.ConcurrentHashMap;
35  
36  /**
37   * <a href="ClassNameLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class ClassNameLocalServiceImpl extends ClassNameLocalServiceBaseImpl {
43  
44      public ClassName addClassName(String value) throws SystemException {
45          long classNameId = counterLocalService.increment();
46  
47          ClassName className = classNamePersistence.create(classNameId);
48  
49          className.setValue(value);
50  
51          classNamePersistence.update(className, false);
52  
53          return className;
54      }
55  
56      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
57      public void checkClassNames() throws SystemException {
58          if (_classNames.isEmpty()) {
59              List<ClassName> classNames = classNamePersistence.findAll();
60  
61              for (ClassName className : classNames) {
62                  _classNames.put(className.getValue(), className);
63              }
64          }
65  
66          List<String> models = ModelHintsUtil.getModels();
67  
68          for (String model : models) {
69              getClassName(model);
70          }
71      }
72  
73      public ClassName getClassName(long classNameId)
74          throws PortalException, SystemException {
75  
76          return classNamePersistence.findByPrimaryKey(classNameId);
77      }
78  
79      public ClassName getClassName(String value) throws SystemException {
80          if (Validator.isNull(value)) {
81              return _nullClassName;
82          }
83  
84          // Always cache the class name. This table exists to improve
85          // performance. Create the class name if one does not exist.
86  
87          ClassName className = _classNames.get(value);
88  
89          if (className == null) {
90              className = classNamePersistence.fetchByValue(value);
91  
92              if (className == null) {
93                  className = classNameLocalService.addClassName(value);
94              }
95  
96              _classNames.put(value, className);
97          }
98  
99          return className;
100     }
101 
102     public long getClassNameId(Class<?> classObj) {
103         return getClassNameId(classObj.getName());
104     }
105 
106     public long getClassNameId(String value) {
107         try {
108             ClassName className = getClassName(value);
109 
110             return className.getClassNameId();
111         }
112         catch (Exception e) {
113             throw new RuntimeException(
114                 "Unable to get class name from value " + value, e);
115         }
116     }
117 
118     private static ClassName _nullClassName = new ClassNameImpl();
119     private static Map<String, ClassName> _classNames =
120         new ConcurrentHashMap<String, ClassName>();
121 
122 }