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