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            @Override
040            public ClassName addClassName(String value) throws SystemException {
041                    ClassName className = classNamePersistence.fetchByValue(value);
042    
043                    if (className == null) {
044                            long classNameId = counterLocalService.increment();
045    
046                            className = classNamePersistence.create(classNameId);
047    
048                            className.setValue(value);
049    
050                            classNamePersistence.update(className);
051                    }
052    
053                    return className;
054            }
055    
056            @Override
057            public void afterPropertiesSet() {
058                    super.afterPropertiesSet();
059    
060                    CacheRegistryUtil.register(this);
061            }
062    
063            @Override
064            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
065            public void checkClassNames() throws SystemException {
066                    List<ClassName> classNames = classNamePersistence.findAll();
067    
068                    for (ClassName className : classNames) {
069                            _classNames.put(className.getValue(), className);
070                    }
071    
072                    List<String> models = ModelHintsUtil.getModels();
073    
074                    for (String model : models) {
075                            getClassName(model);
076                    }
077            }
078    
079            @Override
080            @Skip
081            public ClassName fetchClassName(String value) throws SystemException {
082                    if (Validator.isNull(value)) {
083                            return _nullClassName;
084                    }
085    
086                    ClassName className = _classNames.get(value);
087    
088                    if (className == null) {
089                            className = classNamePersistence.fetchByValue(value);
090    
091                            if (className == null) {
092                                    return _nullClassName;
093                            }
094    
095                            _classNames.put(value, className);
096                    }
097    
098                    return className;
099            }
100    
101            @Override
102            @Skip
103            public long fetchClassNameId(Class<?> clazz) {
104                    return fetchClassNameId(clazz.getName());
105            }
106    
107            @Override
108            @Skip
109            public long fetchClassNameId(String value) {
110                    try {
111                            ClassName className = fetchClassName(value);
112    
113                            return className.getClassNameId();
114                    }
115                    catch (Exception e) {
116                            throw new RuntimeException(
117                                    "Unable to get class name from value " + value, e);
118                    }
119            }
120    
121            @Override
122            @Skip
123            public ClassName getClassName(String value) throws SystemException {
124                    if (Validator.isNull(value)) {
125                            return _nullClassName;
126                    }
127    
128                    // Always cache the class name. This table exists to improve
129                    // performance. Create the class name if one does not exist.
130    
131                    ClassName className = _classNames.get(value);
132    
133                    if (className == null) {
134                            className = classNameLocalService.addClassName(value);
135    
136                            _classNames.put(value, className);
137                    }
138    
139                    return className;
140            }
141    
142            @Override
143            @Skip
144            public long getClassNameId(Class<?> clazz) {
145                    return getClassNameId(clazz.getName());
146            }
147    
148            @Override
149            @Skip
150            public long getClassNameId(String value) {
151                    try {
152                            ClassName className = getClassName(value);
153    
154                            return className.getClassNameId();
155                    }
156                    catch (Exception e) {
157                            throw new RuntimeException(
158                                    "Unable to get class name from value " + value, e);
159                    }
160            }
161    
162            @Override
163            public String getRegistryName() {
164                    return ClassNameLocalServiceImpl.class.getName();
165            }
166    
167            @Override
168            public void invalidate() {
169                    _classNames.clear();
170            }
171    
172            private static Map<String, ClassName> _classNames =
173                    new ConcurrentHashMap<String, ClassName>();
174            private static ClassName _nullClassName = new ClassNameImpl();
175    
176    }