001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.cache.CacheRegistryItem;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.spring.aop.Skip;
021    import com.liferay.portal.kernel.transaction.Propagation;
022    import com.liferay.portal.kernel.transaction.Transactional;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.ClassName;
025    import com.liferay.portal.model.ModelHintsUtil;
026    import com.liferay.portal.model.impl.ClassNameImpl;
027    import com.liferay.portal.service.base.ClassNameLocalServiceBaseImpl;
028    
029    import java.util.List;
030    import java.util.Map;
031    import java.util.concurrent.ConcurrentHashMap;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class ClassNameLocalServiceImpl
037            extends ClassNameLocalServiceBaseImpl implements CacheRegistryItem {
038    
039            public ClassName addClassName(String value) throws SystemException {
040                    ClassName className = classNamePersistence.fetchByValue(value);
041    
042                    if (className == null) {
043                            long classNameId = counterLocalService.increment();
044    
045                            className = classNamePersistence.create(classNameId);
046    
047                            className.setValue(value);
048    
049                            classNamePersistence.update(className);
050                    }
051    
052                    return className;
053            }
054    
055            @Override
056            public void afterPropertiesSet() {
057                    super.afterPropertiesSet();
058    
059                    CacheRegistryUtil.register(this);
060            }
061    
062            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
063            public void checkClassNames() throws SystemException {
064                    List<ClassName> classNames = classNamePersistence.findAll();
065    
066                    for (ClassName className : classNames) {
067                            _classNames.put(className.getValue(), className);
068                    }
069    
070                    List<String> models = ModelHintsUtil.getModels();
071    
072                    for (String model : models) {
073                            getClassName(model);
074                    }
075            }
076    
077            @Skip
078            public ClassName fetchClassName(String value) throws SystemException {
079                    if (Validator.isNull(value)) {
080                            return _nullClassName;
081                    }
082    
083                    ClassName className = _classNames.get(value);
084    
085                    if (className == null) {
086                            className = classNamePersistence.fetchByValue(value);
087    
088                            if (className == null) {
089                                    return _nullClassName;
090                            }
091    
092                            _classNames.put(value, className);
093                    }
094    
095                    return className;
096            }
097    
098            @Skip
099            public long fetchClassNameId(Class<?> clazz) {
100                    return fetchClassNameId(clazz.getName());
101            }
102    
103            @Skip
104            public long fetchClassNameId(String value) {
105                    try {
106                            ClassName className = fetchClassName(value);
107    
108                            return className.getClassNameId();
109                    }
110                    catch (Exception e) {
111                            throw new RuntimeException(
112                                    "Unable to get class name from value " + value, e);
113                    }
114            }
115    
116            @Skip
117            public ClassName getClassName(String value) throws SystemException {
118                    if (Validator.isNull(value)) {
119                            return _nullClassName;
120                    }
121    
122                    // Always cache the class name. This table exists to improve
123                    // performance. Create the class name if one does not exist.
124    
125                    ClassName className = _classNames.get(value);
126    
127                    if (className == null) {
128                            className = classNameLocalService.addClassName(value);
129    
130                            _classNames.put(value, className);
131                    }
132    
133                    return className;
134            }
135    
136            @Skip
137            public long getClassNameId(Class<?> clazz) {
138                    return getClassNameId(clazz.getName());
139            }
140    
141            @Skip
142            public long getClassNameId(String value) {
143                    try {
144                            ClassName className = getClassName(value);
145    
146                            return className.getClassNameId();
147                    }
148                    catch (Exception e) {
149                            throw new RuntimeException(
150                                    "Unable to get class name from value " + value, e);
151                    }
152            }
153    
154            public String getRegistryName() {
155                    return ClassNameLocalServiceImpl.class.getName();
156            }
157    
158            public void invalidate() {
159                    _classNames.clear();
160            }
161    
162            private static Map<String, ClassName> _classNames =
163                    new ConcurrentHashMap<String, ClassName>();
164            private static ClassName _nullClassName = new ClassNameImpl();
165    
166    }