001    /**
002     * Copyright (c) 2000-present 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.spring.aop.Skip;
019    import com.liferay.portal.kernel.transaction.Propagation;
020    import com.liferay.portal.kernel.transaction.Transactional;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.ClassName;
023    import com.liferay.portal.model.ModelHintsUtil;
024    import com.liferay.portal.model.impl.ClassNameImpl;
025    import com.liferay.portal.service.base.ClassNameLocalServiceBaseImpl;
026    
027    import java.util.List;
028    import java.util.Map;
029    import java.util.concurrent.ConcurrentHashMap;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class ClassNameLocalServiceImpl
035            extends ClassNameLocalServiceBaseImpl implements CacheRegistryItem {
036    
037            @Override
038            public ClassName addClassName(String value) {
039                    ClassName className = classNamePersistence.fetchByValue(value);
040    
041                    if (className == null) {
042                            long classNameId = counterLocalService.increment();
043    
044                            className = classNamePersistence.create(classNameId);
045    
046                            className.setValue(value);
047    
048                            classNamePersistence.update(className);
049                    }
050    
051                    return className;
052            }
053    
054            @Override
055            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
056            public void checkClassNames() {
057                    List<ClassName> classNames = classNamePersistence.findAll();
058    
059                    for (ClassName className : classNames) {
060                            _classNames.put(className.getValue(), className);
061                    }
062    
063                    List<String> models = ModelHintsUtil.getModels();
064    
065                    for (String model : models) {
066                            getClassName(model);
067                    }
068            }
069    
070            @Override
071            public ClassName fetchClassName(String value) {
072                    if (Validator.isNull(value)) {
073                            return _nullClassName;
074                    }
075    
076                    ClassName className = _classNames.get(value);
077    
078                    if (className == null) {
079                            className = classNamePersistence.fetchByValue(value);
080    
081                            if (className == null) {
082                                    return _nullClassName;
083                            }
084    
085                            _classNames.put(value, className);
086                    }
087    
088                    return className;
089            }
090    
091            @Override
092            @Skip
093            public ClassName getClassName(String value) {
094                    if (Validator.isNull(value)) {
095                            return _nullClassName;
096                    }
097    
098                    // Always cache the class name. This table exists to improve
099                    // performance. Create the class name if one does not exist.
100    
101                    ClassName className = _classNames.get(value);
102    
103                    if (className == null) {
104                            try {
105                                    className = classNameLocalService.addClassName(value);
106    
107                                    _classNames.put(value, className);
108                            }
109                            catch (Throwable t) {
110                                    className = classNameLocalService.fetchClassName(value);
111    
112                                    if (className == _nullClassName) {
113                                            throw t;
114                                    }
115                            }
116                    }
117    
118                    return className;
119            }
120    
121            @Override
122            @Skip
123            public long getClassNameId(Class<?> clazz) {
124                    return getClassNameId(clazz.getName());
125            }
126    
127            @Override
128            @Skip
129            public long getClassNameId(String value) {
130                    ClassName className = getClassName(value);
131    
132                    return className.getClassNameId();
133            }
134    
135            @Override
136            public String getRegistryName() {
137                    return ClassNameLocalServiceImpl.class.getName();
138            }
139    
140            @Override
141            public void invalidate() {
142                    _classNames.clear();
143            }
144    
145            private static final Map<String, ClassName> _classNames =
146                    new ConcurrentHashMap<>();
147            private static final ClassName _nullClassName = new ClassNameImpl();
148    
149    }