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.util.Validator;
25  import com.liferay.portal.model.ClassName;
26  import com.liferay.portal.model.ModelHintsUtil;
27  import com.liferay.portal.model.impl.ClassNameImpl;
28  import com.liferay.portal.service.base.ClassNameLocalServiceBaseImpl;
29  
30  import java.util.List;
31  import java.util.Map;
32  import java.util.concurrent.ConcurrentHashMap;
33  
34  /**
35   * <a href="ClassNameLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class ClassNameLocalServiceImpl extends ClassNameLocalServiceBaseImpl {
41  
42      public void checkClassNames() throws SystemException {
43          List<String> models = ModelHintsUtil.getModels();
44  
45          for (int i = 0; i < models.size(); i++) {
46              String value = models.get(i);
47  
48              getClassName(value);
49          }
50      }
51  
52      public ClassName getClassName(long classNameId)
53          throws PortalException, SystemException {
54  
55          return classNamePersistence.findByPrimaryKey(classNameId);
56      }
57  
58      public ClassName getClassName(String value) throws SystemException {
59          if (Validator.isNull(value)) {
60              return _nullClassName;
61          }
62  
63          // Always cache the class name. This table exists to improve
64          // performance. Create the class name if one does not exist.
65  
66          ClassName classNameModel = _classNames.get(value);
67  
68          if (classNameModel == null) {
69              classNameModel = classNamePersistence.fetchByValue(value);
70  
71              if (classNameModel == null) {
72                  long classNameId = counterLocalService.increment();
73  
74                  classNameModel = classNamePersistence.create(classNameId);
75  
76                  classNameModel.setValue(value);
77  
78                  classNamePersistence.update(classNameModel, false);
79              }
80  
81              _classNames.put(value, classNameModel);
82          }
83  
84          return classNameModel;
85      }
86  
87      private static ClassName _nullClassName = new ClassNameImpl();
88      private static Map<String, ClassName> _classNames =
89          new ConcurrentHashMap<String, ClassName>();
90  
91  }