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